蓝桥杯单片机必备知识—–(4)pcf8591–DAC
IIC.H文件中添加
dac_pcf8591(unsigned char dat)
IIC.C文件中添加
void dac_pcf8591(unsigned char dat)
{
IIC_Start(); //总线启动条件
IIC_SendByte(0x90); //通过I2C总线发送数据
IIC_WaitAck(); //等待应答
IIC_SendByte(0x40); //通过I2C总线发送数据
IIC_WaitAck(); //等待应答
IIC_SendByte(dat);
IIC_WaitAck();
IIC_Stop();
}
主函数 main.c中添加
dac_pcf8591(dac_value);
注:该ADC模块只能通过0~255来输出最大4.85V,显示时应该需要适当的转换
测试结果:(测试结果没有转换)
完整代码粘贴
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 init_pcf8591(void);
unsigned char adc_pcf8591(void);
void dac_pcf8591(unsigned char dat);
#endif
IIC.C
#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;
}
//DAC
void dac_pcf8591(unsigned char dat)
{
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x40);
IIC_WaitAck();
IIC_SendByte(dat);
IIC_WaitAck();
IIC_Stop();
}
//ADC
void init_pcf8591()
{
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x03);
IIC_WaitAck();
IIC_Stop();
}
unsigned char adc_pcf8591()
{
unsigned char temp;
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
temp = IIC_RecByte();
IIC_SendAck(1);
IIC_Stop();
return temp;
}
MAIN.C
#include <stc15f2k60s2.h>
#include "iic.h"
#define uint unsigned int
#define uchar unsigned char
uchar tab[] = {
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};
uchar dspbuf[8] = {
10,10,10,10,10,10,10,10};
uchar adc_flag,dac_value,adc_value;
uchar s6 = 0,s7 = 0;
void load();
void display();
void read_key();
void cls()
{
P2 = (P2 & 0x1f) | 0x80;
P0 = 0xff;
P2 = 0x1f;
P2 = (P2 & 0x1f) | 0xa0;
P0 = 0x00;
P2 = 0x1f;
}
void main()
{
cls();
AUXR = 0xc0;
TMOD = 0x00;
TL0 = 0xcd;
TH0 = 0xd4;
TR0 = 1;
ET0 = 1;
EA = 1;
init_pcf8591();
while(1)
{
if(adc_flag){
adc_value = adc_pcf8591();adc_flag = 0;}
read_key();
}
}
void time0() interrupt 1
{
static unsigned char intr = 0;
intr++;
if(intr = 50){
intr = 0; adc_flag = 1;}
display();
}
void load()
{
if(s7)
{
s7 = 0;
dac_value = 105.15; //2v
dac_pcf8591(dac_value);
}
if(s6)
{
s6= 0;
dac_value = 210.31; //4v
dac_pcf8591(dac_value);
}
dspbuf[1] = dac_value / 100;
dspbuf[2] = dac_value %100/10;
dspbuf[3] = dac_value %10;
dspbuf[5] = adc_value / 100;
dspbuf[6] = adc_value %100/10;
dspbuf[7] = adc_value %10;
}
void read_key()
{
static unsigned char state = 0;
uchar temp;
temp = P3 & 0x0f;
switch(state)
{
case 0:
{
if(P3 != 0x0f) state = 1;
}break;
case 1:
{
if(P3 != 0x0f)
{
switch(temp)
{
case 0x0e:{
s7= (s7 + 1) % 2; state = 2;}break;
case 0x0d:{
s6= (s6 + 1) % 2; state = 2;}break;
}
}
else state = 0;
}break;
case 2:
{
if(P3 == 0xff) state = 0;
}break;
}
}
void display()
{
static unsigned char dspcom = 0;
load();
P2 = (P2 & 0x1f) | 0xe0;
P0 = 0xff;
P2 = 0x1f;
P2 = (P2 & 0x1f) | 0xc0;
P0 = 1<<dspcom;
P2 = 0x1f;
P2 = (P2 & 0x1f) | 0xe0;
P0 = tab[dspbuf[dspcom]];
P2 = 0x1f;
if(++dspcom == 8) dspcom = 0;
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/100135.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...