voici mon programme au complet et voila le pc de supervision doit envoyer une demande et moi je dois lui envoyer la température et humidité comment faire
Code:*********************************************************/ #include <18F4520.h> #use delay(clock=20M) #include "LCDTEST.C" #include <stdio.h> #include <math.h> #fuses HS,NOLVP,NOWDT,NOPUT #use RS232 (baud = 9600,xmit = PIN_C6, rcv = PIN_C7) enum {TEMP,HUMI}; #define MEASURE_TEMP 0x03 #define MEASURE_HUMI 0x05 //#use I2C (master,sda=PIN_C4, scl=PIN_C3) #bit sck=0xf82.3 #bit data=0xf82.4 #bit sync=0xf82.5 #bit SENS_DATA = 0xf94.4 #define ack 1 #define noack 0 #define d1 -40.1 #define d2 0.01 #define COMMANDE_TEMP 0b00000011 #define COMMANDE_HUMID 0b00000101 #define c1 -2.0468 #define c2 0.0367 #define c3 -1.5955e-6 typedef char bool; char temperature; char hygrometrie; char buffer1[8]; bool fin = false; char carac =0; char buffer[8]; char pos=0x00; char trame; #int_RDA MODBUS_ReceptionCaractere() { carac = getchar (); if ( carac == 8) { buffer[ pos + 1]= '\n'; pos= 0x00; strcpy ( buffer1, buffer); fin = true; } else { pos++; } } void analysetrame ( char P_Anal ) { char Adresse; char cf; char taille[2]; char message[4]; switch (pos) { case 0: if (Adresse == 1) { pos++ ; } else { pos= 0x00; } break; case 1: if (cf != 0x03) { pos= 0x00; } break; case 2: if (taille != taille ) { pos = 0x00; } } } void extraireDonnees ( char * trame) { trame [4]; trame [6]; } void s_transstart(void) { sck=1; delay_us(5); data=0; delay_us(1); sck=0; delay_us(1); sck=1; delay_us(1); data=1; delay_us(1); sck=0; delay_us(1); data=0; delay_us(5); } char s_write_byte(unsigned char value) { unsigned char i,error=0; SENS_DATA=0; // data en sortie for( i=0x80;i>0;i/=2) { if ( i & value )data=1; else data =0; delay_us (1); sck=1; delay_us(1); sck=0; delay_us (1); } SENS_DATA = 1; // data en entrée pour reception ACK delay_us (3); sck=1; delay_us (1); error=data; delay_us (1); sck=0; return error; } char s_read_byte(unsigned char P_ack) //ack = 0 read val et ack = 1 pour chksum { unsigned char i; unsigned char value =0; SENS_DATA = 1; // data en entrée pour reception sck = 0; delay_us (5); for(i=0x80;i>0;i/=2) { sck =1; delay_us (1); if (data) value =( value|i); delay_us (1); sck =0; } //emission ack delay_us (1); SENS_DATA=0; // data en sortie delay_us (1); data=1; delay_us (3); data=P_ack; // 0 pour lecture val et 1 pour chksum delay_us (1); sck=1; delay_us (1); sck=0; delay_us (1); data=1; return value; } float Convert_Temp(int16 P_SOT) { float L_temp = 0.0f; float d2xSOT; d2xSOT = P_SOT * d2; L_temp = d1 + d2xSOT; return L_temp; } float Convert_Humidite(int16 P_SOH) { float L_humid_lin = 0.0f; float C2xSOH; float C3xSOHcarre; C2xSOH = P_SOH * c2; C3xSOHcarre = c3*P_SOH*P_SOH; L_humid_lin = c1 + C2xSOH + C3xSOHcarre; if(L_humid_lin<0) { L_humid_lin=0; } if(L_humid_lin>100) { L_humid_lin=100; } return L_Humid_lin; } void init_bus_repos() { //***** mise en place signaux au repos SENS_DATA=0; // data en sortie data=1; sck=0; delay_ms(100); } char emettre_commande(unsigned char P_mode) { char L_ret; L_ret = s_write_byte(P_mode); return L_ret; } void attendre_fin_mesure() { unsigned char fin; SENS_DATA=1; //data en entree delay_ms(80); do { fin = data; }while(data &0x00010000 != 0); delay_ms(1); } float Acquerir_temperature() { unsigned char L_Msb_Temp; unsigned char L_Lsb_Temp; unsigned char L_ChkSum; float L_temp = 0.0f; int16 L_SOT; L_Msb_Temp= s_read_byte(0); L_Lsb_Temp= s_read_byte(0); L_ChkSum= s_read_byte(1); // printf(lcd_putc,"MSB:%0x LSB:%0x\n",L_Msb_Temp,L_Lsb_Temp); L_SOT = L_Msb_Temp * 256 + L_Lsb_Temp; delay_ms(1); L_Temp = Convert_Temp(L_SOT); return L_temp; } float acquerir_humidite_lin() { float L_humid=0.0f; unsigned char L_Msb_Umid; unsigned char L_Lsb_Umid; unsigned char L_ChkSum; unsigned int16 L_SOH; float L_Humid_lin=0.0f; L_Msb_Umid= s_read_byte(0); L_Lsb_Umid= s_read_byte(0); L_ChkSum= s_read_byte(1); // printf (lcd_putc,"M=%x L=%x \n",L_Msb_Umid,L_Lsb_Umid); L_SOH = L_Msb_Umid *256 + L_Lsb_Umid; L_Humid_lin = Convert_Humidite(L_SOH); return L_humid_lin; } void main() { //température & hygrométrie float L_Humid; float L_Humid_lin; float L_temp = 0.0f; //transfert char temperature; char hygrometrie; char donnees; char chaine; char trame_utile[]; char donnes[2]; enable_interrupts(GLOBAL); enable_interrupts(INT_RDA); lcd_init(); set_tris_c(0b11000111); //c3 et c4 en sortie // c5 en sortie pour synchro sync =0; init_bus_repos(); //***************** while(1) { if ( fin != true); { /***************************************************************** température & hygrométrie ******************************************************************/ // emision start s_transstart(); sync= 1; // emission commande mesure temp emettre_commande(COMMANDE_TEMP); sync = 0; // attente mesure ok attendre_fin_mesure(); L_Temp = Acquerir_temperature(); //printf (lcd_putc,"\fT= %5.2f\n",L_Temp); printf ("\fT= %5.2f\n",L_Temp); delay_ms(2000); s_transstart(); sync= 1; // emission commande mesure temp emettre_commande(COMMANDE_HUMID); sync = 0; // attente mesure ok attendre_fin_mesure(); L_Humid_lin=acquerir_humidite_lin(); //printf (lcd_putc,"h= %5.2f\n",L_Humid_lin); printf ("h= %5.2f\n",L_Humid_lin); delay_ms(2000); if (strcmp (chaine,donnees)==0x00) { extraireDonnees(trame_utile); // température strncpy ( trame_utile,temperature ,1); //hygrométrie strncpy (trame_utile,hygrometrie , 2); } } } }
-----