Hello folks,
I am using DS18B20 with Atmega 32A, I have downloaded a library for atmega 8 @16MHz and modified it for use with atmega 32A @8MHz. The library works fine with atmega 8 but does not work with atmega 32 A. Please help..
This is the code portion where I read and display the temperature on a 16x2 LCD.--
void Measure()
{
while(1)
{
if(status_1)// sensor buffer ready
{
status_1=0; // clear buffer for new data
counts = ReadAndAvg();
rd=(5.0/1024.0)*counts;
sprintf(lcd_buffer, "Counts: %4d",counts);
lcd_gotoxy(0,0);
lcd_puts(lcd_buffer);
}
if(status_2) // buffer full
{
status_2=0; // refresh buffer for new data
//t = Get_Temp();
sprintf(lcd_buffer,"TEMP:");
lcd_gotoxy(0,1);
lcd_puts(lcd_buffer);
//t=(float)(4.8828*read_adc(tmp)/10.0);
t = ds18b20_gettemp();//read
//sprintf(lcd_buffer," %04d Deg",t);
sprintf(lcd_buffer," %03.2f Deg",t);
lcd_gotoxy(5,1);
lcd_puts(lcd_buffer);
}
if(Get_Key_Status(DN))
{
//Scroll Key is pressed
//Check that it was not pressed previously
if(!Get_PrevKey_Status(DN))
{
PREV_KEYS=KEYS;
lcd_clrscr();
break;
}
}
PREV_KEYS=KEYS;
}
}
This is the library :
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include "ds18b20.h"
/*
* ds18b20 init
*/
uint8_t ds18b20_reset() {
uint8_t i;
//low for 480us
DS18B20_PORT &= ~ (1<<DS18B20_DQ); //low
DS18B20_DDR |= (1<<DS18B20_DQ); //output
_delay_us(480);
//release line and wait for 60uS
DS18B20_DDR &= ~(1<<DS18B20_DQ); //input
_delay_us(60);
//get value and wait 420us
i = (DS18B20_PIN & (1<<DS18B20_DQ));
_delay_us(420);
//return the read value, 0=ok, 1=error
return i;
}
/*
* write one bit
*/
void ds18b20_writebit(uint8_t bit){
//low for 1uS
DS18B20_PORT &= ~ (1<<DS18B20_DQ); //low
DS18B20_DDR |= (1<<DS18B20_DQ); //output
_delay_us(1);
//if we want to write 1, release the line (if not will keep low)
if(bit)
DS18B20_DDR &= ~(1<<DS18B20_DQ); //input
//wait 60uS and release the line
_delay_us(60);
DS18B20_DDR &= ~(1<<DS18B20_DQ); //input
}
/*
* read one bit
*/
uint8_t ds18b20_readbit(void){
uint8_t bit=0;
//low for 1uS
DS18B20_PORT &= ~ (1<<DS18B20_DQ); //low
DS18B20_DDR |= (1<<DS18B20_DQ); //output
_delay_us(1);
//release line and wait for 14uS
DS18B20_DDR &= ~(1<<DS18B20_DQ); //input
_delay_us(14);
//read the value
if(DS18B20_PIN & (1<<DS18B20_DQ))
bit=1;
//wait 45uS and return read value
_delay_us(45);
return bit;
}
/*
* write one byte
*/
void ds18b20_writebyte(uint8_t byte){
uint8_t i=8;
while(i--){
ds18b20_writebit(byte&1);
byte >>= 1;
}
}
/*
* read one byte
*/
uint8_t ds18b20_readbyte(void){
uint8_t i=8, n=0;
while(i--){
n >>= 1;
n |= (ds18b20_readbit()<<7);
}
return n;
}
/*
* get temperature
*/
double ds18b20_gettemp() {
uint8_t temperature_l;
uint8_t temperature_h;
double retd = 0;
#if DS18B20_STOPINTERRUPTONREAD == 1
cli();
#endif
ds18b20_reset(); //reset
ds18b20_writebyte(DS18B20_CMD_SKIPROM); //skip ROM
ds18b20_writebyte(DS18B20_CMD_CONVERTTEMP); //start temperature conversion
while(!ds18b20_readbit()); //wait until conversion is complete
ds18b20_reset(); //reset
ds18b20_writebyte(DS18B20_CMD_SKIPROM); //skip ROM
ds18b20_writebyte(DS18B20_CMD_RSCRATCHPAD); //read scratchpad
//read 2 byte from scratchpad
temperature_l = ds18b20_readbyte();
temperature_h = ds18b20_readbyte();
#if DS18B20_STOPINTERRUPTONREAD == 1
sei();
#endif
//convert the 12 bit value obtained
retd = ( ( temperature_h << 8 ) + temperature_l ) * 0.0625;
return retd;
}
And this is the header file:
#ifndef DS18B20_H_
#define DS18B20_H_
#include <avr/io.h>
//setup connection
#define DS18B20_PORT PORTC
#define DS18B20_DDR DDRC
#define DS18B20_PIN PINC
#define DS18B20_DQ 0
//commands
#define DS18B20_CMD_CONVERTTEMP 0x44
#define DS18B20_CMD_RSCRATCHPAD 0xbe
#define DS18B20_CMD_WSCRATCHPAD 0x4e
#define DS18B20_CMD_CPYSCRATCHPAD 0x48
#define DS18B20_CMD_RECEEPROM 0xb8
#define DS18B20_CMD_RPWRSUPPLY 0xb4
#define DS18B20_CMD_SEARCHROM 0xf0
#define DS18B20_CMD_READROM 0x33
#define DS18B20_CMD_MATCHROM 0x55
#define DS18B20_CMD_SKIPROM 0xcc
#define DS18B20_CMD_ALARMSEARCH 0xec
//stop any interrupt on read
#define DS18B20_STOPINTERRUPTONREAD 1
//functions
extern double ds18b20_gettemp();
#endif