1、蓝桥杯单片机比赛底层驱动自测可用蓝桥杯单片机比赛底层驱动(自测可用)1、 温度uchar read_temp(void)a) uint temp;b) uchar low,high;c) Init_DS18B20();d) Write_DS18B20(0xcc);e) Write_DS18B20(0x44);f) while(!DQ);g) Init_DS18B20();h) Write_DS18B20(0xcc);i) Write_DS18B20(0xbe);j) low=Read_DS18B20();k) high=Read_DS18B20();l) temp=high&0x0f;m) t
2、emp=8;n) temp|=low;o) return (uchar)(temp*0.0625);2、 时钟void settime(uchar *p)a) uchar addr=0x8c,i=7;b) DS1302_write(0x8e,0x00); /关闭写保护c) while(i-)d) e) DS1302_write(addr,*p);f) p+;g) addr-=2;h) i) DS1302_write(0x8e,0x80);void gettime(uchar *p)j) uchar addr=0x8d,i=7;k) while(i-)l) m) *p+=DS1302_read(
3、addr);n) addr-=2;o) void settime(uchar *p) uchar addr=0x80,i; Write_Ds1302(0x8e,0x00); for(i=0;i3;i+) Write_Ds1302(addr,*p+); addr+=2; Write_Ds1302(0x8e,0x80);3、 ADvoid adc_init()a) IIC_Start();b) IIC_SendByte(0x90);c) IIC_WaitAck();d) IIC_SendByte(0x05);e) IIC_WaitAck();f) IIC_Stop();void getadc(un
4、signed char *p)g) unsigned char i;h) IIC_Start();i) IIC_SendByte(0x91);j) IIC_WaitAck();k) for(i=0;i3;i+)l) m) *p+=IIC_RecByte();n) IIC_Ack(1);o) p) *p=IIC_RecByte();q) IIC_Ack(0);r) IIC_Stop();4、 eepromvoid eeprom_write(unsigned char addr,unsigned char p)a) IIC_Start();b) IIC_SendByte(0xa0);c) IIC_
5、WaitAck();d) IIC_SendByte(addr);e) IIC_WaitAck();f) IIC_SendByte(p);g) IIC_WaitAck();h) IIC_Stop();i) Delay10ms();unsigned char eeprom_read(unsigned char addr)j) unsigned char i;k) IIC_Start();l) IIC_SendByte(0xa0);m) IIC_WaitAck();n) IIC_SendByte(addr);o) IIC_WaitAck();p) q) IIC_Start();r) IIC_Send
6、Byte(0xa1);s) IIC_WaitAck();t) i=IIC_RecByte();u) IIC_Ack(0);v) IIC_Stop();w) return i;5、 蜂鸣器void BuzzInit()a) P2=(P2&0x1f)|0xa0);b) P0=0x00;c) P2&=0x1f;6、 显示void Display()a) static uchar dspom=0;b) P2=(P2&0x1f)|0xe0);c) P0=0xff;d) P2=(P2&0x1f)|0xc0);e) P0=1dspom;f) P2=(P2&0x1f)|0xe0);g) P0=tabdspbu
7、fdspom;h) P2&=0x1f;i) if(dspom=7)j) dspom=0;k) elsel) dspom+;7、 按键(0-9、-、不亮、C、H)code tab=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0xff,0xc6,0x89;unsigned char key_scan()a) static uchar keytime=0;b) uchar i;c) P3|=0x0f;d) i=P3&0x0f;e) if(i=0x0f)f) if(keytime2) key_time=0; return 1; else
8、 key_time=0; else key_time+; switch(a) case 0x0e: hang=4; break; case 0x0d: hang=3; break; case 0x0b: hang=2; break; case 0x07: hang=1; break; default:hang=0; keyline1=1; keyline2=1; keyline3=1; keyline4=1; P3&=0xf0; if(!keyline1) lie=1; else if(!keyline2) lie=2; else if(!keyline3) lie=3; else lie=4
9、; keyvalue=lie*4+hang-1; return 2; return 0;9、 串口void sendstr(unsigned char *p) ES=0; while(*p!=0) SBUF=*p+; while(!TI); TI=0; ES=1;void uart_isr() interrupt 4 unsigned char a; static unsigned char i; RI=0; rectabi+=SBUF;/ if(rectab0 = A)&(rectab1 = A)&(rectab2 = A)&(rectab3 = S)&(rectab4 = S)&(rect
10、ab5 = S) if(strcmp(rectab,AAASSS)=0) for(a=0;a6;a+) rectaba=0; i=0;/ ES=0; uartflag=1; 10、 超声波unsigned int sonic_test() unsigned int p; for(p=0;p8;p+) TX=1; Delay10us(); TX=0; Delay10us(); while(!RX); TR1=1; while(RX=1)&(TF1=0); TR1=0; if(TF1=1) TF1=0; return 999; else p=TH1; p=8; p|=TL1; p*=0.017; TH1=0; TL1=0; return p;
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2