赞
踩
师从小蜜蜂,简单易懂,很容易看懂,可以交流提意见。
这道题难度正常,感觉今年的题难度大概就都会这样了,不会再难了
这里的读取eeprom内容,我任务第一次读取的时候,可能会出现随机的内容,所以可以对第一次读取的内容进行个修整,到规范的内容。
-
- #include "reg52.h"
- #include "onewire.h"
- #include "iic.h"
- sfr P4 = 0xC0;
- sbit r1=P3^0;
- sbit r2=P3^1;
- sbit r3=P3^2;
- sbit r4=P3^3;
- sbit c1=P4^4;
- sbit c2=P4^2;
- sbit c3=P3^5;
- sbit c4=P3^4;
- typedef unsigned char u8;
- typedef unsigned int u16;
- u8 code duanma[10]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
- u8 code duanma1[10]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};
- u8 mode=1;
- void display_choose();
- void delay(u8 t)
- {while(t--);}
- void delay1(u8 t)
- {while(t--);display_choose();}
-
- void hc573(u8 n)
- {
- switch(n)
- {
- case 4: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;
- case 0:P2=(P2&0x1f)|0x00;break;
-
- }
- }
- void first()
- {
- hc573(4);
- P0=0xff;
- hc573(5);
- P0=0x00;
- hc573(6);
- P0=0x00;
- hc573(7);
- P0=0xff;
- }
- u16 temp=2000;
- void read_wendu()
- {
- u8 L,M;
- init_ds18b20();
- Write_DS18B20(0xcc);
- Write_DS18B20(0x44);
- delay(1000);
- init_ds18b20();
- Write_DS18B20(0xcc);
- Write_DS18B20(0xbe);
- L=Read_DS18B20();
- M=Read_DS18B20();
- temp=(M<<8)|L;
- if((temp&0xf800)==0x0000)//*
- {
- //temp=(temp>>4)+(L&0x0f)*0.0625;//整型这样做会丢失小数
- temp = (temp>>4) * 100;
- temp = temp + (L & 0x0f) * 6.25;
- }
- //temp=100*temp;
- }
- u16 ain3=0;
- /*
- void pcf_adc()
- {
- IIC_Start();
- IIC_SendByte(0x90);
- IIC_WaitAck();
- IIC_SendByye(0x03);
- IIC_WaitAck();
- IIC_Stop();
-
- IIC_Start();
- IIC_SendByte(0x91);
- IIC_WaitAck();
- ain3=IIC_RecByte();
- IIC_SendAck(1);
- IIC_Stop();
- ain3=ain3*(500/256);
- }*/
- void pcf_dac(u8 dat)
- {
- IIC_Start();
- IIC_SendByte(0x90);
- IIC_WaitAck();
- IIC_SendByte(0x40);
- IIC_WaitAck();
- IIC_SendByte(dat);
- IIC_WaitAck();
- IIC_Stop();
- }
- void display(u8 pos,u8 dat)
- {
- hc573(7);///xiaoyin
- P0=0xff;///
- hc573(6);
- P0=0x01<<pos;
- hc573(7);
- P0=dat;
-
- }
- u8 display_stat=1;
- void displayoff()
- {
-
- hc573(6);
- P0=0x00;
- hc573(7);
- P0=0xff;
-
- }
- void display_1()
- {
- P0=0xff;
- display(7,duanma[temp%10]);
- delay(500);
- display(6,duanma[(temp%100)/10]);
- delay(500);
- display(5,duanma1[(temp%1000)/100]);
- delay(500);
- display(4,duanma[(temp%10000)/1000]);
- delay(500);
- display(3,0xff);
- delay(500);
- display(2,0xff);
- delay(500);
- display(1,0xff);
- delay(500);
- display(0,0xc6);
- delay(500);
- displayoff();
- }
- u16 set=25;
- void display_2()
- {
- P0=0xff;
- display(7,duanma[set%10]);
- delay(500);
- display(6,duanma[(set%100)/10]);
- delay(500);
- display(5,0xff);
- delay(500);
- display(4,0xff);
- delay(500);
- display(3,0xff);
- delay(500);
- display(2,0xff);
- delay(500);
- display(1,0xff);
- delay(500);
- display(0,0x8e);
- delay(500);
- displayoff();
- }
-
- void display_3()
- {
- P0=0xff;
- display(7,duanma[ain3%10]);
- delay(500);
- display(6,duanma[(ain3%100)/10]);
- delay(500);
- display(5,duanma1[(ain3%1000)/100]);
- delay(500);
- display(4,0xff);
- delay(500);
- display(3,0xff);
- delay(500);
- display(2,0xff);
- delay(500);
- display(1,0xff);
- delay(500);
- display(0,0x88);
- delay(500);
- displayoff();
- }
- u8 led_stat=0xff;
- void led()
- {
- P0=0xff;
- hc573(4);
- if(mode==1){led_stat=led_stat&0xfe;}
- else{led_stat=led_stat|0x01;}
- if(display_stat==1){led_stat=led_stat&0xfd;}
- else{led_stat=led_stat|0x02;}
- if(display_stat==2){led_stat=led_stat&0xfb;}
- else{led_stat=led_stat|0x04;}
- if(display_stat==3){led_stat=led_stat&0xf7;}
- else{led_stat=led_stat|0x08;}
- P0=led_stat;
- hc573(0);
- }
-
- void display_choose()
- {
- if(display_stat==1){display_1();}
- else if(display_stat==2){display_2();}
- else if(display_stat==3){display_3();}
- led();
- }
- void scan()
- {
- r4=0;//s4
- r1=r2=r3=1;
- if(c1==0)
- {
- delay(100);
- if(c1==0)
- {
- display_stat=display_stat+1;
- if(display_stat==4){display_stat=1;}
- }
- while(c1==0)
- {
- display_choose();
- }
- }
- if(c2==0)//s8
- {
- delay(100);
- if(c2==0)
- {if(display_stat==2){
- if(set>=10)
- {set=set-1;}
- else if(set<10)
- {set=0;}
- }
- }
- while(c2==0)
- {
- display_choose();
- }
- }
- r3=0;//s4
- r1=r2=r4=1;
- if(c1==0)//s5
- {
- delay(100);
- if(c1==0)
- {
- mode=~mode;
- }
- while(c1==0)
- {
- display_choose();
- }
- }
- if(c2==0)//s9
- {
- delay(100);
- if(c2==0)
- { if(display_stat==2){
- set=set+1;}
- }
- while(c2==0)
- {display_choose();}
- }
- }
-
- void mode_choose()
- {
- if(mode==1)
- {
- if(temp<(set*100))
- {ain3=0;}
- else{ain3=500;}
- }
- else
- {
- if (temp <= 2000)
- {
- ain3 = 100;
- }
- else if ((temp < 4000) & (temp > 2000))
- {
- ain3 = ((temp - 2000) * (3 / 20) + 100);//整型这样算会丢失小数
- ain3 = ((temp - 2000) * 3) / 20 + 100;
- }
- else{ain3=400;}
- }
- }
- u8 cnt = 0;
- void main()
- {
- first();
- while(1)
- {
- cnt++;
- if (cnt % 5 == 0)
- //pcf_adc();
- {
- read_wendu();
- }
- if (cnt % 4 == 0)
- {
- pcf_dac(ain3 * (255 / 500));
- }
- scan();
- mode_choose();
- display_choose();
-
- }
- }
-
-
-
-
-
-
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。