Problemas com o DS1822

Software e Hardware para uC da Qualcomm, NXP, FreeScale e Motorola

Moderadores: 51, guest2003

Problemas com o DS1822

Mensagempor icaro51 » 19 Abr 2009 00:23

E ai galera blz?
Sou novo com os produtos da freescale mas acabei comprando um kit e estou no caminho.
Estou tentando acessar o DS1822 mas nao esta dando certo, estou fazendo o programa em C, vou colocar a biblioteca para voces se alguem puder por favor me ajudar.
Estou usando o mc9s08jm60 da familia hcs08jm quando inicio o chip mostra o bus como 8mhz (interno).

Desde ja agradeco,
Abraco,
Icaro

#define DQ PTADD_PTADD0 // DS1822 enable pin direction


// Display configuration global variable
static char i;


//**************************************************************************
//* Prototypes
//**************************************************************************
unsigned char ow_reset(void);
unsigned char read_bit(void);
void write_bit(char bitval);
unsigned char read_byte(void);
void write_byte(char val);

// DELAY - with an 8MHz internal.
// Calling the routine takes about 33us, and then
// each count takes another 22us.
//
void delay(int useconds)
{
int s;
for (s=0; s<useconds;s++);
}

//////////////////////////////////////////////////////////////////////////////
// OW_RESET - performs a reset on the one-wire bus and
// returns the presence detect. Reset is 480us, Presence checked
// another 70us later
//
unsigned char ow_reset(void)
{
unsigned char presence;
DQ = 1; //pull DQ line low
delay(21); // leave it low for 480us
DQ = 0; // allow line to return high
delay(2); // wait for presence
presence = DQ; // get presence signal
delay(18 ); // wait for presence
return(presence); // presence sign
}

//////////////////////////////////////////////////////////////////////////////
// READ_BIT - reads a bit from the one-wire bus. The delay
// required for a read is 15us, so the DELAY routine won't work.
// We put our own delay function in this routine in the form of a
// for() loop.
//
unsigned char read_bit(void)
{
unsigned char i;
DQ = 1; // pull DQ low to start timeslot
DQ = 0; // then return high
for (i=0; i<2; i++); // delay 15us from start of timeslot
return(DQ); // return value of DQ line
}

//////////////////////////////////////////////////////////////////////////////
// WRITE_BIT - writes a bit to the one-wire bus, passed in bitval.
//
void write_bit(char bitval)
{
DQ = 1; // pull DQ low to start timeslot
if(bitval==1) DQ =0; // return DQ high if write 1
delay(4); // hold value for remainder of timeslot
DQ = 0;
}

//////////////////////////////////////////////////////////////////////////////
// READ_BYTE - reads a byte from the one-wire bus.
//
unsigned char read_byte(void)
{
unsigned char i;
unsigned char value = 0;
for (i=0;i<8;i++)
{
if(read_bit()) value|=0x01<<i; // reads byte in, one byte at a time and then
// shifts it left
delay(4); // wait for rest of timeslot
}
return(value);
}


//////////////////////////////////////////////////////////////////////////////
// WRITE_BYTE - writes a byte to the one-wire bus.
//
void write_byte(char val)
{
unsigned char i;
unsigned char temp;
for (i=0; i<8; i++) // writes byte, one bit at a time
{
temp = val>>i; // shifts val right 'i' spaces
temp &= 0x01; // copy that bit to temp
write_bit(temp); // write bit in temp into
}
delay(4);
}
icaro51
Byte
 
Mensagens: 119
Registrado em: 09 Dez 2006 21:56

Mensagempor msamsoniuk » 19 Abr 2009 12:41

essa notacao do seu compilador eh um pouco diferente do que eu uso, mas o que me parece estranho eh que vc definiu o registro de direcao da porta, mas nao definiu o registro de dados. nos hcs que eu uso essas funcionalidade ficam em registros diferentes e eu chuto que o PTADD_PTADD0 eh apenas para definir direcao e o PTAD_PTAD0 deveria ser usado para ler ou escrever dados na porta.
Avatar do usuário
msamsoniuk
Dword
 
Mensagens: 2935
Registrado em: 13 Out 2006 18:04

Mensagempor icaro51 » 20 Abr 2009 01:51

