SPWM Program for AVR Microcontroller

Publisher:a407895356Latest update time:2015-12-31 Source: eefocusKeywords:AVR Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
#include //There are only SPWM-related programs in the program now, and other programs have been deleted

#include

#pragma interrupt_handler T0:17 //Timer 0 overflow interrupt vector


unsigned int SPWM_OCR1A[101]={493,492,491,490,489,488,487,486,485,484,
                              483,482,481,479,478,477,476,475,474,473,
         472,471,470,469,468,46 7,466,465,464,463,
         462,462,461,460,459,458,457,456,455,454,
         453,452,451,450,449,448,448,447,446,445,
         444,443,442,441,440,440,439,438,437,436,
         435,434,434,433,432,431,430,429,429,428,
         427,426,425,424,424,423,422,421,420,420,
         419,418,417,416,416,415,414,413,413,412,
         411,410,410,409,408,407,407,406,405,404,404,};
        //The values ​​in the array correspond to the values ​​of OCR1A from 45.0 to 55.0 Hz, (each cycle is divided into 360 parts, i.e., one degree is one part) with a step of 0.1 Hz. When writing the program,
                             //just Replace the value of OCR1A with the corresponding value above to get the corresponding frequency of the sine wave
double SPWM_sin_value[360]={1.0174,1.0348,1.0523,1.0697,1.0871,1.1045,1.1218,1.1391,1.1564,1.1736,1.1908, 1.2079,1.2249,1.2419,
                1.2588,1.2756,1.2923,1.309,1.3255,1.342,1.3583,1.3746,1.3907,1.4067,1.4226,1.4383,1.4539,1.4694,1.4848,1.4999,
                1.515,1.529 9,1.5446,1.5591,1.5735,1.5877,1.6018,1.6156,1.6293, 1.6427,1.656,1.6691,1.6819,1.6946,1.7071,1.7193,
                1.7313,1.7431,1.7547,1.766,1.7771,1.788,1.7986,1.809,1.8191,1.829,1.8386,1.848,1.8571,1.866,1.8746,1.8829,1.891,
                1.8987,1. 9063,1.9135,1.9205,1.9271,1.9335,1.9396,1.9455, 1.951,1.9563,1.9612,1.9659,1.9702,1.9743,1.9781,1.9816,
                1.9848,1.9876,1.9902,1.9925,1.9945,1.9961,1.9975,1.9986,1.9993,1.9998,2,1.9998,1.9993,1.9986,1.9975,1.9961,1.9945,
                1.9925, 1.9902,1.9876,1.9848,1.9816,1.9781,1.9743,1.9702, 1.9659,1.9612,1.9563,1.951,1.9455,1.9396,1.9335,1.9271,
                1.9205,1.9135,1.9063,1.8987,1.891,1.8829,1.8746,1.866,1.8571,1.848,1.8386,1.829,1.8191,1.809,1.7986,1.788,1.7771,
                1.766,1. 7547,1.7431,1.7313,1.7193,1.7071,1.6946,1.6819, 1.6691,1.656,1.6427,1.6293,1.6156,1.6018,1.5877,1.5735,
                1.5591,1.5446,1.5299,1.515,1.5,1.4848,1.4694,1.4539,1.4383,1.4226,1.4067,1.3907,1.3746,1.3583,1.342,1.3255,1.309,
                1.2923,1 .2756,1.2588,1.2419,1.2249,1.2079,1.1908,1.1736, 1.1564,1.1391,1.1218,1.1045,1.0871,1.0697,1.0523,1.0348,
                1.0174,1,0.9825,0.9651,0.9476,0.9302,0.9128,0.8954,0.8781,0.8608,0.8435,0.8263,0.8091,0.792,0.775,0.758,0.7411,
                0.7243,0.7 076,0.6909,0.6744,0.6579,0.6416,0.6253,0.6092, 0.5932,0.5773,0.5616,0.546,0.5305,0.5151,0.5,0.4849,0.47,
                0.4553,0.4408,0.4264,0.4122,0.3981,0.3843,0.3706,0.3572,0.3439,0.3308,0.318,0.3053,0.2928,0.2806,0.2686,0.2568,0.2452,
                0.2 339,0.2228,0.2119,0.2013,0.1909,0.1808,0.1709,0.1613, 0.1519,0.1428,0.1339,0.1253,0.117,0.1089,0.1012,0.0936,
                0.0864,0.0794,0.0728,0.0664,0.0603,0.0544,0.0489,0.0436,0.0387,0.034,0.0297,0.0256,0.0218,0.0183,0.0151,0.0123,
                0.0097,0.0 074,0.0054,0.0038,0.0024,0.0013,0.0006,0.0001,0, 0.0001,0.0006,0.0013,0.0024,0.0038,0.0054,0.0074,0.0097,
                0.0123,0.0151,0.0183,0.0218,0.0256,0.0297,0.034,0.0387,0.0436,0.0489,0.0544,0.0603,0.0664,0.0728,0.0794,0.0864,0.0936,
                0.1 012,0.1089,0.117,0.1253,0.1339,0.1428,0.1519,0.1613, 0.1709,0.1808,0.1909,0.2013,0.2119,0.2228,0.2339,0.2452,
                0.2568,0.2686,0.2806,0.2928,0.3053,0.318,0.3308,0.3439,0.3572,0.3706,0.3843,0.3981,0.4122,0.4264,0.4408,0.4553,
                0.47,0.484 9,0.4999,0.5151,0.5305,0.546,0.5616,0.5773,0.5932, 0.6092,0.6253,0.6416,0.6579,0.6744,0.6909,0.7076,0.7243,
                0.7411,0.758,0.775,0.792,0.8091,0.8263,0.8435,0.8608,0.8781,0.8954,0.9128,0.9302,0.9476,0.9651,0.9825,0.9999,};        
        
   
