蓝桥杯单片机组编程题准备 -模块编程练习

蓝桥杯单片机组编程题准备 -模块编程练习

回到目录

以下代码复制即可运行。

实验一:实验功能要求:点亮L1、L3、L5、L7;熄灭L2、L4、L6、L8。

#include "reg52.h"

#define uint unsigned int
#define uchar unsigned char
uchar code Duan[21] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x90,0x88,0x83,0xA7,0xA1,0x86,0x8E,0xbf,0xff,0xbf,0x7f}; // ºóÁ©Î»ÊÇ-ºÍ ¡£
uchar code CHAR[] = {0x90,0x88,0xA7,}; // "   ACEFHLPU"

void delayus(uint z)
{
	while(--z);
}

void delay(uint z)
{
	while(--z)
	{
		delayus(245);
		delayus(245);
	}

}
void init()
{
		P2 = 0xa0;P0 = 0x00; // 外设
		P2 = 0x80;P0 = 0xff; // led
		P2 = 0xc0; P0 = 0xff;
		P2 = 0xe0;P0 = 0xff;
}

void main()
{
	init();	
	P2 = 0x80;
	P0 = 0xfe;
	P0 = 0xaa;
	while(1);
}

实验二:实验功能要求:数码管最后两位显示数字88888810,使用按键S7。短按键数字加1,长按键数字清零,清零后用最后两位显示88888800。(短按键:按键时间<300ms;长按键:按键时间>600ms)
KBD模式

下面代码没有通过

#include "reg52.h"

#define uint unsigned int
#define uchar unsigned char
uchar code Duan[21] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x90,0x88,0x83,0xA7,0xA1,0x86,0x8E,0xbf,0xff,0xbf,0x7f}; // ºóÁ©Î»ÊÇ-ºÍ ¡£
uchar ledbuf[8]={0,0,0,0,0,0,0,0};
uchar code CHAR[] = {0x90,0x88,0xA7,}; // "   ACEFHLPU"


sbit s7 = P3^0;


uchar T0RH = 0;
uchar T0RL = 0;

uint ms = 0; // Åжϰ´¼üʱ¼ä
uchar Init_Numbers = 10;
void delayus(uint z)
{
	while(--z);
}

void delay(uint z)
{
	while(--z)
	{
		delayus(245);
		delayus(245);
	}

}
void init()
{
		P2 = 0xa0;P0 = 0x00; // ÍâÉè
		P2 = 0x80;P0 = 0xff; // led
		P2 = 0xc0; P0 = 0xff;
		P2 = 0xe0;P0 = 0xff;
}


void ConfigTimer0(uint ms)
{
	unsigned long tmp = 0;
	
	tmp = 12000000 / 12;  // ¶¨Ê±Æ÷¼¼ÊõƵÂÊ
	tmp = (tmp * ms) / 1000;   // ¶¨Ê±Æ÷ÖØÔØÖµ
	tmp = tmp + 12;  // ÖжÏѹջ²¹³¥
	T0RH = (uchar)(tmp >> 8);
	T0RL = (uchar)tmp; // ½«ÖØÔØÖµ²ð·ÖΪ¸ßµÍ×Ö½Ú
	TMOD &= 0xF0;  // ÇåÁãT0¿ØÖÆλ
	TMOD |= 0x01;  // ÅäÖÃT0Ϊģʽ1
	TH0 = T0RH;
	TL0 = T0RL;
	ET0 = 1;
	TR0 = 1;




}


void display()
{
	static uchar i=0;
	P0 = 0xff;
	switch(i)
	{
		case 0: P2 = 0xc0;P0 = 0x01;P2 = 0x00;P0 = 0xff;P2 = 0xe0;P0 = Duan[ledbuf[0]];i++;break;
		case 1: P2 = 0xc0;P0 = 0x02;P2 = 0x00;P0 = 0xff;P2 = 0xe0;P0 = Duan[ledbuf[1]];i++;break;
		case 2:P2 = 0xc0;P0 = 0x04;P2 = 0x00;P0 = 0xff;P2 =  0xe0;P0 = Duan[ledbuf[2]];i++;break;
		case 3:P2 = 0xc0;P0 = 0x08;P2 = 0x00;P0 = 0xff;P2 =  0xe0;P0 = Duan[ledbuf[3]];i++;break;
		case 4:P2 = 0xc0;P0 = 0x10;P2 = 0x00;P0 = 0xff;P2 =  0xe0;P0 = Duan[ledbuf[4]];i++;break;
		case 5:P2 = 0xc0;P0 = 0x20;P2 = 0x00;P0 = 0xff;P2 =  0xe0;P0 = Duan[ledbuf[5]];i++;break;
		case 6:P2 = 0xc0;P0 = 0x40;P2 = 0x00;P0 = 0xff;P2 =  0xe0;P0 = Duan[ledbuf[6]];i++;break;
		case 7:P2 = 0xc0;P0 = 0x80;P2 = 0x00;P0 = 0xff;P2 =  0xe0;P0 = Duan[ledbuf[7]];i=0;break;
		default:break;
	}
}


