C program for microcontroller to control 18-way servos

Publisher:SerendipitySoulLatest update time:2015-05-19 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
About this program:

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;
}

Reference address:C program for microcontroller to control 18-way servos

Previous article:51 single chip microcomputer drives servo motor program
Next article:51 MCU program for steering gear speed control

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号