📄 xsj_liudong_car.c
字号:
#include <reg51.h>
#include <absacc.h>
#include <stdio.h>
unsigned char TLV=0x00;
unsigned char THV=0xe7;
unsigned short SECTICKS=240; //24; //65536/(65536-(THV*256+TLV))*18;
unsigned short HALFSECTICKS=120; //12;
unsigned char sbuff[7];
unsigned char count=0,triggle=0,chno=0;
unsigned char ticks=0,oldP1Stat;
sbit RLed = P1^3;
bit direction=0,direction1=0,direction2=0;
/*void delay()
{
unsigned short i,j;
for(i=0;i<30;i++) j=i;
}*/
void t0int() interrupt 1
{
//unsigned char i;
TH0=THV; //Bcause TLV=0,no need to init TL0;
count++;
if ((oldP1Stat&0x10)==0x10 && (P1&0x10)==0x00) //UP triggle.
{
if ((P1&0x20)==0x20) direction1=1;
else direction1=0;
}
if ((oldP1Stat&0x20)==0x20 && (P1&0x20)==0x00) //UP triggle.
{
if ((P1&0x10)==0x10) direction2=1;
else direction2=0;
}
if(direction1 && !direction2) direction=1;
if(!direction1 && direction2) direction=0;
if((oldP1Stat^P1) & 0x10) ticks++;
if((oldP1Stat^P1) & 0x20) ticks++;
oldP1Stat=P1;
if(count == HALFSECTICKS)
{
RLed=1;
//triggle=1;
}
if(count == SECTICKS)
{
count=0;
RLed=0;
sbuff[3]=P1;
//Notice P1 had been modified.
if (direction) sbuff[3]=sbuff[3] | 0x20;
else sbuff[3]=(sbuff[3] & 0xDF);
//For not modify the host vb program,add following lines.
if (P1 & 0x10) sbuff[3] |=0x40; //deal pulse.
else sbuff[3] &= 0xbf;
if (P1 & 0x40) sbuff[3] |=0x10; //deal button.
else sbuff[3] &= 0xef;
//end
sbuff[4]=ticks;
ticks=0;
triggle=1;
}
}
void main (void)
{
unsigned char i;
sbuff[0]=0xD7;
sbuff[1]=6;
sbuff[2]='S';
sbuff[5]=0;
sbuff[6]='E';
SCON = 0x50; /* SCON: mode 1, 8-bit UART, enable rcvr */
TMOD |= 0x21; /* TMOD: timer 1, mode 2, 8-bit reload */
TH1 = 0xe8; //0xd8,for now is 11059200; //1200bps .0xfb; // 9600 bps,0xd8; /* TH1: reload value for 1200 baud @ 18.432MHz */
TR1 = 1; /* TR1: timer 1 run */
TR0 = 1;
TI = 1;
oldP1Stat=P1;
IE = 0x82;
while (1)
{
if (triggle)
{
triggle=0;
for(i=0;i<7;i++) putchar(sbuff[i]);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -