求问一个关于单片机的问题,我用的STC的52,郭天祥开发的Tx-1C的板子,下面是我的程序跟想要实现的问题。
数码管前三位以十进制显示AD转换器的数值,未采样时显示000,当上位机收到01后上位机发送turnonthead,并1s间隔输出ad的采样电压数据如果是02就关闭,输入其...
数码管前三位以十进制显示AD转换器的数值,未采样时显示000,当上位机收到01后上位机发送turn on the ad,并1s间隔输出ad的采样电压数据 如果是02就关闭,输入其他数(不是01,02)就发送是error。
但是我的单片机烧录程序后,不管输入什么数都是输出Error,急等大神告知是为什么????在线等。是我哪里的问题
#include<reg52.h>
#include<stdio.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit dula = P2^6;
sbit wela = P2^7;
sbit adwr = P3^6;
sbit adrd = P3^7;
uchar code table[ ] = { 0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07,
0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71};
unsigned char flag_time,flag_on,flag_uart,ad_val;
uchar flag,a;
float ad_vo;
void delayms(uint xms)
{
uint i,j;
for(i = xms;i > 0;i--)
for(j = 110;j > 0;j--);
}
void init ( )
{
TMOD = 0x21;
TH0 = (65536 - 50000)/256;
TL0 = (65536 - 50000)%256;
TH1 = 0xfd;
TL1 = 0xfd;
TR1 = 1; //启动定时器1
ET0 = 1; //打开T0中断
SM0 = 0; //SM工作方式选择
SM1 = 1; //方式1
EA = 1; //全局中断允许
ES = 1; //串行口中断允许
REN = 1; //允许串口接受数据
}
void display(uchar value)
{
uint bai,shi,ge;
bai = value/100;
shi = value%100/10;
ge = value%10;
dula = 1;
P0 = table[bai];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = 0x7e;
wela = 0;
delayms(5);
dula = 1;
P0 = table[shi];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = 0x7d;
wela = 0;
delayms(5);
dula = 1;
P0 = table[ge];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = 0x7b;
wela = 0;
delayms(5);
}
uchar get_ad( )
{
uchar adval;
adwr = 1;
_nop_( );
adwr = 0;
_nop_( );
adwr = 1;
P1 = 0xff;
adrd = 1;
_nop_( );
adrd = 0;
_nop_( );
adval = P1;
adrd = 1;
return adval;
}
void main( )
{
init( );
wela = 1;
P0 = 0xff;
wela = 0;
while(1)
{
if(flag_uart == 1)
{
flag_uart = 0;
ES = 0;//关闭串口,防止未完成动作进入中断使flag_uart置1
TI = 1;//要用put,printf先置TI
switch(flag_on)
{
case 0:puts("Turn on the A/D!\n");
TR0 = 1;
break;
case 1:puts("Turn off the A/D!\n");
TR0 = 0;
break;
case 2: puts("Error!\n");
break;
}
while(!TI);
TI = 0;
ES = 1;
}
if(flag_time == 1)
{
flag_time = 0;
ad_val = get_ad();
ad_vo = (float)ad_val*5.0/256.0;
ES = 0;
TI = 1;
printf("The voltage is %fV\n",ad_vo);
while(!TI);
TI = 0;
ES = 1;
}
display(ad_val);
}
}
void timer0( )interrupt 1
{
uint t0_num = 0;
TH0 = (65536 - 50000)/256;
TL0 = (65536 - 50000)%256;
t0_num++;
if(t0_num == 20)
{
t0_num = 0;
flag_time = 1;
}
}
void ser()interrupt 4
{
RI = 0;//接受完成置1这里清0
a = SBUF;
flag_uart = 1;
if(a == 1)
flag_on = 0;
else if (a == 2)
flag_on = 1;
else
flag_on = 2;
} 展开
但是我的单片机烧录程序后,不管输入什么数都是输出Error,急等大神告知是为什么????在线等。是我哪里的问题
#include<reg52.h>
#include<stdio.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit dula = P2^6;
sbit wela = P2^7;
sbit adwr = P3^6;
sbit adrd = P3^7;
uchar code table[ ] = { 0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07,
0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71};
unsigned char flag_time,flag_on,flag_uart,ad_val;
uchar flag,a;
float ad_vo;
void delayms(uint xms)
{
uint i,j;
for(i = xms;i > 0;i--)
for(j = 110;j > 0;j--);
}
void init ( )
{
TMOD = 0x21;
TH0 = (65536 - 50000)/256;
TL0 = (65536 - 50000)%256;
TH1 = 0xfd;
TL1 = 0xfd;
TR1 = 1; //启动定时器1
ET0 = 1; //打开T0中断
SM0 = 0; //SM工作方式选择
SM1 = 1; //方式1
EA = 1; //全局中断允许
ES = 1; //串行口中断允许
REN = 1; //允许串口接受数据
}
void display(uchar value)
{
uint bai,shi,ge;
bai = value/100;
shi = value%100/10;
ge = value%10;
dula = 1;
P0 = table[bai];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = 0x7e;
wela = 0;
delayms(5);
dula = 1;
P0 = table[shi];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = 0x7d;
wela = 0;
delayms(5);
dula = 1;
P0 = table[ge];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = 0x7b;
wela = 0;
delayms(5);
}
uchar get_ad( )
{
uchar adval;
adwr = 1;
_nop_( );
adwr = 0;
_nop_( );
adwr = 1;
P1 = 0xff;
adrd = 1;
_nop_( );
adrd = 0;
_nop_( );
adval = P1;
adrd = 1;
return adval;
}
void main( )
{
init( );
wela = 1;
P0 = 0xff;
wela = 0;
while(1)
{
if(flag_uart == 1)
{
flag_uart = 0;
ES = 0;//关闭串口,防止未完成动作进入中断使flag_uart置1
TI = 1;//要用put,printf先置TI
switch(flag_on)
{
case 0:puts("Turn on the A/D!\n");
TR0 = 1;
break;
case 1:puts("Turn off the A/D!\n");
TR0 = 0;
break;
case 2: puts("Error!\n");
break;
}
while(!TI);
TI = 0;
ES = 1;
}
if(flag_time == 1)
{
flag_time = 0;
ad_val = get_ad();
ad_vo = (float)ad_val*5.0/256.0;
ES = 0;
TI = 1;
printf("The voltage is %fV\n",ad_vo);
while(!TI);
TI = 0;
ES = 1;
}
display(ad_val);
}
}
void timer0( )interrupt 1
{
uint t0_num = 0;
TH0 = (65536 - 50000)/256;
TL0 = (65536 - 50000)%256;
t0_num++;
if(t0_num == 20)
{
t0_num = 0;
flag_time = 1;
}
}
void ser()interrupt 4
{
RI = 0;//接受完成置1这里清0
a = SBUF;
flag_uart = 1;
if(a == 1)
flag_on = 0;
else if (a == 2)
flag_on = 1;
else
flag_on = 2;
} 展开
1个回答
展开全部
你这程序,应该是先由上位机向单片机发送1,2,或其它字符吧,
下面是单片机的接收程序
void ser()interrupt 4
{
RI = 0;//接受完成置1这里清0
a = SBUF;
flag_uart = 1;
if(a == 1)
flag_on = 0;
else if (a == 2)
flag_on = 1;
else
flag_on = 2;
}
那问题来了,电脑是用什么向单片机发送字符的,是串口调试助手吗?那发送的字符1,2或其它的,以是字符形式发送吧,那就是ASCII码,单片机收到的可就不是1,2了,而是ASCII的1,2。
所以,if 语句的判断就不对了。应该是这样的
void ser()interrupt 4
{
RI = 0;//接受完成置1这里清0
a = SBUF;
flag_uart = 1;
if(a == ‘1’) //这才是ASCII码的形式
flag_on = 0;
else if (a == ‘2’)
flag_on = 1;
else
flag_on = 2;
}
下面是单片机的接收程序
void ser()interrupt 4
{
RI = 0;//接受完成置1这里清0
a = SBUF;
flag_uart = 1;
if(a == 1)
flag_on = 0;
else if (a == 2)
flag_on = 1;
else
flag_on = 2;
}
那问题来了,电脑是用什么向单片机发送字符的,是串口调试助手吗?那发送的字符1,2或其它的,以是字符形式发送吧,那就是ASCII码,单片机收到的可就不是1,2了,而是ASCII的1,2。
所以,if 语句的判断就不对了。应该是这样的
void ser()interrupt 4
{
RI = 0;//接受完成置1这里清0
a = SBUF;
flag_uart = 1;
if(a == ‘1’) //这才是ASCII码的形式
flag_on = 0;
else if (a == ‘2’)
flag_on = 1;
else
flag_on = 2;
}
更多追问追答
追问
行,大神我试试哈
大神对,是对了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询