0% found this document useful (0 votes)
58 views

"Global.h" "Avr/io.h"

This document contains header and source code for a temperature sensor library. It defines macros and functions for initializing ports and pins, communicating with a DS18B20 digital temperature sensor over a 1-Wire interface, reading temperature data, and displaying results on an LCD. The library supports resetting the sensor, reading/writing bits and bytes to the sensor, matching/skipping ROM IDs, reading temperature raw values and in Celsius format, and example programs to read sensor IDs and display temperature continuously.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

"Global.h" "Avr/io.h"

This document contains header and source code for a temperature sensor library. It defines macros and functions for initializing ports and pins, communicating with a DS18B20 digital temperature sensor over a 1-Wire interface, reading temperature data, and displaying results on an LCD. The library supports resetting the sensor, reading/writing bits and bytes to the sensor, matching/skipping ROM IDs, reading temperature raw values and in Celsius format, and example programs to read sensor IDs and display temperature continuously.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

/*

* temp_sensor.h
*
* Created: 9/20/2017 1:32:18 PM
* Author: uidn2752
*/
#include "Global.h"
#include "avr/io.h"
#include <util/delay.h>
#include <stdio.h>
#include <stdlib.h>
#include "HD44780.h"

#ifndef TEMP_SENSOR_H_
#define TEMP_SENSOR_H_

#define THERM_PORT PORTD


#define THERM_DDR DDRD
#define THERM_PIN PIND
#define THERM_IO 0

#define LCD_E 1
#define LCD_RS 0
#define DAT4 2
#define DAT5 3
#define DAT6 4
#define DAT7 5

#define CLEARDISPLAY 0x01


#define SETCURSOR 0x80

#define LED 5
#define ClearBit(x,y) (x&=~(1<<(y)))
#define SetBit(x,y) (x|=1<<(y))
#define ReadBit(x,y) (x&=(1<<(y)))

#define THERM_INPUT() ClearBit(THERM_DDR,THERM_IO) // make pin an input


#define THERM_OUTPUT() SetBit(THERM_DDR,THERM_IO) // make pin an output
#define THERM_LOW() ClearBit(THERM_PORT,THERM_IO) // take (output) pin low
#define THERM_HIGH() SetBit(THERM_PORT,THERM_IO) // take (output) pin high
#define THERM_READ() ReadBit(THERM_PIN,THERM_IO) // get (input) pin value

#define THERM_CONVERTTEMP 0x44


#define THERM_READSCRATCH 0xBE
#define THERM_WRITESCRATCH 0x4E
#define THERM_COPYSCRATCH 0x48
#define THERM_READPOWER 0xB4
#define THERM_SEARCHROM 0xF0
#define THERM_READROM 0x33
#define THERM_MATCHROM 0x55
#define THERM_SKIPROM 0xCC
#define THERM_ALARMSEARCH 0xEC

extern uint8 therm_Reset();


extern void therm_WriteBit(uint8 bit);
extern uint8 therm_ReadBit();
extern void InitAVR();
extern void msDelay(int delay);
extern void FlashLED();
extern void therm_WriteByte(uint8 data);
extern uint8 therm_ReadByte();
extern void therm_MatchRom(uint8 rom[]);
extern void therm_SkipRom(void);
extern void therm_ReadTempRaw(uint8 *t0,uint8 *t1);
extern void therm_ReadTempC(int*whole, int*decimal);
extern void SendNibble(uint8 data);
extern void SendByte(uint8 data);
extern void PulseEnableLine();
extern void RomReaderProgram();
extern void LCD_HexByte(uint8 data);
extern void LCD_Char(uint8 ch);
extern void HexDigit(uint8 data);
extern void LCD_Integer(int data);

#endif /* TEMP_SENSOR_H_ */

/*
* temp_sensor.c
*
* Created: 9/20/2017 1:31:59 PM
* Author: uidn2752
*/
#include "temp_sensor.h"

uint8 therm_Reset()
{
uint8 i;
THERM_OUTPUT(); // set pin as output
THERM_LOW(); // pull pin low for 480uS
_delay_us(480);
THERM_INPUT(); //set pin as input
_delay_us(60); // wait for 60uS
i=THERM_READ(); // get pin value
_delay_us(420); // wait for rest of 480uS period
return i;
}

void therm_WriteBit(uint8 bit)


{
THERM_OUTPUT(); // set pin as output
THERM_LOW(); // pull pin low for 1uS
_delay_us(1);
if (bit)
THERM_INPUT(); // to write 1, float pin
_delay_us(60);
THERM_INPUT(); // wait 60uS & release pin
}

uint8 therm_ReadBit()
{
uint8 bit=0;
THERM_OUTPUT(); // set pin as output
THERM_LOW(); // pull pin low for 1uS
_delay_us(1);
THERM_INPUT(); // release pin& wait 14 uS
_delay_us(14);
if(THERM_READ())
bit=1; // read pin value
_delay_us(45); //wait rest of 60uS period
return bit;
}

void therm_WriteByte(uint8 data)


{
uint8 i=8;
while(i--) // for 8 bits:
{
therm_WriteBit(data&1); // send least significant bit
data>>=1; // shift all bits right
}
}

