Aprendizaje del microcontrolador de la Copa Lanqiao (final): errores y correcciones en artículos anteriores (Adjunto: Preguntas del 14.º concurso de microcontroladores de la Copa Lanqiao)

Cero, quejate

Este año, se convocan juntos el teclado matricial + tres protocolos. Realmente se vuelve más difícil cada año, el año pasado fue la ecografía. Quienes lleguen tarde, ¡vamos!

1. Preguntas sobre plantillas creadas por uno mismo y bibliotecas de plantillas escritas por uno mismo

En esos dos artículos, no es necesario leer el código, solo lea el último, lo he resumido en él.

Aprendizaje del microcontrolador de la Copa Lanqiao (treinta): lista de plantillas, resumen de habilidades y experiencia

Debido a mi propia negligencia personal, hay muchos errores que no se han corregido y actualizaré el uso más adelante. Asegúrese de que el programa sea correcto
y también lo enumeraré aquí.

2. 详解A/D、D/A、PCF8591Algunos errores menores en este artículo.


Corregiré algunos errores en los comentarios de AIN1 y AIN3 uno por uno.

3. Versión final de la plantilla

Función principal : Comenta el teclado matricial y activa las teclas independientes (puedes desbloquear el código del teclado matricial si es necesario)
protocolo ds1302: Al llamar al calendario perpetuo, debe agregar la inicialización de ds1302 cuando se inicializa la función principal. Luego llámelo directamente, read_ds1302, y luego sáquelo shijiande esta秒、分、时、日、月、周、年
protocolo onewire: La lectura de temperatura tempget()se puede llamar directamente, pero parece que la precisión no puede alcanzarse después del punto decimal y el cuerpo de la función tiene una instancia.
protocolo de la cii:
① Respecto a EEPROM , simplemente llame a: EEPROM_read, EEPROM_write, pero preste atención a la dirección
② Acerca de la lectura numérica del fotorresistor : Llamar: read_AIN1()Eso es todo. Hay ejemplos de adquisición debajo del cuerpo de la función, puede echar un vistazo
③ Acerca del potencial de Rb2 Lectura numérica: Llamar : read_AIN3()Eso es todo. Los mismos ejemplos que arriba

C Principal

#include <STC15F2K60S2.h>
#include "onewire.h"
#include "iic.h"
#include "ds1302.h"

#define uchar unsigned char
#define uint unsigned int
extern uchar shijian[7];
//共阳,低四位是abcd
//LED共阳
uchar SMG_duanma[19] = 
					{
    
    0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,
						 0x88,0x80,0xc6,0xc0,0x86,0x8e,
						 0xbf,0x7f,0XFF};//分别是0-9(对应下标),A-F,“-”,“.”

uchar yi,er,san,si,wu,liu,qi,ba;
/*
void keycan16();
void delayms(int b);
*/				 
void SelectHC138(uchar channel);
void initsys();
void DisplaySMG_Bit(uchar pos, uchar value);
void Delay_one_ms_SMG();
void SMG_Display();
void Alone_Key();
void Delay_five_ms_Key();
void Timer0Init(void);


void main()
{
    
    
	yi = er = san = si = wu = liu = qi = ba = 18;
	initsys();
	Timer0Init();
	while(1)
	{
    
    
		Alone_Key();
		SMG_Display();
	}
}


void SelectHC138(uchar channel)
{
    
    
	switch(channel)
	{
    
    
		case 4:    //LED
			P2 = (P2 & 0X1F) | 0X80;
		break;
		case 5:    //蜂鸣器和继电器
			P2 = (P2 & 0X1F) | 0XA0;
		break;
		case 6:    //位码
			P2 = (P2 & 0X1F) | 0XC0;
		break;
		case 7:    //段码
			P2 = (P2 & 0X1F) | 0XE0;
		break;
	}
}

void initsys()
{
    
    
	SelectHC138(5);
	P0 = 0X00;//关闭蜂鸣器和继电器
	//0X10是蜂鸣器,0x40是继电器
	SelectHC138(4);
	P0 = 0XFF;
	
	SelectHC138(6);
	P0 = 0XFF;
	SelectHC138(7);
	P0 = 0XFF;
}

void DisplaySMG_Bit(uchar pos, uchar value)
{
    
    
	SelectHC138(6);
	P0 = 0X01 << pos;
	SelectHC138(7);
	P0 = value;
}

