Hello,
Je me bats depuis déjà quelques temps avec mon 18f45k20 et ma sonde de température (DS1721).
Le code arrive enfin à s’exécuter entièrement mais les 2 octets récupérés valent chacun "-1".
Pour la gestion de l'I2C j'utilise la librairie fournie par Microchip.
Suite aux problèmes sur la version de mon pic (0x18) : http://ww1.microchip.com/downloads/e...Doc/80425G.pdf, j'ai décidé de rajouter une tempo après chaque "StopI2C()".
Le pic fonctionne avec un quartz de 10Mhz.
Je suis débutant, les erreurs "bêtes" sont donc possibles
(init des registres du pic notamment)
Pour info voici le code utilisé :
Code:#pragma config FOSC = XT, FCMEN = OFF, IESO = OFF #include "p18f45k20.h" #include "delays.h" #include "xlcd.h" #include "stdlib.h" #include "Config.h" #include "i2c.h" //#define CLOCK_FREQUENCY 10000000 // TCY = 4/10 = 2/5µs = 0,4µs #define SDA_PIN LATCbits.LATC4 #define SCL_PIN LATCbits.LATC3 #define TRIS_SDA TRISCbits.TRISC4 #define TRIS_SCL TRISCbits.TRISC3 // Function prototypes void init(void); //Function to initalize oscillator registers void init(void) { OSCCONbits.SCS0 = 0; // Internal oscillator block (P.29) OSCCONbits.SCS1 = 0; // Internal oscillator block (P.29) OSCCONbits.OSTS = 1; // device is running from the clock defined by FOSC<2:0> (P.29) ADCON0bits.ADON = 0; // ADC is disabled and consumes no operating current (P.271) SSPCON1bits.SSPM0 = 0; SSPCON1bits.SSPM1 = 0; SSPCON1bits.SSPM2 = 0; SSPCON1bits.SSPM3 = 0; SSPCON1bits.SSPEN = 1; } // Main function void main (void) { char temp2[5]; char temperatureHI = 0b01101010; char temperatureLO = 0b01101010; TRIS_SDA = 1; TRIS_SCL = 1; SDA_PIN = 0; SCL_PIN = 0; //Function to initalize oscillator registers init(); // LCD OpenXLCD(EIGHT_BIT & LINES_5X7); // Init the LCD Display SetDDRamAddr(0x00); putrsXLCD("ligne1"); SetDDRamAddr(0x40); //line 2 putrsXLCD("ligne2"); SetDDRamAddr(0x14); //line 3 putrsXLCD("ligne3"); SetDDRamAddr(0x54); //line 4 // DS1721 (I2C) // Init & conf OpenI2C(MASTER, SLEW_OFF);// Initialize I2C module SSPADD = 0x63; StartI2C(); IdleI2C(); WriteI2C(0x90); //where 9E is the address with the last bit as write IdleI2C(); WriteI2C(0xAC); // Command byte IdleI2C(); WriteI2C(0x02); // Data byte IdleI2C(); StopI2C(); // Start conversion Delay10KTCYx(5); StartI2C(); IdleI2C(); WriteI2C(0x90); // sends address to the IdleI2C(); WriteI2C(0x51); // send command to begin temperature conversions IdleI2C(); StopI2C(); // Read temp Delay10KTCYx(5); StartI2C(); // start I2C communication IdleI2C(); WriteI2C(0x90); // address the chip IdleI2C(); WriteI2C(0xAA); // access the temperature register IdleI2C(); StopI2C(); // Read temp Delay10KTCYx(5); StartI2C(); // Initiate a RESTART command IdleI2C(); WriteI2C(0x91); // address device w/read IdleI2C(); temperatureHI = ReadI2C(); // Returns the MSB byte and stores it in 'temperatureHI' IdleI2C(); AckI2C(); IdleI2C(); temperatureLO = ReadI2C(); // returns the LSB of the temperature IdleI2C(); NotAckI2C(); // send a not-acknowledge IdleI2C(); StopI2C(); // stop all I2C communication SetDDRamAddr(0x54); putrsXLCD("T:"); btoa(temperatureHI, temp2); putsXLCD(temp2); btoa(temperatureLO, temp2); putsXLCD(temp2); while(1) { } }
-----