资讯详情

第十三届蓝桥杯单片机设计与开发模拟试题

# include <reg52.h> # include <iic.h>  //DA每次读数据都是最后一次 所以读两遍!  /*************  本地常量声明    **************/ unsigned char code display_Nodot[32]={                       ///标准字库 //   0    1    2    3    4    5    6    7    8    9    A    B    C    D    E    F     0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71, //black  -     H    J    K    L    N    o   P    U     t    G    Q    r   M    y     0x00,0x40,0x76,0x1E,0x70,0x38,0x37,0x5C,0x73,0x3E,0x78,0x3d,0x67,0x50,0x37,0x6e,}; unsigned char code display_dot[11] = {0xBF,0x86,0xDB,0xCF,0xE6,0xED,0xFD,0x87,0xFF,0xEF,0x46};                                         //0. 1. 2. 3. 4. 5. 6. 7. 8. 9. -1 /*************  本地常量声明    **************/ sbit S4 = P3^3; sbit S5 = P3^2; sbit S6 = P3^1; sbit S7 = P3^0; sbit LED1 = P0^0; sbit LED2 = P0^1; sbit LED3 = P0^2; sbit LED4 = P0^3; sbit LED5 = P0^4; unsigned char S4_Mode = 0; ///切换界面 unsigned int cnt_f = 0; //记录频率值 unsigned int dat_f = 2305; //数码管上显示的频率值 unsigned int dat_T = 0; ////数字管上显示的周期值 单位微秒 unsigned char v_01 = 0; //记录光敏电阻 0-255 unsigned int dat_v_01 = 0; ////数字管上显示光敏电阻的电压值 0-500 unsigned char v_03 = 0; //记录滑动变阻器 0-255 unsigned int dat_v_03 = 0; //数码管上显示滑动变阻器的电压值 0-500 bit U_set = 0; //0为通道1 1为通道3 unsigned int dat_v_03_count = 0; //S按下时收集的通道3电压值 unsigned int dat_f_count = 0; //S按下时采集频率值 bit led_set = 0; //0为LED正常工作 1为不工作  void SelectHC138(unsigned char n) {  switch(n)  {   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 Delay1ms()  //@12.000MHz {  unsigned char i, j;   i = 12;  j = 169;  do  {   while (--j);  } while (--i); }  void Delay1us()  //@12.000MHz {  _nop_();  _nop_();  _nop_();  _nop_(); }  void Init_Timer() //定时器0用于计数 定时器1用于定时 {  TH0 = 0xff; ///定时器0采用8位自动重装 计数  TL0 = 0xff;    TH1 = (65535 - 10000) / 256; //定时10ms 16位手动重装  TL1 = (65535 - 10000) % 256;  TMOD = 0x16; //0001 0110    TR0 = 1;  TR1 = 1;  EA = 1;  ET0 = 1;  ET1 = 1; }  void Service_Timer0() interrupt 1 {  cnt_f  ; }  unsigned int cnt_1s = 0; unsigned int cnt_1s_S7 = 0; void Service_Timer1() interrupt 3 {  TH1 = (65535 - 10000) / 256; //定时10ms 16位手动重装  TL1 = (65535 - 10000) % 256;  if(S7 == 0)  {   Delay1us();   if(S7 == 0)   {    cnt_1s_S7  ;   }  }  if((S7 == 1) & (cnt_1s_S7 >=100))  {   led_set = ~led_set;   cnt_1s_S7 = 0;  }  cnt_1s  ;  if(cnt_1s >= 100)  {   cnt_1s = 0;   dat_f = cnt_f;   cnt_f = 0;  }   }  void Display_SMG(unsigned char pos,unsigned char dat) {  SelectHC138(6);  P0 = 0xff;  SelectHC138(7);  P0 = 0xff;  SelectHC138(6);  P0 = 0x01<<pos; ///从最左端数字管开始显示  SelectHC138(7);  P0 = dat; }  void Close_ALL_SMG() {  SelectHC138(6);  P0 = 0xff;  SelectHC138(7);  P0 = 0xff; }  void PCF8591_AD_01() {  IIC_Start();  IIC_SendByte(0x90);  IIC_WaitAck();  IIC_SendByte(0x41);  IIC_WaitAck();    IIC_Start();  IIC_SendByte(0x91);  IIC_WaitAck();  v_01 = IIC_RecByte();  IIC_SendAck(1);//不回应  IIC_Stop(); }  void PCF8591_AD_03() {  IIC_Start();  IIC_SendByte(0x90);  IIC_WaitAck();  IIC_SendByte(0x43);  IIC_WaitAck();    IIC_Start();  IIC_SendByte(0x91);  IIC_WaitAck();  v_03 = IIC_RecByte();  IIC_SendAck(1);//不回应  IIC_Stop(); }  void Display_f() {  Display_SMG(0,0x8e);  Delay1ms();  if(dat_f > 999999)  {   Display_SMG(1,~display_Nodot[dat_f/1000000]);   Delay1ms();  }  if(dat_f > 99999)  {   Display_SMG(2,~display_Nodot[(dat_f/100000)]);   Delay1ms();  }  if(dat_f > 9999)  {   Display_SMG(3,~display_Nodot[(dat_f/10000)]);   Delay1ms();  }  if(dat_f > 999)  {   Display_SMG(4,~display_Nodot[(dat_f/1000)]);   Delay1ms();  }  if(dat_f > 99)  {   Display_SMG(5,~display_Nodot[(dat_f/100)]);   Delay1ms();  }  if(dat_f > 9)  {   Display_SMG(6,~display_Nodot[(dat_f/10)]);   Delay1ms();  }  Display_SMG(7,~display_Nodot[dat_f]);  Delay1ms();    Close_ALL_SMG(); }   void Display_T() {  dat_T = 1000000/dat_f;  Display_SMG(0,0xc8);  Delay1ms();  if(dat_T > 999999)  {   Display_SMG(1,~display_Nodot[dat_T/1000000]);   Delay1ms();  }  if(dat_T > 99999)  {   Display_SMG(2,~display_Nodot[(dat_T/100000)]);   Delay1ms();  }  if(dat_T > 9999)  {   Display_SMG(3,~display_Nodot[(dat_T/10000)]);   Delay1ms();  }  if(dat_T > 999)  {   Display_SMG(4,~display_Nodot[(dat_T/1000)]);   Delay1ms();  }  if(dat_T > 99)  {   Display_SMG(5,~display_Nodot[(dat_T/100)]);   Delay1ms();  }  if(dat_T > 9)  {   Display_SMG(6,~display_Nodot[(dat_T/10)]);   Delay1ms();  }  Display_SMG(7,~display_Nodot[dat_T]);  Delay1ms();    Close_ALL_SMG(); }  void Display_U() {  if(U_set == 0) ///光敏电阻  {   PCF8591_AD_01();   PCF8591_AD_01();   dat_v_01 = v_01*1.961;   Display_SMG(0,0xc1);  Delay1ms();
		Display_SMG(1,~display_Nodot[17]); //分隔符
		Delay1ms();
		Display_SMG(2,~display_Nodot[1]);
		Delay1ms();
		Display_SMG(5,~display_dot[dat_v_01/100]);
		Delay1ms();
		Display_SMG(6,~display_Nodot[(dat_v_01/10)%10]);
		Delay1ms();
		Display_SMG(7,~display_Nodot[dat_v_01%10]);
		Delay1ms();
		
		Close_ALL_SMG();
	}
	
	else if(U_set == 1) //滑动变阻器
	{
		PCF8591_AD_03();
		PCF8591_AD_03();
		dat_v_03 = v_03*1.961;
		Display_SMG(0,0xc1);
		Delay1ms();
		Display_SMG(1,~display_Nodot[17]); //分隔符
		Delay1ms();
		Display_SMG(2,~display_Nodot[3]);
		Delay1ms();
		Display_SMG(5,~display_dot[dat_v_03/100]);
		Delay1ms();
		Display_SMG(6,~display_Nodot[(dat_v_03/10)%10]);
		Delay1ms();
		Display_SMG(7,~display_Nodot[dat_v_03%10]);
		Delay1ms();
		
		Close_ALL_SMG();
	}
}

