Bonjour,
Je n'arrive pas a recevoir d'info de la sonde de température sur une PICDEM2plus.
De ce que j'ai vu du schéma de cablage de la PICDEM2plus RoHS, le timer de la SHT est cablé sur le pin3 du PORTC et la ligne data sur le pin4.
Une autre ressource utile pour pouvoir vérifier les fonctions de bas niveau (transstart, connectionreset et read/write) : la datasheet des SHT
Mon problème est que la ligne data reste désespérément en état haut, la sonde SHT ne la modifie jamais.
Voici mon code très inspiré de çà.
La variable tmp me sert à forcer le rafraîchissement du PORTC dans le watch de mplab, elle sera donc supprimée du code de production.
Fichier sht.h :fichier main.cpp :Code:#ifndef SHT_H #define SHT_H /* user code must define a few constants defining PIC <-> SHT wirering For instance for PIC18F4520 on PICDEM2PLUS : #define SHT_CK_TRIS TRISCbits.TRISC3 #define SHT_CK_LAT LATCbits.LATC3 #define SHT_DATA_TRIS TRISCbits.TRISC4 #define SHT_DATA_LAT LATCbits.LATC4 #define SHT_DATA_PORT PORTCbits.RC4 */ #define SHT_MODE_TEMP 0 #define SHT_MODE_HUMI 1 #define SHT_noACK 0 #define SHT_ACK 1 //adr command r/w #define SHT_MEASURE_TEMP 0x03 //000 00011 #define SHT_MEASURE_HUMI 0x05 //000 00101 #define SHT_STATUS_REG_W 0x06 //000 00110 #define SHT_STATUS_REG_R 0x07 //000 00111 #define SHT_RESET 0x1e //000 11110 void sht_short_delay(void) { unsigned short i=0; while(i<2) ++i; } unsigned char sht_write_byte(const unsigned char value) { unsigned char i, error=0, tmp; SHT_DATA_TRIS = 0; tmp = PORTC; for (i=0x80; i>0; i/=2) { if(i & value) { SHT_DATA_LAT = 1; } else { SHT_DATA_LAT = 0; } SHT_CK_LAT = 1; tmp = PORTC; sht_short_delay(); SHT_CK_LAT = 0; tmp = PORTC; sht_short_delay(); } SHT_DATA_TRIS = 1; SHT_CK_LAT = 1; sht_short_delay(); tmp = PORTC; error = SHT_DATA_PORT; SHT_CK_LAT = 0; tmp = PORTC; sht_short_delay(); return error; } unsigned char sht_read_byte(const unsigned char ack) { unsigned char i,val=0, tmp; sht_short_delay(); for (i=0x80; i>0; i/=2) { SHT_CK_LAT = 1; sht_short_delay(); tmp = PORTC; if(SHT_DATA_PORT) val=(val | i); SHT_CK_LAT = 0; tmp = PORTC; sht_short_delay(); } SHT_DATA_TRIS = 0; SHT_DATA_LAT = !ack; SHT_CK_LAT = 1; tmp = PORTC; sht_short_delay(); SHT_CK_LAT = 0; tmp = PORTC; sht_short_delay(); SHT_DATA_TRIS = 1; return val; } void sht_transstart(void) { unsigned char tmp = PORTC; SHT_DATA_TRIS = 0; SHT_DATA_LAT = 1; SHT_CK_LAT = 1; tmp = PORTC; sht_short_delay(); SHT_DATA_LAT = 0; tmp = PORTC; sht_short_delay(); SHT_CK_LAT = 0; tmp = PORTC; sht_short_delay(); SHT_CK_LAT = 1; tmp = PORTC; sht_short_delay(); SHT_DATA_LAT = 1; tmp = PORTC; sht_short_delay(); SHT_CK_LAT = 0; tmp = PORTC; SHT_DATA_TRIS = 1; } void sht_connectionreset(void) { unsigned char i; unsigned char tmp = PORTC; SHT_CK_TRIS = 0; SHT_DATA_TRIS = 0; SHT_CK_LAT = 0; SHT_DATA_LAT = 1; tmp = PORTC; sht_short_delay(); for(i=0; i<9; i++) { SHT_CK_LAT = 1; tmp = PORTC; sht_short_delay(); SHT_CK_LAT = 0; tmp = PORTC; sht_short_delay(); } SHT_DATA_TRIS = 1; sht_transstart(); } char sht_softreset(void) { unsigned char error; sht_connectionreset(); error = sht_write_byte(SHT_RESET); return error; } char sht_read_statusreg(unsigned char * p_value, unsigned char * p_checksum) { unsigned char error=0; sht_transstart(); error = sht_write_byte(SHT_STATUS_REG_R); *p_value = sht_read_byte(SHT_ACK); *p_checksum = sht_read_byte(SHT_noACK); return error; } char sht_write_statusreg(unsigned char * p_value) { unsigned char error=0; sht_transstart(); error += sht_write_byte(SHT_STATUS_REG_W); error += sht_write_byte(*p_value); return error; } char sht_measure(unsigned int * p_value, unsigned char * p_checksum, unsigned char mode) { unsigned char error, tmp; unsigned int i; sht_transstart(); switch(mode){ case SHT_MODE_TEMP : error = sht_write_byte(SHT_MEASURE_TEMP); break; case SHT_MODE_HUMI : error = sht_write_byte(SHT_MEASURE_HUMI); break; default : error = 0; break; } for(i=0; i<65535; i++) { tmp = PORTC; if(SHT_DATA_PORT == 0) break; //wait until sensor has finished the measurement } if(SHT_DATA_PORT) error += 1; //timeout is reached *((char *)p_value) = sht_read_byte(SHT_ACK); //read the first byte (MSB) *(((char *)p_value)+1) = sht_read_byte(SHT_ACK); //read the second byte (LSB) *p_checksum = sht_read_byte(SHT_noACK); //read checksum return error; } #endif SHT_HSi quelqu'un a une piste, je la prendrai avec plaisirCode:#include <p18cxxx.h> #define SHT_CK_TRIS TRISCbits.TRISC3 #define SHT_CK_LAT LATCbits.LATC3 #define SHT_DATA_TRIS TRISCbits.TRISC4 #define SHT_DATA_LAT LATCbits.LATC4 #define SHT_DATA_PORT PORTCbits.RC4 #include <sht.h> #pragma config PWRT = OFF // PowerUp timer ON #pragma config WDT = OFF // watchdog timer off #pragma config LVP = OFF // low level voltage progamming OFF #pragma config DEBUG = ON // Debug ON #pragma config PBADEN = OFF // PORTB<4:0> as digital IO on reset #pragma config STVREN = ON // Stack overflow reset ON #pragma config MCLRE = ON // Enable MCLRE for reset function void main(void) { unsigned char error, checksum; unsigned int humi_val, temp_val, i; sht_connectionreset(); while(1) { error = sht_measure(&humi_val, &checksum, SHT_MODE_TEMP); error += sht_measure(&temp_val, &checksum, SHT_MODE_HUMI); if(error!=0) sht_connectionreset(); else { i = 0; while(i<40000) ++i; } } }
-----