uint8 therm_ReadByte()
{
uint8 i=8, data=0;
while(i--) // for 8 bits:
{
data>>=1; // shift all bits right
data|=(therm_ReadBit()<<7); // get next bit (LSB first)
}
return data;
}

void therm_MatchRom(uint8 rom[])


{
therm_WriteByte(THERM_MATCHROM);
for(uint8 i=0; i<8; i++)
therm_WriteByte(rom[i]);
}
void therm_SkipRom(void)
{
therm_WriteByte(THERM_SKIPROM);
}

void therm_ReadTempRaw(/*uint8 id[],*/uint8 *t0,uint8 *t1) // Returns the two


temperature bytes from the scratchpad
{
therm_Reset();
// skip ROM & start temp conversion
//if(id)
//therm_MatchRom(id);
//else
therm_WriteByte(THERM_SKIPROM);
therm_WriteByte(THERM_CONVERTTEMP);
while(!therm_ReadBit());

// wait until conversion completed


therm_Reset();
// read first two bytes from scratchpad
//if(id)
//therm_MatchRom(id);
//else
therm_WriteByte(THERM_SKIPROM);
therm_WriteByte(THERM_READSCRATCH);

*t0=therm_ReadByte();
// first byte
*t1=therm_ReadByte();
// second byte
}

void therm_ReadTempC(/*uint8 id[],*/ int*whole, int*decimal) // returns temperature in


Celsius as WW.DDDD, where W=whole & D=decimal
{
uint8 t0,t1;
therm_ReadTempRaw(/*id,*/&t0,&t1); // get
temperature values
*whole=(t1&0x07)<<4; //
grab lower 3 bits of t1
*whole|=t0>>4;
// and upper 4 bits of t0
*decimal=t0&0x0F;
// decimals in lower 4 bits of t0
*decimal*=625;
// conversion factor for 12-bit resolution
}

void InitAVR()
{
DDRC=0x3F; // 0011.1111; set B0-B5 as outputs
DDRD=0x00; // 0000.0000; set PORTC as inputs
}

void msDelay(int delay) // put into a routine


{ // to remove code inlining
for(int i=0; i<delay; i++) // at cost of timing accuracy
_delay_ms(1);
}

void FlashLED()
{
SetBit(PORTB,LED);
msDelay(250);
ClearBit(PORTB,LED);
msDelay(250);
}

// the following arrays specify the addresses of *my* ds18b20 devices


//uint8 rom0[]={0x28,0xE1,0x21,0xA3,0x02,0x00,0x00,0x5B};
//uint8 rom1[]={0x28,0x1B,0x21,0x30,0x05,0x00,0x00,0xF5};

void PulseEnableLine()
{
SetBit(PORTB,LCD_E); // take LCD enable line high
_delay_us(40); // wait 40 microseconds
ClearBit(PORTB,LCD_E); // take LCD enable line low
}

void SendNibble(uint8 data)


{
PORTB&=0xC3; // 1100.0011 = clear 4 data lines
if (data&(1<<4))
SetBit(PORTB,DAT4);
if (data&(1<<5));
SetBit(PORTB,DAT5);
if (data&(1<<6));
SetBit(PORTB,DAT6);
if(data&(1<<7));
SetBit(PORTB,DAT7);
PulseEnableLine(); // clock 4 bits into controller
}

void SendByte(uint8 data)


{
SendNibble(data); // send upper 4 bits
SendNibble(data<<4); // send lower 4 bits
ClearBit(PORTB,5); // turn off LED
}

void LCD_Char(uint8 ch)


{
SetBit(PORTB,LCD_RS); // R/S line 1 = character data
SendByte(ch); // send it
}

void HexDigit(uint8 data) // lower 4 bits of input -> output hex digit
// helper function for other LCD
hex routines
{
if (data<10)
data+='0';
else
data+='A'-10;
LCD_Char(data);
}

void LCD_HexByte(uint8 data) // displays a two-character hexadecimal value of data @


current LCD cursor
{
HexDigit(data>>4);
HexDigit(data&0x0F);
}

void LCD_Integer(int data) // displays the integer value of DATA at current LCD
cursor position
{
char st[8]=""; // save enough space for result
itoa(data,st,10); // convert to ascii
LCD_WriteText(st); // display in on LCD
}

void RomReaderProgram() // Read the ID of the attached Dallas 18B20


device
// Note: only ONE device should
be on the bus.
{
LCD_WriteText("ID (ROM) Reader:");
while(1)
{
LCD_GoTo(0, 1); // write 64-bit ROM code on first LCD
line
therm_Reset();
therm_WriteByte(THERM_READROM);
for(uint8 i=0; i<8; i++)
{
uint8 data=therm_ReadByte();
LCD_HexByte(data);
}
msDelay(1000); // do a read every second
}
}

void TempProgram()
{
while(1)
{
int whole=0, decimal=0;
LCD_GoTo(0,1);
LCD_WriteText("Temp1 = ");
therm_ReadTempC(&whole,&decimal);
LCD_Integer(whole);
LCD_Char('.');
//LCD_PadInteger(decimal,4,'0');
_delay_ms(1000);
FlashLED();
}
}

You might also like