📄 query.c
字号:
#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
#define INBUF_LEN 4
uchar inbuf[INBUF_LEN];
uchar checksum,count3;
bit read_flag=0;
void InitComm(void)
{
SCON = 0x50; //SCON: serail mode 1, 8-bit UART, enable ucvr
TMOD |= 0x20; //TMOD: timer 1, mode 2, 8-bit reload
PCON |= 0x00; //SMOD=1;
TH1 = 0xF4; //Baud:2400 fosc=11.0592MHz
TL1 = 0xF4; //Baud:2400 fosc=11.0592MHz
// IE |= 0x90; //Enable Serial Interrupt
TR1 = 1; // timer 1 run
}
void SendChar(uchar ch)
{
SBUF=ch;
while(TI==0);
TI=0;
}
void SendString(uchar *str,uint strlen)
{
unsigned int k;
checksum = 0;
for (k=0;k<strlen;k++)
{
checksum ^= *(str+k);
}
checksum |= 0x80;
SendChar(checksum);
k = 0;
do
{
SendChar(*(str + k));
k++;
} while(k < strlen);
}
void serial () interrupt 4 using 1
{
uchar ch;
if(RI)
{
RI = 0;
ch=SBUF;
if(ch>127)
{
count3=0;
checksum= ch&0x7F;
}
else
{
inbuf[count3++]=ch;
checksum ^= ch;
if( (count3==INBUF_LEN) && ((checksum&0x7F)==0) )
{
read_flag=1; //如果串口接收的数据达到INBUF_LEN个,且校验没错,
//就置位取数标志
}
if(count3>=INBUF_LEN) count3 = 0;
}
}
}
uint BIN2BCD(int bin)
{
uint a, v;
a = bin%10;
v = a;
bin /= 10;
a = bin%10;
v |= a<<4;
bin /= 10;
a = bin%10;
v |= a<<8;
bin /= 10;
a = bin%10;
v |= a<<12;
return(v);
}
sbit SRC = P1^0;
sbit SRC1 = P1^1;
sbit SRC2 = P1^2;
sbit SRC3 = P1^3;
sbit QUE = P1^7;
int kbhit()
{
unsigned char i;
SRC = 0;
SRC1 = 0;
SRC2 = 0;
SRC3 = 0;
i =QUE;
if(i==0)
{
return(1);
}
return(0);
}
void Unpack(uint spd,uchar *buf)
{
buf[0] = ((spd&0xF000)>>12) | 0x30;
buf[1] = ((spd&0x0F00)>>8) | 0x30;
buf[2] = ((spd&0x00F0)>>4) | 0x30;
buf[3] = (spd&0x000F) | 0x30;
}
uint Speed = 1000;
void Delay(int t)
{
int i,j;
for(i=0;i<t;i++)
{ for(j=0;j<2;j++); }
}
void main()
{
unsigned int spd; uchar ch;
InitComm();
inbuf[0] = '8';
inbuf[1] = '7';
inbuf[2] = '6';
inbuf[3] = '5';
ch = 0x8C;
SendString(inbuf,INBUF_LEN);
while(1);
// while(1)
// {
Delay(30000);
// if(kbhit())
// {
Speed++;
spd = BIN2BCD(Speed);
Unpack(spd, inbuf);
SendString(inbuf,INBUF_LEN);
// }
// }
while(1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -