蓝桥杯单片机|蓝桥杯单片机-DAC-用板载板载PCF8591芯片在D/A引脚上输出指定的电压

【蓝桥杯单片机|蓝桥杯单片机-DAC-用板载板载PCF8591芯片在D/A引脚上输出指定的电压】
文章目录

  • 题目
  • main.c
  • iic.c
  • iic.h

题目 1.控制板载PCF8591芯片在D/A引脚上输出指定的电压
2.数码管前三位显示输入数据0-255,后两位显示输出的电压值
3.按下按键S4则输入数据+40,按下S5则输入数据-40
main.c
//@mzw //控制板载PCF8591芯片在D/A引脚上输出指定的电压 //数码管前三位显示输入数据0-255,后两位显示输出的电压值 //按下按键S4则输入数据+40,按下S5则输入数据-40 #include #include #include #includesbit hc138_A=P2^5; sbit hc138_B=P2^6; sbit hc138_C=P2^7; sbit S7 = P3^0; sbit S6 = P3^1; sbit S5 = P3^2; sbit S4 = P3^3; unsigned char shuzi[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; //0-9 unsigned char shuzi1[10]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10}; //0-9//含小数点unsigned char key_value = https://www.it610.com/article/0; unsigned char input_v = 0; //输出电压范围0-255 int real_v = 0; //实际输出电压0-5v的10倍(方便数码管显示)/*****配置138译码器*****/ void HC138(unsigned int n) { switch(n) { case 4: hc138_A=0 ; hc138_B=0 ; hc138_C=1; break; case 5: hc138_A=1 ; hc138_B=0 ; hc138_C=1; break; case 6: hc138_A=0 ; hc138_B=1 ; hc138_C=1; break; case 7: hc138_A=1 ; hc138_B=1 ; hc138_C=1; break; } } /*****延时函数*****/ void delay(unsigned int k) { while(k--); }/*****数码管显示函数*****/ void Display() { char x = 0x01; int i = 0; for(i = 1; i<=8 ; i++) { HC138(6); P0 = x; x = _crol_(x,1); HC138(7); P0 = 0xff; delay(20); //数码管消隐放到这个地方好一点 switch (i) { case 1 :P0 = shuzi[input_v/100]; break; case 2 :P0 = shuzi[input_v/10%10]; break; case 3 :P0 = shuzi[input_v%10]; break; case 4 :P0 = 0xff; break; case 5 :P0 = 0xff; break; case 6 :P0 = 0xff; break; case 7 :P0 = shuzi1[real_v / 10]; break; case 8 :P0 = shuzi[real_v % 10]; break; } delay(500); P0 = 0xff; } }/*****按键扫描函数*****/ void key_scan() { if((S4 == 0) || (S5 == 0) || (S6 == 0) || (S7 == 0)) //其中一个按键被按下则为1 { delay(100); //延时消抖 if(S4 == 0) key_value = 4; if(S5 == 0) key_value = 5; if(S6 == 0) key_value = 6; if(S7 == 0) key_value = 7; while((S4 == 0) || (S5 == 0) || (S6 == 0) || (S7 == 0))//等待按键松开 { Display(); } } }void Key_real() { if(key_value == 4) { input_v = input_v + 40; } if(key_value == 5) { input_v = input_v - 40; } key_value = 0; }/***** DAC相关 *****/ /* 在D/A引脚输出0-5v电压*/ /* 参数dat范围0-255,对应电压0-5v */ 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(); }void main() { HC138(5); P0=0x00; //蜂鸣器和继电器初始化、、全关 HC138(4); P0= 0xff; //LED小灯初始化、、全关 while(1) { key_scan(); Key_real(); dac_pcf8591(input_v); real_v = input_v * 5 * 10 / 255; Display(); } }

iic.c
/* 程序说明: IIC总线驱动程序 软件环境: Keil uVision 4.10 硬件环境: CT107单片机综合实训平台 8051,12MHz 日期: 2011-8-9 该程序为蓝桥杯单片机比赛官方所提供的参考代码 */#include "reg52.h" #include "intrins.h"#define somenop {_nop_(); _nop_(); _nop_(); _nop_(); _nop_(); }#define SlaveAddrW 0xA0 #define SlaveAddrR 0xA1//总线引脚定义 sbit SDA = P2^1; /* 数据线 */ sbit SCL = P2^0; /* 时钟线 *///总线启动条件 void IIC_Start(void) { SDA = 1; SCL = 1; somenop; SDA = 0; somenop; SCL = 0; }//总线停止条件 void IIC_Stop(void) { SDA = 0; SCL = 1; somenop; SDA = 1; }//应答位控制 void IIC_Ack(bit ackbit) { if(ackbit) { SDA = 0; } else { SDA = 1; } somenop; SCL = 1; somenop; SCL = 0; SDA = 1; somenop; }//等待应答 bit IIC_WaitAck(void) { SDA = 1; somenop; SCL = 1; somenop; if(SDA) { SCL = 0; IIC_Stop(); return 0; } else { SCL = 0; return 1; } }//通过I2C总线发送数据 void IIC_SendByte(unsigned char byt) { unsigned char i; for(i=0; i<8; i++) { if(byt&0x80) { SDA = 1; } else { SDA = 0; } somenop; SCL = 1; byt <<= 1; somenop; SCL = 0; } }//从I2C总线上接收数据 unsigned char IIC_RecByte(void) { unsigned char da; unsigned char i; for(i=0; i<8; i++) { SCL = 1; somenop; da <<= 1; if(SDA) da |= 0x01; SCL = 0; somenop; } return da; }

iic.h
#ifndef _IIC_H #define _IIC_H//函数声明 void IIC_Start(void); void IIC_Stop(void); void IIC_Ack(bit ackbit); void IIC_SendByte(unsigned char byt); bit IIC_WaitAck(void); unsigned char IIC_RecByte(void); #endif

iic.c和iic.h文件为蓝桥杯单片机比赛官方所提供的例程

    推荐阅读