void Delay_one_ms_SMG()
{
    
    
	uint j;
	for(j = 845; j > 0; j--);
}

void SMG_Display()
{
    
    
	DisplaySMG_Bit(0, SMG_duanma[yi]);
	Delay_one_ms_SMG();
	DisplaySMG_Bit(1, SMG_duanma[er]);
	Delay_one_ms_SMG();
	DisplaySMG_Bit(2, SMG_duanma[san]);
	Delay_one_ms_SMG();
	DisplaySMG_Bit(3, SMG_duanma[si]);
	Delay_one_ms_SMG();
	
	DisplaySMG_Bit(4, SMG_duanma[wu]);
	Delay_one_ms_SMG();
	DisplaySMG_Bit(5, SMG_duanma[liu]);
	Delay_one_ms_SMG();
	DisplaySMG_Bit(6, SMG_duanma[qi]);
	Delay_one_ms_SMG();
	DisplaySMG_Bit(7, SMG_duanma[ba]);
	Delay_one_ms_SMG();
}

void Delay_five_ms_Key()
{
    
    
	uint i,j;
	for(i = 0;i < 5;i++)
		for(j = 845; j > 0;j--);
}

void Alone_Key()
{
    
    
	//S7
	if(P30 == 0)
	{
    
    
		Delay_five_ms_Key();
		if(P30 == 0)
		{
    
    
			yi = 1;
		}
		while(!P30);
	}
	//S6
	if(P31 == 0)
	{
    
    
		Delay_five_ms_Key();
		if(P31 == 0)
		{
    
    
			er = 1;
		}
		while(!P31);
	}
	//S5
	if(P32 == 0)
	{
    
    
		Delay_five_ms_Key();
		if(P32 == 0)
		{
    
    
			san = 1;
		}
		while(!P32);
	}
	//S4
	if(P33 == 0)
	{
    
    
		Delay_five_ms_Key();
		if(P33 == 0)
		{
    
    
			si = 1;
		}
		while(!P33);
	}
}

void Timer0Init(void)		//1毫秒@12.000MHz
{
    
    
	AUXR |= 0x80;		//定时器时钟1T模式
	TMOD &= 0xF0;		//设置定时器模式
	TL0 = 0x20;		//设置定时初值
	TH0 = 0xD1;		//设置定时初值
	TF0 = 0;		//清除TF0标志
	TR0 = 1;		//定时器0开始计时
}

void Timer0Service() interrupt 1
{
    
    
	
}
/*  矩阵键盘
void delayms(int b)
{
	int i,a;
	for(i=b;i>0;i--)
	for(a=110;a>0;a--);
}


void keycan16()
{
	int temp;
	//第一列
	P3=0x7f;P44=0;P42=1;
	temp=P3;
	temp=temp&0x0f;
	if(temp!=0x0f)
	{
		delayms(5);
		temp=P3;
		temp=temp&0x0f;
		if(temp!=0x0f)
		{temp=P3;
			switch(temp)
			{
				case 0x7e: P0=0XFF;break;//S7
				case 0x7d: P0=0xff;break;//S6
			  case 0x7b: P0=0x55;break;//S5
				case 0x77: P0=0x0f;break;//S4	
			}
			while(temp!=0x0f)
			{
				temp=P3;
				temp=temp&0x0f;		
			}	
		}
	}

   P3=0xbf;P44=1;P42=0;
	temp=P3;
	temp=temp&0x0f;
	if(temp!=0x0f)
	{
		delayms(5);
		temp=P3;
		temp=temp&0x0f;
		if(temp!=0x0f)
		{temp=P3;
			switch(temp)
			{
				case 0xbe: P0=0x0f;break;
				case 0xbd: P0=0x00;break;
			  case 0xbb: P0=0x55;break;
				case 0xb7: P0=0xff;break;				
			}
			while(temp!=0x0f)
			{
				temp=P3;
				temp=temp&0x0f;		
			}	
		}
	}
  P3=0xdf;P44=1;P42=1;
	temp=P3;
	temp=temp&0x0f;
	if(temp!=0x0f)
	{
		delayms(5);
		temp=P3;
		temp=temp&0x0f;
		if(temp!=0x0f)
		{temp=P3;
			switch(temp)
			{
				case 0xde: P0=0xf0;break;
				case 0xdd: P0=0xfc;break;
			  case 0xdb: P0=0xf3;break;
				case 0xd7: P0=0xff;break;				
			}
			while(temp!=0x0f)
			{
				temp=P3;
				temp=temp&0x0f;
			}	
		}
	}
   P3=0xef;P44=1;P42=1;
	temp=P3;
	temp=temp&0x0f;
	if(temp!=0x0f)
	{
		delayms(5);
		temp=P3;
		temp=temp&0x0f;
		if(temp!=0x0f)
		{temp=P3;
			switch(temp)
			{
				case 0xee: P0=0xf0;break;
				case 0xed: P0=0xfc;break;
			  case 0xeb: P0=0x55;break;
				case 0xe7: P0=0x0f;break;
			}
			while(temp!=0x0f)
			{
				temp=P3;
				temp=temp&0x0f;		
			}	
		}
	}
}

*/

