蓝桥杯必备代码(省赛) 国赛)
(部分代码参考蜜蜂老师)
省赛
PCF8591-光敏:
void read_rd1() { IIC_Start(); IIC_SendByte(0x90); IIC_WaitAck(); IIC_SendByte(0x01); IIC_WaitAck(); IIC_Start(); IIC_SendByte(0x91); IIC_WaitAck(); rb1=IIC_RecByte(); IIC_SendAck(1); IIC_Stop(); }
PCF8591-可调电阻
void read_rb3() { IIC_Start(); IIC_SendByte(0x90); IIC_WaitAck(); IIC_SendByte(0x03); IIC_WaitAck(); IIC_Start(); IIC_SendByte(0x91); IIC_WaitAck(); rb2=IIC_RecByte(); IIC_SendAck(1); IIC_Stop(); }
DS18B20温度传感器:
float ds18b20_read(void) { unsigned char tml,tmh; init_ds18b20(); // Delay_OneWire(1); Write_DS18B20(0xcc); Write_DS18B20(0x44); init_ds18b20(); // Delay_OneWire(1); Write_DS18B20(0xcc); Write_DS18B20(0xbe); tml=Read_DS18B20(); tmh=Read_DS18B20(); temp=(tmh<<8|tml)*0.625; temp_r=(unsigned int)temp; return temp; }
DS1302实时时钟:
unsigned char code write[7]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c}; unsigned char code read[7]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d}; unsigned char time[4]={0x55,0x23,0x23}/秒,分,日,月,周,年 void ds1302write() { unsigned char i; Write_Ds1302_Byte(0x8e,0x00); for(i=0;i<7;i ) { Write_Ds1302_Byte(write[i],time[i]); } Write_Ds1302_Byte(0x8e,0x80); } void ds1302read() { unsigned char i; for(i=0;i<7;i ) { time[i]=Read_Ds1302_Byte(read[i]); } }
24C02 外部储存器
void write_e2prom(unsigned char pom,unsigned char dat) { IIC_Start(); IIC_SendByte(0xa0); IIC_WaitAck(); IIC_SendByte(pom); IIC_WaitAck(); IIC_SendByte(dat); IIC_WaitAck(); IIC_Stop(); } unsigned char read_e2prom(unsigned char pom) { unsigned char dat; IIC_Start(); IIC_SendByte(0xa0); IIC_WaitAck(); IIC_SendByte(pom); IIC_WaitAck(); IIC_Start(); IIC_SendByte(0xa1); IIC_WaitAck(); dat=IIC_RecByte(); IIC_SendAck(1); IIC_Stop(); return dat; }
PCF8591-DAC输出:
void out_dac(unsigned char dat) { IIC_Start(); IIC_SendByte(0x90); IIC_WaitAck(); IIC SendByte(0x40); IIC_WaitAck(); IIC_SendByte(dat); IIC_WaitAck(); IIC_Stop(); }
NE555测频率:
void Init_Timer() { TH0 = 0xff; TL0 = 0xff; TH1 = (65536 - 50000) / 256; TL1 = (65536 - 50000) % 256; TMOD = 0x16; ///定时器1用法1,定时器0用法2,计数 ET0 = 1; ET1 = 1; EA = 1; TR0 = 1; TR1 = 1; } void Service_T0() interrupt 1 { count_f ; } void Service_T1() interrupt 3 { TH1 = (65536 - 50000) / 256; TL1 = (65536 - 50000) % 256; count_t ; if(count_t == 20) //1s { dat_f = count_f; count_f = 0; count_t = 0; } }
国赛
串口
void Rsevies_Uart() interrupt 4 //接收 { if(RI == 1) { Rsevies_dat=SBUF; RI = 0; } } void Send_Uart(unsigned char Send_dat) //发送 { SBUF = Send_dat; while(TI == 0); TI = 0; } void Send_string(unsigned char *str) { while(*str != '\0') { Send_Uart(*str ); } }
超声波测距(使用显示时需要显示)flag=0)
sbit TX=P1^0; sbit RX=P1^1; unsigned char flag; //使能参数 unsigned int distance ; //距离 void Delay14us() //@12.000MHz 14us { unsigned char i; _nop_(); _nop_(); i = 39; while (--i); } void inte() //定时器1初始化 { TMOD &= 0x0f; TH1=0X00; TL1=0X00; } void send_wave() ///产生8个脉冲信号 { u8 i; for(i=0;i<7;i ) { TX=1; Delay14us(); TX=0; Delay14us(); } } void measure() { u16 time; send_wave(); TR1=1; while((RX==1)&&(TF1==0)) { flag=1; SMG(); } flag=0; TR1=0; if(TF1==0) { time=TH1; time=(time<<8)|TL1; distance = ((time/10)*17)/100; //距离 } else { TF1=0; distance= 999; ///测距跑飞标志 } TH1=0X00; TL1=0X00; }