0

Hello i have microcontroler atmega32, 7 segment display and ds18b20 temperature sensor, i make sensor to read temperature and give me temperature, i need to know how to convert temperature in integer for represent in 7 sengment display

this is the code:

#ifndef DS18B20_H_
#define DS18B20_H_
#define F_CPU 16000000UL
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>

#define DS18B20_PORT PORTC
#define DS18B20_DDR DDRC
#define DS18B20_PIN PINC
#define DS18B20_DQ PC0

#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 orice intrerupere in read
#define DS18B20_STOPINTERRUPTONREAD 1

//functions
extern double ds18b20_gettemp();
#endif

int ms_flag = 0;
int digit_selection_counter = 0;
int temp_flag = 0;

void timer_delay()
{
    TCCR1B |= (1 << WGM12);
    TIMSK |= (1 << OCIE1A);
    sei();
    OCR1A = 0;
    TCCR1B |= ((1 << CS10) | (1 << CS12));// 16MHz/1024
}

ISR (TIMER1_COMPA_vect){
    ms_flag ++;
    temp_flag ++;
}

//-------------------------------------------------------------------

uint8_t ds18b20_reset(){
    uint8_t i;
    
    //low pentru 480 us
    DS18B20_PORT &= ~(1<<DS18B20_DQ); //low
    DS18B20_DDR |= (1<<DS18B20_DQ);//output
    _delay_us(480);
    
    //ridica linia si asteapta 60 us
    DS18B20_DDR &= ~(1<<DS18B20_DQ); //intput
    _delay_us(60);
    
    i = (DS18B20_PIN &(1<<DS18B20_DQ));
    _delay_us(420);
    
    return i;
}

//-------------------------------------------------------------------

void ds18b20_writebit(uint8_t bit){
    
    //low pentru 1 us
    DS18B20_PORT &= ~(1<<DS18B20_DQ); //low
    DS18B20_DDR |= (1<<DS18B20_DQ);//OUTPUT
    _delay_us(1);
    //daca vrem sa scriem 1, eliberam linia (daca nu linia se va pastra low)
    if (bit)
    {
        DS18B20_DDR &= ~(1<<DS18B20_DQ); //input
    }
    //asteapta 60 us si restabileste linia 
    _delay_us(60);
    DS18B20_DDR &= ~(1<<DS18B20_DQ); //input
}

//-------------------------------------------------------------------

uint8_t ds18b20_readbit(void)
{
    uint8_t bit = 0;
    
    //low pentru 1 us
    DS18B20_PORT &= ~(1<<DS18B20_DQ); //low
    DS18B20_DDR |= (1<<DS18B20_DQ); //output
    _delay_us(1);
    
    //ridica linia si asteapta pentru 14us
    DS18B20_DDR &= ~(1<<DS18B20_DQ); //input
    _delay_us(14);
    
    //citeste valoarea
    if(DS18B20_PIN &(1<<DS18B20_DQ)) bit = 1;
    
    //Asteapta 45us si returneaza valoarea citita
    _delay_us(45);
    return bit;
}

//-------------------------------------------------------------------

 void ds18b20_writebyte(uint8_t byte)
 {
     uint8_t i = 8;
     while (i--)
     {
         ds18b20_writebit(byte&1);
         byte >>= 1;
     }
 }

//-------------------------------------------------------------------

uint8_t ds18b20_readbyte(void)
{
uint8_t i = 8;
uint8_t n = 0;
while (i--)
{
    n >>= 1;
    n|=(ds18b20_readbit()<<7);
}
return n;                       
}

//-------------------------------------------------------------------

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 convert temperatur

while(!ds18b20_readbit()); //asteapra pana conversiunea este completa

ds18b20_reset();
ds18b20_writebyte(DS18B20_CMD_SKIPROM); //skip ROM
ds18b20_writebyte(DS18B20_CMD_RSCRATCHPAD);//read scratchpad

//citeste 2 byte din scratchpad
temperature_l = ds18b20_readbyte();
temperature_h = ds18b20_readbyte();

#if DS18B20_STOPINTERRUPTONREAD == 1
sei();
#endif

retd = ((temperature_h << 8) + temperature_l) * 0.0625;
return retd;
}

//-------------------------------------------------------------------

int main(void)
{
    timer_delay();
    sei();
    
    int arr1 [10]={0x81,0xCF, 0x92, 0x86, 0xCC, 0xA4, 0xA0, 0x8F, 0x80, 0x84};
    
    double temperature = 0;
    int nr1 = 0; 
    int nr2 = 0;
    int nr3 = 0;
    int nr4 = 0; 
        
    DDRB = (1<<DDB7) | (1<<DDB6) | (1<<DDB5) | (1<<DDB4) | (1<<DDB3) | (1<<DDB2) | (1<<DDB1) | (1<<DDB0);
    PORTB = (0<<PB7) | (0<<PB6) | (0<<PB5) | (0<<PB4) | (0<<PB3) | (0<<PB2) | (0<<PB1) | (0<<PB0);
    DDRA = (1<<DDA7) | (1<<DDA6) | (1<<DDA5) | (1<<DDA4) | (1<<DDA3) | (1<<DDA2) | (1<<DDA1) | (1<<DDA0);
    PORTA = (0<<PA7) | (0<<PA6) | (0<<PA5) | (0<<PA4) | (1<<PA3) | (1<<PA2) | (1<<PA1) | (1<<PA0);
    
    
    
    while (1) 
    {
        temperature = ds18b20_gettemp();
        _delay_ms(500);
        
        
        if(ms_flag == 15)
        {
            ms_flag = 0;
            digit_selection_counter ++;
            if (digit_selection_counter >= 4)
            {
                digit_selection_counter = 0;
            }
        }
        
        switch(digit_selection_counter){
            case 0:
            PORTA =~(1<<digit_selection_counter); //tranzistorul de afisare ON
            PORTB = arr1[nr1]; //pune valoarea la portul care este necesar pentru afisarea cifrelor corespunzatoare
            break;
            
            case 1:
            PORTA =~(1<<digit_selection_counter); //acelasi lucru de sus doar ca pentru segmentul numrul 2
            PORTB = arr1[nr2];
            break;
            
            case 2:
            PORTA =~(1<<digit_selection_counter); //acelasi lucru de sus doar ca pentru segmentul numrul 3
            PORTB = arr1[nr3];
            break;
            
            /*case 3:
            PORTA =~(1<<digit_selection_counter); //acelasi lucru de sus doar ca pentru segmentul numrul 4
            PORTB = arr1[nr4];
            break;*/
            
            default: digit_selection_counter = 0;
        }

        
        
    }
}

i have double retd = temperature, in function ds18b20_gettemp(), and i have in in main double temperature = ds18b20_gettemp(); , if temperature = 26,32 C i need to convert this in , nr1 = 2, nr2 = 6, nr3 = 3, nr4 = 2

0 Answers0