51单片机通过控制独立按键向串口发送数字,代码一直有问题,数字发送不出去,各位大神帮忙看看,在线等
case 2: sprintf(Test1," 2 dang "), ShowString(0,Test1);break;
case 3: sprintf(Test1," 3 dang "), ShowString(0,Test1);break;
case 4: sprintf(Test1," 4 dang "), ShowString(0,Test1);break;
case 5: sprintf(Test1," 5 dang "), ShowString(0,Test1);break;
case 6: sprintf(Test1," 6 dang "), ShowString(0,Test1);break;
case 7: sprintf(Test1," 7 dang "), ShowString(0,Test1);break;
case 8: sprintf(Test1," 8 dang "), ShowString(0,Test1);break;
case 9: sprintf(Test1," 9 dang "), ShowString(0,Test1);break;
}
while(!jiansu);
}
}
if(tingzhi==0) // 停止
{
delay(10);
if(tingzhi==0)
{
IN1=1;
IN2=1;
sprintf(Test1," tingzhi ");
ShowString(0,Test1);
sprintf(Test2," ");
ShowString(1,Test2);
while(!tingzhi);
}
}
}
void UARTinit(void)
{
SCON = 0x50; /* SCON: 模式 1, 8-bit UART, 使能接收 */
TMOD |= 0x20; /* TMOD: timer 1, mode 2, 8-bit reload */
TH1 = 0xFD; /* TH1: reload value for 9600 baud @ 11.0592MHz */
TL1 =TH1;
TR1 = 1; /* TR1: timer 1 run */
// EA = 1; /*打开总中断*/
// ES = 1; //打开串口中断
//TI = 1; //发送标志位
}
/******************************************************************/
/* 串口中断程序 */
/******************************************************************/
/*
void UART_SER (void) interrupt 4 //串行中断服务程序
{
unsigned char temp;
// n= temp-80;
if(RI) //判断是接收中断产生
{
RI=0; //标志位清零
temp=SBUF; //读入缓冲区的值
// P1=Temp; //把值输出到P1口,用于观察
SBUF=temp; //把接收到的值再发回电脑端
}
if(TI) //如果是发送标志位,清零
TI=0;
} */
void UART_send_byte(unsigned char dat)
{
dat= SBUF ;
SBUF = dat;
while(!TI);
TI=0; // 此句可以不要,不影响后面数据的发送,只供代码查询数据是否发送完成
}
void scan()
{
if(zhengzhuan==0&&fanzhuan==0) // 正转
{
delay(10);
if(zhengzhuan==0&&fanzhuan==0)
{
n=5;
UART_send_byte(n);
}
}
if(jiasu==0) // 加速
{
delay(10);
if(jiasu==0)
{
n++;
UART_send_byte(n);
}
}
if(jiansu==0) //减速
{
delay(10);
if(jiansu==0)
{
n--;
UART_send_byte(n);
}
}
}
void main()
{
InitLcd(); //初始化LCD
time_init();
UARTinit();
while(1)
{
keyscan();
scan();
tiaosu();
djkz(highv,lowv);
}
}
#include <reg52.h>
#include<intrins.h>
#include<stdio.h> //标准输入输出
#define uchar unsigned char
#define uint unsigned int
uchar num;
uchar highv;
uchar lowv;
uint n;
//uchar n;
uchar m;
uchar a;
//uint i=1;
sbit zhengzhuan=P3^2; //按键正转
sbit fanzhuan=P3^3; //按键反转
sbit tingzhi=P3^4; //按键停止
sbit jiasu=P3^5; //按键加速
sbit jiansu=P3^6; //按键减速
sbit IN1=P1^0;
sbit IN2=P1^1;
sbit ENA=P1^2;
sbit RS = P2^4; //Pin4
sbit RW = P2^5; //Pin5
sbit E = P2^6; //Pin6
#define Data P0//数据端口
char data Test1[]=" ";
char data Test2[]=" ";
void delay(uchar z) //在12M下延时z毫秒
{uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void time_init()
{ TMOD=0x01;
TH0=(65536-a)/256;
TL0=(65536-a)%256;
ET0=1;
EA=1;
TR0=1;
}
/*void SendByte(unsigned char dat)
{
SBUF=dat;
while(!TI)
TI=0;
}*/
/*void SendByte()
{
if (TI==1)
{
SBUF='1';
TI=0;
}
}
/******************************************************************/
/* 写入命令函数 */
/******************************************************************/
void WriteCommand(unsigned char c)
{
delay(5);//操作前短暂延时,保证信号稳定
E=0;
RS=0;
RW=0;
_nop_();
E=1;
Data=c;
E=0;
}
/******************************************************************/
/* 写入数据函数 */
/******************************************************************/
void WriteData(unsigned char c)
{
delay(5); //操作前短暂延时,保证信号稳定
E=0;
RS=1;
RW=0;
_nop_();
E=1;
Data=c;
E=0;
RS=0;
}
/******************************************************************/
/* 写入字节函数 */
/******************************************************************/
void ShowChar(unsigned char pos,unsigned char c)
{
unsigned char p;
if (pos>=0x10)
p=pos+0xb0; //是第二行则命令代码高4位为0xc
else
p=pos+0x80; //是第二行则命令代码高4位为0x8
WriteCommand (p);//写命令
WriteData (c); //写数据
}
/******************************************************************/
/* 写入字符串函数 */
/******************************************************************/
void ShowString (unsigned char line,char *ptr)
{
unsigned char l,i;
l=line<<4;
for (i=0;i<16;i++)
ShowChar (l++,*(ptr+i));//循环显示16个字符
}
//初始化LCD
void InitLcd()
{
delay(15);
WriteCommand(0x38); //display mode
WriteCommand(0x38); //display mode
WriteCommand(0x38); //display mode
WriteCommand(0x06); //显示光标移动位置
WriteCommand(0x0c); //显示开及光标设置
WriteCommand(0x01); //显示清屏
}
void djkz(uchar a,uchar b) //PWM控制
{
ENA=1;
delay (a);
ENA=0;
delay (b);
}
void tiaosu() //电机调速
{ //ENA=1;
switch(num)
{
case 1: highv=1,lowv=9; break;
case 2: highv=2,lowv=8; break;
case 3: highv=3,lowv=7; break;
case 4: highv=4,lowv=6; break;
case 5: highv=5,lowv=5; break;
case 6: highv=6,lowv=4; break;
case 7: highv=7,lowv=3; break;
case 8: highv=8,lowv=2; break;
case 9: highv=9,lowv=1; break;
}
}
void keyscan()
{
if(zhengzhuan==0) // 正转
{
delay(10);
if(zhengzhuan==0)
{
IN1=0;
IN2=1;
num=5;
m=5;
// num1=1;
sprintf(Test2,"zhengzhuan");
ShowString(1,Test2);
switch(m)
{
case 1: sprintf(Test1," 1 dang "), ShowString(0,Test1);break;
case 2: sprintf(Test1," 2 dang "), ShowString(0,Test1);break;
case 3: sprintf(Test1," 3 dang "), ShowString(0,Test1);break;
case 4: sprintf(Test1," 4 dang "), ShowString(0,Test1);break;
case 5: sprintf(Test1," 5 dang "), ShowString(0,Test1);break;
case 6: sprintf(Test1," 6 dang "), ShowString(0,Test1);break;
case 7: sprintf(Test1," 7 dang "), ShowString(0,Test1);break;
case 8: sprintf(Test1," 8 dang "), ShowString(0,Test1);break;
case 9: sprintf(Test1," 9 dang "), ShowString(0,Test1);break;
}
while(!zhengzhuan);
}
}
if(fanzhuan==0) // 反转
{
delay(10);
if(fanzhuan==0)
{
IN1=1;
IN2=0;
m=5;
// num1=0;
num=5;
sprintf(Test2,"fanzhuan");
ShowString(1,Test2);
switch(m)
{
case 1: sprintf(Test1," 1 dang "), ShowString(0,Test1);break;
case 2: sprintf(Test1," 2 dang "), ShowString(0,Test1);break;
case 3: sprintf(Test1," 3 dang "), ShowString(0,Test1);break;
case 4: sprintf(Test1," 4 dang "), ShowString(0,Test1);break;
case 5: sprintf(Test1," 5 dang "), ShowString(0,Test1);break;
case 6: sprintf(Test1," 6 dang "), ShowString(0,Test1);break;
case 7: sprintf(Test1," 7 dang "), ShowString(0,Test1);break;
case 8: sprintf(Test1," 8 dang "), ShowString(0,Test1);break;
case 9: sprintf(Test1," 9 dang "), ShowString(0,Test1);break;
}
while(!fanzhuan);
}
}
if(jiasu==0) // 加速
{
delay(10);
if(jiasu==0)
{
num++;
m++;
// if(num==10)
// { num=0;
// m=0; }
switch(m)
{
case 1: sprintf(Test1," 1 dang "), ShowString(0,Test1);break;
case 2: sprintf(Test1," 2 dang "), ShowString(0,Test1);break;
case 3: sprintf(Test1," 3 dang "), ShowString(0,Test1);break;
case 4: sprintf(Test1," 4 dang "), ShowString(0,Test1);break;
case 5: sprintf(Test1," 5 dang "), ShowString(0,Test1);break;
case 6: sprintf(Test1," 6 dang "), ShowString(0,Test1);break;
case 7: sprintf(Test1," 7 dang "), ShowString(0,Test1);break;
case 8: sprintf(Test1," 8 dang "), ShowString(0,Test1);break;
case 9: sprintf(Test1," 9 dang "), ShowString(0,Test1);break;
}
while(!jiasu);
}
}
if(jiansu==0) //减速
{
delay(10);
if(jiansu==0)
{
num--;
m--;
//else
// num=0;
// m=0 ;
switch(m)
{
case 1: sprintf(Test1," 1 dang "), ShowString(0,Test1);break;
case 2: sprintf(Test1," 2 dang "), ShowString(0,Test1);break;
case 3: sprintf(Test1," 3 dang "), ShowString(0,Test1);break;
case 4: sprintf(Test1," 4 dang "), ShowString(0,Test1);break;
case 5: sprintf(Test1," 5 dang "), ShowString(0,Test1);break;
case 6: sprintf(Test1," 6 dang "), ShowString(0,Test1);break;
case 7: sprintf(Test1," 7 dang "), ShowString(0,Test1);break;
case 8: sprintf(Test1," 8 dang "), ShowString(0,Test1);break;
case 9: sprintf(Test1," 9 dang "), ShowString(0,Test1);break;
}
while(!jiansu);
}
}
if(tingzhi==0) // 停止
{
delay(10);
if(tingzhi==0)
{
IN1=1;
IN2=1;
sprintf(Test1," tingzhi ");
ShowString(0,Test1);
sprintf(Test2," ");
ShowString(1,Test2);
while(!tingzhi);
}
}
}
之前的程序没粘贴上,在下面补上了
是什么现象,是什么都发不出去吗, // EA = 1; /*打开总中断*/
// ES = 1; //打开串口中断
这两个要打开,可以固定发一个字符,先确认串口是否能发送数据,再考虑其他的
可不可以不通过中断,通过中断可以发送,但是我想通过按键发送
你现在的程序也没有通过中断发送,至于通过按键发送,那也只是检测到按键后,启动发送而已。
我就是检测到按键后需要发送,但是为什么接收不到发送的数字
EA和ES置1了吗,看你的程序可以把电脑发的数据返回,这个功能,能实现吗
你需要先确认发送数据的功能是否正确,确定单片机能正确发送出想发送的数据,另外
void UART_send_byte(unsigned char dat)
{
dat= SBUF ;
SBUF = dat;
while(!TI);
TI=0; // 此句可以不要,不影响后面数据的发送,只供代码查询数据是否发送完成
}中为什么会有一个dat= SBUF ;,这个多余,会改变你发送出来的数据,但不应该会看不到发送的数据
打开中断可以发送数字的,但是通过按键来发送数字就接收不到
不太确定你说的中断是什么,串口本身也是一个中断,不打开对应中断,不会发送数据,中断程序是产生中断后进行处理的程序,你的程序里串口中断程序虽然屏蔽了,但中断程序里如果去除掉if(TI) //如果是发送标志位,清零
TI=0;这两行,这时串口中断程序也只是一个串口接收处理程序而已。至于发送数据,你的程序void SendByte(unsigned char dat)应该就没什么问题。你总共贴了两个程序,差别也比较大,不知道该看哪个,调程序不适宜每次改动太大,对于已经确定运行正确的部分不适宜改动。
现在可以发送数据了,但是我想问一下单片机发送数字给串口,那labview上位机接收到的是数字还是字符串?
上位机接收到的数据是数字还是字符,其实和上位机发给单片机一样,不在于是什么样的数据,而是在于你如何定义这些数据,比如0x41h,定义它是数字,那就是十六进制的41,定义它是字符,那就是大写字母A。对于通讯来说,除了波特率等硬件的参数外,还有报文的协议,包括报文的结构,报文的数据类型,一个报文也可以同时存在数字和字符格式,报文的发送和接收要求等等内容。