#ifndef _KPWM_H_
#define _KPWM_H_
/*************avr frequency duty cycle adjustable waveform output sub-function**************/
///Formal parameter: Fclk value sets the frequency (in k) Range: 1K to 256K
/// rat sets the duty cycle (in percentage) eg:40 then the output square wave duty cycle is 40%
//Frequency error is less than 2%, duty cycle error is less than 5%
//PWM frequency calculation formula is f=fox/N(1+TOP) fox is the crystal frequency N is the frequency division coefficient TOP is the maximum count
//TOP's inverse calculation formula is TOP=FOX/NFclk-1
//In 15 mode, the maximum count value is OCR1A
//The comparison value is placed in OCR1B
//In 15 mode, when clock 1 reaches OCR1B, the OC1B pin jumps
//And the OC1A pin outputs a 50% duty cycle square wave with twice Fclk
//The default crystal frequency here is 12M, which must be modified according to your own MCU
//
void KPWM(unsigned char fcq,unsigned char rat)
{
unsigned int f,r;
DDRD|=0X30; //Set PD4 and PD5 as the second function pins OC1B and OC1A respectively
TCCR1A|=(0<
f=12000/fcq; //Calculate the maximum value, because the output unit is a square wave in KHZ, so 12 000 000 removes the last three 0s
f=f/1;
f=f-1;
r=f;
r=r/10;
r=r*rat; //Calculate the comparison value, which is actually divided by 100 to prevent overflow
r=r/10;
OCR1A=f;
OCR1B=r;
}
void KPWM_MODE14(unsigned char fcq,unsigned char rat1,unsigned char rat2)
{
unsigned int f,r1,r2;
DDRD|=0X30; //Set PD4 PD5 to the second function pin, OC1B OC1A respectively
TCCR1A|=(1<
f=12000/fcq;// Calculate the maximum value, because the output unit is a square wave in KHZ, so 12 000 000 removes the last 3 0s
f=f/1;
f=f-1;
r1=f;
r1=r1/10;
r1=r1*rat1; // Calculate the comparison value, which is actually divided by 100 to prevent overflowr1
=r1/10;
r2=f;
r2=r2/10;
r2=r2*rat2; // Calculate the comparison value, which is actually divided by 100 to prevent overflowr2
=r2/10;
ICR1=f;
OCR1A=r1;
OCR1B=r2; //Decide the direction here, remember to verify
}
#endif
Previous article:AVR microcontroller (ATMEGA16) serial transceiver program
Next article:AVRmeg16 MCU realizes key control of LCD1602 data display
Recommended ReadingLatest update time:2024-11-16 17:44
- Popular Resources
- Popular amplifiers
- Principles and Applications of Single Chip Microcomputers 3rd Edition (Zhang Yigang)
- Metronom Real-Time Operating System RTOS for AVR microcontrollers
- Learn C language for AVR microcontrollers easily (with video tutorial) (Yan Yu, Li Jia, Qin Wenhai)
- ATmega16 MCU C language programming classic example (Chen Zhongping)
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
- Testing solutions for redundant link networks
- Key wireless technologies for 5G systems
- [NXP Rapid IoT Review] + Rapid IoT App Running Error
- How to Design an RF Power Amplifier: The Basics
- What is the principle of touch switch?
- What is jitter and phase noise?
- [Shanghai Hangxin ACM32F070 development board + touch function evaluation board evaluation] + OLED screen display driver
- Tailing Micro B91 Development Kit Burning Pitfalls Record
- Antai Testing - Sharing of Maintenance Experience of Tektronix AFG3021 Arbitrary Signal Generator
- Interrupt vectors for ARM (Cortex-M3)