Jump to content
ELFORUM - Forumul electronistilor

18F2550 + DS18B20, nu citeste datele


Guest Mareshal

Recommended Posts

Guest Mareshal

Folosesc 18F2550 cu un DS18B20 conectat la PORTC.RC1, compilat cu MikroC for PIC , si Virtual Terminal in Proteus. Terminal-ul merge, restul nu prea.

 

char t;int temp;void ds1820_read(){      Ow_Reset(&PORTC, 1);       //                    'Reset      Ow_Write(&PORTC, 1, 0xCC);//                   'Skip ROM      Ow_Write(&PORTC, 1, 0x4E);//                   'Write to scratchpad      Ow_Write(&PORTC, 1, 0x55);//                   'TH register value  (85 degrees C)      Ow_Write(&PORTC, 1, 0x19);//                   'TL register value  (25 degrees C)      Ow_Write(&PORTC, 1, 0x7F);//                   'Config register value (12bit)                            // 'Do temp conversion     Ow_Reset(&PORTC, 1);//                            ' Reset     Ow_Write(&PORTC, 1, 0xCC);//                    'SKIP_ROM     Ow_Write(&PORTC, 1, 0x44);//                    ' Issue command CONVERT_T     Delay_us(120);//                            'Read Temp     Ow_Reset(&PORTC, 1);     Ow_Write(&PORTC, 1, 0xCC);//                    'SKIP_ROM     Ow_Write(&PORTC, 1, 0xBE);//                    'READ_SCRATCHPAD     temp =  Ow_Read(&PORTC, 1);     temp = (Ow_Read(&PORTC, 1) << 8) + temp;}void main() {     UART1_Init(9600);     Delay_ms(100);     UART1_Write_Text("DS18B20: ");          TRISC = 0b00000010;          while(1)     {      ds1820_read();      UART1_Write_Text(temp);      UART1_Write('\r\n');      Delay_ms(300);     }}

Cineva o solutie? Am impresia ca e o greseala minora, dar nu stiu care e. Nici exemplul de la mikroC nu a mers.

Link to comment
  • Replies 15
  • Created
  • Last Reply

Top Posters In This Topic

Guest Mareshal

Multumesc. Primesc balarii, dar se modifica in momentul in care modific temperatura. Sa vad ce ii pot face

 

LE: cu bucata asta de cod

sprintf(str, "%d", temp);      UART1_Write_Text(str);

Primesc 320, pentru 20. e ceva totusi. Still working... :dans:

 

LELE: Merge acum. O sa postez tot codul si schema, dar sa il aranjez putin doar.

Link to comment
Guest Mareshal

Cod complet: PIC18F2550(8 Mhz) + DS18B20 & Virtual Terminal Proteus, mikroC for PIC

PS: codul se putea aranja si mai bine, poate fi optimizat, dar mai tarziu

 

int temp;void ds1820_read(){      Ow_Reset(&PORTC, 1);              // Reset      Ow_Write(&PORTC, 1, 0xCC);        // Skip ROM      Ow_Write(&PORTC, 1, 0x4E);        // Write to scratchpad      Ow_Write(&PORTC, 1, 0x55);        // TH register value  (85 degrees C)      Ow_Write(&PORTC, 1, 0x19);        // TL register value  (25 degrees C)      Ow_Write(&PORTC, 1, 0x7F);        // Config register value (12bit)     // Temperature conversion     Ow_Reset(&PORTC, 1);               // Reset     Ow_Write(&PORTC, 1, 0xCC);         // SKIP_ROM     Ow_Write(&PORTC, 1, 0x44);         // Issue command CONVERT_T     Delay_us(120);     // Read Temperature     Ow_Reset(&PORTC, 1);     Ow_Write(&PORTC, 1, 0xCC);         // SKIP_ROM     Ow_Write(&PORTC, 1, 0xBE);         // READ_SCRATCHPAD     temp =  Ow_Read(&PORTC, 1);     temp = (Ow_Read(&PORTC, 1) << 8) + temp;}const unsigned short TEMP_RESOLUTION = 12;   // 12 for DS18B20char *text = "000.0000";void Transform_temperature(unsigned int temp2write) {  const unsigned short RES_SHIFT = TEMP_RESOLUTION - 8;  char temp_whole;  unsigned int temp_fraction;  // Check if temperature is negative  if (temp2write & 0x8000) {     text[0] = '-';     temp2write = ~temp2write + 1;     }  // Extract temp_whole  temp_whole = temp2write >> RES_SHIFT ;    text[1] = (temp_whole/10)%10 + 48;             // Extract tens digit  text[2] =  temp_whole%10     + 48;             // Extract ones digit  // Extract temp_fraction and convert it to unsigned int  temp_fraction  = temp2write << (4-RES_SHIFT);  temp_fraction &= 0x000F;  temp_fraction *= 625;  // Convert temp_fraction to characters  text[4] =  temp_fraction/1000    + 48;         // Extract thousands digit  text[5] = (temp_fraction/100)%10 + 48;         // Extract hundreds digit  text[6] = (temp_fraction/10)%10  + 48;         // Extract tens digit  text[7] =  temp_fraction%10      + 48;         // Extract ones digit  temp = text;}void main() {     UART1_Init(9600);     Delay_ms(100);     UART1_Write_Text("DS18B20: ");     TRISC = 0b00000010;     while(1)     {      ds1820_read();      Transform_temperature(temp);      UART1_Write_text(temp);      UART1_Write('\r\n');      Delay_ms(500);     }}

Schema conectare Proteus

Link to comment

In functia void ds1820_read() observ ca intre comanda Convert T (0x44) si Read Scrachpad (0xBE) ai lasat doar 120uS. Eu din cate stiu dupa comanda de conversie a temperaturi abia dupa 95-750 mS trebuie citit scrachpadul. Din codul prezentat nu m-as baza pe temperatura masurata.

Link to comment

Asa arata in exemplul lor: http://www.mikroe.com/esupport/index.ph ... icleid=140

Din datasheet-ul senzorului DS18x20 stiu ca dupa comanda de conversie a temperaturi senzorul trebuie lasat in pace timpul necesar (95ms-750ms). Abia dupa tconv trimiti comanda de Read Scrachpad si citesti cei 9 octeti din scrachpad. In exemplul ala nu vad rostul la Delay_ms(500) la finalul operatiei de transformare a informatiilor. Eu consider gresit exemplul, inlocuieste Delay_us(120) cu Delay_ms(500).La 1 Wire pinul de date se conecteaza cu un rezistor de 4k7 la VDD. Asa cere protocolul.LE. Aici foloseste 750ms si tot microC foloseste http://www.edaboard.com/thread148568.html#post637618
Link to comment
Guest Mareshal

Ok, am corectat. intr-adevar, primele 3 valori care le citeam erau eronate, primeam valoarea maxima a senzorului, adica 85 de grade.

Link to comment
  • 4 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.Terms of Use si Guidelines