#include "kernel.h"
unsigned int PWM_Buf[6]; //6-channel PWM frequency
unsigned long timer0_ticks;
unsigned long timer0_tickssec;
unsigned long timer1_ticks;
unsigned long timer1_ticksmin;
unsigned long timer2_ticks;
unsigned long timer2_tickssec;
unsigned long timer3_ticks;
unsigned long timer3_ticksmin;
unsigned long timer4_ticks;
unsigned long timer4_ticksmin;
unsigned long timer5_ticks;
unsigned long timer5_ticksmin;
void Timer0_Init(void)
{
timer0_ticks = 0;
timer0_tickssec = 0;
TCCR0A = 0x00;
TCCR0B = 0x00;
TCNT0 = 0x00;
OCR0A = F_CPU/(1024-1)/20; //Frequency F_PWM
OCR0B = OCR0A/2; //Duty cycle 50%
//TCCR0A = 0xA3; //COM0A1 COM0A0 COM0B1 COM0B0 – – WGM01 WGM00
TCCR0B = 0xCD; //FOC0A FOC0B – – WGM02 CS02 CS01 CS00
/*
TCCR0A = 0x00;
TCCR0B = 0x00;//stop
TCNT0 = 0xE7;//25us
OCR0A = 0x18;
OCR0B = 0x18;
TCCR0A = 0x03;
TCCR0B = 0x02;//start,clkT0S/8
TIMSK0|= 0x01;//enable 0verflow interrupt*/
}
void TC0_Set_PWM(unsigned int curF)
{//20~80 is clkT0S/1024, 80~300 is clkT0S/256, 300~2000 is clkT0S/64
if(curF==0)
{
OCR0A = 0;
OCR0B = 0;
TCCR0A = 0;
}
else if((curF>=20)&&(curF<80))
{
OCR0A = F_CPU/(1024-1)/curF;
OCR0B = OCR0A/2;
TCCR0A = 0xA3;
TCCR0B = 0xCD;
}
else if((curF>=80)&&(curF<300))
{
OCR0A = F_CPU/(256-1)/curF;
OCR0B = OCR0A/2;
TCCR0A = 0xA3;
TCCR0B = 0xCC;
}
else if((curF>=300)&&(curF<=2000))
{
OCR0A = F_CPU/(64-1)/curF;
OCR0B = OCR0A/2;
TCCR0A = 0xA3;
TCCR0B = 0xCB;
}
}
void Timer1_Init(void)
{
unsigned int temp;
timer1_ticks = 0;
timer1_ticksmin = 0;
TCCR1A = 0x00;
TCCR1B = 0x00;
TCCR1C = 0x00;
TCNT1H = 0x00;
TCNT1L = 0x00;
temp = F_CPU/16/20;
OCR1A=temp;
//temp = temp/2;
//OCR1B=temp;
//OCR1C=temp;
//TCCR1A=(0< TCCR1A = 0x00;//COM1A1 COM1A0 COM1B1 COM1B0 COM1C1 COM1C0 WGM11 WGM10 TCCR1B = 0x00;//stop,ICNC1 ICES1 – WGM13 WGM12 CS12 CS11 CS10 TCCR1C = 0x00;//FOC1A FOC1B FOC1C – – – – – TCNT1H = 0xE3; TCNT1L = 0xE0; OCR1AH = 0x1C; OCR1AL = 0x1F; OCR1BH = 0x1C; OCR1BL = 0x1F; OCR1CH = 0x1C; OCR1CL = 0x1F; ICR1H = 0x00; ICR1L = 0x00; TCCR1B = 0x05; // TIMSK |= 0x04;//enable 0verflow interrupt*/ } void TC1_Set_PWM(unsigned int curF) { unsigned int temp; if(curF==0) { //OCR1A = 0; //OCR1B = 0; TCCR1A=0; } else if((curF>=20)&&(curF<=2000)) { temp = F_CPU/16/curF; OCR1A=temp; TCCR1A=(0< } void Timer2_Init(void) { timer2_ticks = 0; timer2_tickssec = 0; TCCR2A = 0x00; TCCR2B = 0x00; TCNT2 = 0x00; OCR2A = F_CPU/(1024-1)/20; //Frequency F_PWM OCR2B = OCR2A/2; //Duty cycle 50% //TCCR2A = 0xA3; //COM2A1 COM2A0 COM2B1 COM2B0 – – WGM21 WGM20 TCCR2B = 0xCF; //FOC2A FOC2B – – WGM22 CS22 CS21 CS20 // TCCR2 = 0x00;//stop,FOC2 WGM20 COM21 COM20 WGM21 CS22 CS21 CS20 // TCNT2 = 0xB8;//set count 10ms // OCR2 = 0x47; // TCCR2 = 0x05;//start,clkT0S/1024 // TIMSK|= 0x40;//enable 0verflow interrupt } void TC2_Set_PWM(unsigned int curF) {//20~80 is clkT0S/1024, 80~300 is clkT0S/256, 300~2000 is clkT0S/64 if(curF==0) { OCR2A = 0; OCR2B = 0; TCCR2A = 0; } else if((curF>=20)&&(curF<80)) { OCR2A = F_CPU/(1024-1)/curF; OCR2B = OCR2A/2; TCCR2A = 0xA3; TCCR2B = 0xCF; } else if((curF>=80)&&(curF<300)) { OCR2A = F_CPU/(256-1)/curF; OCR2B = OCR2A/2; TCCR2A = 0xA3; TCCR2B = 0xCE; } else if((curF>=300)&&(curF<=2000)) { OCR2A = F_CPU/(64-1)/curF; OCR2B = OCR2A/2; TCCR2A = 0xA3; TCCR2B = 0xCC; } } void Timer3_Init(void) { unsigned int temp; timer3_ticks = 0; timer3_ticksmin = 0; TCCR3A = 0x00; TCCR3B = 0x00; TCCR3C = 0x00; TCNT3H = 0x00; TCNT3L = 0x00; temp = F_CPU/16/20; OCR3A=temp; //temp = temp/2; //OCR3B=temp; //OCR3C=temp; //TCCR3A=(0< TCCR3A = 0x00;//COM3A1 COM3A0 COM3B1 COM3B0 COM3C1 COM3C0 WGM31 WGM30 TCCR3B = 0x00;//stop,ICNC3 ICES3 – WGM33 WGM32 CS32 CS31 CS30 TCCR3C = 0x00;//FOC3A FOC3B FOC3C – – – – – TCNT3H = 0xE3; TCNT3L = 0xE0;//set count 1s OCR3AH = 0x1C; OCR3AL = 0x1F; OCR3BH = 0x1C; OCR3BL = 0x1F; OCR3CH = 0x1C; OCR3CL = 0x1F; ICR3H = 0x00; ICR3L = 0x00; TCCR3B = 0x05;//start,clkT0S/1024 // ETIMSK|= 0x04;//enable 0verflow interrupt*/ } void TC3_Set_PWM(unsigned int curF) { unsigned int temp; if(curF==0) { //OCR3A = 0; //OCR3B = 0; TCCR3A=0; } else if((curF>=20)&&(curF<=2000)) { temp = F_CPU/16/curF; OCR3A=temp; TCCR3A=(0< } void Timer4_Init(void) { unsigned int temp; timer4_ticks = 0; timer4_ticksmin = 0; TCCR4A = 0x00; TCCR4B = 0x00; TCCR4C = 0x00; TCNT4H = 0x00; TCNT4L = 0x00; temp = F_CPU/16/20; OCR4A=temp; //temp = temp/2; //OCR4B=temp; //OCR4C=temp; //TCCR4A=(0< void TC4_Set_PWM(unsigned int curF) { unsigned int temp; if(curF==0) { //OCR4A = 0; //OCR4B = 0; TCCR4A=0; } else if((curF>=20)&&(curF<=2000)) { temp = F_CPU/16/curF; OCR4A=temp; TCCR4A=(0< } void Timer5_Init(void) { unsigned int temp; timer5_ticks = 0; timer5_ticksmin = 0; TCCR5A = 0x00; TCCR5B = 0x00; TCCR5C = 0x00; TCNT5H = 0x00; TCNT5L = 0x00; temp = F_CPU/16/20; OCR5A=temp; //temp = temp/2; //OCR5B=temp; //OCR5C=temp; //TCCR5A=(0< void TC5_Set_PWM(unsigned int curF) { unsigned int temp; if(curF==0) { //OCR5A = 0; //OCR5B = 0; TCCR5A=0; } else if((curF>=20)&&(curF<=2000)) { temp = F_CPU/16/curF; OCR5A=temp; TCCR5A=(0< } /******************************************************************************* * Function Name : TIMER0_OVF_vectFunc * Description: Timer 0 overflow interrupt routine * Input : None * Output : None * Return : None *******************************************************************************/ #pragma vector = TIMER0_OVF_vect __interrupt void TIMER0_OVF_vectFunc(void) { } /******************************************************************************* * Function Name : TIMER1_OVF_vectFunc * Description: Timer 1 overflow interrupt routine * Input : None * Output : None * Return : None *******************************************************************************/ #pragma vector = TIMER1_OVF_vect __interrupt void TIMER1_OVF_vectFunc(void) { } /******************************************************************************* * Function Name : TIMER2_OVF_vectFunc * Description: Timer 2 overflow interrupt routine * Input : None * Output : None * Return : None *******************************************************************************/ #pragma vector = TIMER2_OVF_vect __interrupt void TIMER2_OVF_vectFunc(void) { } /******************************************************************************* * Function Name : TIMER3_OVF_vectFunc * Description: Timer 3 overflow interrupt routine * Input : None * Output : None * Return : None *******************************************************************************/ #pragma vector = TIMER3_OVF_vect __interrupt void TIMER3_OVF_vectFunc(void) { }
Previous article:AVR microcontroller temperature measurement based on DS18B20
Next article:ATmage16-ADC
- Popular Resources
- Popular amplifiers
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
- 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
- Sony employee stole $154 million in public funds and exchanged them for Bitcoin: when it was recovered, it appreciated to over $180 million
- 8-bit, 16-bit and 32-bit MCUs, are more bits better?
- [Evaluation of EVAL-M3-TS6-665PN development board] 3. Power-on test
- Get a gift for grabbing the post: Watch the live broadcast and gain an in-depth understanding of the principle, operation, and waterproof structure design of ST's latest MEMS barometer
- CPAFC Carrier Synchronization
- When should I use a PWM controller?
- Goodbye 2019, hello 2020!
- SensorTile.box unpacking issues
- Are the EX0 bit in the IE register and the IE0 bit in the TCON register in the 51 microcontroller duplicated?
- Detailed diagram of microwave oven capacitor connection