eu nao defini o registro porque o ds1822 utiliza um resistor pull up na conexao entre a porta e ele entao para dar nivel logico 1 eu seto a porta como entrada (nada a linha para 1) e para dar nivel logico zero eu mudo a porta para entrada, eu normalmente faco isso com o PIC e eu estava meio na duvida tambem mas fiz uns teste no osciloscopio e parece estar funcionando legal (essa parte)
icaro51
Byte
 
Mensagens: 119
Registrado em: 09 Dez 2006 21:56

Mensagempor icaro51 » 20 Abr 2009 01:54

hahaha valeu acho que achei o erro, a logica funciona para mandar sinal mas nao para ler (hahaha) na parte da leitura estou lendo DQ que e o PTADD_PTADD0 e deveria ser o PTADD_PTAD0, vou fazer as mudancas nescessarias e posto resultados assim que possivel.

Mais uma vez... value!!!
icaro51
Byte
 
Mensagens: 119
Registrado em: 09 Dez 2006 21:56

Mensagempor icaro51 » 20 Abr 2009 04:49

Valeu pessoal esta funcionando agora, estou com problemas para acertar os intervalos (rotinas de delay) tive que fazer com o osciloscopio, quando compilo o compilador diz que esta com 8Mhz no bus mas quando tento usar isso os tempos ficam muito curtos, por isso se voces usarem essa biblioteca podem ter problemas com os delays.

PROGRAMA PRINCIPAL


#include <hidef.h> /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */
#include "my_lcd.c" /* LCD library */
#include "my_ds1822.c" /* DS1822 - Temperature Sensor - library */


void main(void) {
char get[10]; /* to read sensor (I will put it at the library soon) */
int k,k2,a;

SOPT1 = 0x13; /* chip settings */

EnableInterrupts; /* enable interrupts */

LCD_init(DISPLAY_8X5|_4_LINES,DISPLAY_ON|CURSOR_OFF|CURSOR_NOBLINK); // start display
LCD_write_char('\f'); // clear the display
a=0;

LCD_pos_xy(0,1); // set cursor
LCD_write_string(" Temp. Sensor");
LCD_pos_xy(0,2); // set cursor
LCD_write_string(" DS1822");
LCD_pos_xy(0,3); // set cursor
LCD_write_string("Temp: ");
LCD_pos_xy(8,3); // set cursor
LCD_write_string(" C");
LCD_pos_xy(0,4); // set cursor
LCD_write_string("Temp: ");
LCD_pos_xy(8,4); // set cursor
LCD_write_string(" F");
for (;;) {
a++;
if (ow_reset()){
//LCD_write_string("No answer"); // check for sensor
};
write_byte(0xCC); // Skip ROM
write_byte(0x44); // Start Conversion
for (k=1000;k;k--){};
if (ow_reset()){
//LCD_write_string("No answer"); // write on the display
};
write_byte(0xCC); // Skip ROM
write_byte(0xBE); // Read Scratch Pad
for (k=0;k<9;k++){get[k]=read_byte();}
LCD_pos_xy(6,3); // set cursor
LCD_write_string(LCD_int_char (convert_temp(get[0],get[1],0)));
LCD_pos_xy(6,4); // set cursor
LCD_write_string(LCD_int_char (convert_temp(get[0],get[1],1)));
LCD_pos_xy(14,4); // set cursor
LCD_write_string(LCD_int_char(a));
for (k=1000;k;k--){
for (k2=1000;k2;k2--){
};
};
};
}




BIBLIOTECA PARA O SENSOR DS1822


#define T PTAD_PTAD0 // LCD enable pin on PTB1
#define DQ PTADD_PTADD0 // LCD enable pin direction
// DQ = 0 -> input -> high
// DQ = 1 -> output

// Display configuration global variable
static char i;


//**************************************************************************
//* Prototypes
//**************************************************************************
unsigned char ow_reset(void);
unsigned char read_bit(void);
void write_bit(char bitval);
unsigned char read_byte(void);
void write_byte(char val);
int convert_temp(char temp_lsb, char temp_msb, char type);

// DELAY - with an 11.059MHz crystal.
// Calling the routine takes about 24us, and then
// each count takes another 16us.
//
void delay(int useconds)
{
int s;
for (s=0; s<useconds;s++);
}

