小R科技-WIFI机器人网·机器人创意工作室

 找回密码
 立即注册
查看: 5303|回复: 0

哪位大神帮我修改这段代码,可以使用工作室上位机程序。

[复制链接]
发表于 2014-11-27 01:03:17 | 显示全部楼层 |阅读模式
哪位大神帮我修改这段代码,可以使用工作室上位机程序。
  1. #include "reg51.h"
  2. #include <string.h>

  3. typedef bit BOOL;
  4. typedef unsigned char BYTE;
  5. typedef unsigned int WORD;


  6. #define BAUD  0xFE80   // 9600bps @ 11.0592MHz
  7. sfr AUXR = 0x8E;


  8. BYTE PWM_T = 0;   //占空比控制变量,从0到100,表示每个PWM波周期中低电平(LED点亮)的长短,
  9. bit onflag = 1;

  10. BYTE come=0;
  11. BYTE ser_receive;         //串口接收到的数据
  12.   
  13. sbit IN1 = P3^0;
  14. sbit IN2 = P3^1;       
  15. sbit IN3 = P3^2;
  16. sbit IN4 = P3^3;       
  17. sbit PWM_LED=P3^4;
  18. sbit RXB = P3^5;                                 //define UART TX/RX port

  19. BYTE gear;                                                                        //0,1,2,3 三档调速        0为全速
  20. bit IN=0;                                                                //调速
  21. BYTE direction=0;                                                         //1.前进 2.后退 3.左 4.右 0 停止

  22. BYTE TBUF,RBUF;
  23. BYTE TDAT,RDAT;
  24. BYTE TCNT,RCNT;
  25. BYTE TBIT,RBIT;
  26. BOOL TING,RING;
  27. BOOL TEND,REND;

  28. void UART_INIT();

  29. BYTE t, r;
  30. BYTE buf[6];

  31. void main()
  32. {
  33.       
  34.           bit flag = 1;     //控制灯渐亮渐熄方式,1表示渐亮。渐亮其实就是说随着占空比的增加
  35.           
  36.           PWM_LED=0;
  37.       direction =0;   //1.前进 2.后退 3.左 4.右 0 停止
  38.           gear=2;                 //开始的时候速度为第二档

  39.       TMOD = 0x00;                           //timer0 in 16-bit auto reload mode
  40.       AUXR = 0x80;                           //timer0 working at 1T mode
  41.       TL0 = BAUD;
  42.       TH0 = BAUD>>8;                          //initial timer0 and set reload value
  43.       TR0 = 1;                                //tiemr0 start running
  44.       ET0 = 1;                                //enable timer0 interrupt
  45.       PT0 = 1;                                //improve timer0 interrupt priority
  46.       EA = 1;                                 //open global interrupt switch

  47.       UART_INIT();

  48.       while (1)
  49.       {      

  50.             if(direction==0){
  51.                         IN1=0;                       
  52.                         IN2=0;                               
  53.                         IN3=0;                       
  54.                         IN4=0;       
  55.       }
  56.                 if(direction==1){
  57.                         IN1=0;
  58.                         IN2=IN;
  59.                         IN3=0;
  60.                         IN4=IN;
  61.       }
  62.                 if(direction==2){
  63.                         IN1=IN;
  64.                         IN2=0;
  65.                         IN3=IN;
  66.                         IN4=0;
  67.       }
  68.                 if(direction==3){
  69.                         IN1=IN;
  70.                         IN2=0;
  71.                         IN3=0;
  72.                         IN4=IN;
  73.       }
  74.                 if(direction==4){
  75.                         IN1=0;
  76.                         IN2=IN;
  77.                         IN3=IN;
  78.                         IN4=0;
  79.       }

  80. /*---^_^-----------^_^-------^_^---------^_^-------^_^---------^_^-------^_^---------^_^-------^_^------http://IslandDream.taobao.com-*/
  81. if(onflag == 1){
  82.                          //这个100个亮度级都经历了,小的话比如10可能跳跃性太大,不可控
  83.   if(flag==1)      //灯渐亮,占空比变量自增

  84.    PWM_T++;  

  85.   else       //灯渐熄,占空比自减

  86.    PWM_T--;



  87.   if(PWM_T>=100) //设置灯亮度级别为100,到此后开始逐渐变暗

  88.    flag=0;

  89.   if(PWM_T==0)   //限定最低亮度级别为0,到此后又开始逐渐变亮

  90.    flag = 1;

  91. }      
  92. onflag=0;
  93. /*---^_^-----------^_^-------^_^---------^_^-------^_^---------^_^-------^_^---------^_^-------^_^------http://IslandDream.taobao.com-*/
  94.           
  95.                 //user's function
  96.             if (REND)
  97.             {        
  98.                             ser_receive=RBUF;
  99.                             REND = 0;
  100.                                 if(come==1)
  101.                                                 {
  102.                                                         if(ser_receive!='\0'&&ser_receive!='a')
  103.                                                         buf[r++ & 0x0f] = ser_receive;
  104.                                                         if(r==6)
  105.                                                         {
  106.                                                            r=0;
  107. /*---^_^-----------^_^-------^_^---------^_^-------^_^---------^_^-------^_^---------^_^-------^_^------http://IslandDream.taobao.com-*/
  108.                                                           if((strstr(buf,"spedup")!= NULL) ) {
  109.                                                                 if(gear==3) gear=0;       
  110.                                                                         else gear+=1;                                          
  111.                                                                   }

  112.                                                           if((strstr(buf,"speddo")!= NULL) ) {
  113.                                                                      if(gear==0) gear=3;       
  114.                                                                         else gear-=1;                                                  
  115.                                                                   }
  116. /*---^_^-----------^_^-------^_^---------^_^-------^_^---------^_^-------^_^---------^_^-------^_^------http://IslandDream.taobao.com-*/
  117.                                                            if((strstr(buf,"zeroze")!= NULL) ) {
  118.                                                                      gear=0;//全速                                                  
  119.                                                                   }
  120.                                                            if((strstr(buf,"oneone")!= NULL) ) {
  121.                                                                      gear=1;//一速                                                  
  122.                                                                   }
  123.                                                            if((strstr(buf,"twotwo")!= NULL) ) {
  124.                                                                      gear=2;//二速                                                  
  125.                                                                   }
  126.                                                                 if((strstr(buf,"threet")!= NULL) ) {
  127.                                                                      gear=3;//全速                                                  
  128.                                                                   }
  129.                                                             if((strstr(buf,"upupup")!= NULL) ) {
  130.                                                                         direction=1;          
  131.                                                                   }
  132.                                                                   if((strstr(buf,"downdo")!= NULL) )  {
  133.                                                                         direction=2;       
  134.                                                                
  135.                                                                   }
  136.                                                                   if((strstr(buf,"leftle")!= NULL) )  {
  137.                                                                         direction=3;                                                                               
  138.                                                                   }
  139.                                                                   if((strstr(buf,"rightr")!= NULL) )  {
  140.                                                                         direction=4;                                                                               
  141.                                                                   }
  142.                                                                    if((strstr(buf,"puusep")!= NULL) )  {
  143.                                                                     direction=0;
  144.                                                                   }
  145.                                                            come=0;
  146.                                                         }
  147.                                                 }
  148.                                                         else if(ser_receive=='a')
  149.                                                                                 {         
  150.                                                                                         come=1;               
  151.                                                                                 }
  152.             }

  153.             if (TEND)
  154.             {
  155.                   if (t != r)
  156.                   {
  157.                       TEND = 0;
  158.                       TBUF = buf[t++ & 0x0f];
  159.                       TING = 1;
  160.                   }
  161.             }
  162.       }
  163. }
  164. /*---^_^-----------^_^-------^_^---------^_^-------^_^---------^_^-------^_^---------^_^-------^_^------http://IslandDream.taobao.com-*/  

  165. void tm0() interrupt 1 using 1
  166. {
  167. /*---^_^-----------^_^-------^_^---------^_^-------^_^---------^_^-------^_^---------^_^-------^_^------http://IslandDream.taobao.com-*/
  168. static  BYTE  t,t1,t2 ;   //PWM计数
  169. t++;    //每次定时器溢出加1
  170. t1++;
  171. /*---^_^-----------^_^---用于调速----^_^---------^_^-------^_^---------^_^-------^_^---------^_^-------http://IslandDream.taobao.com-*/

  172.    if(gear == 0)
  173.   {       
  174.         IN=1;                   
  175.   }

  176. if(gear != 0)
  177. {       
  178.    
  179.         IN=1;
  180.         t2++;
  181.                        
  182.         if(t2==(gear+1))  
  183.       {
  184.                          IN=0;
  185.                          t2=0;                    
  186.       }
  187.           
  188.   }
  189. /*---^_^-----------^_^-------^_^---------^_^-------^_^---------^_^-------^_^---------^_^-------^_^------http://IslandDream.taobao.com-*/

  190. if(t1==150)  

  191. {
  192.   t1=0;  
  193.   onflag = 1;
  194. }
  195. if(t==100)   //PWM周期 100个单位

  196. {
  197.   t=0;  //使t=0,开始新的PWM周期
  198.   PWM_LED=0;  //使LED灯亮  ,输出端口 ,每次开始一个新的PWM波,
  199.             //都是先低电平(LED亮),亮PWM_T个单位时间。
  200. }

  201. if(PWM_T==t)  //按照当前占空比切换输出为高电平 ,使LED灯灭 。
  202.                //其实本程序的占空比是从0%,1%,…,99%,100%,99%,…,2%,…导致渐亮渐暗
  203.   PWM_LED=1;        
  204. /*---^_^-----------^_^-------^_^---------^_^-------^_^---------^_^-------^_^---------^_^-------^_^------http://IslandDream.taobao.com-*/
  205.       if (RING)
  206.       {
  207.             if (--RCNT == 0)
  208.             {
  209.                   RCNT = 3;                   //reset send baudrate counter
  210.                   if (--RBIT == 0)
  211.                   {
  212.                       RBUF = RDAT;              //save the data to RBUF
  213.                       RING = 0;                 //stop receive
  214.                       REND = 1;                 //set receive completed flag
  215.                   }
  216.                   else
  217.                   {
  218.                       RDAT >>= 1;
  219.                       if (RXB) RDAT |= 0x80;  //shift RX data to RX buffer
  220.                   }
  221.             }
  222.       }
  223.       else if (!RXB)
  224.       {
  225.             RING = 1;                         //set start receive flag
  226.             RCNT = 4;                         //initial receive baudrate counter
  227.             RBIT = 9;                         //initial receive bit number (8 data bits + 1 stop bit)
  228.       }
  229.      
  230.       if (--TCNT == 0)
  231.       {
  232.             TCNT = 3;                         //reset send baudrate counter
  233.             if (TING)                         //judge whether sending
  234.             {
  235.                   if (TBIT == 0)
  236.                   {
  237. //                      TXB = 0;                 //send start bit
  238.                       TDAT = TBUF;              //load data from TBUF to TDAT
  239.                       TBIT = 9;                //initial send bit number (8 data bits + 1 stop bit)
  240.                }
  241.                                else
  242.                   {
  243.                       TDAT >>= 1;             //shift data to CY
  244.                       if (--TBIT == 0)
  245.                       {
  246. //                            TXB = 1;
  247.                             TING = 0;           //stop send
  248.                             TEND = 1;           //set send completed flag
  249.                       }
  250.                       else
  251.                       {
  252. //                            TXB = CY;           //write CY to TX port
  253.                       }
  254.                   }
  255.             }
  256.       }
  257. }

  258. /*---^_^-----------^_^-------^_^---------^_^-------^_^---------^_^-------^_^---------^_^-------^_^------http://IslandDream.taobao.com-*/

  259. void UART_INIT()
  260. {
  261.       TING = 0;
  262.       RING = 0;
  263.       TEND = 1;
  264.       REND = 0;
  265.       TCNT = 0;
  266.       RCNT = 0;
  267. }
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

新品特惠推荐上一条 /2 下一条

QQ|QQ技术咨询1|QQ技术咨询2|商务合作微信1:xiaorgeek001|商务合作微信2:XiaoRGEEK|诚聘英才|Archiver|手机版|小R科技-WIFI机器人网·机器人创意工作室 ( 粤ICP备15000788号-6 )

GMT+8, 2024-4-25 22:57 , Processed in 1.070426 second(s), 20 queries .

Powered by XiaoR GEEK X3.4

© 2014-2021 XiaoR GEEK

快速回复 返回顶部 返回列表