|
哪位大神帮我修改这段代码,可以使用工作室上位机程序。
- #include "reg51.h"
- #include <string.h>
- typedef bit BOOL;
- typedef unsigned char BYTE;
- typedef unsigned int WORD;
- #define BAUD 0xFE80 // 9600bps @ 11.0592MHz
- sfr AUXR = 0x8E;
- BYTE PWM_T = 0; //占空比控制变量,从0到100,表示每个PWM波周期中低电平(LED点亮)的长短,
- bit onflag = 1;
- BYTE come=0;
- BYTE ser_receive; //串口接收到的数据
-
- sbit IN1 = P3^0;
- sbit IN2 = P3^1;
- sbit IN3 = P3^2;
- sbit IN4 = P3^3;
- sbit PWM_LED=P3^4;
- sbit RXB = P3^5; //define UART TX/RX port
- BYTE gear; //0,1,2,3 三档调速 0为全速
- bit IN=0; //调速
- BYTE direction=0; //1.前进 2.后退 3.左 4.右 0 停止
- BYTE TBUF,RBUF;
- BYTE TDAT,RDAT;
- BYTE TCNT,RCNT;
- BYTE TBIT,RBIT;
- BOOL TING,RING;
- BOOL TEND,REND;
- void UART_INIT();
- BYTE t, r;
- BYTE buf[6];
- void main()
- {
-
- bit flag = 1; //控制灯渐亮渐熄方式,1表示渐亮。渐亮其实就是说随着占空比的增加
-
- PWM_LED=0;
- direction =0; //1.前进 2.后退 3.左 4.右 0 停止
- gear=2; //开始的时候速度为第二档
- TMOD = 0x00; //timer0 in 16-bit auto reload mode
- AUXR = 0x80; //timer0 working at 1T mode
- TL0 = BAUD;
- TH0 = BAUD>>8; //initial timer0 and set reload value
- TR0 = 1; //tiemr0 start running
- ET0 = 1; //enable timer0 interrupt
- PT0 = 1; //improve timer0 interrupt priority
- EA = 1; //open global interrupt switch
- UART_INIT();
- while (1)
- {
- if(direction==0){
- IN1=0;
- IN2=0;
- IN3=0;
- IN4=0;
- }
- if(direction==1){
- IN1=0;
- IN2=IN;
- IN3=0;
- IN4=IN;
- }
- if(direction==2){
- IN1=IN;
- IN2=0;
- IN3=IN;
- IN4=0;
- }
- if(direction==3){
- IN1=IN;
- IN2=0;
- IN3=0;
- IN4=IN;
- }
- if(direction==4){
- IN1=0;
- IN2=IN;
- IN3=IN;
- IN4=0;
- }
- /*---^_^-----------^_^-------^_^---------^_^-------^_^---------^_^-------^_^---------^_^-------^_^------http://IslandDream.taobao.com-*/
- if(onflag == 1){
- //这个100个亮度级都经历了,小的话比如10可能跳跃性太大,不可控
- if(flag==1) //灯渐亮,占空比变量自增
- PWM_T++;
- else //灯渐熄,占空比自减
- PWM_T--;
-
- if(PWM_T>=100) //设置灯亮度级别为100,到此后开始逐渐变暗
- flag=0;
- if(PWM_T==0) //限定最低亮度级别为0,到此后又开始逐渐变亮
- flag = 1;
- }
- onflag=0;
- /*---^_^-----------^_^-------^_^---------^_^-------^_^---------^_^-------^_^---------^_^-------^_^------http://IslandDream.taobao.com-*/
-
- //user's function
- if (REND)
- {
- ser_receive=RBUF;
- REND = 0;
- if(come==1)
- {
- if(ser_receive!='\0'&&ser_receive!='a')
- buf[r++ & 0x0f] = ser_receive;
- if(r==6)
- {
- r=0;
- /*---^_^-----------^_^-------^_^---------^_^-------^_^---------^_^-------^_^---------^_^-------^_^------http://IslandDream.taobao.com-*/
- if((strstr(buf,"spedup")!= NULL) ) {
- if(gear==3) gear=0;
- else gear+=1;
- }
- if((strstr(buf,"speddo")!= NULL) ) {
- if(gear==0) gear=3;
- else gear-=1;
- }
- /*---^_^-----------^_^-------^_^---------^_^-------^_^---------^_^-------^_^---------^_^-------^_^------http://IslandDream.taobao.com-*/
- if((strstr(buf,"zeroze")!= NULL) ) {
- gear=0;//全速
- }
- if((strstr(buf,"oneone")!= NULL) ) {
- gear=1;//一速
- }
- if((strstr(buf,"twotwo")!= NULL) ) {
- gear=2;//二速
- }
- if((strstr(buf,"threet")!= NULL) ) {
- gear=3;//全速
- }
- if((strstr(buf,"upupup")!= NULL) ) {
- direction=1;
- }
- if((strstr(buf,"downdo")!= NULL) ) {
- direction=2;
-
- }
- if((strstr(buf,"leftle")!= NULL) ) {
- direction=3;
- }
- if((strstr(buf,"rightr")!= NULL) ) {
- direction=4;
- }
- if((strstr(buf,"puusep")!= NULL) ) {
- direction=0;
- }
- come=0;
- }
- }
- else if(ser_receive=='a')
- {
- come=1;
- }
- }
- if (TEND)
- {
- if (t != r)
- {
- TEND = 0;
- TBUF = buf[t++ & 0x0f];
- TING = 1;
- }
- }
- }
- }
- /*---^_^-----------^_^-------^_^---------^_^-------^_^---------^_^-------^_^---------^_^-------^_^------http://IslandDream.taobao.com-*/
- void tm0() interrupt 1 using 1
- {
- /*---^_^-----------^_^-------^_^---------^_^-------^_^---------^_^-------^_^---------^_^-------^_^------http://IslandDream.taobao.com-*/
- static BYTE t,t1,t2 ; //PWM计数
- t++; //每次定时器溢出加1
- t1++;
- /*---^_^-----------^_^---用于调速----^_^---------^_^-------^_^---------^_^-------^_^---------^_^-------http://IslandDream.taobao.com-*/
- if(gear == 0)
- {
- IN=1;
- }
- if(gear != 0)
- {
-
- IN=1;
- t2++;
-
- if(t2==(gear+1))
- {
- IN=0;
- t2=0;
- }
-
- }
- /*---^_^-----------^_^-------^_^---------^_^-------^_^---------^_^-------^_^---------^_^-------^_^------http://IslandDream.taobao.com-*/
- if(t1==150)
- {
- t1=0;
- onflag = 1;
- }
- if(t==100) //PWM周期 100个单位
- {
- t=0; //使t=0,开始新的PWM周期
- PWM_LED=0; //使LED灯亮 ,输出端口 ,每次开始一个新的PWM波,
- //都是先低电平(LED亮),亮PWM_T个单位时间。
- }
- if(PWM_T==t) //按照当前占空比切换输出为高电平 ,使LED灯灭 。
- //其实本程序的占空比是从0%,1%,…,99%,100%,99%,…,2%,…导致渐亮渐暗
- PWM_LED=1;
- /*---^_^-----------^_^-------^_^---------^_^-------^_^---------^_^-------^_^---------^_^-------^_^------http://IslandDream.taobao.com-*/
- if (RING)
- {
- if (--RCNT == 0)
- {
- RCNT = 3; //reset send baudrate counter
- if (--RBIT == 0)
- {
- RBUF = RDAT; //save the data to RBUF
- RING = 0; //stop receive
- REND = 1; //set receive completed flag
- }
- else
- {
- RDAT >>= 1;
- if (RXB) RDAT |= 0x80; //shift RX data to RX buffer
- }
- }
- }
- else if (!RXB)
- {
- RING = 1; //set start receive flag
- RCNT = 4; //initial receive baudrate counter
- RBIT = 9; //initial receive bit number (8 data bits + 1 stop bit)
- }
-
- if (--TCNT == 0)
- {
- TCNT = 3; //reset send baudrate counter
- if (TING) //judge whether sending
- {
- if (TBIT == 0)
- {
- // TXB = 0; //send start bit
- TDAT = TBUF; //load data from TBUF to TDAT
- TBIT = 9; //initial send bit number (8 data bits + 1 stop bit)
- }
- else
- {
- TDAT >>= 1; //shift data to CY
- if (--TBIT == 0)
- {
- // TXB = 1;
- TING = 0; //stop send
- TEND = 1; //set send completed flag
- }
- else
- {
- // TXB = CY; //write CY to TX port
- }
- }
- }
- }
- }
- /*---^_^-----------^_^-------^_^---------^_^-------^_^---------^_^-------^_^---------^_^-------^_^------http://IslandDream.taobao.com-*/
- void UART_INIT()
- {
- TING = 0;
- RING = 0;
- TEND = 1;
- REND = 0;
- TCNT = 0;
- RCNT = 0;
- }
复制代码
|
|