ADC0809转换程序
时间:10-02
整理:3721RD
点击:
这个是ADC0809转换及显示程序,我想问AD_DATA[0]-AD_DATA[3]是十进制的吗?
#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
uchar code LEDData[]=
{
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f
};
sbit OE = P1^4;
sbit EOC = P1^5;
sbit ST = P1^6;
sbit CLK = P1^3;
uchar AD_DATA[5];
int i;
void DelayMS(uint ms)
{
uchar i;
while(ms--)
{
for(i=0;i<120;i++);
}
}
void Display_Result(uchar d)
{
for(i=0;i<100;i++)
{
P2 = 0xf7;
P0 = LEDData[d%10];
DelayMS(5);
P2 = 0xfb;
P0 = LEDData[d%100/10];
DelayMS(5);
P2 = 0xfd;
P0 = LEDData[d/100];
DelayMS(5);
}
}
void main()
{
TMOD = 0x02;
TH0 = 0x14;
TL0 = 0x00;
IE = 0x82;
TR0 = 1;
while(1)
{
P1 = 0x7c;
ST = 0;
ST = 1;
ST = 0;
while(EOC == 0);
OE = 1;
AD_DATA[0]=P3;
Display_Result(AD_DATA[0]);
OE = 0;
DelayMS(500);
P1 = 0x7b;
ST = 0;
ST = 1;
ST = 0;
while(EOC == 0);
OE = 1;
AD_DATA[1]=P3;
Display_Result(AD_DATA[1]);
OE = 0;
DelayMS(500);
P1 = 0x7a;
ST = 0;
ST = 1;
ST = 0;
while(EOC == 0);
OE = 1;
AD_DATA[2]=P3;
Display_Result(AD_DATA[2]);
OE = 0;
DelayMS(500);
P1 = 0x79;
ST = 0;
ST = 1;
ST = 0;
while(EOC == 0);
OE = 1;
AD_DATA[3]=P3;
Display_Result(AD_DATA[3]);
OE = 0;
DelayMS(500);
P1 = 0x78;
ST = 0;
ST = 1;
ST = 0;
while(EOC == 0);
OE = 1;
AD_DATA[4]=P3;
Display_Result(AD_DATA[4]);
OE = 0;
DelayMS(500);
}
}
void Timer0_INT() interrupt 1
{
CLK = !CLK;
}
#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
uchar code LEDData[]=
{
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f
};
sbit OE = P1^4;
sbit EOC = P1^5;
sbit ST = P1^6;
sbit CLK = P1^3;
uchar AD_DATA[5];
int i;
void DelayMS(uint ms)
{
uchar i;
while(ms--)
{
for(i=0;i<120;i++);
}
}
void Display_Result(uchar d)
{
for(i=0;i<100;i++)
{
P2 = 0xf7;
P0 = LEDData[d%10];
DelayMS(5);
P2 = 0xfb;
P0 = LEDData[d%100/10];
DelayMS(5);
P2 = 0xfd;
P0 = LEDData[d/100];
DelayMS(5);
}
}
void main()
{
TMOD = 0x02;
TH0 = 0x14;
TL0 = 0x00;
IE = 0x82;
TR0 = 1;
while(1)
{
P1 = 0x7c;
ST = 0;
ST = 1;
ST = 0;
while(EOC == 0);
OE = 1;
AD_DATA[0]=P3;
Display_Result(AD_DATA[0]);
OE = 0;
DelayMS(500);
P1 = 0x7b;
ST = 0;
ST = 1;
ST = 0;
while(EOC == 0);
OE = 1;
AD_DATA[1]=P3;
Display_Result(AD_DATA[1]);
OE = 0;
DelayMS(500);
P1 = 0x7a;
ST = 0;
ST = 1;
ST = 0;
while(EOC == 0);
OE = 1;
AD_DATA[2]=P3;
Display_Result(AD_DATA[2]);
OE = 0;
DelayMS(500);
P1 = 0x79;
ST = 0;
ST = 1;
ST = 0;
while(EOC == 0);
OE = 1;
AD_DATA[3]=P3;
Display_Result(AD_DATA[3]);
OE = 0;
DelayMS(500);
P1 = 0x78;
ST = 0;
ST = 1;
ST = 0;
while(EOC == 0);
OE = 1;
AD_DATA[4]=P3;
Display_Result(AD_DATA[4]);
OE = 0;
DelayMS(500);
}
}
void Timer0_INT() interrupt 1
{
CLK = !CLK;
}
我可以肯定的告诉你,肯定是16进制!
为什么呢?首先,单片机是以二进制形式才能识别才能运行的,其内部数据如RAM,ROM都是以十六进制进行存储的。编译器会把我们编写的所有程序都以十六进制形式存在ROM中,包括你问的问题。
这时你可能有疑惑了,举个例子:P0 = LEDData[d/100];这里的100是十进制,为什么d(十六进制)/100(十进制),这样除能行的通?为什么能行的通呢?
编译程序的时候编译器会自动把100变为十六进制,这里d/100就变为十六进制除以十六进制,而不是十六进制除以十进制。所以我们不需要把100变为十六进制再相除!
明白不?
大神,那我想要把它和一个十进制的数进行比较怎么办?
一样的,比如if(AD_DATA[0]==60)和(AD_DATA[0]==0x3c)这2种写法都是一样的啊!
不对呀,我下面的程序不论e是什么值,都只执行 motor_forever();
e=0x96; //150
if(AD_DATA[0]>e)
motor_appoint();
else motor_forever();
你把AD_DATA[0]=0x97;试试
嗯,可以,为什么呢?
AD_DATA[0]有可能为0啊!不知你AD转换对不对
对着呢,我还有数码管显示呢,
亲,我邀请你加入,我qq1753493268。