二. 程序源码
#include
#include //需求用到_nop_()函数
#define uchar unsigned char
#define uint unsigned int
sbit dula = P2 ^ 6; //声明U1锁存器的锁存端
sbit wela = P2 ^ 7; //声明U2锁存器的锁存端
sbit adwr = P3 ^ 6; //声明AD的WR端口
sbit adrd = P3 ^ 7; //声明AD的RD端口
uint g_iNum; //声明变量获取AD转化后的数字量
uchar g_cT0; //声明变量便于定时器0计数
uchar g_cFlag; //声明变量完成AD转化函数与主函数间的通讯
uchar code table[] = { //数码管的编码界说(0,1,…,9,A,B,C,D,E,F)
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71
};
void serial_com(uint i_num); //声明串口数据送核算机的函数
void delayms(uint xms); //声明延时xms ms函数
void tude_display(uint i_num); //声明数码管显现函数
void init(); //声明串口,中止,定时器0初始化函数
void main()
{
uchar i;
//串口, 中止, 定时器初始化
init();
//主循环(重复履行)
while (1)
{
if (g_cFlag == 1) //检测AD是否产生
{
for (i = 100; i > 0; i–)
tude_display(g_iNum); //将ad转化的数字量显现在数码管
serial_com(g_iNum); //将ad转化数字量对应的模拟量发送到核算机
}
}
}
//延时ms函数
void delayms(uint xms)
{
uint i, j;
for (i = xms; i > 0; i–)
for (j = 110; j > 0; j–)
;
}
//数码管显现函数
void tude_display(uint i_num)
{
uchar bai, shi, ge;
bai = i_num / 100;
shi = i_num % 100 / 10;
ge = i_num % 10;
dula = 1;
P0 = table[bai];
dula = 0;
//P0 = 0xff; //ADC0804的CS端衔接在U2锁存器的Q7上,所以Q7位需求为0, 所以不能做清影动作
wela = 1;
P0 = 0x7e; //ADC0804的CS端衔接在U2锁存器的Q7上,所以Q7位需求为0, 下同
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);
//每次显现数据后将数码管全灭
dula = 1;
P0 = 0x00;
dula = 0;
wela = 1;
P0 = 0x00;
wela = 0;
}
void T0_timer() interrupt 1
{
TH0 = (65536 – 50000) / 256;
TL0 = (65536 – 50000) % 256;
g_cT0++;
if (g_cT0 == 20)
{
g_cT0 = 0;
adwr = 1;
_nop_();
adwr = 0;
_nop_();
adwr = 1;
P1 = 0xff;
adrd = 1;
_nop_();
adrd = 0;
_nop_();
g_iNum = P1; //获取Ad转化的数字量
g_cFlag = 1; //标志方位位, AD与主函数间通讯
}
}
void init()
{
//选通AD的CS端, ADC0804的CS端与U2锁存器的Q7端衔接
wela = 1;
P0 = 0x7f;
wela = 0;
//串口工作方式
SM0 = 0;
SM1 = 1;
TMOD = 0x21;
//波特率为1200bsp
TH1 = 0xE8;
TL1 = 0xE8;
//发动定时器1
TR1 = 1;
//敞开总中止
EA = 1;
//敞开定时器0中止
ET0 = 1;
//发动定时器0
TR0 = 1;
}
void serial_com(uint i_num)
{
long temp;
uchar sCom[7];
uchar i;
//将AD转化的数字量转化为关于的模拟量
temp = (i_num * 500000) / 256;
sCom[0] = temp / 100000;
sCom[1] = .;
sCom[2] = temp / 1000 % 10;
sCom[3] = temp / 1000 % 10;
sCom[4] = temp / 100 % 10;
sCom[5] = temp / 10 % 10;
sCom[6] = temp % 10;
//发送三个空格
for (i = 0; i < 3; i++)
{
SBUF = 0x20; //0x20为空格的ascii码
while (!TI);
TI = 0;
}
//将AD数字量关于模拟量发送到核算机
for (i = 0; i < 7; i++)
{
if (i != 1)
SBUF = sCom[i] + 0x30; //0x30为0的ascii码
else
SBUF = sCom[i]; //.不需求加0的ascii码
while (!TI);
TI = 0;
}
}