Blue Bridge Cup MCU | Special training case [Advanced 05] Sampling the voltage of photoresistor and adjustable resistor

【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. The photoresistor rd1 is connected to the AIN1 channel of the PCF8591 ; the adjustable resistor rb2 is connected to the AIN3 channel of the PCF8591.

3. After the system is powered on, the voltage of AIN1 channel and AIN3 channel is sampled cyclically, and the A/D conversion result is read out, converted into the corresponding actual voltage value, and 2 decimal places are reserved, and the unit is V.

4. Display the sampled real-time voltage value on the digital tube. The display format is as shown in the figure. The three digits at the left end of the digital tube display the AIN1 channel, that is, the voltage value of the sampling photoresistor rd1. The three digits at the right end of the digital tube display AIN3 channel, that is, sampling the voltage value of the adjustable resistor rd3, the unused digital tube goes out.

【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

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

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

//定义动态显示中单个数码管点亮时长
#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 adc1_value = 0;		//AIN1的采样数据
float adc1_volt = 0;				//AIN1的换算电压
unsigned int smg1_volt = 0;			//AIN1的显示电压
unsigned char adc3_value = 0;		//AIN3的采样数据
float adc3_volt = 0;				//AIN3的换算电压
unsigned int smg3_volt = 0;			//AIN3的显示电压


/*====锁存器设置和数码管显示的代码参考前面的案例====*
//===================电压值显示函数===================
void DisplaySMG_ADC()
{
	//数码管右起第0位
	DisplaySMG_Bit(7,SMG_NoDot[smg1_volt	% 10]);
	//数码管右起第1位
	DisplaySMG_Bit(6,SMG_NoDot[(smg1_volt / 10) % 10]);		
	//数码管右起第2位
	DisplaySMG_Bit(5,SMG_Dot[smg1_volt / 100]);				

	//数码管右起第5位
	DisplaySMG_Bit(2,SMG_NoDot[smg3_volt	% 10]);
	//数码管右起第6位
	DisplaySMG_Bit(1,SMG_NoDot[(smg3_volt / 10) % 10]);		
	//数码管右起第7位
	DisplaySMG_Bit(0,SMG_Dot[smg3_volt / 100]);		
}
//===============PCF8591电压采样处理函数===============
unsigned char Read_PCF8591_ADC(unsigned char ain)
{
	unsigned char tmp;
	IIC_Start();					
	IIC_SendByte(0x90);		//PCF8591的写设备地址 
	IIC_WaitAck();				
	if(ain == 1)
	{
		IIC_SendByte(0x01); //通道1,光敏电阻电压
	}
	else if(ain == 3)
	{
		IIC_SendByte(0x03); //通道3,可调电阻电压
	}		
	IIC_WaitAck();  						
	IIC_Stop(); 
	
	DisplaySMG_ADC();			//等待电压转换完成
	
	IIC_Start();									
	IIC_SendByte(0x91); 	//PCF8591的读设备地址
	IIC_WaitAck(); 								
	tmp = IIC_RecByte(); 	//读出AD采样数据
	IIC_SendAck(1);			 	//产生非应答信号 								
	IIC_Stop();
	return tmp;
}
//=============采样光敏电阻和可调电阻的电压=============
void Read_AIN1_AIN3()
{
	adc1_value = Read_PCF8591_ADC(1);
	adc1_volt = adc1_value * (5.0 / 255);
	smg1_volt = adc1_volt * 100;
	
	adc3_value = Read_PCF8591_ADC(3);
	adc3_volt = adc3_value * (5.0 / 255);
	smg3_volt = adc3_volt * 100;
}

[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/123968723