//////////////////////////////////////////////////////////////////////////////
// OW_RESET - performs a reset on the one-wire bus and
// returns the presence detect. Reset is 480us, so delay
// value is (480-24)/16 = 28.5 - we use 29. Presence checked
// another 70us later, so delay is (70-24)/16 = 2.875 - we use 3.
//
unsigned char ow_reset(void)
{
unsigned char presence;
DQ = 1; //pull DQ line low
// delay(29); // leave it low for 480us
for (i=200;i;i--);
for (i=200;i;i--);
for (i=200;i;i--);
for (i=30;i;i--);


// for (i=200;i;i--){}
// for (i=200;i;i--){}
// for (i=200;i;i--){}
// for (i=30;i;i--){}
DQ = 0; // allow line to return high
// delay(3); // wait for presence
// for (i=100;i;i--){}
for (i=100;i;i--);
presence = T; // get presence signal
// delay(25); // wait for presence
for (i=200;i;i--);
for (i=200;i;i--);
for (i=30;i;i--);
return(presence); // presence sign
}

//////////////////////////////////////////////////////////////////////////////
// READ_BIT - reads a bit from the one-wire bus. The delay
// required for a read is 15us, so the DELAY routine won't work.
// We put our own delay function in this routine in the form of a
// for() loop.
//
unsigned char read_bit(void)
{
unsigned char i2;
DQ = 1; // pull DQ low to start timeslot
DQ = 0; // then return high
// for (i=0; i<3; i++); // delay 15us from start of timeslot
for (i2=0; i2<20; i2++); // delay 15us from start of timeslot
return(T); // return value of DQ line
}

//////////////////////////////////////////////////////////////////////////////
// WRITE_BIT - writes a bit to the one-wire bus, passed in bitval.
//
void write_bit(char bitval)
{
DQ = 1; // pull DQ low to start timeslot
if(bitval==1) DQ =0; // return DQ high if write 1
// delay(5); // hold value for remainder of timeslot
for (i=110;i;i--);
DQ = 0;
}// Delay provides 16us per loop, plus 24us. Therefore delay(5) = 104us


//////////////////////////////////////////////////////////////////////////////
// READ_BYTE - reads a byte from the one-wire bus.
//
unsigned char read_byte(void)
{
unsigned char i2;
unsigned char value = 0;
for (i2=0;i2<8;i2++)
{
if(read_bit()) value|=0x01<<i2; // reads byte in, one byte at a time and then
// shifts it left
// delay(5); // wait for rest of timeslot
for (i=110;i;i--);
}
return(value);
}


//////////////////////////////////////////////////////////////////////////////
// WRITE_BYTE - writes a byte to the one-wire bus.
//
void write_byte(char val)
{
unsigned char i2;
unsigned char temp;
for (i2=0; i2<8; i2++) // writes byte, one bit at a time
{
temp = val>>i2; // shifts val right 'i' spaces
temp &= 0x01; // copy that bit to temp
write_bit(temp); // write bit in temp into
}
// delay(5); // wait for rest of timeslot
for (i=110;i;i--);
}

//////////////////////////////////////////////////////////////////////////////
// convert_temp - convert temperature.
// type = 0 temp in C
// type = 1 temp in F
//
int convert_temp(char temp_lsb, char temp_msb, char type)
{
if (temp_msb <= 0x80)
{temp_lsb = (temp_lsb/2);} // shift to get whole degree
temp_msb = temp_msb & 0x80; // mask all but the sign bit
if (temp_msb >= 0x80)
{temp_lsb = (~temp_lsb)+1;} // twos complement
if (temp_msb >= 0x80)
{temp_lsb = (temp_lsb/2);}// shift to get whole degree
if (temp_msb >= 0x80)
{temp_lsb = ((-1)*temp_lsb);} // add sign bit
if (!type)
return (temp_lsb);
if (type)
return ((((int)temp_lsb)* 9)/5 + 32);
}
icaro51
Byte
 
Mensagens: 119
Registrado em: 09 Dez 2006 21:56


Voltar para NXP (ex-FreeScale (ex-Motorola))

Quem está online

Usuários navegando neste fórum: Nenhum usuário registrado e 1 visitante

x