salut,
jai trouver un code sur ce forum pour le ds1820 le voici ds1wire.c
j'utulise le ds18b20 connecté a RA0 une une resistane de 4.7k connecté a 5v, pour cet example il utilise l'afficheur lcd donc j'ai une petite modification dans mon main pour afficher sur 4 digit 7 segmentCode:////////////////////////////////////////////////////////////////////// // J.Oinonen 2005-09-29 // // // // // // Dallas 1 wire routines interfacing with the DS1820 temperature // // sensor. // // // // Possible future improvements: // // -One universal delay routine. (to get rid of all do while // // statements) // // -Support for multiple sensors (currently only one allowed) // // -Support for sensor addressing // // // // Compiles with B. Knudsen CC5X(Trial version/without optimization)// // Timings are critical! // ////////////////////////////////////////////////////////////////////// #define DQ_TRI TRISA.0 //Tristate register of DQ pin #define DQ_PIN PORTA.0 //Pin connected to DQ on DS1820 ////////////////////////////////////////////////////////////////////// // Main delay routine. Delays the uprocessor for 5 us. // // 2 + 1 + 2 = 5 // // GOTO + NOP + RETURN = 5us // ////////////////////////////////////////////////////////////////////// void Delay5us() { nop(); } /*void delay( char millisec) { do{ char t=200; do { Delay5us(); }while(--t); } while(--millisec); } */ ////////////////////////////////////////////////////////////////////// // This function Resets the 1 wire bus and checks for presence of a // // 1 Wire device. If no device is detected, Led on Portb.2 is lit // ////////////////////////////////////////////////////////////////////// void Reset(void) { char count; bit present; DQ_TRI=1; // Set the Correct port pin settings DQ_PIN=0; DQ_TRI=0; // Pull Line Low to start reset pulse count=40; do Delay5us(); //200 uS Delay while (-- count > 0); DQ_TRI=1; // Release the line count=12; do // = 60us delay to middle of presence pulse Delay5us(); while (-- count > 0); present=DQ_PIN; // Get Presence on pin, DS1820 pulls it low if there count=34; do Delay5us(); // 170 us delay to finish reset while (-- count > 0); if(present==1) { PORTB.2=1; // If no device, Indicate error on LED } Delay5us(); Delay5us(); // Rise + Min Space = 10 us } ////////////////////////////////////////////////////////////////////// // This function Writes a byte to the 1 wire bus. // // // ////////////////////////////////////////////////////////////////////// void Write(char Data) { char count=8,n; bit DQdata; for(;count>0;count--) { DQdata= Data.0; // Put the bit to be written in the right place DQ_PIN=0; DQ_TRI=0; // Lower the port nop(); // Time slot start time // 5us according to datasheet // If statements below create some more delay if(DQdata== 1) // Output data to the port DQ_TRI = 1; else DQ_TRI = 0; n=10; do Delay5us(); //50us to finish timeslot while (-- n > 0); DQ_TRI=1; // Make sure port is released nop(); // Some Delay Between Bits nop(); Data=Data>>1; // Shift next bit into place // Future improvement using rr(Data) } Delay5us(); // Rise + Min Space = 5 us } ////////////////////////////////////////////////////////////////////// // This function Reads a byte from the 1 Wire bus and returns the // // byte read. // ////////////////////////////////////////////////////////////////////// char Read(void) { char count=8,data=0,n; bit DQdata; for(;count>0;count--) { DQ_PIN=0; DQ_TRI=0; // Lower port Delay5us(); // Timeslot start time DQ_TRI=1; // Set port for reading Delay5us(); // Wait until center of timeslot DQdata=DQ_PIN; // Read the bit on the bus n=11; do Delay5us(); //55us to finish timeslot while (-- n > 0); // Put data into the byte data = data >> 1; // Shift previous bits (future improvement rr..) if(DQdata==1) // Add 1 if the data on the bus was 1 data +=0x80; Delay5us(); //-- Delay Between Bits } Delay5us(); // Rise + Min Space = 5 us return(data); }
une partie de mon main pour remplir le tableau digits des 2 numero (10 par example) suivi de ° et C :
et la fonction read tempCode:char buffer[9]; // Used to store 9 bytes read from DS1820 char temperature; // Same as above, only need one of these global. Future imrovement. char s[11]; uns8 bb,cc; int digits[4]; while(1) { readtemp(); temperature=buffer[0]>>1; chartoa(temperature); bb=((uns8)dd/10); cc=((uns8)dd%10); digits[0]=bb; //dizaine digits[1]=cc;//unité digits[2]=11; //° , 11 corespond a portb=0b0b01110001 pour afficher le signe ° digits[3]=13;//C }
mais malheureusement je vois toujours 00°cCode:void readtemp() { char i,n; Reset(); // Reset is required before any command is issued Write(0xCC); // Skip Rom Write(0x44); // Tell DS1829 to Start Temp. Conv RBIE = 1; // Turn on interrupts while waiting GIE = 1; delay(250); // delay(250); // 700ms delay required delay(100); // for temperature conversion delay(100); // RBIE = 0; /* local Disble */ // Turn of interrupts before speaking to 1 wire device GIE = 0; /* global disable */ Reset(); Write(0xCC); // Skip Rom Write(0xBE); // Read the result of the temp. Conversion for(i=0; i<9 ;i++){ // Read 9 bytes, only the first two bytes n=Read(); // Are needed. buffer[i]=n; } }
est ce que ca peut être la variable température parce que c'est une chaine de caractère ??
-----