MEGA8 a DS18S20

zdravim machri. neviete niekto preco nechce slapat DS18s20 s MEGA8? kod je odniekial z webu, priposobeny pre moje porty. celkom chapem co to ma robit, ale nechapem preco to nerobi :slight_smile:

#include "avr\io.h"
#include "util\delay.h"
#include "avr\iom8.h"
#include "util\lcd.c"
#include "avr\interrupt.h"
#include "avr/pgmspace.h" 
#include "inttypes.h" 
#include "stdlib.h"

#define THERM_DECIMAL_STEPS_12BIT 625 //.0625 
#define THERM_CMD_CONVERTTEMP 0x44
#define THERM_CMD_RSCRATCHPAD 0xbe
#define THERM_CMD_WSCRATCHPAD 0x4e
#define THERM_CMD_CPYSCRATCHPAD 0x48
#define THERM_CMD_RECEEPROM 0xb8
#define THERM_CMD_RPWRSUPPLY 0xb4
#define THERM_CMD_SEARCHROM 0xf0
#define THERM_CMD_READROM 0x33
#define THERM_CMD_MATCHROM 0x55
#define THERM_CMD_SKIPROM 0xcc
#define THERM_CMD_ALARMSEARCH 0xec

#define THERM_PORT PORTB
#define THERM_DDR DDRB
#define THERM_PIN PINB
#define THERM_DQ PB0

#define THERM_INPUT_MODE() THERM_DDR&=~(1<<THERM_DQ)
#define THERM_OUTPUT_MODE() THERM_DDR|=(1<<THERM_DQ)
#define THERM_LOW() THERM_PORT&=~(1<<THERM_DQ)
#define THERM_HIGH() THERM_PORT|=(1<<THERM_DQ)

char buffer[16];

}
uint8_t therm_reset(void){
uint8_t i;

//Pull line low and wait for 480uS

THERM_LOW();
THERM_OUTPUT_MODE();
_delay_us(480);

//Release line and wait for 60uS

THERM_INPUT_MODE();
_delay_us(60);

//Store line value and wait until the completion of 480uS period

i=(THERM_PIN & (1<<THERM_DQ));
_delay_us(420);

//Return the value read from the presence pulse (0=OK, 1=WRONG)

return i;
}

void therm_write_bit(uint8_t bit){

//Pull line low for 1uS

THERM_LOW();
THERM_OUTPUT_MODE();
_delay_us(1);

//If we want to write 1, release the line (if not will keep low)

if(bit) THERM_INPUT_MODE();

//Wait for 60uS and release the line

_delay_us(60);
THERM_INPUT_MODE();
};

uint8_t therm_read_bit(void){
uint8_t bit=0;

//Pull line low for 1uS

THERM_LOW();
THERM_OUTPUT_MODE();
_delay_us(1);

//Release line and wait for 14uS

THERM_INPUT_MODE();
_delay_us(14);

//Read line value

if(THERM_PIN&(1<<THERM_DQ)) bit=1;

//Wait for 45uS to end and return read value

_delay_us(45);
return bit;
}

uint8_t therm_read_byte(void){
uint8_t i=8, n=0;
while(i--){

//Shift one position right and store read value

n>>=1;
n|=(therm_read_bit()<<7);
}
return n;
}
void therm_write_byte(uint8_t byte){
uint8_t i=8;
while(i--){

//Write actual bit and shift one position right to make  the next bit ready

therm_write_bit(byte&1);
byte>>=1;
}
}

void therm_read_temperature(char *buffer){

// Buffer length must be at least 12bytes long! "+XXX.XXXX C"]

uint8_t temperature[2];
int8_t digit;
uint16_t decimal;

//Reset, skip ROM and start temperature conversion

therm_reset();
therm_write_byte(THERM_CMD_SKIPROM);
therm_write_byte(THERM_CMD_CONVERTTEMP);

//Wait until conversion is complete

while(!therm_read_bit());

//Reset, skip ROM and send command to read Scratchpad

therm_reset();
therm_write_byte(THERM_CMD_SKIPROM);
therm_write_byte(THERM_CMD_RSCRATCHPAD);

//Read Scratchpad (only 2 first bytes)

temperature[0]=therm_read_byte();
temperature[1]=therm_read_byte();
therm_reset();

//Store temperature integer digits and decimal digits
//digit=temperature[0]>>4;
//digit|&=temperature[1]&0x7;
//Store decimal digits
//decimal=temperature[0]&0xf;
//decimal*=THERM_DECIMAL_STEPS_12BIT;

//zmenil som prevod na decimalne hodnotym aby som videl ake byty a bity to posiela
			itoa(temperature[0], buffer, 2);
			lcd_gotoxy(0,0);
			lcd_puts(buffer);

			itoa(temperature[1], buffer, 2);
			lcd_gotoxy(0,1);
			lcd_puts(buffer); 
}

int main(void) 

{
 lcd_init(LCD_DISP_ON) ;
 lcd_command(LCD_FUNCTION_4BIT_2LINES);
 lcd_clrscr();
 
 while (1)
 {
 therm_read_temperature(buffer); //naco je tento parameter???
 _delay_ms(100);
 }
 
} 	

vypisuje to len 6bitov z prveho bytu (a menia sa podla teploty), a iba jednu nulu z druheho bytu, nic viac. to je asi malo na prevod na teplotu v °C, ze? :slight_smile:. Pozrite sa na to niekto, mozno len nevidim chybu v kode… ked z toho dostanem tie spravne bity, premenit ich uz budem vediet :wink:.

Vdaka. Riso

neviem ako sa to dostalo do 8051…