这是一个基于蓝桥杯赛事的模板做的温度传感器,因为蓝桥杯的赛事性,以及网上的过多资源是需要大量资源去购买的,在这里,我也想记录自己学习的过程,和大家一起探讨在蓝桥杯学习过后的收获。因为弄了半天不知道怎么上传附件,所以程序就直接粘贴复制了。
- #include <STC15F2K60S2.H>
- #include <intrins.h>
- typedef unsigned int u16;
- typedef unsigned char u8;
- sbit key=P1^4;
- u8 code tab[]={0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90,0XBF,0XFF};
- u16 yi,er,san,si,wu,liu,qi,ba,jiu;
- void Delay100us()
- {
- unsigned char i, j;
- _nop_();
- _nop_();
- i = 2;
- j = 15;
- do
- {
- while (--j);
- } while (--i);
- }
- void Delay500us()
- {
- unsigned char i, j;
- _nop_();
- _nop_();
- i = 6;
- j = 93;
- do
- {
- while (--j);
- } while (--i);
- }
- void delayms(int ms)
- {
- int i,j;
- for(i=ms;i>0;i--)
- for(j=845;j>0;j--);
- }
- void chushihua()
- {
- key=0;
- Delay500us();
- key=1;
- Delay500us();
- }
- void write(u8 dat)
- {
- u16 i;
- for(i=0;i<8;i++)
- {
- key=0;
- key=dat&0x01;
- Delay100us();
- key=1;
- dat=dat>>1;
- }
- }
- u16 read()
- {
- u16 i;
- u16 dat;
- for (i=0;i<8;i++)
- {
- key=0;
- _nop_();
- dat=dat>>1;
- key=1;
- if(key==1)
- {
- dat=dat|0x80;
- }
-
- Delay100us();
- }
- return dat;
- }
- u16 tempget()
- {
- u16 temp;
- u16 low,height;
-
- chushihua();
- write(0xcc);
- write(0x44);
- Delay500us();
- Delay500us();/*yidian*/
-
- chushihua();
- write(0xcc);
- write(0xbe);
-
- low=read();
- height=read();
-
- temp=height<<4;
- temp=temp|(low>>4);
-
- return temp;
- }
- void allinit()
- {
- P2=0XA0;
- P0=0X00;
-
- P2=0X80;
- P0=0XFF;
-
- P2=0XC0;
- P0=0XFF;
- P2=0XFF;
- P0=0XFF;
- }
- void desplay1 (u16 yi,u16 er)
- {
- P2=0XC0;
- P0=0X01;
- P2=0XFF;
- P0=tab[yi];
- delayms(1);
-
- P2=0XC0;
- P0=0X02;
- P2=0XFF;
- P0=tab[er];
- delayms(1);
- }
- void desplay2 (u16 san,u16 si)
- {
- P2=0XC0;
- P0=0X04;
- P2=0XFF;
- P0=tab[san];
- delayms(1);
-
- P2=0XC0;
- P0=0X08;
- P2=0XFF;
- P0=tab[si];
- delayms(1);
- }
- void desplay3 (u16 wu,u16 liu)
- {
- P2=0XC0;
- P0=0X10;
- P2=0XFF;
- P0=tab[wu];
- delayms(1);
-
- P2=0XC0;
- P0=0X20;
- P2=0XFF;
- P0=tab[liu];
- delayms(1);
- }
- void desplay4 (u16 qi,u16 ba)
- {
- P2=0XC0;
- P0=0X40;
- P2=0XFF;
- P0=tab[qi];
- delayms(1);
-
- P2=0XC0;
- P0=0X80;
- P2=0XFF;
- P0=tab[ba];
- delayms(1);
- }
- void main()
- {
- u16 wendu;
- allinit();
- yi=11;
- er=11;
- san=11;
- si=11;
- wu=11;
- liu=0;
- qi=0;
- ba=0;
- while(1)
- {
- wendu=tempget();
-
- liu=wendu/100;
- qi=wendu%100/10;
- ba=wendu%10;
-
- desplay1(yi,er);
- desplay2(san,si);
- desplay3(wu,liu);
- desplay4(qi,ba);
- }
-
- }
复制代码
|