AVR frequency duty cycle adjustable waveform output sub-function

Publisher:SereneHarmonyLatest update time:2018-06-28 Source: eefocusKeywords:avr Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

#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< TCCR1B|=(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 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< levelTCCR1B|=(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

Keywords:avr Reference address:AVR frequency duty cycle adjustable waveform output sub-function

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

AVR Driver for 25C256
/************************************Hardware Macro Definition************ *************************/ #define SelectMemory1() (PORTA&=~0X01) #define DeselectMemory1() (PORTA|=0X01) #define ClrHoldMemory1( ) (PORTA&=~(0X01 #define SetHoldMemory1() (PORTA|=(0X01 /********************** ******************Initialize S
[Microcontroller]
Unused I/O port settings in AVR microcontrollers
        If there are pins that are not used, it is recommended to give them a certain level. Although most digital inputs are disabled in Deep Sleep mode, it is still necessary to avoid pins without a certain level consuming current in other digital input enabled modes (reset, working mode, idle mode).        The simp
[Microcontroller]
AVR microcontroller (learning) - (KZ), power management and sleep mode - 01
Power Management and Sleep Mode Sleep mode allows the application to shut down unused modules in the MCU, thereby reducing power consumption. AVR has different sleep modes, allowing users to tailor them to their application requirements. The condition for entering sleep mode is to set SE in the MCUCR register and then
[Microcontroller]
AVR microcontroller (learning) - (KZ), power management and sleep mode - 01
ATmega8 Introduction
ATmega8 is a low-power 8-bit CMOS microcontroller based on the enhanced AVR RISC structure. Due to its advanced instruction set and single-clock cycle instruction execution time, ATmega8 has a data throughput of up to 1 MIPS/MHz, which can alleviate the contradiction between power consumption and processing speed in t
[Microcontroller]
DHT11 temperature and humidity sensor AVR microcontroller routine
/************************************************************************ Program: ICC-AVR  Function: Baud rate 9600 Serial port sends temperature and humidity data   Data format: humidity temperature checksum For              example, the serial port sends data 59 00 24 00 53, then the humidity is 59, the temperature
[Microcontroller]
DHT11 temperature and humidity sensor AVR microcontroller routine
AVR microcontroller-controlled electric bicycle drive system
#include  #include  //Electric vehicle dual closed loop program, using dual closed loop method to control the motor to get the best zh speed performance, and can //Limit the maximum current of the motor. This application uses two CCP components, CCP1 is used for PWM output to control //Control motor voltage; CCP
[Microcontroller]
AVR ATMega16 displays infrared codes on segment LCD
Hardware: ATMega16 (8MRC) + HT1621 + integrated infrared receiver Idea: Infrared decoding uses interrupt capture mode (NEC encoding), and the display uses LCD driver HT1261 The program is as follows (compiled in WinAVR GCC environment): #include avr/io.h #include avr/delay.h   #include avr/signal.h #include avr/inte
[Microcontroller]
AVR Fuse Bits Quick Start
AVR uses fuses to control some functions inside the chip, such as JTAG, clock usage, power-off detection voltage, whether debugging is allowed, etc. The STK500 in AVR Studio has a huge advantage in handling fuse bits: it allows users to configure them in the form of functional combinations. Compared with PnoyPr
[Microcontroller]
AVR Fuse Bits Quick Start
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号