ds1302,h

//主函数有下面这一行
//extern uchar shijian[7];
//秒、分、时、日、月、周、年

#ifndef __DS1302_H__
#define __DS1302_H__

#include <STC15F2K60S2.h>
#include <intrins.h>

sbit SCK = P1^7;		
sbit SDA = P2^3;		
sbit RST = P1^3; 

void Write_Ds1302(unsigned char temp);
void Write_Ds1302_Byte( unsigned char address,unsigned char dat );
unsigned char Read_Ds1302_Byte( unsigned char address );

#endif

ds1302.c

#include "ds1302.h"  									

#define DecToBCD(dec) (dec/10*16)+(dec%10)
#define BCDToDec(BCD) (BCD/16*10)+(BCD%16)
unsigned char shijian[7]={
    
    50,59,23,0,0,0,0};
//写字节
void Write_Ds1302(unsigned  char temp) 
{
    
    
	unsigned char i;
	for (i=0;i<8;i++)     	
	{
    
     
		SCK = 0;
		SDA = temp&0x01;
		temp>>=1; 
		SCK=1;
	}
}   

//向DS1302寄存器写入数据
void Write_Ds1302_Byte( unsigned char address,unsigned char dat )     
{
    
    
 	RST=0;	_nop_();
 	SCK=0;	_nop_();
 	RST=1; 	_nop_();  
 	Write_Ds1302(address);	
 	Write_Ds1302(dat);		
 	RST=0; 
}

//从DS1302寄存器读出数据
unsigned char Read_Ds1302_Byte ( unsigned char address )
{
    
    
 	unsigned char i,temp=0x00;
 	RST=0;	_nop_();
 	SCK=0;	_nop_();
 	RST=1;	_nop_();
 	Write_Ds1302(address);
 	for (i=0;i<8;i++) 	
 	{
    
    		
		SCK=0;
		temp>>=1;	
 		if(SDA)
 		temp|=0x80;	
 		SCK=1;
	} 
 	RST=0;	_nop_();
 	SCK=0;	_nop_();
	SCK=1;	_nop_();
	SDA=0;	_nop_();
	SDA=1;	_nop_();
	return (temp);			
}

void Init_DS1302()
{
    
    
	unsigned char i,add;
	add = 0X80;
	Write_Ds1302_Byte(0X8E, 0X00);
	for(i = 0; i < 7; i++)
	{
    
    
		Write_Ds1302_Byte(add,DecToBCD(shijian[i]));
		add = add + 2;
	}
}

void Read_DS1302()
{
    
    
	unsigned char i,add;
	add = 0X81;
	for(i = 0; i < 7; i++)
	{
    
    
		shijian[i] = BCDToDec(Read_Ds1302_Byte(add));
		add = add + 2;
	}
	
}

onewire.h

#ifndef __ONEWIRE_H__
#define __ONEWIRE_H__

#include <STC15F2K60S2.h>

sbit DQ = P1^4;  

unsigned char rd_temperature(void);  
unsigned char tempget();

#endif

onewire.c

#include "onewire.h"

//单总线内部延时函数
void Delay_OneWire(unsigned int t)  
{
    
    
	while(t--);
}

//单总线写操作
void Write_DS18B20(unsigned char dat)
{
    
    
	unsigned char i;
	for(i=0;i<8;i++)
	{
    
    
		DQ = 0;
		DQ = dat&0x01;
		Delay_OneWire(50);
		DQ = 1;
		dat >>= 1;
	}
	Delay_OneWire(50);
}

