Close or Esc Key

Arduino Projects   |   Raspberry Pi   |   Electronic Circuits   |   AVR   |   PIC   |   8051   |   Electronic Projects

Atmega32

2 posts / 0 new
Last post
s.manisrinivas
s.manisrinivas's picture
Offline
Last seen: 1 month 2 weeks ago
Joined: 16/09/2015 - 12:05
Atmega32

i am using lm75 b temperature sensor interface to atmega32 controller .my problem is i sending the address to read i.e. 10010001 (0x91) but the value is displaying in lcd display .

iam using pull up resistors - 10k 

cpu freq -16mhz

code is below

 

this is main file
 
 
#define F_CPU 16000000UL    //this must be first line 
 
#include <avr/io.h>
#include "i2c.h"
#include "LCDa.h"
#include <util/delay.h>
#include <stdlib.h>
 
 
 
unsigned int read_temperature(unsigned char LM75_addr);
 
unsigned int temperature;
 char buffer[14];
 
unsigned int read_temperature(unsigned char LM75_addr) //function for reading temperature
{
 
unsigned int temp;
 
I2C_Start(); // start i2c
 
I2C_Write(0x90);// write address
I2C_Start();
// I2C_Write(0x01);
_delay_ms(1);
I2C_Write(0x00);
//I2C_Stop();
_delay_ms(1);
 
I2C_Write(LM75_addr);
 
 
temp=I2C_Read_ACK(); //get high byte
temp<<=8;
temp|=I2C_Read_NACK(); // get low byte
temp>>=5;
I2C_Stop();
 
return temp; //return temperature coded in second complement
}
 
 
 
 
 
int main()
{
     
 
 _delay_ms(100);
 
    lcd_init();
Init_I2C();
lcd_display_on_off(1,1,1);
 
 
while(1)
{
lcd_clear_display();
    temperature = read_temperature(0x91);//0x90+1
    itoa(temperature,buffer,10); 
lcd_gotoxy(0,0);
lcd_puts("temp is:");
    lcd_gotoxy(0,1);
lcd_puts(buffer);
_delay_ms(200);
}
   
   
}
 
this is i2c routines
 
 
 
#include <avr/io.h>
#include "i2c.h"
 
 
unsigned char I2C_Read_NACK(void)
{
TWCR = (1<<TWINT)|(1<<TWEN);
while ((TWCR & (1<<TWINT)) == 0);
return TWDR;
}
void Init_I2C()
{
#define SCL_presceler 1
TWSR=0;  //no presceler
TWBR=0x20; //  ((F_CPU/F_SCL)-16)/2 -> F_SCL=cca 10000Hz
TWCR = (1<<TWEN);
}
unsigned char I2C_get_status(void)
{
unsigned char  status;
status = TWSR & 0xF8;
return status;
}
void I2C_Start()
{
TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
while ((TWCR & (1<<TWINT)) == 0 );
 
}
void I2C_Stop()
{
TWCR = (1<<TWINT)|(1<<TWSTO)|(1<<TWEN);
}
void I2C_Write(unsigned char data)
{
TWDR = data;
TWCR = (1<<TWINT)|(1<<TWEN);
while ((TWCR & (1<<TWINT)) == 0);
}
 
unsigned char I2C_Read_ACK(void)
{
TWCR = (1<<TWINT)|(1<<TWEA)|(1<<TWEN);
while ((TWCR & (1<<TWINT)) == 0);
return TWDR;
}
 
 
Faroogh
Faroogh's picture
Offline
Last seen: 1 day 19 hours ago
Joined: 24/01/2016 - 19:43
Lm35 is not 12c device use

Lm35 is not 12c device use ADC to read the signal and apply formulas to the adc read value then print it to Lcd