void S7_scan() // ¼ì²âS7
{
	if(s7 ==0)
	{
		delay(10);
		if(s7 ==0)
		{
			
			while(!s7);
			if(ms < 300)
				Init_Numbers++;
			if(ms ==600)
				Init_Numbers = 0;
			ms = 0; // ÇåÁã
		}
	}


}

void main()
{

	init();	
	EA = 1;
	ConfigTimer0(1);

	
	while(1)
	{
			ledbuf[6] = Init_Numbers /10;
			ledbuf[7] = Init_Numbers %10;
		S7_scan();
	}
}



void InterruptTimer0(void) interrupt 1
{
	if(ms <600 && s7==0)
		ms++;
	
	TH0 = (65536- 1000)/256;
	TL0 = (65536- 1000)%256;
	
	display();
}

实验三:实验功能要求:向串口发送“LAN QIAO”,串口返回“GOOD” ;向串口发送“GXCT”,串口返回“NICE”,字母都为大写。
波特率9600,无检验位,停止位为1.

实验四:实验功能要求:超声波测距功能,要求至少实现20-50cm内的距离测量,距离数据显示在数码管最后两位,数据保留整数,例如:88888825。

实验五:实验功能要求:向串口发送10-99任意数字,数码管最后两位显示该数字。例如发送"20",数码管显示"88888820"。
波特率9600,无检验位,停止位为1.
串口的发送方式选择字符串

#include "STC15F2K60S2.H"


#define FOSC 12000000L
#define uint unsigned int
#define uchar unsigned char
uchar code Duan[21] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x90,0x88,0x83,0xA7,0xA1,0x86,0x8E,0xbf,0xff,0xbf,0x7f}; // ºóÁ©Î»ÊÇ-ºÍ ¡£
uchar ledbuf[8]={0,0,0,0,0,0,0,0};
uchar code CHAR[] = {0x90,0x88,0xA7,}; // "   ACEFHLPU"
uchar code Chars[] = "IAP15F2K61S2\r\n";
uchar receive_buf[2];
sbit s7 = P3^3;


uchar T0RH = 0;
uchar T0RL = 0;
uchar flag = 0;
uint ms = 0; // Åжϰ´¼üʱ¼ä




void init()
{
		P2 = 0xa0;P0 = 0x00; // ÍâÉè
		P2 = 0x80;P0 = 0xff; // led
		P2 = 0xc0; P0 = 0xff;
		P2 = 0xe0;P0 = 0xff;
}


void ConfigUart(uint baud)
{
	PCON &=0x7f;
	SCON = 0x50;
	AUXR &= 0xBF;  // AUXR.6  Ϊ0ÔòÊ®¶þ·ÖƵ£¬Îª1Ôò²»·ÖƵ
	AUXR &= 0xFE; // AUXR.0 Ϊ0ÔòΪĬÈ϶¨Ê±Æ÷1×ö´®¿Ú£¬Îª1ÔòΪ¶¨Ê±Æ÷2×ö´®¿Ú
	TMOD &= 0x0f; // Çå³þ¶¨Ê±Æ÷1ģʽλ
	TMOD |= 0x20; // ¶¨Ê±Æ÷1λ8λ×Ô¶¯ÖØ×°
	
	TL1 = 256 - (FOSC / 12 / 32 ) / baud;
	TH1 = TL1;
	ET1 = 0;
	TR1 = 1;
	ES = 1;
}

void ConfigTimer0(uint ms)
{
	unsigned long tmp = 0;
	
	tmp = 12000000 / 12;  // ¶¨Ê±Æ÷¼¼ÊõƵÂÊ
	tmp = (tmp * ms) / 1000;   // ¶¨Ê±Æ÷ÖØÔØÖµ
	tmp = tmp + 12;  // ÖжÏѹջ²¹³¥
	T0RH = (uchar)(tmp >> 8);
	T0RL = (uchar)tmp; // ½«ÖØÔØÖµ²ð·ÖΪ¸ßµÍ×Ö½Ú
	//TMOD &= 0xF0;  // ÇåÁãT0¿ØÖÆλ
	TMOD |= 0x01;  // ÅäÖÃT0Ϊģʽ1
	TH0 = T0RH;
	TL0 = T0RL;
	ET0 = 1;
	TR0 = 1;




}


