AVR M16 Experiment 6 DS18B20 Temperature Measurement

Publisher:快乐球球Latest update time:2015-11-12 Source: eefocusKeywords:AVR Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
/**********************************************************************
* File name: main.c
* Program author: kidcao1987
* Program version: V1.0
* Function description: Install LCD1602 and display the current temperature on it.
* Compiler: WinAVR-20090313
* Chip: ATmega16, external 11.0592MHZ crystal oscillator
* Technical support: http://bbs.cepark.com
**********************************************************************/
#include
#include
#define uchar unsigned char
#define uint unsigned int
#define DQ PORTC&=~(1<#define DQ_ActiveHigh DDRC&=~(1<#define DQ_ActiveLow DDRC|=(1<#define DQ_SetInput DDRC&=~(1<#define DQ_Readback (PINC&0x80)
#define RS PA4
#define RW PA5
#define EN PA6
unsigned char const LedData[]=
{0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xF8,
0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
unsigned char const LedPos[]=
{0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
uchar DisplayNum[]={"0123456789.-C"};
uchar const CharacterChar[]={"Now the Tempera-ture is: "};
uchar Num[4];
uchar TemperatureL,TemperatureH,m;
uint Temperature;
void LCD1602_Initial(void);
void LCD1602_CommandWrite(uchar com);
void LCD1602_ByteWrite(uchar date);
void LCD1602_Display(uchar adr,uchar date);
void LCD1602_BusyCheck(void);
void DS18B20_Reset(void);
void DS18B20_PORTx_Initial(void);
void DS18B20_High_BitWrite(void);
void DS18B20_Low_BitWrite(void);
void DS18B20_ByteWrite(uchar command);
uchar DS18B20_ByteRead(void);
int main(void)
{
DS18B20_PORTx_Initial();
LCD1602_Initial();
while(1)
{
DS18B20_Reset();//复位
DS18B20_ByteWrite(0xcc);//跳过ROM匹配
DS18B20_ByteWrite(0x44);//温度转换
_delay_ms(600);//延时等待温度转换结束,这个很重要!!
DS18B20_Reset();//复位
DS18B20_ByteWrite(0xcc);//跳过ROM匹配
DS18B20_ByteWrite(0xbe);//读取温度
TemperatureL=DS18B20_ByteRead();
TemperatureH=DS18B20_ByteRead();//先低字节后高字节
DS18B20_Reset();//复位打算DS18B20的输出
/*以下是温度数据处理以及显示部分*/
Temperature=TemperatureL*6.25+TemperatureH*1600;
Num[0]=Temperature/1000;
Num[1]=(Temperature%1000/100);
Num[2]=Temperature%100/10;
Num[3]=Temperature%10;
for(m=0;m<16;m++)
{
LCD1602_Display(0x80+m,CharacterChar[m]);
}
for(m=0;m<9;m++)
{
LCD1602_Display(0x80+0x40+m,CharacterChar[16+m]);
}
LCD1602_ByteWrite(DisplayNum[Num[0]]);
LCD1602_ByteWrite(DisplayNum[Num[1]]);
LCD1602_ByteWrite(DisplayNum[10]);
LCD1602_ByteWrite(DisplayNum[Num[2]]);
LCD1602_ByteWrite(DisplayNum[Num[3]]);; 
LCD1602_ByteWrite(0xdf);
LCD1602_ByteWrite(DisplayNum[12]);
}
}[page]
void DS18B20_PORTx_Initial(void)
{
DDRB=0xff;//B口全部输出
PORTB=0xff;//B口全部低电平
DQ;
DQ_ActiveHigh;
}
void DS18B20_Reset(void)
{
DQ_ActiveLow;
_delay_us(600);//延时至少480us
DQ_ActiveHigh;//释放数据线
_delay_us(70);//延时大于60us
while(DQ_Readback);//Detect the presence of signal_delay_us
(500);//Delay to ensure timing integrity
}
void DS18B20_High_BitWrite(void)
{
DQ_ActiveLow;//Pull to low
level_delay_us(2);//Maintain at least 1us
DQ_ActiveHigh;//Release the data line_delay_us
(65);//Wait for enough time for DS18B20 to sample the data line
}
void DS18B20_Low_BitWrite(void)
{
DQ_ActiveLow;//Pull to low
level_delay_us(65);//Wait for enough time for DS18B20 to sample the data line
DQ_ActiveHigh;//Release the data line
}
uchar DS18B20_BitRead(void)
{
DQ_ActiveLow;//Pull to low
level_delay_us(2);//Maintain at least 1us
DQ_ActiveHigh;//Release data line
_delay_us(4);//Delay to allow the host to read the data line in the later stage of 15us
if(DQ_Readback)
return 1;
else
return 0;
}
/*DS18B20 byte write function*/
void DS18B20_ByteWrite(uchar command)
{
uchar n=0;
for(n=0;n<8;n++)
{
if(command&0x01)
DS18B20_High_BitWrite();
else
DS18B20_Low_BitWrite();
command>>=1;
_delay_us(5);
}
}
/*DS18B20 byte read function*/
uchar DS18B20_ByteRead(void)
{
uchar n=0,Readback=0;
for(n=0;n<8;n++)
{
Readback>>=1;
if(DS18B20_BitRead())
Readback|=0x80;
_delay_us(65);
}
return Readback;
}
void LCD1602_Initial(void)
{
PORTA&=~((1<DDRA|=(1<
PORTB=0x00;
DDRB=0xff; 

LCD1602_CommandWrite(0x38);
_delay_ms(15);
LCD1602_CommandWrite(0x38);
_delay_ms(5);
LCD1602_CommandWrite(0x38);
_delay_ms(5);
LCD1602_CommandWrite(0x01);
_delay_ms(1);
LCD1602_CommandWrite(0x38);
_delay_ms(1);
LCD1602_CommandWrite(0x0c);
_delay_ms(1);
LCD1602_CommandWrite(0x06);
}
/*1602写命令字子函数*/
void LCD1602_CommandWrite(uchar com)
{
LCD1602_BusyCheck();
PORTA&=~(1<PORTB=com; //Send data to 8-bit parallel data port
PORTA|=(1<_delay_ms(1); //Delay 5ms, keep EN level
PORTA&=~(1<}
/*1602 write data sub-function*/
void LCD1602_ByteWrite(uchar date)
{
LCD1602_BusyCheck();
PORTA|=(1<PORTB=date; //Send data to 8-bit parallel data port
PORTA|=(1<_delay_ms(1); //Delay 5ms, keep EN level
PORTA&=~(1<}
/*Display a character at a certain position, parameter adr is the address to be written, date is the byte to be written*/
void LCD1602_Display(uchar adr,uchar date)
{
LCD1602_BusyCheck();
LCD1602_CommandWrite(adr) ; LCD1602_BusyCheck
();
LCD1602_ByteWrite(date); 
}
void LCD1602_BusyCheck(void)
{
PORTB=0xff;
DDRB=0x00;
PORTA|=(1<PORTA&=~(1<DOOR|=(1<_delay_us(5);
while(PINB&0x80);
PORTA&=~(1<PORTA&=~(1<PORTB=0xff;
DDRB=0xff;

}

Video address: http://v.youku.com/v_show/id_XMTYxNDkyOTgw.html

Keywords:AVR Reference address:AVR M16 Experiment 6 DS18B20 Temperature Measurement

Previous article:AVR M16 Experiment 7 DS1302 Test
Next article:AVR M16 Experiment 5 Matrix Keyboard

Recommended ReadingLatest update time:2024-11-16 16:02

avr learning 2 AVR assembler
When I first started learning about each CPU, I would first study the corresponding assembly instructions. I felt that this would allow me to quickly experience the hardware principles of the CPU, which would be very helpful for subsequent learning. Many people think otherwise, and feel that assembly is cumbersome and
[Microcontroller]
AVR PCF8591 AD&DA digital tube display
#include "iom16v.h" #include "macros.h" #define uchar unsigned char #define uint unsigned int /******************************************* Various states of data in TWSR, #include "iom16v.h" #include "macros.h" #define uchar unsigned char #define uint unsigned int /******************************************* The vari
[Microcontroller]
Basic hardware circuit design of AVR microcontroller
AVR microcontrollers can run naked, why?   In fact, this sentence was said by me when I was explaining AVR to my juniors in school. It means that AVR microcontrollers can run without any external circuits.   However, in fact, AVR microcontrollers sometimes still need to add some external circuits. Why? Let me tell y
[Microcontroller]
Basic hardware circuit design of AVR microcontroller
How to Design a Solar Cell Controller Using an AVR Microcontroller
With the deepening of energy crisis and environmental pollution, the research and utilization of solar energy has received widespread attention. Solar energy is an inexhaustible renewable energy source for human beings. It is also a clean energy source that does not produce any environmental pollution. In the effectiv
[Microcontroller]
How to Design a Solar Cell Controller Using an AVR Microcontroller
AVR microcontroller drives passive buzzer
Internal 1 M crystal. Timer implementation, periodic signal drives passive buzzer, these experiments are based on the CTC mode of the timer, the frequency signal is generated by hardware. The program implements a single frequency periodic prompt tone. The program uses a single task mode and software dela
[Microcontroller]
AVR's SD card basic reading and writing procedures (Part 2)
SD card read and write subroutine Hardware platform: atmega8L minimum system                     hard SPI (the SD card initialization uses the IO port simulation timing, because it is found in the experiment that a lower rate is required for stable initialization) Software development platform: ICC-AVR version 6.31a
[Microcontroller]
AVR MCU controls RTL8019AS to implement Ethernet interface design
Introduction: This article introduces a method of using AVR microcontroller to realize Ethernet interface circuit, which has few hardware circuits, simple structure and easy use. With the rapid development of the Internet, various household appliances and instruments are gradually becoming networked in order to shar
[Microcontroller]
AVR MCU controls RTL8019AS to implement Ethernet interface design
How to design an SPI serial peripheral interface based on AVR microcontroller?
1. SPI interface of AVR microcontroller The SPI (Serial Peripheral INTERFACE) bus system is a synchronous serial peripheral interface that allows the MCU to communicate and exchange data with various peripheral devices in a serial manner. It is widely used in various industrial control fields. Based on this standard,
[Microcontroller]
How to design an SPI serial peripheral interface based on AVR microcontroller?
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号