Blue Bridge Cup MCU | Special training case [Advanced 06] DAC analog voltage output based on PCF8591

【1】Title requirements

Create a new project, write code in I/O mode , and implement the following functions on the CT107D MCU comprehensive training platform:

1. Correctly transplant the underlying driver code file of the IIC bus into the project.

2. Configure J5 in BTN mode , set S4 as an independent button, and select the output mode of DAC.

3. The display format of the digital tube is as shown in the figure below. The leftmost 3 digits of the digital tube display the current DAC output mode , and the rightmost 3 digits of the digital tube display the current DAC output voltage . The voltage unit is V, and 2 decimal places are reserved.

4. There are three output modes of the DAC.
    In mode 1 , the left side of the digital tube displays "-1- ", and the DAC outputs a fixed voltage of 2.00V .
    In mode 2 , the left side of the digital tube displays " -2- ", and the DAC outputs a fixed voltage of 4.00V .
    In mode 3 , the left side of the digital tube displays " -3- ", the right side of the digital tube displays the real-time input voltage of the AIN3 channel of the PCF8591 chip , and this voltage is used as the output parameter of the DAC, so that the output voltage of the DAC and the input voltage of the AIN3 channel of the PCF8591 chip To keep the synchronous change, the output voltage of the DAC can be changed by changing the adjustable resistance of Rb2 .

5. After the system is powered on, it works in mode 1. Press the S4 button in mode 1 to enter mode 2; press the S4 button in mode 2 to enter mode 3; press the S4 button in mode 3 to return to mode 1, and so on.

6. Use a multimeter to measure the 19 and 20 pins of J3 , that is, the DAC output voltage. The value displayed by the multimeter should be basically the same as the voltage displayed on the right side of the digital tube.

【hint】:

The basic principle of IIC interface can be seen in this blog: "[Blue Bridge Cup MCU Advanced Strengthening-01] IIC Bus Interface Technology Basics" .

The basic principle of PCF8591 can be seen in this blog: "[Blue Bridge Cup MCU Advanced Enhancement-02] Basic Principle and A/D Conversion Application of PCF8591"

[2] Analysis of core source code

/*==================蓝桥杯单片机特训==================
【进阶06】:基于PCF8591的DAC模拟电压输出
**平  台:CT107D单片机综合实训平台
**模  式:IO模式
**底层驱动文件:2022年竞赛资源数据包提供的文件
**设  计:欧浩源(小蜜蜂老师,[email protected])
**时  间:2022-04-05
**更多详见:www.xmf393.com
====================================================*/

#include "reg52.h"
#include "iic.h"

sbit S4 = P3^3;

//定义动态显示中单个数码管点亮时长
#define TSMG	500
//-------共阳数码管的段码编码表(无小数点)--------
//0 1 2 3 4 5 6 7 8 9 A B C D E F - .
unsigned char code SMG_NoDot[18]={0xc0,0xf9,
	  0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,
    0x88,0x80,0xc6,0xc0,0x86,0x8e,0xbf,0x7f};
//-------共阳数码管的段码编码表(带小数点)--------
//0. 1. 2. 3. 4. 5. 6. 7. 8. 9.
unsigned char code SMG_Dot[10]={0x40,0x79,
		0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};
//-----------------------------------------------

unsigned char adc_value = 0;		//AIN3的采样数据
float adc_volt = 0;					//AIN3的换算电压
unsigned int smg_volt = 0;			//AIN3的显示电压

unsigned char stat_smg = 1;			//当前显示界面标志
unsigned char mode = 1;				//模式

/*====锁存器设置和数码管显示的代码参考前面的案例====*/
//===================电压值显示函数===================
void DisplaySMG_ADC()
{
	//输出电压
	DisplaySMG_Bit(7,SMG_NoDot[smg_volt	% 10]);
	DisplaySMG_Bit(6,SMG_NoDot[(smg_volt / 10) % 10]);		
	DisplaySMG_Bit(5,SMG_Dot[smg_volt / 100]);				
	//工作模式
	DisplaySMG_Bit(2,SMG_NoDot[16]);
	DisplaySMG_Bit(1,SMG_NoDot[mode]);		
	DisplaySMG_Bit(0,SMG_NoDot[16]);		
}

//===============PCF8591电压采样处理函数===============
void Read_PCF8591_AIN3()
{
	IIC_Start();					
	IIC_SendByte(0x90);		//PCF8591的写设备地址 
	IIC_WaitAck();				
	IIC_SendByte(0x43); 	//输出DAC,转换AIN3			
	IIC_WaitAck();  						
	IIC_Stop(); 
	
	DisplaySMG_ADC();		//等待电压转换完成
	
	IIC_Start();									
	IIC_SendByte(0x91); 	//PCF8591的读设备地址
	IIC_WaitAck(); 								
	adc_value = IIC_RecByte();	//读出AD采样数据
	IIC_SendAck(1);			 	//产生非应答信号 								
	IIC_Stop();
	//将ADC采样到的数据换算成对应的电压值
	adc_volt = adc_value * (5.0 / 255);
	smg_volt = adc_volt * 100;
}
//===============PCF8591电压输出设置函数===============
void Set_PCF8591_DAC(unsigned char dat)
{
	IIC_Start();					
	IIC_SendByte(0x90);		//PCF8591的写设备地址 
	IIC_WaitAck();	
	IIC_SendByte(0x43); 	//输出DAC,转换AIN3	
	IIC_WaitAck(); 								
	IIC_SendByte(dat); 		//设置DAC电压输出参数
	IIC_WaitAck(); 			//产生非应答信号 								
	IIC_Stop();
}
//===================按键扫描处理函数==================
void Scan_Keys()
{
	if(S4 == 0)
	{
		DelaySMG(500);	
		if(S4 == 0)
		{
			if(mode == 1)						
			{
				mode = 2;					//切换为模式2
				Set_PCF8591_DAC(204);	    //输出固定电压4V
				smg_volt = 400;				//显示电压:4.00
			}
			else if(mode == 2)
			{
				mode = 3;					//切换为模式3
			}
			else if(mode == 3)
			{
				mode = 1;					//切换为模式1
				Set_PCF8591_DAC(102);	    //输出固定电压2V
				smg_volt = 200;				//显示电压:2.00
			}
			while(S4 == 0)					//松手检测
			{	
				DisplaySMG_ADC();			//保持数码管动态显示
			}
		}
	}
}

[Note]: For more information about the Blue Bridge Cup MCU preparation content, please refer to the "Blue Bridge Cup MCU Design and Development" Little Bee Special Training Manual , which can be downloaded from this site. More related Blue Bridge Cup case complete source code and study preparations Competition notes, welcome to the official account of  " Little Bee Notes "  .

Guess you like

Origin blog.csdn.net/ohy3686/article/details/123969207