void display()
{
	static uchar i=0;
	P0 = 0xff;
	switch(i)
	{
		case 0: P2 = 0xc0;P0 = 0x01;P2 = 0x00;P0 = 0xff;P2 = 0xe0;P0 = Duan[ledbuf[0]];i++;break;
		case 1: P2 = 0xc0;P0 = 0x02;P2 = 0x00;P0 = 0xff;P2 = 0xe0;P0 = Duan[ledbuf[1]];i++;break;
		case 2:P2 = 0xc0;P0 = 0x04;P2 = 0x00;P0 = 0xff;P2 =  0xe0;P0 = Duan[ledbuf[2]];i++;break;
		case 3:P2 = 0xc0;P0 = 0x08;P2 = 0x00;P0 = 0xff;P2 =  0xe0;P0 = Duan[ledbuf[3]];i++;break;
		case 4:P2 = 0xc0;P0 = 0x10;P2 = 0x00;P0 = 0xff;P2 =  0xe0;P0 = Duan[ledbuf[4]];i++;break;
		case 5:P2 = 0xc0;P0 = 0x20;P2 = 0x00;P0 = 0xff;P2 =  0xe0;P0 = Duan[ledbuf[5]];i++;break;
		case 6:P2 = 0xc0;P0 = 0x40;P2 = 0x00;P0 = 0xff;P2 =  0xe0;P0 = Duan[ledbuf[6]];i++;break;
		case 7:P2 = 0xc0;P0 = 0x80;P2 = 0x00;P0 = 0xff;P2 =  0xe0;P0 = Duan[ledbuf[7]];i=0;break;
		default:break;
	}
}




void main()
{
	char i=0;
	init();	
	ConfigTimer0(1);
	ConfigUart(9600);
	EA = 1;
	for(i=0;i<6;i++)
		ledbuf[i] = 0xff;
	while(1)
	{	
		ledbuf[6] = receive_buf[0] - '0';
		ledbuf[7] = receive_buf[1] - '0';
		//´®¿ÚµÄ·¢ËÍ·½Ê½Ñ¡Ôñ×Ö·û´®£¡£¡£¡£¡£¡
		//´®¿ÚµÄ·¢ËÍ·½Ê½Ñ¡Ôñ×Ö·û´®£¡£¡£¡£¡£¡
		//´®¿ÚµÄ·¢ËÍ·½Ê½Ñ¡Ôñ×Ö·û´®£¡£¡£¡£¡£¡
		//´®¿ÚµÄ·¢ËÍ·½Ê½Ñ¡Ôñ×Ö·û´®£¡£¡£¡£¡£¡
	}

	

}



void InterruptTimer0(void) interrupt 1
{

	
	TH0 = (65536- 1000)/256;
	TL0 = (65536- 1000)%256;
	
	display();


}


void UART_Interrupt(void) interrupt 4
{
	static uchar i=0;
	if(RI)
	{
		receive_buf[i] = SBUF;
		i++;
		if(i==2)i=0;
		RI= 0;
	}
	
	if(TI)
	{
		TI = 0;}
		
	}
	

实验六:实验功能要求:通过按键触发串口上报数据功能。使用按键S4,一次按键触发一次串口上报数据“IAP15F2K61S2”
按键KBD模式

#include "STC15F2K60S2.H"


#define FOSC 12000000L
#define uint unsigned int
#define uchar unsigned char
uchar code Duan[21] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x90,0x88,0x83,0xA7,0xA1,0x86,0x8E,0xbf,0xff,0xbf,0x7f}; // 后俩位是-和 。
uchar ledbuf[8]={0,0,0,0,0,0,0,0};
uchar code CHAR[] = {0x90,0x88,0xA7,}; // "   ACEFHLPU"
uchar code Chars[] = "IAP15F2K61S2\r\n";

sbit s7 = P3^3;


uchar T0RH = 0;
uchar T0RL = 0;
uchar flag = 0;
uint ms = 0; // 判断按键时间
uchar Init_Numbers = 10;


void delayus(uint z)
{
    while(--z);
}

void delay(uint z)
{
    while(--z)
    {
        delayus(245);
        delayus(245);
    }

}

void init()
{
		P2 = 0xa0;P0 = 0x00; // 外设
		P2 = 0x80;P0 = 0xff; // led
		P2 = 0xc0; P0 = 0xff;
		P2 = 0xe0;P0 = 0xff;
}


void ConfigUart(uint baud)
{
	PCON &=0x7f;
	SCON = 0x50;
	AUXR &= 0xBF;  // AUXR.6  为0则十二分频,为1则不分频
	AUXR &= 0xFE; // AUXR.0 为0则为默认定时器1做串口,为1则为定时器2做串口
	TMOD &= 0x0f; // 清楚定时器1模式位
	TMOD |= 0x20; // 定时器1位8位自动重装
	
	TL1 = 256 - (FOSC / 12 / 32 ) / baud;
	TH1 = TL1;
	ET1 = 0;
	TR1 = 1;
	ES = 1;
}