//单总线读操作
unsigned char Read_DS18B20(void)
{
    
    
	unsigned char i;
	unsigned char dat;
  
	for(i=0;i<8;i++)
	{
    
    
		DQ = 0;
		dat >>= 1;
		DQ = 1;
		if(DQ)
		{
    
    
			dat |= 0x80;
		}	    
		Delay_OneWire(50);
	}
	return dat;
}

//DS18B20初始化
bit init_ds18b20(void)
{
    
    
  	bit initflag = 0;
  	
  	DQ = 1;
  	Delay_OneWire(120);//12
  	DQ = 0;
  	Delay_OneWire(800);//80
  	DQ = 1;
  	Delay_OneWire(100); //10
    initflag = DQ;     
  	Delay_OneWire(50);//5
  
  	return initflag;
}
unsigned char tempget()
{
    
    
	unsigned char high,low,temp;
	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0x44);
	
	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0xbe);
	
	low=Read_DS18B20();
	high=Read_DS18B20();
	temp=(unsigned char )((high<<8|low)*0.0625f);
	return temp;
}


//temp = tempget();
//qi = temp / 10;
//ba = temp % 10;

iic.h

#ifndef __IIC_H__
#define __IIC_H__

#include <STC15F2K60S2.h>
#include "intrins.h"

sbit SD = P2^1;
sbit SCL = P2^0;

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); 
//输出DAC
void Read_DAC(unsigned char date);
unsigned char Write_ADC(unsigned char add);

//读取电位器Rb2
unsigned char read_AIN3();
//读取光敏电阻
unsigned char read_AIN1();

unsigned char EEPROM_read(unsigned char add);

void EEPROM_write(unsigned char add,unsigned char date);

#endif

iic.c

#include "iic.h"

#define DELAY_TIME 5

//I2C总线内部延时函数
void IIC_Delay(unsigned char i)
{
    
    
    do{
    
    _nop_();}
    while(i--);        
}

//I2C总线启动信号
void IIC_Start(void)
{
    
    
    SD = 1;
    SCL = 1;
    IIC_Delay(DELAY_TIME);
    SD = 0;
    IIC_Delay(DELAY_TIME);
    SCL = 0;	
}

//I2C总线停止信号
void IIC_Stop(void)
{
    
    
    SD = 0;
    SCL = 1;
    IIC_Delay(DELAY_TIME);
    SD = 1;
    IIC_Delay(DELAY_TIME);
}

//发送应答或非应答信号
void IIC_SendAck(bit ackbit)
{
    
    
    SCL = 0;
    SD = ackbit;  					
    IIC_Delay(DELAY_TIME);
    SCL = 1;
    IIC_Delay(DELAY_TIME);
    SCL = 0; 
    SD = 1;
    IIC_Delay(DELAY_TIME);
}

//等待应答
bit IIC_WaitAck(void)
{
    
    
    bit ackbit;
	
    SCL  = 1;
    IIC_Delay(DELAY_TIME);
    ackbit = SD;
    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) SD  = 1;
        else SD  = 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(SD) da |= 1;
	SCL = 0;
	IIC_Delay(DELAY_TIME);
    }
    return da;    
}

unsigned char Write_ADC(unsigned char add)
{
    
    
	unsigned char dat;
	IIC_Start();
	IIC_SendByte(0X90);
	IIC_WaitAck();
	IIC_SendByte(add);
	IIC_WaitAck();
	
	IIC_Start();
	IIC_SendByte(0X91);
	IIC_WaitAck();
	dat = IIC_RecByte();
	IIC_SendAck(1);
	IIC_Stop();
	
	return dat;
}

//输出DAC
void Read_DAC(unsigned char date)
{
    
    
	IIC_Start();
	IIC_SendByte(0X90);
	IIC_WaitAck();
	IIC_SendByte(0X40);
	IIC_WaitAck();
	IIC_SendByte(date);
	IIC_WaitAck();
	IIC_Stop();
}

void EEPROM_write(unsigned char add,unsigned char date)
{
    
    
	IIC_Start();
	IIC_SendByte(0XA0);
	IIC_WaitAck();
	IIC_SendByte(add);
	IIC_WaitAck();
	IIC_SendByte(date);
	IIC_WaitAck();
	IIC_Stop();
	
	IIC_Delay(5);
}