unsigned int M=100;

 

void PORT_inti() // Port initialization
{
DDRA=0xff;
PORTA=0x00;
DDRB=0xff; // Ports B, D, E are output
PORTB=0x00; // The initial values ​​of the ports are all 0, or in other words, they do not have pull-up resistors
DDRD=0xff;
DDRE=0x00;
PORTD=0x00;
PORTE=0x00;
DDRF=0X00; // AD conversion port
PORTF=0X00; 
DDRG=0X07;
DDRC=0X00;
}
   
 void TO_inti() // Timer 0 initialization
{
TCCR0=0x02; // 8-division
TCNT0=0x9C; // Determine the interrupt time, 0.05ms interrupt once
TIMSK|=BIT(0); // T/C0 overflow interrupt enable
}


void T1_inti()
{
TCNT1H=0xfc; //Counter
clearedTCNT1L=0xe0;    
TCCR1A=0x2d; // 0010 1101 frequency phase correction PWM, when a match occurs, the level is 0
TCCR1B=0x11; // 0001 0001 no frequency division, if the frequency is divided, the switching frequency will not be that high
TCCR1C=0x00; //No effectOCR1A
=493; // In this mode, the value in the maximum value register OCR1A is related to the frequency of the square wave and the duty cycle of the square wave
}
 
void T0() //In the interrupt program of timer 0, send the values ​​in the sine function table to OCR1A and OCR1B. Adjusting the values ​​of M and OCR1A can adjust the frequency and amplitude
{   

 if(count<360)
   {   
   OCR1B=SPWM_sin_value[count]*M+OCR1A/2-M; //The formula should be OCR1A=OCR1A/2+M(SPWM_sin_value[count]-1) where OCR1A is the maximum value of the triangle wave, M is the amplitude adjustment coefficient, the range is 0~OCR1A/2, (SPWM_sin_value[count]-1) minus one should be made because the sine table is a full positive table, now it needs to be changed to a table between (-1~+1), adjusting the value of OCR1A can adjust the switching frequency, adjusting the value of M can adjust the duty cycle of the related square wave
   OCR1C=SPWM_sin_value[count]*M+OCR1A/2-M
   count++;
          }
 else 
 count=0;  
        TCNT0=0x9C; //The time used can be further shortened, so that a better transmission effect can be achieved, but it also occupies more clock resources of the microcontroller. 
}

 