void display()
{
	static uchar i=0;
	P0 = 0xff;
	switch(i)
	{
		case 0: P2 = 0xc0;P0 = 0x01;P2 = 0x00;P0 = 0xff;P2 = 0xe0;P0 = Duan[ledbuf[0]];i++;break;
		case 1: P2 = 0xc0;P0 = 0x02;P2 = 0x00;P0 = 0xff;P2 = 0xe0;P0 = Duan[ledbuf[1]];i++;break;
		case 2:P2 = 0xc0;P0 = 0x04;P2 = 0x00;P0 = 0xff;P2 =  0xe0;P0 = Duan[ledbuf[2]];i++;break;
		case 3:P2 = 0xc0;P0 = 0x08;P2 = 0x00;P0 = 0xff;P2 =  0xe0;P0 = Duan[ledbuf[3]];i++;break;
		case 4:P2 = 0xc0;P0 = 0x10;P2 = 0x00;P0 = 0xff;P2 =  0xe0;P0 = Duan[ledbuf[4]];i++;break;
		case 5:P2 = 0xc0;P0 = 0x20;P2 = 0x00;P0 = 0xff;P2 =  0xe0;P0 = Duan[ledbuf[5]];i++;break;
		case 6:P2 = 0xc0;P0 = 0x40;P2 = 0x00;P0 = 0xff;P2 =  0xe0;P0 = Duan[ledbuf[6]];i++;break;
		case 7:P2 = 0xc0;P0 = 0x80;P2 = 0x00;P0 = 0xff;P2 =  0xe0;P0 = Duan[ledbuf[7]];i=0;break;
		default:break;
	}
}


void S7_scan() // 检测S7
{
	if(s7 ==0)
	{
		
		if(s7 ==0)
		{
			flag = 1;
			while(!s7);

		}
	}


}

void main()
{
	char i=0;
	init();	
	ConfigUart(9600);
	EA = 1;
	while(1)
	{	S7_scan();
		if(flag ==1 )
		{
		while(Chars[i] != '\0')
		{
			SBUF = Chars[i];
			while(!TI);
			i++;

		}
		i = 0;
		flag = 0;
		}

	}

	

}



void InterruptTimer0(void) interrupt 1
{
	if(ms <600 && s7==0)
		ms++;
	
	TH0 = (65536- 1000)/256;
	TL0 = (65536- 1000)%256;
	
	display();


}


void UART_Interrupt(void) interrupt 4
{

	
	if(TI)
	{
		TI = 0;}
		
	}
	

实验七:实验功能要求:使用单片机获取PCF8591通道3采集的电压值,数据保留小数点后两位,使用最后3数码管显示,例如:888882.50表示2.50V。电压值可以通过电位器RB2调节,显示范围0-5V.

实验八:实验功能要求:单片机控制PCF8591进行DAC输出,输出电压值2.00V.

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

#define uint unsigned int
#define uchar unsigned char
uchar code Duan[21] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x90,0x88,0x83,0xA7,0xA1,0x86,0x8E,0xbf,0xff,0xbf,0x7f}; // ºóÁ©Î»ÊÇ-ºÍ ¡£
uchar ledbuf[8]={0,0,0,0,0,0,0,0};


uchar T0RH = 0;
uchar T0RL = 0;

uint ms = 0; // Åжϰ´¼üʱ¼ä
uchar Init_Numbers = 20;
void delayus(uint z)
{
	while(--z);
}

void delay(uint z)
{
	while(--z)
	{
		delayus(245);
		delayus(245);
	}

}
void init()
{
		P2 = 0xa0;P0 = 0x00; // ÍâÉè
		P2 = 0x80;P0 = 0xff; // led
		P2 = 0xc0; P0 = 0xff;
		P2 = 0xe0;P0 = 0xff;
}


void ConfigTimer0(uint ms)
{
	unsigned long tmp = 0;
	
	tmp = 12000000 / 12;  // ¶¨Ê±Æ÷¼¼ÊõƵÂÊ
	tmp = (tmp * ms) / 1000;   // ¶¨Ê±Æ÷ÖØÔØÖµ
	tmp = tmp + 12;  // ÖжÏѹջ²¹³¥
	T0RH = (uchar)(tmp >> 8);
	T0RL = (uchar)tmp; // ½«ÖØÔØÖµ²ð·ÖΪ¸ßµÍ×Ö½Ú
	TMOD &= 0xF0;  // ÇåÁãT0¿ØÖÆλ
	TMOD |= 0x01;  // ÅäÖÃT0Ϊģʽ1
	TH0 = T0RH;
	TL0 = T0RL;
	ET0 = 1;
	TR0 = 1;




}


