Q: Timer 1 controls 9 PWM channels. I have heard from experts that 8 channels are usually appropriate because it seems that one cycle of the servo is divided into 8 parts to control them separately. If it exceeds 8 channels, it seems that the cycle may change.
A: Yes, each PWM channel has about 2.5ms to occupy the timer independently, 8*2.5 is exactly 20ms, and I only use the range of each servo from 0.7 to 2.0, so there is no interference between the 9 channels. But there is interference between the timers.
Q: I saw a small bug. The cycle from pwm0 to pwm8 and then to pwm0 may not be 20ms.
A: Hey, I combined the low level time of each channel, then looked at its assembly code, corrected the time used by the interrupt statement, calculated it on the host computer, and used Keil simulation to get a very accurate result. When the two timers are not interrupted at the same time, the accuracy is 0.5us. Once they are interrupted at the same time, the error of one PWM channel will reach 7 or 8us.
Well, this interrupt program has not ended. If other interrupts occur, the subsequent interrupts will be lost. So you have to use a microcontroller with good performance. Haha.
Note that you must select the pdata mode when compiling this program, otherwise it will fail. This is the project file download that has already set the mode: http://www.51hei.com/f/duoji_c.rarProgram
source code:
/******************************************************************************
File name: pwm_18out.cFunction
description: Use STC89C52RC as the lower computer to control 18-channel servo PWM
double speed, machine cycle 0.5us
Date: 2012-3-19Author
: cazy peach-ATP
Remarks: 18-channel servo control lower computer
**************************************************************************/
#define uint unsigned int
#define uchar unsigned char
#include
sbit pwm9=P1^0;
sbit pwm10=P1^1;
sbit pwm11=P1^2;
sbit pwm12=P1^3;
sbit pwm13=P1^4;
sbit pwm14=P1^5;
sbit pwm15=P1^6;
sbit pwm16=P1^7;
sbit pwm17=P0^7;
sbit pwm0=P2^7; sbit pwm1
=
P2^6;
sbit pwm2=P2^5; sbit pwm3=P2^4;
sbit pwm4=P2^3
;
sbit pwm5=P2 ^2
;
sbit pwm6=P2^1; sbit pwm7=P2^0
;
//Instruction format: two bytes for one PWM channel, the first one is the TH value of the PWM high level timer, and the second one is the TL value, which is calculated by the host computer, for a total of 20 groups. The last two groups are the sum of the low levels of each of the 9 channels of PWM.
uint data pwm_val[40]={
0XF4, 0XF0, 0XF7, 0X9B, 0XF6, 0XAE,
0XF4, 0XF9, 0XF8, 0X01, 0XF6, 0X8F, 0XF5,
0X02, 0XF7, 0X2B, 0XF6, 0X9D, 0 XF4
, 0XF0, 0XF3, 0X1D, 0XF3, 0X16, 0XF4
, 0XF9, 0XF2, 0X8B, 0XF3, 0XB0,
0XF5, 0X02, 0XF2, 0XE9, 0XF3, 0X9B,
0XBB, 0X55, 0XD3, 0X04};
uchar zhilin[61];
uchar *point;
uchar data pwm_select=0;
uchar data pwm_scan=0; //timer1 uses
uchar data pwm_scan1=0; //timer2 uses
uchar data SCI_get=0;
//**************************Function declaration****************************
void SCI_INT(void) ;
void timer_INT(void) ;
//******************************Main function********************************
void main()
{
uchar j;
P0=0X00;
P1=0x00;
P2=0X00;
point=zhilin;
SCI_INT();
timer_INT();
for(j=0;j<61;j++){zhilin[j]=0;}
while(1)
{
for(;;) //Instruction receiving
{
if(RI){RI=0;SCI_get=SBUF;*point++=SCI_get;j++;}else{continue;}
if(SCI_get==0xff){point=zhilin;break;} //0xff instruction terminator
}
for(j=0;j<21;j++) //Instruction processing
{
if(zhilin[j*3]==255){break;} //Judge whether it is the end of instruction
pwm_select=zhilin[j*3];
pwm_val[pwm_select]=zhilin[j*3+1];
pwm_val[pwm_select+1]=zhilin[j*3+2];
}
}
}[page]
//***************************Timer interrupt**************************
void timer0() interrupt 1 using 1
{
if(pwm_scan==1) //1st pwm.
{
pwm0=1;
TH0=pwm_val[0];
TL0=pwm_val[1];
}
else if(pwm_scan==2) //2nd pwm.
{
pwm0=0;
pwm1=1;
TH0=pwm_val[2];
TL0=pwm_val[3];
}
else if(pwm_scan==3) //No. 3 pwm.
{
pwm1=0;
pwm2=1;
TH0=pwm_val[4];
TL0=pwm_val[5];
}
else if(pwm_scan==4) //The 4th pwm.
{
pwm2=0;
pwm3=1;
TH0=pwm_val[6];
TL0=pwm_val[7];
}
else if(pwm_scan==5) //No. 5 pwm.
{
pwm3=0;
pwm4=1;
TH0=pwm_val[8];
TL0=pwm_val[9];
}
else if(pwm_scan==6) //The 6th pwm.
{
pwm4=0;
pwm5=1;
TH0=pwm_val[10];
TL0=pwm_val[11];
}
else if(pwm_scan==7) //The 7th pwm.
{
pwm5=0;
pwm6=1;
TH0=pwm_val[12];
TL0=pwm_val[13];
}
else if(pwm_scan==8) //The 8th pwm.
{
pwm6=0;
pwm7=1;
TH0=pwm_val[14];
TL0=pwm_val[15];
}
else if(pwm_scan==9) //9th pwm.
{
pwm7=0;
pwm8=1;
TH0=pwm_val[16];
TL0=pwm_val[17];
}
else if(pwm_scan==10) //Low level.
{
pwm8=0;
TH0=pwm_val[36];
TL0=pwm_val[37];
pwm_scan=0;
}
pwm_scan++;
}
void timer1() interrupt 3 using 2
{
if(pwm_scan1==1) //The 10th pwm.
{
pwm9=1;
TH1=pwm_val[18];
TL1=pwm_val[19];
}
else if(pwm_scan1==2) //The 11th pwm.
{
pwm9=0;
pwm10=1;
TH1=pwm_val[20];
TL1=pwm_val[21];
}
else if(pwm_scan1==3) //The 12th pwm.
{
pwm10=0;
pwm11=1;
TH1=pwm_val[22];
TL1=pwm_val[23];
}
else if(pwm_scan1==4) //The 13th pwm.
{
pwm11=0;
pwm12=1;
TH1=pwm_val[24];
TL1=pwm_val[25];
}
else if(pwm_scan1==5) //No. 14 pwm.
{
pwm12=0;
pwm13=1;
TH1=pwm_val[26];
TL1=pwm_val[27];
}
else if(pwm_scan1==6) //No. 15 pwm.
{
pwm13=0;
pwm14=1;
TH1=pwm_val[28];
TL1=pwm_val[29];
}
else if(pwm_scan1==7) //No. 16 pwm.
{
pwm14=0;
pwm15=1;
TH1=pwm_val[30];
TL1=pwm_val[31];
}
else if(pwm_scan1==8) //The 17th pwm.
{
pwm15=0;
pwm16=1;
TH1=pwm_val[32];
TL1=pwm_val[33];
}
else if(pwm_scan1==9) //The 18th pwm.
{
pwm16=0;
pwm17=1;
TH1=pwm_val[34];
TL1=pwm_val[35];
}
else if(pwm_scan1==10) //Low level.
{
pwm17=0;
TH1=pwm_val[38];
TL1=pwm_val[39];
pwm_scan1=0;
}
pwm_scan1++;
}
//************************ *************Function**********************
void timer_INT(void) //timer0, timer1 initialization
{
uchar a ,b,c;
TMOD|=0X11;
IE|=0X8A; //EA=1;ET0=1;ET1=1;
TH0=256;
TL0=255;
TH1=256;
TL1=255;
TR0=1;
for (c=1;c>0;c--)
{
for(b=38;b>0;b--)
{
for(a=130;a>0;a--);
}
}
TR1=1;
}
void SCI_INT(void) //timer2
{
SCON=0X50;
T2CON=0X30;
TL2=RCAP2L=0XB2; //9600 0XFFB2, 19200 0XFFD9
TH2=RCAP2H=0XFF;
PCON=0X00;
TR2=1;
}
Previous article:51 single chip microcomputer drives servo motor program
Next article:51 MCU program for steering gear speed control
- Popular Resources
- Popular amplifiers
- Siemens Motion Control Technology and Engineering Applications (Tongxue, edited by Wu Xiaojun)
- Easy to play with STM32 microcontroller (Yang Baijun)
- Linux-UNIX System Programming Manual (Volumes 1 and 2)
- Learn electronics and Arduino from scratch: A beginner\'s guide to the development board (with full-color illustrations)
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
- Pressure sensor problem
- [Popular Science] What is an attenuator? How to use an attenuator?
- Using FPGA to implement optimized fingerprint recognition preprocessing algorithm.pdf
- MSP430 Series Embedded Experiment Tutorial---Basics
- About MicroPython ESP8266 WIFI settings problem - thanks!
- [STM32WB55 review]——by freebsder
- There is a large supply of microcontrollers!
- Share the C language program of FIR filter implemented by MSP430 microcontroller
- [2022 Digi-Key Innovation Design Competition] CAN Communication Data Display Terminal
- Oscilloscope waveform jitter can be turned into stable waveform by setting trigger properly