#include /* for EnableInterrupts macro */ #include "derivative.h" /* include peripheral declarations */ #define DQ PTBD_PTBD5 #define _DQ PTBDD_PTBDD5 //Cada 1mSeg void init_MTIM(void){ MTIMMOD = 250; //(32 ÷ BUS)*250=0.001 MTIMCLK = 0x05; //BUS Clock MTIM clock source ÷ 32 MTIMSC = 0x40; //Interrupcion Encendida, MTIM On } void init_CLK(void){ ICSTRM = *(unsigned char*far)0xFFAF; /* Initialize ICSTRM register from a non volatile memory */ ICSSC = *(unsigned char*far)0xFFAE; /* Initialize ICSSC register from a non volatile memory */ } // DELAY - with an 8.0MHz bus. void delay(unsigned int n) { //2uSeg Aprox n=n/2; asm { LDHX (n) LOOP1: AIX #-1 ;[2] NOP NOP CPHX #0 ;[3] BNE LOOP1 ;[3] } } ////////////////////////////////////////////////////////////////////////////// // OW_RESET - performs a reset on the one-wire bus and // returns the presence detect. unsigned char ow_reset(void){ unsigned char presence; DQ = 0; // ponemos el pin a bajo _DQ = 1; // ponemos el pin como salida delay(480); // esperamos aprox 480uS _DQ = 0; // ponemos el pin como entrada delay(60); // esperamos el tiempo minimo de presencia presence = DQ; // leemos el pin delay(420); // esperamos el tiempo de slot return(presence); // devolvemos si hay o no presencia } // 0=presence, 1 = no part ////////////////////////////////////////////////////////////////////////////// // READ_BIT - reads a bit from the one-wire bus. The delay // required for a read is 15us, // unsigned char read_bit(void){ DQ = 0; // ponemos el pin a bajo _DQ = 1; // ponemos el pin como salida delay(2); _DQ = 0; // ponemos el pin como entrada delay(16); // se necesita 15uS el llamado se lleva 6 return(DQ); // return value of DQ line } ////////////////////////////////////////////////////////////////////////////// // WRITE_BIT - writes a bit to the one-wire bus, passed in bitval. // void write_bit(char bitval){ DQ = 0; // ponemos el pin a bajo _DQ = 1; // ponemos el pin como salida delay(2); if(bitval==1) DQ = 1; // return DQ high if write 1 delay(60); // dejamos el tiempo de slot _DQ = 0; // ponemos el pin como entrada } ////////////////////////////////////////////////////////////////////////////// // READ_BYTE - reads a byte from the one-wire bus. // unsigned char read_byte(void){ unsigned char i; unsigned char value = 0; for (i=0;i<8;i++){ if(read_bit()) value|=0x01<>i; // shifts val right 'i' spaces temp &= 0x01; // copy that bit to temp write_bit(temp); // write bit in temp into } delay(100); } static char get[10]; static int k; static int temp=0; #define DS18B20_PRESENCIA 0 #define DS18B20_CONVERTIR 1 #define DS18B20_MEDIR 2 unsigned char DS18B20_state=0; unsigned short DS18B20_delay=0; void tic_DS18B20_1mS(void){ //Si DS18B20_delay vale mas que cero restale 1 if(DS18B20_delay)DS18B20_delay--; } void DS18B20_Step(void) { if(DS18B20_delay){ //Si esta esperando me voy return; } switch(DS18B20_state){ case DS18B20_PRESENCIA: if(!ow_reset()){ //Si hay sensor vamos a convertir DS18B20_state=DS18B20_CONVERTIR; }else{ //Si no hay sensor me fijo de nuevo en 1Seg DS18B20_delay=1000; DS18B20_state=DS18B20_PRESENCIA; } break; case DS18B20_CONVERTIR: write_byte(0xCC); //Le hablamos a todas los sensores conectados write_byte(0x44); //Empezamos a convertir //Por defecto esta configurado en 12 bits y tarda 750mSeg en convertir DS18B20_delay=750; DS18B20_state=DS18B20_MEDIR; //Si estamos en modo parasito hay que poner en ALTO el pin DQ = 1; //ponemos el pin a alto _DQ = 1; //ponemos el pin como salida break; case DS18B20_MEDIR: if(!ow_reset()){ write_byte(0xCC); //Le hablamos a todas los sensores conectados write_byte(0xBE); //Leemos el Scratch Pad for (k=0;k<9;k++){ //nos traemos todos los bytes get[k]=read_byte(); } //Aca habria que verificar el CRC //.. //Temperatura viaja en el byte 0 y 1 //nos quedamos solo con la parte entera temp=get[1]<<8; temp|=get[0]; temp=temp>>4; if(get[1]&0x80){ //es negativo temp|=0xF000; } //Volvemos a leer la temp en 1Seg DS18B20_delay=1000; DS18B20_state=DS18B20_PRESENCIA; }else{ //Volvemos a leer la temp en 1Seg DS18B20_delay=1000; DS18B20_state=DS18B20_PRESENCIA; } break; } } void main(void) { SOPT1_COPT = 0; // deshabilita watchdog init_CLK(); //fBus 8MHz init_MTIM(); //Interrupcion cada 1mSeg EnableInterrupts; for(;;) { __RESET_WATCHDOG(); DS18B20_Step(); } } interrupt VectorNumber_Vmtim void ISR_Vmtim(void){ (void)MTIMSC; MTIMSC_TOF = 0; tic_DS18B20_1mS(); //TIC de la maquina de estados del sensor de TEMP }