void display()
{
	static uchar i=0;
	P0 = 0xff;
	switch(i)
	{
		case 0: P2 = 0xc0;P0 = 0x01;P2 = 0x00;P0 = 0xff;P2 = 0xe0;P0 = Duan[ledbuf[0]];i++;break;
		case 1: P2 = 0xc0;P0 = 0x02;P2 = 0x00;P0 = 0xff;P2 = 0xe0;P0 = Duan[ledbuf[1]];i++;break;
		case 2:P2 = 0xc0;P0 = 0x04;P2 = 0x00;P0 = 0xff;P2 =  0xe0;P0 = Duan[ledbuf[2]];i++;break;
		case 3:P2 = 0xc0;P0 = 0x08;P2 = 0x00;P0 = 0xff;P2 =  0xe0;P0 = Duan[ledbuf[3]];i++;break;
		case 4:P2 = 0xc0;P0 = 0x10;P2 = 0x00;P0 = 0xff;P2 =  0xe0;P0 = Duan[ledbuf[4]];i++;break;
		case 5:P2 = 0xc0;P0 = 0x20;P2 = 0x00;P0 = 0xff;P2 =  0xe0;P0 = Duan[ledbuf[5]];i++;break;
		case 6:P2 = 0xc0;P0 = 0x40;P2 = 0x00;P0 = 0xff;P2 =  0xe0;P0 = Duan[ledbuf[6]];i++;break;
		case 7:P2 = 0xc0;P0 = 0x80;P2 = 0x00;P0 = 0xff;P2 =  0xe0;P0 = Duan[ledbuf[7]];i=0;break;
		default:break;
	}
}



void Out_DAC(unsigned char Digital)
{
	IIC_Start();
	IIC_SendByte(0x90);
	IIC_WaitAck();
	IIC_SendByte(0x40);
	IIC_WaitAck();
	IIC_SendByte(Digital);
	IIC_WaitAck();
	IIC_Stop();
}

void main()
{
	uchar digital = 20 * 255 / 50;
	init();	
	EA = 1;    // ²»ÏÔʾÔÚÊýÂë¹ÜÉÏ
	ConfigTimer0(1);
	ledbuf[6] = Init_Numbers /10;
	ledbuf[7] = Init_Numbers %10;
	Out_DAC(digital);
	
	while(1)
	{
			

	}
}



void InterruptTimer0(void) interrupt 1
{

	
	TH0 = (65536- 1000)/256;
	TL0 = (65536- 1000)%256;
	
	display();


}

实验九:实验功能要求:定义一个初始值为20,显示到数码管最后两位。定义S4为加1按键,S5为减1按键。定义S8为加2按键,S9为减2按键。通过按键实现相应的加减操作,数码管上显示的数字为相应的结果。

实验十:实验功能要求:使用数码管显示常见大写字母:“ACEFHLPU”.

#include "reg52.h"

#define uint unsigned int
#define uchar unsigned char
uchar code Duan[21] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x90,0x88,0x83,0xA7,0xA1,0x86,0x8E,0xbf,0xff,0xbf,0x7f}; // ºóÁ©Î»ÊÇ-ºÍ ¡£
uchar code CHAR[] = {0x88,0xc6,0x86,0x8e,0x89,0xc7,0x8c,0xc1}; // "   ACEFHLPU"
uchar ledbuf[8]={0,0,0,0,0,0,0,0};

uchar T0RH;
uchar T0RL;

void init()
{
		P2 = 0xa0;P0 = 0x00; // jidianqi
		P2 = 0x80;P0 = 0xff; // led
		P2 = 0xc0; P0 = 0xff;
		P2 = 0xe0;P0 = 0xff;
}

void ConfigTimer0(uint ms)
{
	unsigned long tmp = 0;
	
	tmp = 12000000 / 12;  // ¶¨Ê±Æ÷¼¼ÊõƵÂÊ
	tmp = (tmp * ms) / 1000;   // ¶¨Ê±Æ÷ÖØÔØÖµ
	tmp = tmp + 12;  // ÖжÏѹջ²¹³¥
	T0RH = (uchar)(tmp >> 8);
	T0RL = (uchar)tmp; // ½«ÖØÔØÖµ²ð·ÖΪ¸ßµÍ×Ö½Ú
	//TMOD &= 0xF0;  // ÇåÁãT0¿ØÖÆλ
	TMOD |= 0x01;  // ÅäÖÃT0Ϊģʽ1
	TH0 = T0RH;
	TL0 = T0RL;
	ET0 = 1;
	TR0 = 1;




}
void display()
{
	static uchar i=0;
	P0 = 0xff;
	switch(i)
	{
		case 0: P2 = 0xc0;P0 = 0x01;P2 = 0x00;P0 = 0xff;P2 = 0xe0;P0 = ledbuf[0];i++;break;
		case 1: P2 = 0xc0;P0 = 0x02;P2 = 0x00;P0 = 0xff;P2 = 0xe0;P0 = ledbuf[1];i++;break;
		case 2:P2 = 0xc0;P0 = 0x04;P2 = 0x00;P0 = 0xff;P2 =  0xe0;P0 = ledbuf[2];i++;break;
		case 3:P2 = 0xc0;P0 = 0x08;P2 = 0x00;P0 = 0xff;P2 =  0xe0;P0 = ledbuf[3];i++;break;
		case 4:P2 = 0xc0;P0 = 0x10;P2 = 0x00;P0 = 0xff;P2 =  0xe0;P0 = ledbuf[4];i++;break;
		case 5:P2 = 0xc0;P0 = 0x20;P2 = 0x00;P0 = 0xff;P2 =  0xe0;P0 = ledbuf[5];i++;break;
		case 6:P2 = 0xc0;P0 = 0x40;P2 = 0x00;P0 = 0xff;P2 =  0xe0;P0 = ledbuf[6];i++;break;
		case 7:P2 = 0xc0;P0 = 0x80;P2 = 0x00;P0 = 0xff;P2 =  0xe0;P0 = ledbuf[7];i=0;break;
		default:break;
	}
}
void main()
{
	uchar i = 0;
	init();	

	ConfigTimer0(1);
	EA = 1;
	for(i=0;i<8;i++)
		ledbuf[i] = CHAR[i];
	while(1)
	{};


}


