小弟想跨专业自学单片机遇到问题求好心高手们不吝解答
时间:10-02
整理:3721RD
点击:
#include<reg52.h> //小弟这个程序主要就是根据你输入串口的数字来选择单片机输出到串口的内容,但是最后在interrupt4中#include<intrins.h> //标志位flag3却没能被正常赋值,用的STC 89c52
#include<stdio.h>
sbit dula=P2^6;
sbit wela=P2^7;
sbit adwr=P3^6;
sbit adrd=P3^7;
#define uchar unsigned char
#define uint unsigned int
uchar code turnon[]="Turn on ad!";
uchar code turnoff[]="Turn off ad!";
uchar code duan[]= {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
uchar code erro[]="erro!";
uchar ge,shi,bai,num,flag1;
int take,flag2,flag3,save;
float v;
void delayxms(uint xms)
{
uint i;
for(xms;xms;xms--)
for(i=110;i;i--);
}
void display(uint ge,uint shi,uint bai)
{
P0=0x7e;
wela=1;
wela=0;
P0=duan[bai];
dula=1;
dula=0;
delayxms(5);
P0=0x7d;
wela=1;
wela=0;
P0=duan[shi];
dula=1;
dula=0;
delayxms(5);
P0=0x7b;
wela=1;
wela=0;
P0=duan[ge];
dula=1;
dula=0;
delayxms(5);
P0=0x7f;
wela=1;
wela=0;
}
void init()
{
TMOD=0x21;
SCON=0x50;
TH1=0xfd;
TL1=0xfd;
TH0=(65536-46082)/256;
TL0=(65536-46082)%256;
ET0=1;
EA=1;
ES=1;
TR1=1;
TR0=1;
}
void main()
{
uint i;
init();
while(1)
{
if(flag1==1)
{
adwr=1;
_nop_();
adwr=0;
_nop_();
adwr=1;
_nop_();
_nop_();
adrd=1;
_nop_();
adrd=0;
_nop_();
take=P1;
adrd=1;
ge=take%10;
shi=take%100/10;
bai=take/100;
flag1=0;
v=(float)take/256.0*5.0;
}
display(ge,shi,bai);
if(flag2==1)
{
ES=0;
flag2=0;
if(flag3==1)
{
for(i=0;i<11;i++)
{
SBUF=turnon[i];
while(!TI);
TI=0;
}
TI=1;
printf("The voltage is %fV\n",v);
while(!TI);
TI=0;
TI=1;
printf("The flag3 is %d\n",flag3); //发现flag3的值时钟不变,所以写这一句来输出flag3观察
while(!TI);
TI=0;
}
if(flag3==2)
{
for(i=0;i<12;i++)
{
SBUF=turnoff[i];
while(!TI);
TI=0;
}
}
if(flag3==3)
{
for(i=0;i<5;i++)
{
SBUF=erro[i];
while(!TI);
TI=0;
}
}
TI=1;
switch(flag3)
{
case 1:printf("Turn on ad.\nThe voltage is %fV\n",v);
break;
case 2:puts("turn off ad");
break;
case 3:puts("erro!");
break;
}
while(!TI);
TI=0;
ES=1;
}
}
}
void T0_time() interrupt 1
{
TL0=(65536-46082)%256;
TH0=(65536-46082)/256;
num++;
if(num==20)
{
num=0;
flag1=1;
}
}
void chuankou() interrupt 4
{
RI=0;
save=SBUF;
flag2=1;
if(save==1)
flag3=1;
else if(save==2)
flag3=2;
else
flag3=3;
}
小弟在看郭天祥的C51书的时候,有个题目要求根据你的输入来输出采样A/D电压值。检测发现interrupt4中save能正常取到输入SBUF的值,但是问题是最后的中断interrupt4中的flag3永远都只取到最后一个else的值,似乎if和 else并没有进行判断。请各位高手不吝过目指导。
#include<stdio.h>
sbit dula=P2^6;
sbit wela=P2^7;
sbit adwr=P3^6;
sbit adrd=P3^7;
#define uchar unsigned char
#define uint unsigned int
uchar code turnon[]="Turn on ad!";
uchar code turnoff[]="Turn off ad!";
uchar code duan[]= {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
uchar code erro[]="erro!";
uchar ge,shi,bai,num,flag1;
int take,flag2,flag3,save;
float v;
void delayxms(uint xms)
{
uint i;
for(xms;xms;xms--)
for(i=110;i;i--);
}
void display(uint ge,uint shi,uint bai)
{
P0=0x7e;
wela=1;
wela=0;
P0=duan[bai];
dula=1;
dula=0;
delayxms(5);
P0=0x7d;
wela=1;
wela=0;
P0=duan[shi];
dula=1;
dula=0;
delayxms(5);
P0=0x7b;
wela=1;
wela=0;
P0=duan[ge];
dula=1;
dula=0;
delayxms(5);
P0=0x7f;
wela=1;
wela=0;
}
void init()
{
TMOD=0x21;
SCON=0x50;
TH1=0xfd;
TL1=0xfd;
TH0=(65536-46082)/256;
TL0=(65536-46082)%256;
ET0=1;
EA=1;
ES=1;
TR1=1;
TR0=1;
}
void main()
{
uint i;
init();
while(1)
{
if(flag1==1)
{
adwr=1;
_nop_();
adwr=0;
_nop_();
adwr=1;
_nop_();
_nop_();
adrd=1;
_nop_();
adrd=0;
_nop_();
take=P1;
adrd=1;
ge=take%10;
shi=take%100/10;
bai=take/100;
flag1=0;
v=(float)take/256.0*5.0;
}
display(ge,shi,bai);
if(flag2==1)
{
ES=0;
flag2=0;
if(flag3==1)
{
for(i=0;i<11;i++)
{
SBUF=turnon[i];
while(!TI);
TI=0;
}
TI=1;
printf("The voltage is %fV\n",v);
while(!TI);
TI=0;
TI=1;
printf("The flag3 is %d\n",flag3); //发现flag3的值时钟不变,所以写这一句来输出flag3观察
while(!TI);
TI=0;
}
if(flag3==2)
{
for(i=0;i<12;i++)
{
SBUF=turnoff[i];
while(!TI);
TI=0;
}
}
if(flag3==3)
{
for(i=0;i<5;i++)
{
SBUF=erro[i];
while(!TI);
TI=0;
}
}
TI=1;
switch(flag3)
{
case 1:printf("Turn on ad.\nThe voltage is %fV\n",v);
break;
case 2:puts("turn off ad");
break;
case 3:puts("erro!");
break;
}
while(!TI);
TI=0;
ES=1;
}
}
}
void T0_time() interrupt 1
{
TL0=(65536-46082)%256;
TH0=(65536-46082)/256;
num++;
if(num==20)
{
num=0;
flag1=1;
}
}
void chuankou() interrupt 4
{
RI=0;
save=SBUF;
flag2=1;
if(save==1)
flag3=1;
else if(save==2)
flag3=2;
else
flag3=3;
}
小弟在看郭天祥的C51书的时候,有个题目要求根据你的输入来输出采样A/D电压值。检测发现interrupt4中save能正常取到输入SBUF的值,但是问题是最后的中断interrupt4中的flag3永远都只取到最后一个else的值,似乎if和 else并没有进行判断。请各位高手不吝过目指导。
求帅哥美女指导