unsigned char EEPROM_read(unsigned char add)
{
    
    
	unsigned char dat;
	IIC_Start();
	IIC_SendByte(0XA0);
	IIC_WaitAck();
	IIC_SendByte(add);
	IIC_WaitAck();
	
	IIC_Start();
	IIC_SendByte(0XA1);
	IIC_WaitAck();
	dat = IIC_RecByte();
	IIC_SendAck(1);
	IIC_Stop();
	return dat;
}


/*
STC89C52RC内部EEPROM详细地址表:
第一扇区                   第二扇区                    第三扇区                    第四扇区
起始地址  结束地址   起始地址   结束地址   起始地址   结束地址   起始地址    结束地址
2000h      21FFh       2200h       23FFh      2400h       25FFh       2600h        27FFH
第五扇区                    第六扇区                     第七扇区                    第八扇区
起始地址   结束地址   起始地址   结束地址    起始地址   结束地址   起始地址    结束地址
2800h       29FFh       2A00h      2BFFh        2C00h      2DFFh      2E00h        2FFFh
*/

//读取光敏电阻
unsigned char	read_AIN1()
{
    
    
		unsigned char dat1;
    IIC_Start();    //IIC总线起始信号
    IIC_SendByte(0x90);     //PCF8591的写设备地址
    IIC_WaitAck();  //等待从机应答
    IIC_SendByte(0x01);     //写入PCF8591的控制字
    IIC_WaitAck();  //等待从机应答
    IIC_Stop();     //IIC总线停止信号
    
    IIC_Start();    //IIC总线起始信号
    IIC_SendByte(0x91);     //PCF8591的读设备地址
    IIC_WaitAck();  //等待从机应答
    dat1 = IIC_RecByte();     //读取PCF8591通道1的数据
    IIC_Stop();     //IIC总线停止信号
		return dat1;
    
}
/*
		liu = temp / 100;
		qi  = temp % 100 / 10;
		ba  = temp % 10;
*/

//读取电位器Rb2
unsigned char read_AIN3()
{
    
    
		unsigned char dat3;
    IIC_Start();    //IIC总线起始信号
    IIC_SendByte(0x90);     //PCF8591的写设备地址
    IIC_WaitAck();  //等待从机应答
    IIC_SendByte(0x03);     //写入PCF8591的控制字
    IIC_WaitAck();  //等待从机应答
    IIC_Stop();     //IIC总线停止信号
    
    IIC_Start();    //IIC总线起始信号
    IIC_SendByte(0x91);     //PCF8591的读设备地址
    IIC_WaitAck();  //等待从机应答
    dat3 = IIC_RecByte();     //读取PCF8591通道1的数据
    IIC_Stop();     //IIC总线停止信号
    return dat3;
}
/*
		liu = temp / 100;
		qi  = temp % 100 / 10;
		ba  = temp % 10;
*/

Adjunto, preguntas del 14º Concurso de Microcontroladores de la Copa Lanqiao

Insertar descripción de la imagen aquí
Insertar descripción de la imagen aquí
Insertar descripción de la imagen aquí
Insertar descripción de la imagen aquí
Insertar descripción de la imagen aquí

Insertar descripción de la imagen aquí
Insertar descripción de la imagen aquí

#include <STC15F2K60S2.h>
#include "onewire.h"
#include "iic.h"
#include "ds1302.h"

#define uchar unsigned char
#define uint unsigned int
extern uchar shijian[7];

sbit LED_0 = P0^0;
sbit LED_1 = P0^1;
sbit LED_2 = P0^2;

sbit LED_3 = P0^3;
sbit LED_4 = P0^4;
//共阳,低四位是abcd
//LED共阳
uchar SMG_duanma[21] = 
					{
    
    0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,
						 0x88,0x80,0xc6,0xc0,0x86,0x8e,
						 0xbf,0x7f,0XFF,0X8C,0X89};//分别是0-9(对应下标),A-F,“-”,“.”,"灭",“P”,"H"

