#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
{
}
}
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
- Popular Resources
- Popular amplifiers
- Wireless Sensor Network Technology and Applications (Edited by Mou Si, Yin Hong, and Su Xing)
- Modern Electronic Technology Training Course (Edited by Yao Youfeng)
- Modern arc welding power supply and its control
- Small AC Servo Motor Control Circuit Design (by Masaru Ishijima; translated by Xue Liang and Zhu Jianjun, by Masaru Ishijima, Xue Liang, and Zhu Jianjun)
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
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
- Detailed explanation of intelligent car body perception system
- How to solve the problem that the servo drive is not enabled
- Why does the servo drive not power on?
- What point should I connect to when the servo is turned on?
- How to turn on the internal enable of Panasonic servo drive?
- What is the rigidity setting of Panasonic servo drive?
- How to change the inertia ratio of Panasonic servo drive
- What is the inertia ratio of the servo motor?
- Is it better for the motor to have a large or small moment of inertia?
- What is the difference between low inertia and high inertia of servo motors?
- The firmware flashing tool is perfectly adapted to esp32-c3
- Ask about the selection of bidirectional thyristor
- 2-way ADC0808 measurement digital tube display assembly program
- [TI star product limited time purchase] shopping experience
- Infineon SiC Product Catalog
- Another new board has arrived~~Jiefa AC7801X motor demo board is here, and the evaluation activity will be online soon...
- Which expert can help me solve the volume control problem of the digital-analog chip WM8766G?
- Recommend! Good domestic chips you have used
- I heard someone bought a fake chip? I'm doubting myself after adjusting the fake chip
- Warm congratulations to the Chinese men's football team for drawing with the powerful Philippines