// ADC0809模数转换
//ADC0809采样通道3输入的模拟量,转换后结果显示在数码管上
#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
uchar code LEDData[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}
//ADC0809引脚定义
sbit OE=P1^0
sbit EOC=P1^1
sbit ST=P1^2
sbit CLK=P1^3
//延喊毁睁时
void Delayms(uint ms)
{
uchar i
while(ms--)for(i=0i<120i++)
}
//显示转换结果
void Display_Result(uchar d)
{
P2=0xf7
P0=LEDData[d%10]
Delayms(5)
P2=0xfd
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
P1=0x3f//选择ADC0809通道3(0111)
//高余信4位设通道地郑岁址为0111
while(1)
{
ST=0ST=1ST=0//启动转换
while(EOC==0) //等待转换结果
OE=1 //允许输出
Display_Result(P3) //显示A/D转换结果
OE=0 //关闭输出
}
}
void Timer0_INT() interrupt 1
{
CLK=!CLK//ADC0809时钟信号
}
汇编:ORG 0000h
LJMP MAIN
====================
ORG 0003h
SETB P3.0OE=1
MOV R3,P2READ ADC
CLR P3.0 END
RETI
=====================
ORG 000Bh
CPL P3.4 CREAT CLK
RETI
===================
ORG 001Bh
CLR TR1 close T1
CLR P3.1 ALE=0
CLR P3.3 start=0
nop
nop
SETB P3.1ALE=1
SETB P3.3start=1
nop
nop
CLR P3.1 ALE=0
CLR P3.3 start=0
MOV TH1,#3DH
MOV TL1,#0B0H
SETB TR1 open T1
RETI
==================
ORG 0040H
MAIN: CLR P3.0
CLR P3.1
CLR P3.3
SETB EA
SETB EX0
SETB IT0
SETB PX0
SETB ET0
SETB ET1
SETB PT1
MOV TMOD,#12H
MOV TH0,#0FDH
MOV TL0,#0FDH
MOV TH1,#3DH
MOV TL1,#0B0H
SETB TR0
SETB TR1
LOOP: MOV P1,R3
SJMP LOOP
end
c语言:
#include"reg52.h"源旦孝迟答
#include"intrins.h"
unsigned int tt,dd
sbit clock=P3^0
sbit start=P3^1
sbitOE=P3^3
sbit ALE=P3^4
sbit addA=P3^5
sbit rs=P3^6
sbit e=P3^7
void delay(unsigned char us)
{
while(us--)
}
void w1602(bit w,unsigned char dat)
{
rs=w
e=0
P1=dat
delay(4)
e=1
delay(8)
e=0
}
void init1602()
{
w1602(0,0x38)
w1602(0,0x06)
w1602(0,0x0c)
w1602(0,0x01)
}
void main()
{
init1602()
start=0
OE=0
ALE=0
EA=1
EX0=1
IT0=1
PX0=1
ET0=1
ET1=1
PT1=1
TMOD=0x12
TH0=0xf0
TL0=0xf0
TH1=0x4d
TL1=0xb0
TR0=1
TR1=1
while(1)
{
dd=tt*196/100
w1602(0,0x80)
w1602(1,0x30+dd/100)
w1602(1,0x2e)
w1602(1,0x30+(dd%100)/10)
w1602(1,0x30+(dd%100)%10)
}
}
void ddd() interrupt 0
{
OE=1 //EOC _______________|---|___
tt=P0 //OE _______________|---|___read dat to tt
OE=0
}
void clockt() interrupt 1
{
clock=~clock /雹稿/creat clk pulse for ADC0808
}
void wdata() interrupt 3
{
TR1=0 //close C/T1
ALE=0
start=0
_nop_()
ALE=1
start=1
_nop_()
start=0 // ALE ___|---|___________________
ALE=0 //start ___|---|___________________
_nop_() //finsh EOC _____________________|---|___
TH1=0x4d
TL1=0xb0
TR1=1 //reset C/T1 for Next ADC
}
首先你没有给电路连接图,仅笑禅凭汇编中的地址很难想象,下面只有使用伪代码表示,控制接口自行定义了。假镇升带设可以位寻址。int seg_table[]={0x3F,0x06,...}//数码管的表
GPIO(start) = 0;//启动,低电平有效
while(!EOC)//等待转换完成
LED = DATA//读入数据,LED显示
int temp = DATA
delay_ms()
int i =0
while(temp)
{
duan = temp&0x0f御芦
temp >>= 4
GPIO(duan) = 0
GPIO(duan) = seg_table[duan]
GPIO(duan) = 0
wei = i++
GPIO(wei) = 0
GPIO(wei) = wei
delay_ms()
}
对于串行发送很简单,
for(i= 0;i<8;i++)
{
GPIO_BITx = DATA&0x01
ATA >>= 1
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)