uchar yi,er,san,si,wu,liu,qi,ba;
	 
 
void keycan16();
void delayms(int b);				 
void SelectHC138(uchar channel);
void initsys();
void DisplaySMG_Bit(uchar pos, uchar value);
void Delay_one_ms_SMG();
void SMG_Display();
void Timer0Init(void);
void S4_chocie(uchar S4_st);
void MODE_back(uchar S5_st);
void Check_light();

//原初界面-0和采集界面-1
uchar Two_win = 0;
//s4的三个界面
uchar S4_st = 0;
//s5的三个回显
uchar S5_st = 0;
//采集次数
uchar gather = 0;
//温度参数
uchar wen_can = 30;
//
uchar old_light = 0; 

uchar shijian_new[7];

uchar shidu = 0;

uchar wendu_table[10] = 0;

void main()
{
    
    
 uchar temp;
	yi = er = san = si = wu = liu = qi = ba = 18;
	initsys();
	Timer0Init();
 Init_DS1302();
 
	while(1)
	{
    
    

		keycan16();
 S4_chocie(S4_st);

		SMG_Display();
	}
}
//S5界面选择
void MODE_back(uchar S5_st)
{
    
    
 uchar wendu_show_max=0;
 uchar wendu_show_avr=0;
 uchar i;
 if(S5_st==0)
 {
    
    
  {
    
    
   wendu_show_max = tempget();
   yi = 12;
   er=18;
   san = wendu_show_max / 10;
   si  = wendu_show_max % 10;
   wu = 16;
   liu = wendu_show_max / 10;
   qi  = wendu_show_max % 10;
   ba  = (wendu_show_max * 10) % 10;
  }
 
 }
 else if(S5_st==1)
 {
    
    
  yi = 20;
  wu = 16;
  er = 18 ;
  san = si = liu = ba = qi = 10;
 }
 else if(S5_st==2)
 {
    
    
  yi = 15;
  er = san = 0;
  si = shijian[2] / 10;
  wu = shijian[2] % 10;
  liu = 16;
  qi = shijian[1] / 10;
  ba = shijian[1] % 10;
 }
}


//S4界面选择
void S4_chocie(uchar S4_st)
{
    
    
 if(Two_win==0)
 {
    
    
  P0 &=0xFF;
  if(S4_st==0)
  {
    
    
   Read_DS1302();
   SelectHC138(4);
   LED_0 = 0;LED_1 = 1;LED_2 = 1;
   yi = shijian[2] / 10;er = shijian[2] % 10;
   san = 16;
   si = shijian[1] / 10;wu = shijian[1] % 10;
   liu = 16;
   qi = shijian[0] / 10;ba = shijian[0] % 10;
  }
  else if(S4_st==1)
  {
    
    
    SelectHC138(4);
    LED_0 = 1;LED_1 = 0;LED_2 = 1;
    MODE_back(S5_st);
  }
  else if(S4_st==2)
  {
    
    
    SelectHC138(4);
    LED_0 = 1;LED_1 = 1;LED_2 = 0;
    yi = 19;er=san=si=wu=liu=18;
    qi = wen_can / 10;
    ba = wen_can % 10;
  }
 }
}

void Check_light()
{
    
    
  uchar light_temp;
  uchar wendu;
  uchar i;
  light_temp = read_AIN1();
  liu = light_temp / 100;
  qi  = light_temp % 100 /10;
  ba  = light_temp % 10; 
  if(old_light - light_temp > 50)
  {
    
    
   Two_win = 1;
  }
  else if(old_light - light_temp < 50)
  {
    
    
   Two_win = 0;
  }
  old_light = light_temp;
   delayms(10000);
 
  if(Two_win == 1)
  {
    
    
   gather++;
   wendu = tempget();
   shidu = 0;
   for(i=0;i<3;i++)
   {
    
    
    shijian_new[i] = shijian[i];
   }
   for(i=0;i=10;i++)
   {
    
    
    if(wendu_table[i]==0)
    {
    
    
     wendu_table[i]=wendu;
    }
   }
  }

  
}

void SelectHC138(uchar channel)
{
    
    
	switch(channel)
	{
    
    
		case 4:    //LED
			P2 = (P2 & 0X1F) | 0X80;
		break;
		case 5:    //蜂鸣器和继电器
			P2 = (P2 & 0X1F) | 0XA0;
		break;
		case 6:    //位码
			P2 = (P2 & 0X1F) | 0XC0;
		break;
		case 7:    //段码
			P2 = (P2 & 0X1F) | 0XE0;
		break;
	}
}

