
forum.eepw.com.cn/thread/204901/1
我用2.4TFT编写了个界面,可以有5个演示功能,其中包括,万年历,温度显示仪(曲线实时显示),弹球小游戏。

DS18B20测温数码管显示程序:
#include<reg52.h>
#include<math.h>
#include<INTRINS.H>
#define uchar unsigned char
#define uint unsigned int;
/******************************************************************/
/* 定义端口 */
/******************************************************************/
sbit seg1=P2^0;
sbit seg2=P2^1;
sbit seg3=P2^2;
sbit DQ=P1^3;//ds18b20 端口
sfr dataled=0x80;//显示数据端口
/******************************************************************/
/* 全局变量 */
/******************************************************************/
uint temp;
uchar flag_get,count,num,minute,second;
uchar code tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
//7段数码管段码表共阳
uchar str[6];
/******************************************************************/
/* 函数声明 */
/******************************************************************/
void delay1(uchar MS);
unsigned int ReadTemperature(void);
void Init_DS18B20(void);
unsigned char ReadOneChar(void);
void WriteOneChar(unsigned char dat);
void delay(unsigned int i);
/******************************************************************/
/* 主函数 */
/******************************************************************/
main()
{
unsigned char TempH,TempL;
TMOD|=0x01;//定时器设置
TH0=0xef;
TL0=0xf0;
IE=0x82;
TR0=1;
P2=0x00;
count=0;
while(1)
{
str[5]=0x39; //显示C符号
str[1]=tab[TempH/100]; //十位温度
str[2]=tab[(TempH%100)/10]; //十位温度
str[3]=tab[(TempH%100)%10]|0x80; //个位温度,带小数点
str[4]=tab[TempL];
if(flag_get==1) //定时读取当前温度
{
temp=ReadTemperature();
if(temp&0x8000)
{
str[0]=0x40;//负号标志
temp=~temp; // 取反加1
temp +=1;
}
else
str[0]=0;
TempH=temp>>4;
TempL=temp&0x0F;
TempL=TempL*6/10;//小数近似处理
flag_get=0;
}
}
}
/******************************************************************/
/* 定时器中断 */
/******************************************************************/
void tim(void) interrupt 1 using 1//中断,用于数码管扫描和温度检测间隔
{
TH0=0xef;//定时器重装值
TL0=0xf0;
num++;
if (num==50)
{num=0;
flag_get=1;//标志位有效
second++;
if(second>=60)
{second=0;
minute++;
}
}
count++;
if(count==1)
{P2=0;
dataled=str[0];}//数码管扫描
if(count==2)
{P2=1;
dataled=str[1];}
if(count==3)
{ P2=2;
dataled=str[2];
}
if(count==4)
{ P2=3;
dataled=str[3];
}
if(count==5)
{ P2=4;
dataled=str[4];
}
if(count==6)
{ P2=5;
dataled=str[5];
count=0;}
}
/******************************************************************/
/* 延时函数 */
/******************************************************************/
void delay(unsigned int i)//延时函数
{
while(i--);
}
/******************************************************************/
/* 初始化 */
/******************************************************************/
void Init_DS18B20(void)
{
unsigned char x=0;
DQ = 1; //DQ复位
delay(8); //稍做延时
DQ = 0; //单片机将DQ拉低
delay(80); //精确延时 大于 480us
DQ = 1; //拉高总线
delay(10);
x=DQ; //稍做延时后 如果x=0则初始化成功 x=1则初始化失败
delay(5);
}
/******************************************************************/
/* 读一个字节 */
/******************************************************************/
unsigned char ReadOneChar(void)
{
unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--)
{
DQ = 0; // 给脉冲信号
dat>>=1;
DQ = 1; // 给脉冲信号
if(DQ)
dat|=0x80;
delay(5);
}
return(dat);
}
/******************************************************************/
/* 写一个字节 */
/******************************************************************/
void WriteOneChar(unsigned char dat)
{
unsigned char i=0;
for (i=8; i>0; i--)
{
DQ = 0;
DQ = dat&0x01;
delay(5);
DQ = 1;
dat>>=1;
}
delay(5);
}
/******************************************************************/
/* 读取温度 */
/******************************************************************/
unsigned int ReadTemperature(void)
{
unsigned char a=0;
unsigned int b=0;
unsigned int t=0;
Init_DS18B20();
WriteOneChar(0xCC); // 跳过读序号列号的操作
WriteOneChar(0x44); // 启动温度转换
delay(200);
Init_DS18B20();
WriteOneChar(0xCC); //跳过读序号列号的操作
WriteOneChar(0xBE); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度
a=ReadOneChar(); //低位
b=ReadOneChar(); //高位
b<<=8;
t=a+b;
return(t);
}

