历史上的今天
今天是:2024年10月12日(星期六)
2019年10月12日 | 分享:LCD1602温度检测显示程序(ATmega16)
2019-10-12 来源:eefocus
单片机源程序如下:
//**********************************************************************//
//************************* 头文件定义 **********************//
//**********************************************************************//
#include
#include
//宏定义
#define uchar unsigned char
#define uint unsigned int
//温度18b20(数据线端口)
#define tmp (PINB&BIT(PB3))
#define temp_h PORTB |= BIT(PB3)
#define temp_l PORTB &=~BIT(PB3)
#define temp_o DDRB |= BIT(PB3)
#define temp_i DDRB &=~BIT(PB3)
//LCD1602液晶显示(数据线端口)
#define rs_h PORTB |= BIT(PB0)//数据/命令选择
#define rs_l PORTB &=~BIT(PB0)
#define rw_h PORTB |= BIT(PB1)//读/写选择
#define rw_l PORTB &=~BIT(PB1)
#define en_h PORTB |= BIT(PB2)//使能信号
#define en_l PORTB &=~BIT(PB2)
//温度18b20(变量定义)
unsigned char dat1=0x00;//保存读出的温度 L
unsigned char dat2=0x00;//保存读出的温度 H
unsigned long int dat=0;//保存读出的温度 XS
unsigned char flag=0;//错误标志位
//按键定义
unsigned char key1=0;
unsigned char key2=0;
//unsigned char key3=0;
//unsigned char key4=0;
//返回值变量
unsigned char keyvalue=0;
//温度H
unsigned char tempH=30;
//温度L
unsigned char tempL=20;
//**********************************************************************//
//************************* IO 端口定义 **********************//
//**********************************************************************//
void IO_init(void)
{
DDRA = 0XFF;
DDRB = 0XF0;
DDRC = 0XFF;
DDRD = 0XFF;
PORTA = 0X00;
PORTB = 0X00;
PORTC = 0XFF;
PORTD = 0XFF;
}
//**********************************************************************//
//************************* 延时函数 **********************//
//**********************************************************************//
void delayms(uint z) //8M晶振下,延时1ms
{
uint x,y;
for(x=z;x>0;x--)
for(y=1333;y>0;y--);
}
//**********************************************************************//
//*************************** 18B20 ************************//
//**********************************************************************//
void Ds18b20_reset(void)//DS18B20初始化
{
uint count;
temp_o;
temp_l;
for(count=700;count>0;count--);//延时480us
temp_h;
temp_i;//不须配置PORT内部上拉电阻,MCU输入输出自动切换
while((tmp==0x08));//&&(i>0)) i--;
for(count=700;count>0;count--);//延时480us
}
void Ds18b20_write(uchar dat)//向DS18B20写一个字节
{
uchar count;
uchar i;
temp_o;
for(i=8;i>0;i--)
{
temp_l;
for(count=2;count>0;count--);
//temp_h;//不能有此语句
if(dat&0x01==0x01)
temp_h;
else
temp_l;
for(count=120;count>0;count--);//延时60us
temp_h;
dat>>=1;
}
}
uchar Ds18b20_read(void)//从DS18B20读一个字节
{
uchar i,datt;
uchar count;
for(i=8;i>0;i--)
{
datt>>=1;
temp_o;
temp_l;
for(count=2;count>0;count--);
temp_h;//此语句必须有,参考datasheet的P15
for(count=1;count>0;count--);
temp_i;
if(tmp==0x08)
datt|=0x80;
for(count=120;count>0;count--); //延时60us
}
return datt;
}
void temp_Read(void)//温度读取
{
Ds18b20_reset();//DS18B20初始化
Ds18b20_write(0xcc);//跳过ROM
Ds18b20_write(0x44);//发送温度转换命令
delayms(1000);//延时1s,等待温度转换完成
Ds18b20_reset();//DS18B20初始化
Ds18b20_write(0xcc);//跳过ROM
Ds18b20_write(0xbe);//发送读温度寄存器命令
dat1=Ds18b20_read();//读温度值的低字节
dat2=Ds18b20_read();//读温度值的高字节
}
void temp_display(void)//温度显示
{
if(dat2>=240)//dat2温度值的高字节为1时为负温度
{
dat=(~(dat2*256+dat1)+1)*0.625;//负温度:取反加一,保留一位小数
flag=1;
}
else
{
dat=(dat2*256+dat1)*0.625;
flag=0;
}
if(flag==1)//负温度显示
{
LCD_write_str(0,0," 18B20 ");
LCD_write_str(3,1,"Temp:");
LCD_write_str(8,1,"-");// 符号“- ”
LCD_write_char(9,1,0x30+dat/1000);
LCD_write_char(10,1,0x30+dat%1000/100);
LCD_write_char(11,1,0x30+dat%100/10);
LCD_write_str (12,1,".");// 符号“. ”
LCD_write_char(13,1,0x30+dat%10);
}
if(flag==0)//正温度显示
{
LCD_write_str(0,0," 18B20 ");
LCD_write_str(3,1,"Temp:");
LCD_write_str(8,1," ");// 符号“+ ”
LCD_write_char(9,1,0x30+dat/1000);
LCD_write_char(10,1,0x30+dat%1000/100);
LCD_write_char(11,1,0x30+dat%100/10);
LCD_write_str (12,1,".");// 符号“. ”
LCD_write_char(13,1,0x30+dat%10);
}
}
void tempH_Setting(void)//最高温度设置显示
{
LCD_write_str(0,0," temp(H)Setting ");
LCD_write_char(6,1,0x30+tempH%1000/100);
LCD_write_char(7,1,0x30+tempH%100/10);
LCD_write_char(8,1,0x30+tempH%10);
}
void tempL_Setting(void)//最低温度设置显示
{
LCD_write_str(0,0," temp(L)Setting ");
LCD_write_char(6,1,0x30+tempL%1000/100);
LCD_write_char(7,1,0x30+tempL%100/10);
LCD_write_char(8,1,0x30+tempL%10);
}
void temp_police(void)//温度报警
{
if(dat/10>=tempH)//最高检测温度>=设定温度:灯亮
{
PORTC&=~BIT(7);
史海拾趣
|
大家好,本人今年要参加全国电子设计竞赛,各位大哥哥大姐姐谁有相关的板子卖给小弟!不胜感激!FPGA板要求:板子要精炼,不需要太多花俏的东西例如数码管什么的,只要把线全部引出了即可,支持JTAG下载!芯片最好clylone!! 不是也没关系! ...… 查看全部问答> |
|
Spartan-6 and Virtex-6 FPGA Embedded Kit FAQ 1. Where can I purchase an Embedded kit? 2. How much do the Spartan-6 and Virtex-6 FPGA Embedded Kits cost? 3. When will I get my kit? 4. What are included in the Embedded Kits? 5. What expansion ports are avail ...… 查看全部问答> |
|
是BPB还是FAT或FDT的损坏? 除了硬件损坏,文件系统突然断电会有什么样的写入错误造成存贮设备不可用吗? 一般的文件系统如果是先写数据,再记录FAT和FDT会提高保护的可能性吗。… 查看全部问答> |
|
利用6517A对惰性气体或高真空中的小型晶体高值电阻测量的典型误差来源 屏蔽与防护 为了降低静电干扰,利用具有地电位的导体包围样本和线缆(屏蔽)。 为了防止漏电流影响测量,利用具有同样电势的导体对至静电计[1]输入端的连接进行包围(防护)。测量电流时,需要将防护装置连接至电压源的LO端。 在高真空系统( ...… 查看全部问答> |
|
最近在看一份富士通半导体的产品选型文件中出现的名词ECO不解,在百度上找到的结果是:Engineering Change Order。还有的说是在与环保相关的概念和资料中,ECO 是ECOLOGICAL的缩写,用来表示生态环保的意思。小弟就是想知道ECO到底是个什么技术?还 ...… 查看全部问答> |