void main()
{
 PORT_inti(); //Port initialization
 TO_inti(); //Generate kHZ switching frequency
 T1_inti(); //Phase-corrected PWM is used to generate two-way SPWM waves
    while(1) //Infinite loop, waiting for interruption
    { 
    }
}

Keywords:AVR Reference address:SPWM Program for AVR Microcontroller

Previous article:Application of delay function in avr-gcc
Next article:avrI/O port operation and pull-up resistors

Recommended ReadingLatest update time:2024-11-15 09:15

The single chip microcomputer controls the digital tube to move left
#include reg52.h sbit LATCH1=P2^2; //segment latch sbit LATCH2=P2^3; //bit latch unsigned char code DuanMa ={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; // Display segment code value 0~9 unsigned char code WeiMa ={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; // respectively correspond to the corresponding dig
[Microcontroller]
#51 MCU# Characteristics of Modbus communication protocol and introduction of RTU protocol frame data
Features of Modbus communication protocol Modbus protocol is a universal language used in electronic controllers. Through this protocol, controllers can communicate with each other and with other devices via the network. Control devices produced by different manufacturers can be connected into an industrial network fo
[Microcontroller]
#51 MCU# Characteristics of Modbus communication protocol and introduction of RTU protocol frame data
Analysis of the reset function inside the AVR microcontroller
Reset of AVR microcontroller: watchdog reset, power-on reset, power-off reset. I see that everyone is not very clear about the reset function, especially the reset function inside the AVR microcontroller. Now I will briefly talk about it here, and I hope you can have a general understanding at home. There are four t
[Microcontroller]
Analysis of the reset function inside the AVR microcontroller
A brief note on bit operations in C language in single-chip microcomputers and embedded systems
I wrote a PIC project, and I have a lot of experience with bit operations in C language, so I would like to record it. For example, a char type variable is defined in the program, purge_short_enable_flag, //bit 0 -- start short circuit circle timing //bit 1 --short circuit enable //bit 2 --purge H2 enable So when co
[Microcontroller]
Application and Program of 74HC595 in Single Chip Microcomputer
The 74HC595 has an 8-bit serial-input and parallel-output three-state gate circuit. High speed: up to 55MHz (5V operating voltage) Wide operating voltage range: 2V -6V Maximum current value of each port (QA-QH): ±35mA When the I/O resources of the microcontroller are limited, using 595 and cascading it is a good
[Microcontroller]
Analysis of the calculation method of stack in MCS51 single chip computer programming
Using C language to program MCS51 series microcontrollers is an inevitable trend in the development and application of microcontrollers. Keil's C51 compiler supports the classic 8051 and 8051 derivatives, commonly known as Cx51. It should be said that Cx51 is an extension of C language on MCS51 microcontrollers. It ha
[Microcontroller]
Analysis of the calculation method of stack in MCS51 single chip computer programming
The most effective way to learn microcontrollers
  For a beginner of single-chip microcomputer, if you learn in a textbook way, you will start with a lot of instructions and nouns. After learning for a long time, you still can't figure out what these instructions do and what actual effects they can produce. Then you may feel boring and give up halfway in a few days.
[Microcontroller]
Detailed explanation of the internal clock circuit of MCS-51 single chip microcomputer
There is a high-gain inverting amplifier inside the MCS-51 microcontroller chip, whose input is the chip pin XTAL1 and whose output is the pin XTAL2. Outside the chip, a crystal oscillator and a fine-tuning capacitor are connected across XTAL1 and XTAL2 to form a stable self-excited oscillator, which is the clock circ
[Microcontroller]
Detailed explanation of the internal clock circuit of MCS-51 single chip microcomputer
Latest Microcontroller Articles
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号