#include<reg52.h> //包含头文件,一般情况不需要改动,头文件包含特殊功能寄存器的定义
code unsigned char tab[]=
{0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
//共阴数码管 0-9
unsigned char Dis_Shiwei; //定义十位
unsigned char Dis_Gewei; //定义个位
unsigned char S_Dis_Shiwei;//定义秒十位
unsigned char S_Dis_Gewei; //定义秒个位
unsigned char second,msecond;
/******************************************************************/
/* 函数声明 */
/******************************************************************/
void CLR(void);
/******************************************************************/
/* 延时函数 */
/******************************************************************/
void delay(unsigned int cnt)
{
while(--cnt);
}
/******************************************************************/
/* 主函数 */
/******************************************************************/
main()
{
EX0=1;//外部中断0设置
IT0=1;
EX1=1;//外部中断1设置
IT1=1;
TMOD |=0x01;//定时器设置 10ms in 12M crystal
TH0=0xd8;
TL0=0xf0;
ET0=1; //打开中断
TR0=0;
EA=1;
CLR();
while(1)
{
P0=S_Dis_Shiwei;//显示秒十位
P2=1;
delay(300); //短暂延时
P0=S_Dis_Gewei; //显示秒个位
P2=2;
delay(300);
P0=0x40; //显示秒个位
P2=3;
delay(300);
P0=Dis_Shiwei; //显示十位
P2=4;
delay(300); //短暂延时
P0=Dis_Gewei; //显示个位
P2=5;
delay(300);
}
}
/******************************************************************/
/* 定时器中断函数 */
/******************************************************************/
void tim(void) interrupt 1 using 1
{
TH0=0xd8;//重新赋值
TL0=0xf0;
msecond++;
if (msecond==100)
{
msecond=0;
second++;//秒加1
if(second==100)
second=0;
S_Dis_Shiwei=tab[second/10];//十位显示值处理
S_Dis_Gewei=tab[second%10]; //个位显示处理
}
Dis_Shiwei=tab[msecond/10];//十位显示值处理
Dis_Gewei=tab[msecond%10]; //个位显示处理
}
/******************************************************************/
/* 外部中断函数 */
/******************************************************************/
void ISR_INT0(void) interrupt 0 using 1
{
TR0=!TR0; //利用外部中断打开和关闭定时器0 用于开始和停止计时
}
void ISR_INT1(void) interrupt 2 using 1
{
if(TR0==0)//停止时才可以清零
CLR();
}
void CLR(void)
{
second=0; //利用外部中断清零
msecond=0;
Dis_Shiwei=tab[msecond/10];//十位显示值处理
Dis_Gewei=tab[msecond%10]; //个位显示处理
S_Dis_Shiwei=tab[second/10];//十位显示值处理
S_Dis_Gewei=tab[second%10]; //个位显示处理
}
回复
打赏帖 | |
---|---|
汽车电子中巡航控制系统的使用被打赏10分 | |
分享汽车电子中巡航控制系统知识被打赏10分 | |
分享安全气囊系统的检修注意事项被打赏10分 | |
分享电子控制安全气囊计算机知识点被打赏10分 | |
【分享开发笔记,赚取电动螺丝刀】【OZONE】使用方法总结被打赏20分 | |
【分享开发笔记,赚取电动螺丝刀】【S32K314】芯片启动流程分析被打赏40分 | |
【分享开发笔记,赚取电动螺丝刀】【S32K146】S32DS RTD 驱动环境搭建被打赏12分 | |
【分享开发笔记,赚取电动螺丝刀】【IAR】libc标注库time相关库函数使用被打赏23分 | |
LP‑MSPM0L1306开发版试用结果被打赏10分 | |
【分享开发笔记,赚取电动螺丝刀】【LP-MSPM0L1306】适配 RT-Thread Nano被打赏23分 |