📄 main.c
字号:
/****************************************Copyright (c)**************************************************
** Guangzou ZLG-MCU Development Co.,LTD.
** graduate school
** http://www.zlgmcu.com
**
**--------------File Info-------------------------------------------------------------------------------
** File name: main.c
** Last modified Date: 2004-09-16
** Last Version: 1.0
** Descriptions: The main() function example template
**
**------------------------------------------------------------------------------------------------------
** Created by: Chenmingji
** Created date: 2004-09-16
** Version: 1.0
** Descriptions: The original version
**
**------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Descriptions:
**
********************************************************************************************************/
#include "config.h"
#include "LCD.h"
#define DQ 1<<19; //DS18b20与ARM的接口
int16 temp=0; //温度
uint8 tflag=0; //温度正负标志
uint8 fuhao[]={"-"}; //显示温度
//延时函数
void Delay15(uint8 Dly) //延时15us的整数倍,不是精确的
{
uint8 i;
while(--Dly)
{
i=78;
while(--i);
}
}
void Delay1us(uint8 n)
{
uint8 i=5;
do
{
while(--i);
}while(--n);
}
//初始化DS18b20,读存在脉冲,无存在脉冲则置位错误标志
void RST18B20(void)
{
IO0CLR=DQ; //复位脉冲
Delay15(60);
IO0SET=DQ; //恢复
Delay15(4);
}
/******************************************************************************
uint8 tmpread(void) //读一字节
{
uint8 i,j,dat=0;
for(i=0;i<8;i++)
{
IO0CLR=DQ;
Delay1us(40);
IO0SET=DQ;
Delay1us(2);
IO0DIR=0<<17;
Delay15(1);
if((IO0PIN&1<<17)!=0)
j=1;
else
j=0;
dat=(j<<7)|(dat>>1); //读出的数据最低位在最前面,这样刚好
Delay15(4);
}
return(dat);
}
********************************************************************************/
uint8 tmpreadbit(void) //读一位
{
uint8 i;
IO0CLR=DQ;
Delay1us(6);
//IO0SET=DQ;
//Delay1us(2);
IO0DIR=0<<19;
Delay1us(10);
if((IO0PIN&1<<19)!=0)
i=1;
else
i=0;
Delay15(7);
return(i);
}
uint8 tmpread(void)
{
uint8 i,j,dat;
dat=0;
for(i=0;i<8;i++)
{
j=tmpreadbit();
dat=(j<<7)|(dat>>1);
//Delay1us(1);
}
return(dat);
}
//写一个字节到DS18B20里
void tmpwritebyte(uint8 dat)
{
uint8 i;
uint8 testb;
IO0DIR=1<<19;
for(i=0;i<8;i++)
{
testb=dat&0x01;
dat=dat>>1;
if(testb) // 写1部分
{
IO0CLR=DQ;
Delay1us(6);
IO0SET=DQ;
Delay15(10);
}
else
{
IO0CLR=DQ; //写0部分
Delay15(10);
IO0SET=DQ;
Delay1us(6);
}
}
}
void tmpchange(void) //发送温度转换命令
{
RST18B20(); //初始化DS18B20
Delay15(35); //延时
tmpwritebyte(0xcc); // 跳过序列号命令
tmpwritebyte(0x44); //发送温度转换命令
}
fp32 tmp() //获得温度
{
fp32 tt;
uint8 a,b;
RST18B20();
Delay15(35);
tmpwritebyte(0xcc);
tmpwritebyte(0xbe); //发送读取数据命令
a=tmpread(); //连续读两个字节数据
b=tmpread();
temp=b;
temp<<=8;
temp=temp|a; //两字节合成一个整型变量。
tt=temp*0.0625; //得到真实十进制温度值
return tt; //返回温度值
}
void ds18b20disp(fp32 tmpe)
{
//float *p;
// p=&l_tmp;
if(tflag==0) //正温度不显示符号
Disp_anynum(0,0,tmpe);
else
{
LCD_Write_char_string(0,0,fuhao);
Disp_anynum(0,1,tmpe);
}
}
int main (void)
{
fp32 tmpr;
PINSEL0=0x00000000;
PINSEL1=0x00000000;
IO0DIR=DQ;
Lcd_init();
// Delay1us(1);
// Write_char(0,0x01);
//Write_char(0,0x08);
Write_char(1,0x80);
RST18B20();
while(1)
{
tmpchange(); //温度转换
tmpr=tmp(); //读取温度值
if(tmpr<0)
tflag=1; //判断温度为负温度,前面加"-"
else
tflag=0;
ds18b20disp(tmpr); //显示温度
Delaynms(5000);
Lcd_init();
Write_char(0,0x01);
// Write_char(0,0x08);
}
return 0;
}
/*********************************************************************************************************
** End Of File
********************************************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -