標(biāo)題: 單片機(jī)ADC0804采樣代碼 [打印本頁]
作者: 吃掉大菠蘿 時(shí)間: 2022-5-29 13:33
標(biāo)題: 單片機(jī)ADC0804采樣代碼
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit wr=P3^6;
sbit rd=P3^7;
sbit cs=P3^5;
uchar code tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};//共陰數(shù)碼管碼表
uchar s,g,f,l,x;//數(shù)碼管顯示變量
float A;
uint K;
void delay(uint);
void adc();
void adc()
{
//啟動ADC0804采樣//
cs=0;wr=0;rd=1;
delay(50);
cs=1;wr=1;rd=1; //WR由低到高時(shí),AD開始轉(zhuǎn)換
delay(100);
//讀AD數(shù)值//
cs=0;wr=1;rd=0;//讀取轉(zhuǎn)換數(shù)據(jù)結(jié)果
delay(50);
A=P1;//P1的新數(shù)據(jù)賦值給adval,用于數(shù)碼管顯示的s g f的分離
A=A*144/256;
K=P1*144/256;
cs=1;wr=1;rd=1;//停止AD讀取
}
void main()
{
uint s,g,f,l,x;
while(1)
{
adc();
s=K/10;
l=s/10;
x=s%10;
g=K%10;
f=(A-K)*10;
P2=0XFE; //11111110 選中十位數(shù)碼管
P0=tab[l]; //顯示
delay(50);
P2=0XFD; //11111110 選中十位數(shù)碼管
P0=tab[x]; //顯示
delay(50);
P2=0XFB; //11111101 選中個(gè)位數(shù)碼管
P0=tab[g]+0x80; //顯示
delay(50);
P2=0XF7;//11111011 選中小數(shù)點(diǎn)后數(shù)碼管
P0=tab[f]; //顯示
delay(50);
}
}
void delay(uint m)//延時(shí)子程序,約500個(gè)機(jī)器周期
{
while(m--);}
| 歡迎光臨 (http://www.raoushi.com/bbs/) |
Powered by Discuz! X3.1 |