void initsys()
{
    
    
	SelectHC138(5);
	P0 = 0X00;//关闭蜂鸣器和继电器
	//0X10是蜂鸣器,0x40是继电器
	SelectHC138(4);
	P0 = 0XFF;
	
	SelectHC138(6);
	P0 = 0XFF;
	SelectHC138(7);
	P0 = 0XFF;
}

void DisplaySMG_Bit(uchar pos, uchar value)
{
    
    
	SelectHC138(6);
	P0 = 0X01 << pos;
	SelectHC138(7);
	P0 = value;
}

void Delay_one_ms_SMG()
{
    
    
	uint j;
	for(j = 845; j > 0; j--);
}

void SMG_Display()
{
    
    
	DisplaySMG_Bit(0, SMG_duanma[yi]);
	Delay_one_ms_SMG();
	DisplaySMG_Bit(1, SMG_duanma[er]);
	Delay_one_ms_SMG();
	DisplaySMG_Bit(2, SMG_duanma[san]);
	Delay_one_ms_SMG();
	DisplaySMG_Bit(3, SMG_duanma[si]);
	Delay_one_ms_SMG();
	
	DisplaySMG_Bit(4, SMG_duanma[wu]);
	Delay_one_ms_SMG();
	DisplaySMG_Bit(5, SMG_duanma[liu]);
	Delay_one_ms_SMG();
	DisplaySMG_Bit(6, SMG_duanma[qi]);
	Delay_one_ms_SMG();
	DisplaySMG_Bit(7, SMG_duanma[ba]);
	Delay_one_ms_SMG();
 SelectHC138(6);
	P0 = 0XFF;
	SelectHC138(7);
	P0 = 0XFF;
}


void Timer0Init(void)		//1毫秒@11.0592MHz
{
    
    
	AUXR |= 0x80;		//定时器时钟1T模式
	TMOD &= 0xF0;		//设置定时器模式
	TL0 = 0xCD;		//设置定时初值
	TH0 = 0xD4;		//设置定时初值
	TF0 = 0;		//清除TF0标志
	TR0 = 1;		//定时器0开始计时
}

void Timer0Service() interrupt 1
{
    
    
		TL0 = 0xCD;		//设置定时初值
  TH0 = 0xD4;		//设置定时初值

}

void delayms(int b)
{
    
    
	int i,a;
	for(i=b;i>0;i--)
	for(a=110;a>0;a--);
}


void keycan16()
{
    
    
	int temp;
	//第一列
	P3=0x7f;P44=0;P42=1;
	temp=P3;
	temp=temp&0x0f;
	if(temp!=0x0f)
	{
    
    
		delayms(5);
		temp=P3;
		temp=temp&0x0f;
		if(temp!=0x0f)
		{
    
    temp=P3;
			switch(temp)
			{
    
    
			  case 0x7b: 
      if(Two_win==0 && S4_st==1)
      {
    
    
       S5_st++;
       if(S5_st==3)
       {
    
    
        S5_st=0;
       }
      }
     ;break;//S5
				case 0x77: 
     if(Two_win==0)
      {
    
    
       S4_st++;
       if(S4_st==3)
       {
    
    
        S4_st=0;
       }
      }
    ;break;//S4	
			}
			while(temp!=0x0f)
			{
    
    
				temp=P3;
				temp=temp&0x0f;		
			}	
		}
	}
 P3=0xbf;P44=1;P42=0;
	temp=P3;
	temp=temp&0x0f;
	if(temp!=0x0f)
	{
    
    
		delayms(5);
		temp=P3;
		temp=temp&0x0f;
		if(temp!=0x0f)
		{
    
    temp=P3;
			switch(temp)
			{
    
    
			  case 0xbb: 
     if(Two_win==0 && S4_st==2)
     {
    
    
      wen_can -= 1;
     }
     ;break;//s9
				case 0xb7: 
     if(Two_win==0 && S4_st==2)
     {
    
    
      wen_can += 1; 
     }
    ;break;//s8
			}
			while(temp!=0x0f)
			{
    
    
				temp=P3;
				temp=temp&0x0f;		
			}	
		}
	}

}



Supongo que te gusta

Origin blog.csdn.net/m0_52592798/article/details/130027258
Recomendado
Clasificación