void InterruptTimer0(void) interrupt 1
{

	
	TH0 = (65536- 1000)/256;
	TL0 = (65536- 1000)%256;
	
	display();


}

实验十一:实验功能要求:单片机采集PCF8591通道3电压值,使用最后三位数码管显示,例如:888882.00表示2.00V,数据保留小数点后两位。电压值可以通过电位器RB2调节,显示范围0-5V。当输出Vout<2.50V时,L1亮,其它灯灭;当输出Vout≥2.50V时,L2亮,其它灯灭。

实验十二:实验功能要求:按下按键S7,点亮L1-L8指示灯;按下按键S6,熄灭L1-L8指示灯.
KBD模式

#include "reg52.h"

#define uint unsigned int
#define uchar unsigned char
uchar code Duan[21] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90, 0x90, 0x88, 0x83, 0xA7, 0xA1, 0x86, 0x8E, 0xbf, 0xff, 0xbf, 0x7f}; // 后俩位是-和 。
uchar ledbuf[8] = {0, 0, 0, 0, 0, 0, 0, 0};
uchar code CHAR[] = {0x90, 0x88, 0xA7,}; // "   ACEFHLPU"


sbit s7 = P3 ^ 0;
sbit s6 = P3 ^ 1;

uchar T0RH = 0;
uchar T0RL = 0;

uint ms = 0; // 判断按键时间
uchar Init_Numbers = 20;
void delayus(uint z)
{
    while(--z);
}

void delay(uint z)
{
    while(--z)
    {
        delayus(245);
        delayus(245);
    }

}
void init()
{
    P2 = 0xa0;
    P0 = 0x00; // 外设
    P2 = 0x80;
    P0 = 0xff; // led
    P2 = 0xc0;
    P0 = 0xff;
    P2 = 0xe0;
    P0 = 0xff;
}


void main()
{

    init();
    while(1)
    {
        if(s7 == 0 )
        {
            delay(10);
            if(s7 == 0)
            {
                P2 = 0x80;
                P0 = 0x00;
                while(!s7);


            }
        }
        if(s6 == 0)
        {
            delay(10);
            if(s6 == 0)
            {
                P2 = 0x80;
                P0 = 0xff;
                while(!s6);
            }
        }
    }
}

实验十三:实验功能要求:设置DS1302初始时间为:10-59-50,显示在数码管上。时钟走到整点时,L1点亮持续5秒钟,然后熄灭。
KBD模式

#include "reg52.h"
#include "ds1302.h"
#define uint unsigned int
#define uchar unsigned char
uchar code Duan[21] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90, 0x90, 0x88, 0x83, 0xA7, 0xA1, 0x86, 0x8E, 0xbf, 0xff, 0xbf, 0x7f}; // ºóÁ©Î»ÊÇ-ºÍ ¡£
uchar ledbuf[8] = {0, 0, 0, 0, 0, 0, 0, 0};
uchar time[3] = {10,59,50};
uchar code CHAR[] = {0x90, 0x88, 0xA7,}; // "   ACEFHLPU"
uchar T0RH = 0;
uchar T0RL = 0;


void delayus(uint z)
{
    while(--z);
}

void delay(uint z)
{
    while(--z)
    {
        delayus(245);
        delayus(245);
    }

}

void init()
{
    P2 = 0xa0;
    P0 = 0x00; // ÍâÉè
    P2 = 0x80;
    P0 = 0xff; // led
    P2 = 0xc0;
    P0 = 0xff;
    P2 = 0xe0;
    P0 = 0xff;
}

