Recently I plan to make a Bluetooth receiver myself and connect it to the quadcopter, so I study how to output 4-channel PWM.
The plan is to use the timer interrupt, calculate the duty cycle during the interrupt, and then switch the specified PIN pin between high and low levels.
It took me a long time to understand the usage of this. The following code implements an interrupt once per second on the 16M ATMEGA8 chip.
void setup() {
pinMode(13, OUTPUT);
//Disable interrupts
cli();
//TCNT1 is the accumulator register here, which increases once per clock
//TCNT1 = 0;
//TCCR1 control register
TCCR1A = 0;
TCCR1B = 0;
//OCR1A is the accumulation upper limit register. Adjusting this value can control the frequency.
// Here 15625 is calculated as follows: 16M / 1024 = 15625
OCR1A = 15625;
// Set the frequency division to 1024
// Here _BV is a macro, which is the function of 1 << n. The various ways of setting bits feel quite fragmented.
TCCR1B |= (_BV(CS12) | _BV(CS10));
// The CTC mode controlled by WGM12 means that TCNT1 will be cleared to 0 by itself, otherwise it needs to be cleared to 0 manually
TCCR1B |= 1 << WGM12;
// Set up comparison interrupt
bitSet(TIMSK, OCIE1A);
// Enable interrupt
be();
}
int flag = 0;
void loop() {
// put your main code here, to run repeatedly:
}
ISR(TIMER1_COMPA_vect) {
if (flag == 0)
{
flag = 1;
digitalWrite(13, HIGH);
}
else
{
flag = 0;
digitalWrite(13, LOW);
}
}
Previous article:AVR timer interrupt overflow
Next article:ATMEGA128 PWM output series routines
Recommended ReadingLatest update time:2024-11-16 20:56
- 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
- Download Center’s April benefit: a collection of Bluetooth articles will be given away for free!
- Analysis of common problems of touch switches
- What sensor can measure the droplet falling (through the infusion tube)
- Which power chip is BNOF?
- How to connect the communication interface of Topmicro intelligent module (I)
- Award-winning live broadcast|When smart grid meets TI Sitara AM335x
- The 2019 TI Industrial Applications Seminar is coming, and the Ningbo special session is waiting for you to sign up!
- [ST NUCLEO-H743ZI Review] Try to play with DAC2ADC
- 360 was reported to have a massive layoff, and the entire security service team was disbanded on the spot.
- [Anxinke UWB indoor positioning module NodeMCU-BU01] No.002-Hardware test environment construction