Bonjour,
je programme un thermometre via la sonde tres sympathique DS18B20.
le PIC est un 18F4550 et le quartz 4Mhz (1cycle = 1us).
Je pense respecter la routine et le timing.
ci joint les codes main et ds18B20:
le main:
les routines DS18B20Code:#include <p18f4550.h> #include <delays.h> #include <stdio.h> #include "xlcd.h" #include "project.h" #pragma config WDT = OFF //watchdog OFF #pragma config FOSC = HS //definition des variables globales unsigned char sondeT[5]={0xFF,0x07,0xFF,0x07};//sondeT[5]={0xFF,0x07,0xFF,0x07} char temp1 [15] = "";//variable affichage temperature char temp2 [15] = "";//variable affichage temperature //******************************************************************* // Fonction Main * //******************************************************************* void main (void) { ADCON0=0; ADCON1=0x0f; //initialisation hardware //initialisation logicielle LcdInit(); while(1) //boucle infinie du programme vide car uniquement interruptions { //sondes DS18B20 Delay10TCYx(10); Sonde_Reset(); Sonde_Write(0xcc); Delay10TCYx(10); Sonde_Write(0x44); Delay10KTCYx(100); Sonde_Reset(); Sonde_Write(0xcc); Delay10TCYx(10); Sonde_Write(0xbe); Delay10TCYx(10); Sonde_Readbyte(sondeT,0);//1er octet Delay10TCYx(10); Sonde_Readbyte(sondeT,1);//2eme octet Delay10TCYx(10); Sonde_Reset(); Temp_String (temp1,sondeT[0],sondeT[1]);//conversion temperature en chaine de caractere pour affichage sondeT[0],sondeT[1] SetDDRamAddr(0x80);//debut 1ere ligne putsXLCD(temp1);//affichage de l'heure while( BusyXLCD() ); SetDDRamAddr(0xC0);//debut seconde ligne putsXLCD(temp2);//affichage de la temperature while( BusyXLCD() ); }//fin de la boucle }//fin du main //******************************************************************* // Fonctions d'Initialisation * //******************************************************************* void LcdInit(void) // initalisation du LCD sur PORTD (D0-D6) { PORTD=0x00; TRISD=0x00; LATD=0x00; OpenXLCD( FOUR_BIT & LINES_5X7 ); while( BusyXLCD() ); WriteCmdXLCD( FOUR_BIT & LINES_5X7 ); while( BusyXLCD() ); WriteCmdXLCD( DON & BLINK_OFF); while( BusyXLCD() ); } //******************************************************************* // Fonctions de temporisation * // pour les fonctions LCD * //******************************************************************* void DelayFor18TCY( void ) //50micro { Delay10TCYx(6); return; } void DelayPORXLCD(void)//15ms { Delay1KTCYx(16); return; } void DelayXLCD(void)//5ms { Delay1KTCYx(6); return; }
Donc sur les routines ds18B20 je peux gerer 2 sondes (sur RA0 et RA1) mais je ne gere que celle de RA0 pour le moment et c'est deja pas mal vu tous les deboires que ca m'apporte.Code://************************************************* //reset pour demarrage 1 wire communication * //************************************************* void Sonde_Reset (void) { TRISAbits.TRISA0 = 0; PORTAbits.RA0 = 0; TRISAbits.TRISA1 = 0; PORTAbits.RA1 = 0; Delay10TCYx(50); TRISAbits.TRISA0 = 1; TRISAbits.TRISA1 = 1; Delay10TCYx(50); return; } //************************************************** //ecriture d'octet de communication sur RA0 et RA1 * //************************************************** void Sonde_Write (int valeur) { TRISAbits.TRISA0 = 0;PORTAbits.RA0 = 0; TRISAbits.TRISA1 = 0;PORTAbits.RA1 = 0; _asm nop nop nop nop _endasm//15us if(valeur&0x01) {TRISAbits.TRISA0 = 1;TRISAbits.TRISA1 = 1;} Delay10TCYx(5); TRISAbits.TRISA0 = 1;TRISAbits.TRISA1 = 1; Delay10TCYx(1);//fin 1er bit //************************************************* TRISAbits.TRISA0 = 0;TRISAbits.TRISA1 = 0; PORTAbits.RA0 = 0;PORTAbits.RA1 = 0; _asm nop nop nop nop _endasm//15us if(valeur&0x02) {TRISAbits.TRISA0 = 1;TRISAbits.TRISA1 = 1;} Delay10TCYx(5); TRISAbits.TRISA0 = 1;TRISAbits.TRISA1 = 1; Delay10TCYx(1);//fin 2 bit //************************************************ TRISAbits.TRISA0 = 0;TRISAbits.TRISA1 = 0; PORTAbits.RA0 = 0;PORTAbits.RA1 = 0; _asm nop nop nop nop _endasm//15us if(valeur&0x04) {TRISAbits.TRISA0 = 1;TRISAbits.TRISA1 = 1;} Delay10TCYx(5); TRISAbits.TRISA0 = 1;TRISAbits.TRISA1 = 1; Delay10TCYx(1);//fin 3 bit //************************************************ TRISAbits.TRISA0 = 0;TRISAbits.TRISA1 = 0; PORTAbits.RA0 = 0;PORTAbits.RA1 = 0; _asm nop nop nop nop _endasm//15us if(valeur&0x08) {TRISAbits.TRISA0 = 1;TRISAbits.TRISA1 = 1;} Delay10TCYx(5); TRISAbits.TRISA0 = 1;TRISAbits.TRISA1 = 1; Delay10TCYx(1);//fin 4 bit //************************************************* TRISAbits.TRISA0 = 0;TRISAbits.TRISA1 = 0; PORTAbits.RA0 = 0;PORTAbits.RA1 = 0; _asm nop nop nop nop _endasm//15us if(valeur&0x10) {TRISAbits.TRISA0 = 1;TRISAbits.TRISA1 = 1;} Delay10TCYx(5); TRISAbits.TRISA0 = 1;TRISAbits.TRISA1 = 1; Delay10TCYx(1);//fin 5 bit //************************************************* TRISAbits.TRISA0 = 0;TRISAbits.TRISA1 = 0; PORTAbits.RA0 = 0;PORTAbits.RA1 = 0; _asm nop nop nop nop _endasm//15us if(valeur&0x20) {TRISAbits.TRISA0 = 1;TRISAbits.TRISA1 = 1;} Delay10TCYx(5); TRISAbits.TRISA0 = 1;TRISAbits.TRISA1 = 1; Delay10TCYx(1);//fin 6 bit //*************************************************** TRISAbits.TRISA0 = 0;TRISAbits.TRISA1 = 0; PORTAbits.RA0 = 0;PORTAbits.RA1 = 0; _asm nop nop nop nop _endasm//15us if(valeur&0x40) {TRISAbits.TRISA0 = 1;TRISAbits.TRISA1 = 1;} Delay10TCYx(5); TRISAbits.TRISA0 = 1;TRISAbits.TRISA1 = 1; Delay10TCYx(1);//fin 7 bit //************************************************* TRISAbits.TRISA0 = 0;TRISAbits.TRISA1 = 0; PORTAbits.RA0 = 0;PORTAbits.RA1 = 0; _asm nop nop nop nop _endasm//15us if(valeur&0x80) {TRISAbits.TRISA0 = 1;TRISAbits.TRISA1 = 1;} Delay10TCYx(5); TRISAbits.TRISA0 = 1;TRISAbits.TRISA1 = 1; Delay10TCYx(1);;//fin 8 bit } //******************************************************* // fonction de lecture de la temperature sur RA0 ou RA1 *//on peut surement faire les deux en meme temps //******************************************************* unsigned char Sonde_Readbyte(char* buffer,unsigned char flag) { unsigned char valeur1=0;//byte sonde RA0 unsigned char valeur2=0;//byte sonde RA1 //lecture 1er bit**************************************** TRISAbits.TRISA0 = 0;TRISAbits.TRISA1 = 0; PORTAbits.RA0 = 0;PORTAbits.RA1 = 0; _asm nop nop nop nop nop nop nop nop nop nop nop nop nop nop _endasm//15us TRISAbits.TRISA0 = 1;TRISAbits.TRISA1 = 1; _asm nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop _endasm//15us if (PORTAbits.RA0) { valeur1=valeur1|0x01; } if (PORTAbits.RA1) { valeur2=valeur2|0x01; } Delay10TCYx(5); //lecture 2eme bit**************************************** TRISAbits.TRISA0 = 0;TRISAbits.TRISA1 = 0; PORTAbits.RA0 = 0;PORTAbits.RA1 = 0; _asm nop nop nop nop nop nop nop nop nop nop nop nop nop nop _endasm//15us TRISAbits.TRISA0 = 1;TRISAbits.TRISA1 = 1; _asm nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop _endasm//15us if (PORTAbits.RA0) { valeur1=valeur1|0x02; } if (PORTAbits.RA1) { valeur2=valeur2|0x02; } Delay10TCYx(5); //lecture 3eme bit**************************************** TRISAbits.TRISA0 = 0;TRISAbits.TRISA1 = 0; PORTAbits.RA0 = 0;PORTAbits.RA1 = 0; _asm nop nop nop nop nop nop nop nop nop nop nop nop nop nop _endasm//15us TRISAbits.TRISA0 = 1;TRISAbits.TRISA1 = 1; _asm nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop _endasm//15us if (PORTAbits.RA0) { valeur1=valeur1|0x04; } if (PORTAbits.RA1) { valeur2=valeur2|0x04; } Delay10TCYx(5); //lecture 4eme bit**************************************** TRISAbits.TRISA0 = 0;TRISAbits.TRISA1 = 0; PORTAbits.RA0 = 0;PORTAbits.RA1 = 0; _asm nop nop nop nop nop nop nop nop nop nop nop nop nop nop _endasm//15us TRISAbits.TRISA0 = 1;TRISAbits.TRISA1 = 1; _asm nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop _endasm//15us if (PORTAbits.RA0) { valeur1=valeur1|0x08; } if (PORTAbits.RA1) { valeur2=valeur2|0x08; } Delay10TCYx(5); //lecture 5eme bit**************************************** TRISAbits.TRISA0 = 0;TRISAbits.TRISA1 = 0; PORTAbits.RA0 = 0;PORTAbits.RA1 = 0; _asm nop nop nop nop nop nop nop nop nop nop nop nop nop nop _endasm//15us TRISAbits.TRISA0 = 1;TRISAbits.TRISA1 = 1; _asm nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop _endasm//15us if (PORTAbits.RA0) { valeur1=valeur1|0x10; } if (PORTAbits.RA1) { valeur2=valeur2|0x10; } Delay10TCYx(5); //lecture 6eme bit**************************************** TRISAbits.TRISA0 = 0;TRISAbits.TRISA1 = 0; PORTAbits.RA0 = 0;PORTAbits.RA1 = 0; _asm nop nop nop nop nop nop nop nop nop nop nop nop nop nop _endasm//15us TRISAbits.TRISA0 = 1;TRISAbits.TRISA1 = 1; _asm nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop _endasm//15us if (PORTAbits.RA0) { valeur1=valeur1|0x20; } if (PORTAbits.RA1) { valeur2=valeur2|0x20; } Delay10TCYx(5); //lecture 7eme bit**************************************** TRISAbits.TRISA0 = 0;TRISAbits.TRISA1 = 0; PORTAbits.RA0 = 0;PORTAbits.RA1 = 0; _asm nop nop nop nop nop nop nop nop nop nop nop nop nop nop _endasm//15us TRISAbits.TRISA0 = 1;TRISAbits.TRISA1 = 1; _asm nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop _endasm//15us if (PORTAbits.RA0) { valeur1=valeur1|0x40; } if (PORTAbits.RA1) { valeur2=valeur2|0x40; } Delay10TCYx(5); //lecture 8eme bit**************************************** TRISAbits.TRISA0 = 0;TRISAbits.TRISA1 = 0; PORTAbits.RA0 = 0;PORTAbits.RA1 = 0; _asm nop nop nop nop nop nop nop nop nop nop nop nop nop nop _endasm//15us TRISAbits.TRISA0 = 1;TRISAbits.TRISA1 = 1; _asm nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop _endasm//15us if (PORTAbits.RA0) { valeur1=valeur1|0x80; } if (PORTAbits.RA1) { valeur2=valeur2|0x80; } Delay10TCYx(5); //***************test octet L ou H avec le flag if (flag) {buffer[1]=valeur1;buffer[3]=valeur2;} if (!flag) {buffer[0]=valeur1;buffer[2]=valeur2;} } //****************************************************** //fonction de conversion octets L et H en string * //****************************************************** void Temp_String (char * buffer,unsigned char byteL,unsigned char byteH) { static unsigned int decimales;static unsigned char result; if((byteH&0xF8)==0xF8) { //la virgule decimales =(((0xFF-byteL)+1)&0x0F)*625; //les decimales result=(((0xFF-byteL)+1)>>4)|(((0xFF-byteH)&0x07)<<4); //digit2 = (((0xFF-byteH)&0x07)<<4); //result=digit|digit2; //en string //if (result>9)sprintf (buffer, "-%d.%3u C",result,decimales); //else sprintf (buffer, "-0%d.%3u C",result,decimales); buffer[0]=0x2D; buffer[1]=((result/10)+48); buffer[2]=((result-((buffer[1]-48)*10))+48); buffer[3]=0x2E; buffer[4]=((decimales/1000)+48); buffer[5]=(((decimales-((buffer[4]-48)*1000))/100)+48); buffer[6]=0x20; buffer[7]=0x43; } else { //la virgule decimales =(byteL&0x0F)*625; //les decimales //digit = (byteL>>4); //digit2 = ((byteH&0x07)<<4); result=(byteL>>4)|((byteH&0x07)<<4); //en string //if (result>9)sprintf (buffer, "+%d.%3u C",result,decimales); //else sprintf (buffer, "+0%d.%3u C",result,decimales); buffer[0]=0x2B; buffer[1]=((result/10)+48); buffer[2]=((result-((buffer[1]-48)*10))+48); buffer[3]=0x2E; buffer[4]=((decimales/1000)+48); buffer[5]=(((decimales-((buffer[4]-48)*1000))/100)+48); buffer[6]=0x20; buffer[7]=0x43; } }
Je suis completement perdu car ca n'a pas l'air difficile d'autant que la simulation isis fonctionne tres bien.
Je vous ai joint aussi les captures de l'oscillo pour vous montrer que tout semble bon.
On dirait que la sonde ne comprends pas les ordres 0xcc et 0x44 mais elle repond a 0xcc et 0xBE...
Enfin c'est navrant...pourquoi??? j'ai 10 sondes ici j'ai essayer avec les 10 et c'est pareil.
Alors pourriez vous m'aider..SVP.
merci de me faire partager votre exoperience sur le sujet car si il y avait un moyen de savoir si la sonde fonctionne bien lors de la conversion...
0xcc_0x44.JPG0xCC_0XBE.JPGresultat xFF xFF.JPG
-----