Single-chip microcomputer-dual computer communication

**Realization function: ** Both machines can control each other's potentiometer module to open and close (the single-chip microcomputer is the 89C51 single-chip microcomputer of Puzhong Technology)
** Connection: **P3 1 connects to P3 0, P3 0 connects to P3 1;
experimental principle :
1. Schematic diagram of AD analog-to-digital conversion circuit:

2. The main technical indicators of the AD analog-to-digital converter:
①The resolution of the
ADC refers to the amount of change in the input analog voltage required to make the output digital change an adjacent digital. Commonly used binary digit representation. For example, the resolution of a 12-bit ADC is 12 bits, or the resolution is 1/(2^12) of the full scale.
②The quantization error
ADC converts the analog quantity into a digital quantity, and approximates the analog quantity with the digital quantity. This process is called quantization. The quantization error is the error caused by the quantization of the analog quantity by the finite number of ADCs.
③Offset error The
offset error refers to the value at which the output signal is not zero when the input signal is zero, so it is sometimes called zero error. Assuming that the ADC has no nonlinear error, the connection between the midpoints of the steps of the conversion characteristic curve must be a straight line, and the input voltage value corresponding to the intersection of this straight line and the horizontal axis is the offset error.

④Full-scale error
Full-scale error is also called gain error. The ADC's full-scale error refers to the difference between the actual input voltage and the ideal input voltage corresponding to the full-scale output code.
⑤ Linearity
Linearity is sometimes called nonlinearity, which refers to the maximum deviation between the actual conversion characteristics of the converter and the ideal straight line.
⑥ Absolute accuracy
In a converter, the maximum value of the difference between the actual analog input and the theoretical analog input corresponding to any number is called absolute accuracy. For ADC, it can be measured at the midpoint of each step, which includes all errors.
⑦ Conversion rate
ADC conversion rate is the speed at which data can be repeatedly converted, that is, the number of conversions per second. The time required to complete an A/D conversion (including stabilization time) is the inverse of the conversion rate.
3. Control word description:

That is, in this experiment, I want to detect the analog signal of the switch potentiometer, and the value of the control word command register is 0X94.
4. Timing diagram:

As can be seen in the timing diagram, we need to write first and then read. Before writing, we must pull down the clock signal and read 12bit data;
C language code:

①shuangji.c
#include "reg52.h"			 
#include"XPT2046.h"	

#define uchar unsigned char
#define uint unsigned int

sbit LSA=P2^2;//数码管使能
sbit LSB=P2^3;
sbit LSC=P2^4;
sbit K3=P3^2; //独立按键
sbit K4=P3^3;
uchar flag;   //存储接受的指令

uchar disp[4];
uchar code smgduan[10]={
    
    0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //数码管显示

void delay(uint i)
{
    
    
	while(i--);	
}

void datapros()
{
    
    
	uint temp;
	static uchar i;
	if(i==50)
	{
    
    
		i=0;
		temp = Read_AD_Data(0x94);		//   AIN0 电位器
	}
	i++;
	disp[0]=smgduan[temp/1000];//千位
	disp[1]=smgduan[temp%1000/100];//百位
	disp[2]=smgduan[temp%1000%100/10];//个位
	disp[3]=smgduan[temp%1000%100%10];		
}

void DigDisplay()
{
    
    
	uchar i;
	for(i=0;i<4;i++)
	{
    
    
		switch(i)	 //位选,选择点亮的数码管,
		{
    
    
			case(0):
				LSA=1;LSB=1;LSC=1; break;//显示第0位
			case(1):
				LSA=0;LSB=1;LSC=1; break;//显示第1位
			case(2):
				LSA=1;LSB=0;LSC=1; break;//显示第2位
			case(3):
				LSA=0;LSB=0;LSC=1; break;//显示第3位	
		}
		P0=disp[i];//发送数据
		delay(100); //间隔一段时间扫描	
		P0=0x00;//消隐
	}		
}

void UART_INIT(){
    
    
	SCON=0x50;//设定串口工作方式0101 0000
	PCON=0x00;//设置了SMOD,波特率不翻倍
	TMOD=0x20;//设置定时器1方式二
	EA=1;//开中断
	ES=1;//允许串口1发起中断
	TL1=0xfd;//波特率9600
	TH1=0xfd;
	TR1=1;//定时器1开始计时
}	

void main(){
    
    	
	UART_INIT();

	while(1){
    
    
		uchar i;
		if(K3==0){
    
    
			SBUF=0x00;    //发送0x00给单片机2
		}else if(K4==0){
    
    
			SBUF=0x01;    //发送0x01给单片机2
		}
		switch(flag)     //接收到0x00开启电位器,0x01关闭电位器
		{
    
    
			case(0x00):     //开启电位器
				datapros();
				DigDisplay();
				break;
			case(0x01):     //关闭电位器
				for(i=0;i<4;i++)
				{
    
    
					disp[i]=0;
				}
				break;
		}
	}		
}

//串口中断
void Interrupt_Receive() interrupt 4
{
    
      
	if(TI==1) TI=0;
	if(RI==1)
    {
    
    
        RI=0;
        flag=SBUF;
     }
}      
     
②XPT2046.c
#include"XPT2046.h"

void SPI_Write(uchar dat)   //写数据
{
    
    
	uchar i;
	CLK = 0;
	for(i=0; i<8; i++)      //先写高位,再写低位
	{
    
    
		DIN = dat >> 7;  	//放置最高位
		dat <<= 1;          //左移一位,从最高位放起
		CLK = 0;			//上升沿放置数据

		CLK = 1;

	}
}

uint SPI_Read(void)         //读数据
{
    
    
	uint i, dat=0;
	CLK = 0;
	for(i=0; i<12; i++)		//接收12位数据
	{
    
    
		dat <<= 1;          //读取最高位

		CLK = 1;
		CLK = 0;

		dat |= DOUT;        //DOUT数据输出管脚

	}
	return dat;	
}

uint Read_AD_Data(uchar cmd)
{
    
    
	uchar i;
	uint AD_Value;
	CLK = 0;             //拉低时钟信号再开始写入
	CS  = 0;             //拉低片选信号
	SPI_Write(cmd);      //写一个字节
	for(i=6; i>0; i--);   //延时一个时钟周期,等待转换结果
	CLK = 1;	         //发送一个时钟周期,清除BUSY
	_nop_();
	_nop_();
	CLK = 0;
	_nop_();
	_nop_();
	AD_Value=SPI_Read(); //返回的是12位的数据,想要显示电压还需要进行处理
	CS = 1;              //拉高片选信号,停止传输数据
	return AD_Value;	
}

Flow chart:

①Main function:
Insert picture description here
②Delay function:
Insert picture description here

③Data receiving conversion function (datapros):
Insert picture description here

④Digital tube display function:
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_46837674/article/details/113029607