Hardware: STC12C5A60S2
Function: To realize PWM wave generation. The program cycle is 256ms and the output port is P1.3.
Code:
#include "reg51.h"
#include "intrins.h"
#define DARK 11059260L
#define T1MS (65536-FOSC/12/1000)
//The pulse generated by the timer is about 1MS, and the time after PWM is divided by 256 is 256ms
//By adjusting the value of T1MS, the maximum cycle time of PWM is 16s.
typedef unsigned char BYTE;
typedef unsigned int WORD;
/*Declare SFR associated with the PCA */
sfr CCON = 0xD8; //PCA control register
sbit CCF0 = CCON^0; //PCA module-0 interrupt flag
sbit CCF1 = CCON^1; //PCA module-1 interrupt flag
sbit CR = CCON^6; //PCA timer run control bit
sbit CF = CCON^7; //PCA timer overflow flag
sfr CMOD = 0xD9; //PCA mode register
sfr CL = 0xE9; //PCA base timer LOW
sfr CH = 0xF9; //PCA base timer HIGH
sfr CCAPM0 = 0xDA; //PCA module-0 mode register
sfr CCAP0L = 0xEA; //PCA module-0 capture register LOW
sfr CCAP0H = 0xFA; //PCA module-0 capture register HIGH
sfr CCAPM1 = 0xDB; //PCA module-1 mode register
sfr CCAP1L = 0xEB; //PCA module-1 capture register LOW
sfr CCAP1H = 0xFB; //PCA module-1 capture register HIGH
sfr PCAPWM0 = 0xf2;
sfr PCAPWM1 = 0xf3;
void Delay_xms(unsigned int z){
unsigned char i, j;
unsigned int k;
for(k=0;k
_nop_();
_nop_();
i = 12;
j = 84;
do{while (--j);}
while (--i);
}
}
// Initialize timer 0
void Time0Init()
{
TMOD = 0x01;
TL0=T1MS;
TH0=T1MS>>8;
TR0=1;
ET0=1;
EA=1;
}
void PWMInit()
{
CCON = 0; //Initial PCA control register
//PCA timer stop running
//Clear CF flag
//Clear all module interrupt flag
CL = 0; //Reset PCA base timer
CH = 0;
// CMOD = 0x00; //Set PCA timer clock source as Fosc/2
CMOD = 0x04;
//Disable PCA timer overflow interrupt
CCAP0H = CCAP0L = 0x80; //PWM0 port output 50% duty cycle square wave
CCAPM0 = 0x42; //The device is timer 0 overflow pulse
CCAP1H = CCAP1L = 0xff; //PWM1 port output 0% duty cycle square wave
PCAPWM1 = 0x03;
CCAPM1 = 0x42; //PCA module-1 work in 8-bit PWM mode
//and no PCA interrupt
CR = 1; //PCA timer start run
}
void PWMSet(float a) //Input a represents the duty cycle
{
CCAP0H =CCAP0L =(BYTE)((1-a)*256);
}
void main()
{
Time0Init();
PWMInit();
PWMSet(0.1);
while(1)
{
PWMSet(0.5); //duty cycle is 50%
Delay_xms(2000);
}
}
//Timer 0 interrupt
void tm0_isr() interrupt 1 using 1
{
TL0=T1MS;
TH0=T1MS>>8;
}
Previous article:51 Study Notes: Using hardware spi and max6675 communication to realize K-type thermocouple temperature measurement
Next article:51 Study Notes: The meaning of the keywords data, bdata, idata, pdata, xdata, and code
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Apple faces class action lawsuit from 40 million UK iCloud users, faces $27.6 billion in claims
- Apple faces class action lawsuit from 40 million UK iCloud users, faces $27.6 billion in claims
- The US asked TSMC to restrict the export of high-end chips, and the Ministry of Commerce responded
- The US asked TSMC to restrict the export of high-end chips, and the Ministry of Commerce responded
- ASML predicts that its revenue in 2030 will exceed 457 billion yuan! Gross profit margin 56-60%
- Detailed explanation of intelligent car body perception system
- How to solve the problem that the servo drive is not enabled
- Why does the servo drive not power on?
- What point should I connect to when the servo is turned on?
- How to turn on the internal enable of Panasonic servo drive?
- CircuitPython in Space
- Schematic diagram of KeyStone DSP synchronous buck converter with VID function
- Experience sharing on porting ardupilot to ti platform
- CS8683 Mono 130W High Power Class D Amplifier IC Solution
- Qorvo has acquired Decawave and Custom MMIC, so powerful!
- EEWORLD University Hall----Statistical Machine Learning
- HMI chip solutions are coming soon
- True RTOS
- Should you enter the IT industry? Four factors will tell you at a glance
- What are the commonly used chips that make up DSP systems?