数字/模拟
A/D转换 模拟量转数字量
#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit wela=P2^7;
sbit dula=P2^6;
sbit adwr=P3^6;
sbit adrd=P3^7;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void display(uchar bai,uchar shi,uchar ge)
{
dula=1;
P0=table[bai];
dula=0;
P0=0xff;
wela=1;
P0=0x7e;
wela=0;
delay(5);
dula=1;
P0=table[shi];
dula=0;
P0=0xff;
wela=1;
P0=0x7d;
wela=0;
delay(5);
dula=1;
P0=table[ge];
dula=0;
P0=0xff;
wela=1;
P0=0x7b;
wela=0;
delay(5);
}
void main()
{
uchar a,A1,A2,A3,adval;
wela=1;
P0=0x7f; //置CSAD为0,选通ADCS以后不必再管ADCS
wela=0;
while(1)
{
adwr=1;
_nop_();
adwr=0; //启动A/D转换
_nop_();
adwr=1;
for(a=10;a>0;a--)
{
display(A1,A2,A3); //显示程序
}
P1=0xff; //读取P1口之前先给其全1
adrd=1; //选通ADCS
_nop_();
adrd=0; //A/D读
_nop_();
adval=P1; //A/D数据给P1口
adrd=1;
A1=adval/100;
A2=adval%100/10;
A3=adval%10;
}
}
之前显示函数里面 使用的 fe fd fb 这里用7 因为我们要同时保持cs端低电平
D/A数模转换
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit dula=P2^6;
sbit wela=P2^7;
sbit dawr=P3^6;
sbit dacs=P3^2;
sbit fm=P2^3;
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void main()
{
uchar val,flag;
wela=0;
dula=0;
dacs=0;
dawr=0;
P0=0;
while(1)
{
if(flag==0)
{
val+=5;
P0=val;
if(val==255)
{
flag=1;
fm=0;
delay(100);
fm=1;
}
delay(50);
}else
{
val-=5;
P0=val;
if(val==0)
{
flag=0;
fm=0;
delay(100);
fm=1;
}
delay(50);
}
}
}