不提供题目 以下为我的代码 功能全部实现
仅供大家参考,写的比较冗长,没有优化。如果有可以改进的点,希望大佬能留言帮助我及时改进
#include <stc15f2k60s2.h>
#include <intrins.h>
sbit DQ = P1^4;
code unsigned char number[] =
{
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xbf,0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10
};//+17后为带.字
unsigned int ui_tep;
unsigned char uc_low = 24,uc_high = 27,uc_mod,uc_out,uc_count = 0,uc_ledtime = 100;
void smg();
bit flag,led1;
unsigned char flag1;
unsigned char weizhi[5] = {0,0,0,0,0};
void smg_show(unsigned char wei,duan);
void HC138_choose(unsigned char uc_m);
void Timer1Init(void) //10毫秒@11.0592MHz
{
AUXR &= 0xBF; //定时器时钟12T模式
TMOD &= 0x0F; //设置定时器模式
TL1 = 0x00; //设置定时初值
TH1 = 0xDC; //设置定时初值
TF1 = 0; //清除TF1标志
TR1 = 1; //定时器1开始计时
EA = 1;
ET1 = 1;
}
void scan_key() //按键扫描模块
{
P34 = 1;
P35 = 1;
P42 = 1;
P30 = 0;
if(P42 == 0)
{
while(P42 == 0)
{
smg();
}
uc_out = 0;
}
else if(P35 == 0)
{
while(P35 == 0)
{
smg();
}
uc_out = 1;
}
else if(P34 == 0)
{
while(P34 == 0)
{
smg();
}
uc_out = 2;
}
P30 = 1;
P31 = 0;
if(P42 == 0)
{
while(P42 == 0)
{
smg();
}
uc_out = 3;
}
else if(P35 == 0)
{
while(P35 == 0)
{
smg();
}
uc_out = 4;
}
else if(P34 == 0)
{
while(P34 == 0)
{
smg();
}
uc_out = 5;
}
P31 = 1;
P32 = 0;
if(P42 == 0)
{
while(P42 == 0)
{
smg();
}
uc_out = 6;
}
else if(P35 == 0)
{
while(P35 == 0)
{
smg();
}
uc_out = 7;
}
else if(P34 == 0)
{
while(P34 == 0)
{
smg();
}
uc_out = 8;
}
P32 = 1;
P33 = 0;
if(P42 == 0)
{
while(P42 == 0)
{
smg();
}
uc_out = 9;
}
else if(P35 == 0)
{
while(P35 == 0)
{
smg();
}
uc_out = 10;
}
else if(P34 == 0)
{
while(P34 == 0)
{
smg();
uc_out = 11;
}
}
P33 = 1;
P34 = 0;
P35 = 0;
P42 = 0;
}
void key() interrupt 3
{
uc_count++;
if(uc_count >= uc_ledtime)
{
HC138_choose(4);
P0 = 0xff;
P00 = led1;
led1 = ~led1;
uc_count = 0;
HC138_choose(0);
} //以上用来实现LED1闪烁
scan_key();
if(uc_out == 10) //进入设置界面
{
flag = 1;
flag1 = 0;
while(flag)
{
uc_out = 255;
scan_key();
smg_show(1,16);
smg_show(6,16);
if(uc_out != 11 && uc_out != 255 && uc_out != 10 && flag1 != 4)
{
flag1++;
weizhi[flag1] = uc_out;
}
else if(uc_out == 11 && flag1 != 0)
{
flag1--;
if(uc_high <= uc_low)
{
HC138_choose(4);
P0 = 0xff;
}
}
else if(flag1 == 4 && uc_out == 10)
{
uc_high = weizhi[1] * 10 + weizhi[2];
uc_low = weizhi[3] * 10 + weizhi[4];
if(uc_high <= uc_low)
{
HC138_choose(4);
P0 = 0xff;
P02 = 0;
}
else
{
flag = 0;
}
}
switch(flag1) //逐位显示原理
{
case 0:
{
break;
}
case 1:
{
smg_show(2,weizhi[1]);
break;
}
case 2:
{
smg_show(2,weizhi[1]);
smg_show(3,weizhi[2]);
break;
}
case 3:
{
smg_show(2,weizhi[1]);
smg_show(3,weizhi[2]);
smg_show(7,weizhi[3]);
break;
}
default :
{
smg_show(2,weizhi[1]);
smg_show(3,weizhi[2]);
smg_show(7,weizhi[3]);
smg_show(8,weizhi[4]);
break;
}
}
}
uc_out = 255;
}
}
void Delay_OneWire(unsigned int t)
{
unsigned char i;
while(t--){
for(i=0;i<12;i++);
}
}
void Write_DS18B20(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
DQ = 0;
DQ = dat&0x01;
Delay_OneWire(5);
DQ = 1;
dat >>= 1;
}
Delay_OneWire(5);
}
unsigned char Read_DS18B20(void)
{
unsigned char i;
unsigned char dat;
for(i=0;i<8;i++)
{
DQ = 0;
dat >>= 1;
DQ = 1;
if(DQ)
{
dat |= 0x80;
}
Delay_OneWire(5);
}
return dat;
}
bit init_ds18b20(void)
{
bit initflag = 0;
DQ = 1;
Delay_OneWire(12);
DQ = 0;
Delay_OneWire(80);
DQ = 1;
Delay_OneWire(10);
initflag = DQ;
Delay_OneWire(5);
return initflag;
}
void ds18b20()
{
unsigned char low,high;
smg();
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
f1:; //goto跳转点 酌情使用,会打断代码原有流程
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
smg();
low = Read_DS18B20();
high = Read_DS18B20();
smg();
ui_tep = high;
ui_tep = ui_tep << 8 ;
ui_tep = ui_tep | low;
if(ui_tep < 0xfa00) //正负判断 由于用不到负数 就不写负数算法了
{
ui_tep = ui_tep * 0.0625;
}
if(ui_tep == 85 || ui_tep == 0)
{
goto f1; //防止初始值显示错误 我的板子初始值是85
}
}
void Delay300us() //@11.0592MHz
{
unsigned char i, j;
_nop_();
_nop_();
i = 4;
j = 54;
do
{
while (--j);
} while (--i);
}
void HC138_choose(unsigned char uc_m)
{
switch(uc_m)
{
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 smg_show(unsigned char wei,duan)
{
HC138_choose(6);
P0 = 0x01 <<(wei - 1);
HC138_choose(7);
P0 = number[duan];
Delay300us();Delay300us();
P0 = 0xff;
}
void smg()
{
if(flag == 1) //当进入设置时候的显示 flag1为进入设置标志符号
{
smg_show(1,16);
smg_show(6,16);
switch(flag1)
{
case 0:
{
break;
}
case 1:
{
smg_show(2,weizhi[1]);
break;
}
case 2:
{
smg_show(2,weizhi[1]);
smg_show(3,weizhi[2]);
break;
}
case 3:
{
smg_show(2,weizhi[1]);
smg_show(3,weizhi[2]);
smg_show(7,weizhi[3]);
break;
}
default :
{
smg_show(2,weizhi[1]);
smg_show(3,weizhi[2]);
smg_show(7,weizhi[3]);
smg_show(8,weizhi[4]);
break;
}
}
}
else //正常显示区域
{
smg_show(1,16);
if(ui_tep < uc_low )
{
uc_mod = 0;
}
else if(ui_tep > uc_high)
{
uc_mod = 2;
}
else
{
uc_mod = 1;
}
smg_show(2,uc_mod);
smg_show(3,16);
smg_show(7,ui_tep / 10 );
smg_show(8,ui_tep % 10 );
}
}
void scan_mod()
{
switch(uc_mod)
{
case 0:
{
uc_ledtime = 80;
HC138_choose(5);
P0 = 0x00;
break;
}
case 1:
{
uc_ledtime = 40;
HC138_choose(5);
P0 = 0x10;
break;
}
case 2:
{
uc_ledtime = 20;
HC138_choose(5);
P0 = 0x10;
break;
}
}
}
void init()
{
HC138_choose(6);
P0 = 0x00;
HC138_choose(5);
P0 = 0x00;
HC138_choose(4);
P0 = 0xff;
}
void main()
{
init();
Timer1Init();
while(1)
{
ds18b20();
smg();
scan_mod();
}
}
以上,有问题可以留言,我会尽可能帮助,一起学习进步。