///////////////////////////////////////////////////////////////////////// #include <12F683.h> #include #define BZERO(d,n) memset(d,0,n) #define BCMP(s,d,n) memcmp(d,s,n) #define BCOPY(s,d,n) memcpy(d,s,n) #define TALLOC(n,t,g) (t*)malloc((n)*sizeof(t)) #fuses INTRC_IO,NOWDT,NOPROTECT,NOBROWNOUT,NOMCLR,PUT #use delay(clock=4000000) #define OWPIN_I PIN_A2 #define LED PIN_A4 // prototypes int init(); void onewire_reset_I(); void onewire_write_I(int data); int onewire_read_I(); float ds1820_read_I(); BYTE gethex1(); // from CSS BYTE gethex(); // from CSS // console functions #use rs232(stream=console,baud=9600,parity=N,bits=8,XMIT=PIN_A0,RCV=PIN_A1) //globals short timeout_error; int duty=0; float temp=0; int pollsecs=3; // interrupt routine #int_rtcc void rtccinterrupt() { // hits this every ~~~.1 sec? if ((duty*.08)>pollsecs) { duty=0; temp = ds1820_read_I(); fprintf(console,"temp\t%3.1fF\t%3.1fC\r\n", (temp*9/5)+32, temp); } duty++; return; } // Main void main() { long i=0; char cmd; init(); // fprintf(console,"up\r\n"); delay_ms(1000); output_low(LED); enable_interrupts(INT_TIMER0); // enables the timer0 interrupt enable_interrupts(GLOBAL); // enables the timer0 interrupt setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256|RTCC_8_BIT); // set_timer0(0); //this sets timer0 register to 0 while (true) { fprintf(console,"> "); cmd = getchar(console); disable_interrupts(INT_TIMER0); switch (cmd) { case 'p': { output_high(LED); fprintf(console,"New secs: "); pollsecs = gethex(); fprintf(console,"\r\nSecs: %d\r\n", pollsecs); output_low(LED); break; } case '+': { output_high(LED); break; } case '-': { output_low(LED); break; } default: { fprintf(console,"%c\r\n", cmd); break; } } enable_interrupts(INT_TIMER0); } } int init() { // output_high(LED_R); switch ( restart_cause() ) { case NORMAL_POWER_UP: { // fprintf(console,"\r\nNormal power up!\r\n"); // output_low(LED_R); break; } default: { // fprintf(console,"UNKNOWN RESTART!!\n"); break; } } setup_adc_ports(NO_ANALOGS); setup_adc(ADC_OFF); // setup_psp(PSP_DISABLED); // setup_spi(FALSE); // setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); // setup_timer_1(T1_DISABLED); // setup_timer_2(T2_DISABLED,0,1); // setup_comparator(NC_NC_NC_NC); // setup_vref(VREF_LOW|-2); return 0; } /*******************1-wire communication functions********************/ /************onewire_reset*************************************************/ /*This function initiates the 1wire bus */ /* */ /*PARAMETERS: */ /*RETURNS: */ /*********************************************************************/ void onewire_reset_I() // OK if just using a single permanently connected device { output_low(OWPIN_I); delay_us( 500 ); // pull 1-wire low for reset pulse output_float(OWPIN_I); // float 1-wire high delay_us( 500 ); // wait-out remaining initialisation window. output_float(OWPIN_I); } /*********************** onewire_write() ********************************/ /*This function writes a byte to the sensor.*/ /* */ /*Parameters: byte - the byte to be written to the 1-wire */ /*Returns: */ /*********************************************************************/ void onewire_write_I(int data) { int count; for (count=0; count<8; ++count) { output_low(OWPIN_I); delay_us( 2 ); // pull 1-wire low to initiate write time-slot. output_bit(OWPIN_I,shift_right(&data,1,0)); // set output bit on 1-wire delay_us( 60 ); // wait until end of write slot. output_float(OWPIN_I); // set 1-wire high again, delay_us( 2 ); // for more than 1us minimum. } } /*********************** read1wire() *********************************/ /*This function reads the 8 -bit data via the 1-wire sensor. */ /* */ /*Parameters: */ /*Returns: 8-bit (1-byte) data from sensor */ /*********************************************************************/ int onewire_read_I() { int count, data; for (count=0; count<8; ++count) { output_low(OWPIN_I); delay_us( 2 ); // pull 1-wire low to initiate read time-slot. output_float(OWPIN_I); // now let 1-wire float high, delay_us( 8 ); // let device state stabilise, shift_right(&data,1,input(OWPIN_I)); // and load result. delay_us( 120 ); // wait until end of read slot. } return( data ); } float ds1820_read_I() { int8 busy=0, temp1, temp2; signed int16 temp3; float result; onewire_reset_I(); onewire_write_I(0xCC); onewire_write_I(0x44); while (busy == 0) busy = onewire_read_I(); onewire_reset_I(); onewire_write_I(0xCC); onewire_write_I(0xBE); temp1 = onewire_read_I(); temp2 = onewire_read_I(); temp3 = make16(temp2, temp1); //result = (float) temp3 / 2.0; //Calculation for DS18S20 with 0.5 deg C resolution result = (float) temp3 / 16.0; //Calculation for DS18B20 with 0.1 deg C resolution delay_ms(200); return(result); } BYTE gethex1() { char digit; digit = getc(); putc(digit); if(digit<='9') { return(digit-'0'); } else { return((toupper(digit)-'A')+10); } } BYTE gethex() { int lo,hi; hi = gethex1(); lo = gethex1(); if(lo==0xdd) { return(hi); } else { return( hi*16+lo ); } }