To configure the CCP module for PWM mode, follow these steps:
1. Write to the PR2 register to program the PWM period.
2. Write to the DCxB9:DCxB0 bits to program the PWM duty cycle.
3. Clear the appropriate TRIS bit to make the CCPx pin an output.
4. Write to T2CON to program the TMR2 prescaler and enable Timer2.
5. Configure the CCP module for PWM mode.
program:
#include
#define uint unsigned int
#define uchar unsigned char
__CONFIG(0x3B31);
void initPWM();
void Delay1ms(uint time);
/******************************************
***Main function: PWM initialization, PWM duty cycle periodic change
*******************************************/
void main()
{
uchar i;
initPWM();
while(1)
{
for(i=0;i<256;i++)
{
CCPR1L = i;
TMR2ON = 1;
Delay1ms(100);
TMR2ON = 0;
}
}
}
/******************************************************************
***Function: PWM initialization, set PWM period;
PWM period = [(PR2)+1]*4*TOSC*(TMR2 pre-scaling ratio)
= (255+1)*4*(1/4000000)*16 s = 4.096 ms
PWM duty cycle = (DCxB9:DCxB0)*Tosc*(TMR2 prescaler ratio)
= 1024*(1/4000000)*16 s = 4.096 ms
Note: If the PWM duty cycle value is greater than the PWM period, the CCPx pin will
not be cleared and the duty cycle will reach 100%;
*********************************************************************/
void initPWM()
{
INTCON = 0x00; //Disable general interrupt and external interrupt
PR2 = 0xff; //Write to PR2 register to set PWM period, PWM period = [(PR2)+1]*4*TOSC*(TMR2 prescaler)
CCP1CON = 0x3C; //Set the lower 2 bits of PWM duty cycle to 11, set to PWM mode
CCPR1L = 0x00; //Set the higher 8 bits of PWM duty cycle
TRISC = 0xfb; //Set CCP1 port to output
T2CON = 0x02; //Set TMR2 prescaler to 16;
}
void Delay1ms(uint time)
{
uint i,j;
for(i=time;i>0;i--)
for(j=110;j>0;j--);
}
Previous article:Design of Intelligent Heat Meter Based on PIC16C73 Single Chip Microcomputer
Next article:Operation of PIC Internal EEPROM
Recommended ReadingLatest update time:2024-11-16 18:10
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Today at 10:00 AM, live broadcast with prizes: ST's PLC solutions in industrial automation
- MOS tube G pole protection problem
- Understanding Output Voltage Ripple and Noise Part 2: Sources and Suppression of High-Frequency Noise Components
- Interface Board
- Crystal-less BAW Multi-protocol 2.4GHz Wireless MCU
- Sugar Glider Part ⑥ Hardware Design of the Exercise Reward System for Sugar Glider Based on RSL10
- [Technical Discussion] Is it necessary to bring a laptop when testing the distance of a wireless communication module?
- About op amp compensation
- [National Technology M4 core hot-selling N32G45XVL evaluation] Part 2 IO port multiplexing configuration and IO output frequency evaluation
- [Evaluation of domestic FPGA Gaoyun GW1N-4 series development board] Static digital tube display