void display()
{
	static uchar i=0;
	
	P0 = 0xff;
	
	switch(i)
	{
		case 0:P2 = 0xc0;P0 = 0x01;P2 = 0x00;P0 = 0xff;P2 =  0xe0;P0 = Duan[ledbuf[0]];i++;break;
		case 1:P2 = 0xc0;P0 = 0x02;P2 = 0x00;P0 = 0xff;P2 =  0xe0;P0 = Duan[ledbuf[1]];i++;break;
		case 2:P2 = 0xc0;P0 = 0x04;P2 = 0x00;P0 = 0xff;P2 =  0xe0;P0 = Duan[ledbuf[2]];i++;break;
		case 3:P2 = 0xc0;P0 = 0x08;P2 = 0x00;P0 = 0xff;P2 =  0xe0;P0 = Duan[ledbuf[3]];i++;break;
		case 4:P2 = 0xc0;P0 = 0x10;P2 = 0x00;P0 = 0xff;P2 =  0xe0;P0 = Duan[ledbuf[4]];i++;break;
		case 5:P2 = 0xc0;P0 = 0x20;P2 = 0x00;P0 = 0xff;P2 =  0xe0;P0 = Duan[ledbuf[5]];i++;break;
		case 6:P2 = 0xc0;P0 = 0x40;P2 = 0x00;P0 = 0xff;P2 =  0xe0;P0 = Duan[ledbuf[6]];i++;break;
		case 7:P2 = 0xc0;P0 = 0x80;P2 = 0x00;P0 = 0xff;P2 =  0xe0;P0 = Duan[ledbuf[7]];i=0;break;
		default:break;
	}

}




void main()
{

  init();
	
	EA = 1;
	ET0 = 1;
	TR0 = 1;
	
	
	TH0 = (65536 - 1000) /256;
	TL0 = (65536 - 1000) %256;
	
	
	TMOD = 0x01;
	
	
	write_time(time[0],time[1],time[2]);
	
	
	
	while(1)
	{

		ledbuf[0] = time[2] / 16;
		ledbuf[1] = time[2] % 16;
		ledbuf[2] = 19;
		ledbuf[3] = time[1] / 16;
		ledbuf[4] = time[1] % 16;
		ledbuf[5] = 19;
		ledbuf[6] = time[0] / 16;
		ledbuf[7] = time[0] % 16;
	}
}

void t0(void) interrupt 1
{
	static uint i=0;
	static uint tim=0;
	i++;
	if( i == 1000)
	{
		time[2] = Ds1302_Single_Byte_Read(0x85);
		time[1] = Ds1302_Single_Byte_Read(0x83);
		time[0] = Ds1302_Single_Byte_Read(0x81);	
		if(time[1] % 16==0 && tim<6){tim++;P2 = 0x80;P0 =0xfe;P2 = 0x00;}
		else if(time[1] == 0) {P2 = 0x80; P0 = 0xff;P2 = 0x00;}
		else{tim=0;}
		i=0;
	}
	
	TH0 = (65536 - 1000) /256;
	TL0 = (65536 - 1000) %256;
	
	display();

}


实验十四:实验功能要求:按下S4按键,点亮LD8,松开后熄灭
KBD模式

#include "reg52.h"

#define uint unsigned int
#define uchar unsigned char
uchar code Duan[21] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90, 0x90, 0x88, 0x83, 0xA7, 0xA1, 0x86, 0x8E, 0xbf, 0xff, 0xbf, 0x7f}; // 后俩位是-和 。
uchar ledbuf[8] = {0, 0, 0, 0, 0, 0, 0, 0};
uchar code CHAR[] = {0x90, 0x88, 0xA7,}; // "   ACEFHLPU"


sbit s4 = P3 ^ 3;


uchar T0RH = 0;
uchar T0RL = 0;

uint ms = 0; // 判断按键时间
uchar Init_Numbers = 20;
void delayus(uint z)
{
    while(--z);
}

void delay(uint z)
{
    while(--z)
    {
        delayus(245);
        delayus(245);
    }

}
void init()
{
    P2 = 0xa0;
    P0 = 0x00; // 外设
    P2 = 0x80;
    P0 = 0xff; // led
    P2 = 0xc0;
    P0 = 0xff;
    P2 = 0xe0;
    P0 = 0xff;
}


void main()
{

    init();
    while(1)
    {
        if(s4 == 0 )
        {
            delay(10);
            if(s4 == 0)
            {
                while(!s4)
                {
                    P2 = 0x80;
                    P0 = 0x7f;
                }
                P2 = 0x80;
                P0 = 0xff;
            }
        }
    }
}

实验十五:实验功能要求:模拟时钟。上电开始走时,初始化时间为23-59-55。定义按键S4为清零按键,按键按下后,将当前时钟设置重置为00-00-00,开始走时。

