/****************************************************************/ /* */ /* Remote Environmental Monitoring and Control via Internet */ /* Daniel John McAuley */ /* Level 4 1999/2000 */ /* */ /* NAME: control.c */ /* FUNCTION: Initial test program for establishing */ /* proper operation, communication and control */ /* of the microcontroller */ /* DEVICE: For use with Microchip 16F876. */ /* */ /* NOTES: Compiler PIC C does not seem to like comments */ /* on the same line as the device specification */ /* */ /* Use hyperterminal 9600,8,1,N for debug comms */ /* */ /* TODO Use interrupt serial comms */ /* Error handling */ /* Use watchdog */ /* */ /* NOTE Code could do with a clean-up! */ /* */ /* */ /****************************************************************/ #include #include #include <16c76.h> #device PIC16C76 #use delay(clock = 4000000) /* Clock speed */ #use fast_io(A) /* DEFINE ADDRESSES*/ #byte PORTA = 5 #byte PORTB = 6 #byte PORTC = 7 /* PortC address 7h */ #byte RCSTA = 0X18 /* Receive Status and Control Reg */ #byte TXSTA = 0X98 /* Transmit Status and Control Reg */ #byte SPBRG = 0X99 /* Serial Port Baud Rate Gen. Reg */ #byte TXREG = 0X19 /* USART Transmit data register */ #byte RCREG = 0x1A /* USART Receive data register */ #byte PIR1 = 0X0C /* PIR1 register - used for tx and rx buffer status */ #define TXIF 4 /* USART Transmit Interrupt Flag - Bit 4 of PIR1 */ #define RCIF 5 /* USART Receive Interrupt Flag - Bit 5 of PIR1 */ #define DELAY_TIME 1 /* Specify a delay time of 5 */ #define MESSAGE_SIZE 16 #define NO_COMMANDS 16 /* Specify the number of user commands */ #define CMD_SIZE 10 /* Specify the fixed size of data field */ #define rx_ready bit_test(PIR1,RCIF) /* Handy function alias */ /* CONSTANT MESSAGES */ char const msg[110] = {'N','E','T','C','O','N',' ','T','e','c','h','n','o','l','o','g','y','.','.','.','.'}; /* GLOBALS */ static char my_address[2] = {'0','1'}; static char resp_header[4] = {'0','0','0','1'}; static char mess_buff[MESSAGE_SIZE]; static char resp_buff[MESSAGE_SIZE]; static char dest_address[2]; static char src_address[2]; static char data[10]; static char temp[2]; /* FLAGS */ int message_received = 0; /* FUNCTION PROTOTYPES */ int init_USART(void); void poll_and_receive(void); void gen_output_buff(char *buffer, int buff_size); void extract_address(char *buffer); void extract_data(char *buffer); int str_compare(char *string1,char *string2, int size); int id_fn_do_cmd(void); void flush_buffer(char * buffer, int size); // DS1820 1-wire prototypes void _1w_init(int sensor); int _1w_in_byte(int sensor); void _1w_out_byte(int d, int sensor); void _1w_pin_hi(int sensor); void _1w_pin_low(int sensor); #separate void _Read_temp(void); /* MAIN */ main() { int cmd_code,i=0; signed int result = -1; char temp_buff[10]; char no_msg[16] = {'M','s','g',' ','n','o','t',' ','f','o','r',' ','m','e','\r'}; init_USART(); /* Initialise the USART */ for (i=0; i<21;i++) /* Output the greeting */ { TXREG = msg[i]; delay_ms(DELAY_TIME); } i=0; while(1){ /* Loop */ poll_and_receive(); /* Poll the serial receive reg to determine when char rx */ if (message_received == 1 ){ /* If a message is received */ extract_address(mess_buff); /* Extract the destination and source addresses */ if((dest_address[0] == my_address[0]) && (dest_address[1] == my_address[1])){ extract_data(mess_buff); message_received = 0; id_fn_do_cmd(); /* Call the main identification and action fn */ } else{ gen_output_buff(no_msg,16); /* Otherwise output that message not for me! */ message_received = 0; } } } } /************************************************************************************************/ /* */ /* FUNCTIONS */ /* */ /************************************************************************************************/ /************************************************************************************************/ init_USART() { PORTC = 0; SET_TRIS_C(0Xc0); PORTB = 0; SET_TRIS_B(0xF); PORTA = 0; SET_TRIS_A(0x00); RCSTA = 0X90; TXSTA = 0X24; SPBRG = 25; return(0); } /************************************************************************************************/ poll_and_receive() { int i,ch; if (rx_ready){ /* Wait until there is a char in receive buffer */ ch = RCREG; /* Get character from receive reg */ switch (ch){ /* Look at received character */ case '*': /* If it is the chosen SOT char '*' in this case */ { bit_set(PORTC,0); /* Turn Rx LED ON */ mess_buff[0] = ch; /* Save SOT in mess_rx array */ /* save the rest of the incoming fixed-length message */ for(i=1;i>1) | 0x80; // least sig bit first } else { i_byte=i_byte >> 1; } delay_us(60); } return(i_byte); } // write a byte to the DS1820 void _1w_out_byte(int d, int sensor) { int n, mask; mask = 0xff & (~(0x01<>1; } } // Put DS1820 DQ pin high void _1w_pin_hi(int sensor) { SET_TRIS_A(0xff); } // Put DS1820 DQ pin low void _1w_pin_low(int sensor) { PORTA = 0x00; SET_TRIS_A(0xff & (~(0x01 << sensor))); } // Perform a read temperature process #separate void _Read_temp(void) { int lsb,n,buff[2],sensor=0; // Sensor is 0 for PORTA Pin0 as required _1w_init(sensor); _1w_out_byte(0xcc, sensor); // skip ROM _1w_out_byte(0x44, sensor); // perform temperature conversion while(_1w_in_byte(sensor) != 0xFF) { /* Wait here til temp conversion done */ } _1w_init(sensor); _1w_out_byte(0xcc, sensor); // skip ROM _1w_out_byte(0xbe, sensor); // read the result for (n=0; n<2; n++) { temp[n]=_1w_in_byte(sensor); //Store in temp buffer } _1w_init(sensor); } // END