ADC&DAC就没笔记了
想知道原理的可以看看数据手册和相关视频
使用总之比较简单
框架+底层驱动,知道怎么使用就好了
练习:数码管通过rb2旋转读取 ADC 结果
main.c
#include "reg52.h"
#include "iic.h"
int abc;
int a,b,c;
unsigned char code duanma[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};
unsigned char code duanmadot[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};
void SelectHC573(unsigned char n){
switch(n){
case 4:
P2 = (P2 & 0x1f) | 0x80;
break;
case 5:
P2 = (P2 & 0x1f) | 0xa0;
break;
case 6:
P2 = (P2 & 0x1f) | 0xc0;
break;
case 7:
P2 = (P2 & 0x1f) | 0xe0;
break;
}
}
void Display(unsigned char pos,unsigned char value){
SelectHC573(7);
P0=0xff;
SelectHC573(6);
P0=0x01<<pos;
SelectHC573(7);
P0=value;
}
void delay(unsigned int t){
while(t--);
}
void SystemInit(){
SelectHC573(4);
P0=0xff;
SelectHC573(5);
P0=0x00;
}
void show(){
abc=PCF8591_Adc();
a=abc/100;
b=(abc%100)/10;
c=abc%10;
Display(5,duanmadot[a]);
delay(500);
Display(6,duanma[b]);
delay(500);
Display(7,duanma[c]);
delay(500);
Display(0,duanma[10]);
delay(500);
Display(1,duanma[10]);
delay(500);
Display(2,duanma[10]);
delay(500);
Display(3,duanma[10]);
delay(500);
Display(4,duanma[10]);
delay(500);
}
void main(){
SystemInit();
while(1){
show();
}
}
iic.c
/*
程序说明: IIC总线驱动程序
软件环境: Keil uVision 4.10
硬件环境: CT107单片机综合实训平台 8051,12MHz
日 期: 2011-8-9
*/
#include "reg52.h"
#include "intrins.h"
#define DELAY_TIME 5
#define SlaveAddrW 0xA0
#define SlaveAddrR 0xA1
//总线引脚定义
sbit SDA = P2^1; /* 数据线 */
sbit SCL = P2^0; /* 时钟线 */
void IIC_Delay(unsigned char i)
{
do{_nop_();}
while(i--);
}
//总线启动条件
void IIC_Start(void)
{
SDA = 1;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 0;
IIC_Delay(DELAY_TIME);
SCL = 0;
}
//总线停止条件
void IIC_Stop(void)
{
SDA = 0;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 1;
IIC_Delay(DELAY_TIME);
}
//发送应答
void IIC_SendAck(bit ackbit)
{
SCL = 0;
SDA = ackbit; // 0:应答,1:非应答
IIC_Delay(DELAY_TIME);
SCL = 1;
IIC_Delay(DELAY_TIME);
SCL = 0;
SDA = 1;
IIC_Delay(DELAY_TIME);
}
//等待应答
bit IIC_WaitAck(void)
{
bit ackbit;
SCL = 1;
IIC_Delay(DELAY_TIME);
ackbit = SDA;
SCL = 0;
IIC_Delay(DELAY_TIME);
return ackbit;
}
//通过I2C总线发送数据
void IIC_SendByte(unsigned char byt)
{
unsigned char i;
for(i=0; i<8; i++)
{
SCL = 0;
IIC_Delay(DELAY_TIME);
if(byt & 0x80) SDA = 1;
else SDA = 0;
IIC_Delay(DELAY_TIME);
SCL = 1;
byt <<= 1;
IIC_Delay(DELAY_TIME);
}
SCL = 0;
}
//从I2C总线上接收数据
unsigned char IIC_RecByte(void)
{
unsigned char i, da;
for(i=0; i<8; i++)
{
SCL = 1;
IIC_Delay(DELAY_TIME);
da <<= 1;
if(SDA) da |= 1;
SCL = 0;
IIC_Delay(DELAY_TIME);
}
return da;
}
//以下是需要自己添加的
unsigned char PCF8591_Adc(void)
{
unsigned char temp;
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x43); //0x41光敏 0x43不带光敏 允许 DAC,ADC 通道 3
IIC_WaitAck();
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
temp = IIC_RecByte();
IIC_SendAck(1);
IIC_Stop();
return temp;
}
void PCF8591_Dac(unsigned char dat)
{
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x43); // 允许 DAC,ADC 通道 3
IIC_WaitAck();
IIC_SendByte(dat); // dat-输出数模转换的数据
IIC_WaitAck();
IIC_Stop();
}
iic.h
#ifndef _IIC_H
#define _IIC_H
void IIC_Start(void);
void IIC_Stop(void);
bit IIC_WaitAck(void);
void IIC_SendAck(bit ackbit);
void IIC_SendByte(unsigned char byt);
unsigned char IIC_RecByte(void);
unsigned char PCF8591_Adc(void);
#endif
关于PCF8591_Dac输出电压,参数设置为51就输出1V,255就5V,1个51就是1V,具体原因就不解释了,相信都懂