void Display()//总数码管显示函数
{
	switch(S4_Mode)
	{
		case 0:
			Display_f();
		break;
		case 1:
			Display_T();
		break;
		case 2:
			Display_U();
		break;
	}
}

void Scan_key()
{
	if(S5 == 0)
	{
		Delay1ms();
		if(S5 == 0)
		{
			while(S5 == 0)
			{
				Display();
			}
			U_set = ~U_set;
		}
	}
	if(S4 == 0)
	{
		Delay1ms();
		if(S4 == 0) //S4按下
		{
			U_set = 0;
			while(S4 == 0)
			{
				Display();
			}
			if(S4_Mode == 0)
			{
				S4_Mode = 1;
			}
			else if(S4_Mode == 1)
			{
				S4_Mode = 2;
			}
			else if(S4_Mode == 2)
			{
				S4_Mode = 0;
			}
		}
	}
	if(S6 == 0)
	{
		Delay1ms();
		if(S6 == 0)
		{
			while(S6 == 0)
			{
				Display();
			}
			PCF8591_AD_03();
			PCF8591_AD_03();
			dat_v_03_count = v_03 * 1.961; //S6按下采集3通道电压值
		}
	}
	if(S7 == 0)
	{
		Delay1ms();
		if(S7 == 0)
		{
			while(S7 == 0)
			{
				Display();
			}
			dat_f_count = dat_f;
		}
	}
}

void Led_Mode()
{
	SelectHC138(4);
	if(led_set == 0)
	{
		PCF8591_AD_03();
		PCF8591_AD_03();
		dat_v_03 = v_03 *1.961;
		if(dat_v_03 > dat_v_03_count) //通道3实际电压大于缓存电压
		{
			LED1 = 0;
		}
		if(dat_v_03 <= dat_v_03_count) //通道3实际电压小于缓存电压
		{
			LED1 = 1;
		}
		if(dat_f >dat_f_count) //实际频率大于缓存频率
		{
			LED2 = 0;
		}
		if(dat_f <= dat_f_count) //实际频率大于缓存频率
		{
			LED2 = 1;
		}
		if(S4_Mode == 0)
		{
			LED3 = 0;
		}
		if(S4_Mode == 1)
		{
			LED4 = 0;
		}
		if(S4_Mode == 2)
		{
			LED5 = 0;
		}
	}
	if(led_set == 1)
	{
		P0 = 0xff;
	}
}

void Init_System()
{
	SelectHC138(4);
	P0 = 0xff;
	SelectHC138(5);
	P0 = 0x00;
	SelectHC138(0);
}

void main()
{
	Init_System();
	Init_Timer();
	while(1)
	{
		Display();
		Scan_key();
		Led_Mode();
	}
}

标签: 继电器3e47d计数继电器1e计数继电器

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台