#include "reg52.h"
#include "ds1302.h"
#define uint unsigned int
#define uchar unsigned char
uchar code Duan[21] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90, 0x90, 0x88, 0x83, 0xA7, 0xA1, 0x86, 0x8E, 0xbf, 0xff, 0xbf, 0x7f}; // ºóÁ©Î»ÊÇ-ºÍ ¡£
uchar ledbuf[8] = {0, 0, 0, 0, 0, 0, 0, 0};
uchar time[3] = {23, 59, 55};
uchar code CHAR[] = {0x90, 0x88, 0xA7,}; // "   ACEFHLPU"



sbit s4 = P3 ^ 3;

uchar T0RH = 0;
uchar T0RL = 0;


void delayus(uint z)
{
    while(--z);
}

void delay(uint z)
{
    while(--z)
    {
        delayus(245);
        delayus(245);
    }

}

void init()
{
    P2 = 0xa0;
    P0 = 0x00; // ÍâÉè
    P2 = 0x80;
    P0 = 0xff; // led
    P2 = 0xc0;
    P0 = 0xff;
    P2 = 0xe0;
    P0 = 0xff;
}

void display()
{
	static uchar i=0;
	P0 = 0xff; // ÏûÓ°
	switch(i)
	{
	case 0:P2 = 0xc0;P0 = 0x01;P2 = 0x00;P0 = 0xff;P2 =  0xe0;P0 = Duan[ledbuf[0]];i++;break;
	case 1:P2 = 0xc0;P0 = 0x02;P2 = 0x00;P0 = 0xff;P2 =  0xe0;P0 = Duan[ledbuf[1]];i++;break;
	case 2:P2 = 0xc0;P0 = 0x04;P2 = 0x00;P0 = 0xff;P2 =  0xe0;P0 = Duan[ledbuf[2]];i++;break;
	case 3:P2 = 0xc0;P0 = 0x08;P2 = 0x00;P0 = 0xff;P2 =  0xe0;P0 = Duan[ledbuf[3]];i++;break;
	case 4:P2 = 0xc0;P0 = 0x10;P2 = 0x00;P0 = 0xff;P2 =  0xe0;P0 = Duan[ledbuf[4]];i++;break;
	case 5:P2 = 0xc0;P0 = 0x20;P2 = 0x00;P0 = 0xff;P2 =  0xe0;P0 = Duan[ledbuf[5]];i++;break;
	case 6:P2 = 0xc0;P0 = 0x40;P2 = 0x00;P0 = 0xff;P2 =  0xe0;P0 = Duan[ledbuf[6]];i++;break;
	case 7:P2 = 0xc0;P0 = 0x80;P2 = 0x00;P0 = 0xff;P2 =  0xe0;P0 = Duan[ledbuf[7]];i=0;break;
	default:break;
	}
}


void main()
{

    init();
    EA = 1;
    ET0 = 1;
    TR0 = 1;
    TH0 = (65536 - 1000) / 256;
    TL0 = (65536 - 1000) % 256;
    TMOD = 0x01;

    write_time(time[0], time[1], time[2]);
    while(1)
    {
        if(s4 == 0)
        {
            delay(10);
            if(s4 == 0)
            {
                write_time(0, 0, 0);
                time[2] = Ds1302_Single_Byte_Read(0x85);
                time[1] = Ds1302_Single_Byte_Read(0x83);
                time[0] = Ds1302_Single_Byte_Read(0x81);
                while(!s4);
            }
        }

        ledbuf[0] = time[2] / 16;
        ledbuf[1] = time[2] % 16;
        ledbuf[2] = 19;
        ledbuf[3] = time[1] / 16;
        ledbuf[4] = time[1] % 16;
        ledbuf[5] = 19;
        ledbuf[6] = time[0] / 16;
        ledbuf[7] = time[0] % 16;
    }
}

void t0(void) interrupt 1
{
    static uint i = 0;
    static uint tim = 0;
    i++;
    if( i == 1000)
    {
        time[2] = Ds1302_Single_Byte_Read(0x85);
        time[1] = Ds1302_Single_Byte_Read(0x83);
        time[0] = Ds1302_Single_Byte_Read(0x81);
        i = 0;
    }

    TH0 = (65536 - 1000) / 256;
    TL0 = (65536 - 1000) % 256;

    display();

}


实验十六:实验功能要求:PCF8591读取Rb2电位器电压值。当读取电压小于2V时LD1点亮, 剩余指示灯熄灭。当读取电压大于2V时小于3V时,LD2点亮,剩余指示灯熄灭。当读取电压大于3V时,LD3点亮,剩余指示灯熄灭。

总结

1.像整点亮灯实现,为了防止BUG,应该这么写:P2 = 0x80;P0 = 0xfe;P2 = 0x00;要注意最后一行代码P2=0x00,防止bug

回到目录

  • 5
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

瞲_大河弯弯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值