//--------------------------------------------------------------------------- // Attempt to open a com port. Keep the handle in ComID. // Set the starting baud rate to 9600. // // 'portnum' - number 0 to MAX_PORTNUM-1. This number provided will // be used to indicate the port number desired when calling // all other functions in this library. // // 'port_zstr' - zero terminate port name. NOT USED. // // // Returns: TRUE(1) - success, COM port opened // FALSE(0) - failure, could not open specified port // SMALLINT OpenCOM(int portnum, char *port_zstr) { port_zstr = 0; //to silence compiler if(portnum==0) serial0_init(BAUD9600_TIMER_RELOAD_VALUE); else serial1_init(BAUD9600_TIMER_RELOAD_VALUE); FlushCOM(portnum); return TRUE; }
//--------------------------------------------------------------------------- // Attempt to open a com port. Keep the handle in ComID. // Set the starting baud rate to 9600. // // 'port_zstr' - zero terminate port name. // // Returns: valid handle, or -1 if an error occurred // SMALLINT OpenCOM(int portnum, char *port_zstr) { int actualPortnum; if(port_zstr) { if (strcmp(port_zstr,DS400_SERIAL0_STRING) == 0) actualPortnum = DS400_SERIAL0; else if (strcmp(port_zstr,DS400_SERIAL1_STRING) == 0) actualPortnum = DS400_SERIAL1; else if (strcmp(port_zstr,DS400_SERIAL2_STRING) == 0) actualPortnum = DS400_SERIAL2; else return FALSE; } else actualPortnum = portnum; // attempt to open the communications port port_handle[portnum] = actualPortnum; switch (actualPortnum) { case DS400_SERIAL0: serial0_init(BAUD9600_TIMER_RELOAD_VALUE); break; case DS400_SERIAL1: serial1_init(BAUD9600_TIMER_RELOAD_VALUE); break; case DS400_SERIAL2: serial2_init(BAUD9600_TIMER_RELOAD_VALUE); break; } FlushCOM(portnum); return TRUE; }
int main(void) { //variable declarations static uint32_t current_ms = 0; static uint32_t last_send_ms = 0; static uint8_t serial_fsm_state = 0; static uint8_t serial_byte_in = 0; static uint8_t parameter_in_1 = 0; static uint8_t parameter_in_2 = 0; static uint8_t parameter_in_3 = 0; static uint8_t temp_parameter_in_1 = 0; static uint8_t temp_parameter_in_2 = 0; static uint8_t temp_parameter_in_3 = 0; //initialisation section, runs once serial0_init(); //initialise serial subsystem serial2_init(); //initialise serial subsystem _delay_ms(10); //short delay adc_init(); //initialse ADC lcd_init(); //initialise milliseconds_init(); //initialise timer3 to track milliseconds _delay_ms(20); //main loop while(1) { current_ms = milliseconds; //sending section if(current_ms-last_send_ms >= 100) //sending rate controlled here { last_send_ms = current_ms; serial2_write_byte(255); //send start byte serial2_write_byte((uint32_t)adc_read(0)*253/1023); //send first parameter serial2_write_byte((uint32_t)adc_read(1)*253/1023); //send second parameter serial2_write_byte((uint32_t)adc_read(14)*253/1023); //send third parameter serial2_write_byte((uint32_t)adc_read(15)*253/1023); //send fourth parameter serial2_write_byte(0); //send fifth parameter serial2_write_byte(254); //send stop byte } //receiving section if(UCSR2A&(1<<RXC2)) //if new serial byte has arrived { serial_byte_in = UDR2; //move serial byte into variable switch(serial_fsm_state) //switch by the current state { case 0: //do nothing, if check after switch case will find start byte and set serial_fsm_state to 1 break; case 1: //waiting for first parameter temp_parameter_in_1 = serial_byte_in; serial_fsm_state++; break; case 2: //waiting for second parameter temp_parameter_in_2 = serial_byte_in; serial_fsm_state++; break; case 3: //waiting for third parameter temp_parameter_in_3 = serial_byte_in; serial_fsm_state++; break; case 4: //waiting for stop byte if(serial_byte_in == 254) //stop byte { parameter_in_1 = temp_parameter_in_1; parameter_in_2 = temp_parameter_in_2; parameter_in_3 = temp_parameter_in_3; sprintf(serial_string, "1:%d, 2:%d, 3:%d\n", parameter_in_1, parameter_in_2, parameter_in_3); serial0_print_string(serial_string); //update controller based on new parameters here if you only want to run it after a new message } // if the stop byte is not received, there is an error, so no commands are implemented serial_fsm_state = 0; //do nothing next time except check for start byte (below) break; } if(serial_byte_in == 255) //if start byte is received { serial_fsm_state=1; //reset on 255 } } } return(1); } //end main
//---------------------------------------------------------------------- // Main for tstfind // void main(void) { uchar rslt; int cnt; //use port for 1-wire uchar portnum = ONEWIRE_P; //initialize I/O port #if STDOUT_P==0 serial0_init(BAUD9600_TIMER_RELOAD_VALUE); #else serial1_init(BAUD9600_TIMER_RELOAD_VALUE); #endif printf("Beginning tstfindm\r\n"); // attempt to acquire the 1-Wire Net if (!owAcquire(portnum,NULL)) { printf("owAcquire failed\r\n"); while(owHasErrors()) printf(" - Error %d\r\n", owGetErrorNum()); return; } //---------------------------------------- // Introduction printf("\r\n/----------------\r\n"); printf(" All iButtons.\r\n\r\n"); do { printf("-- Start\r\n"); cnt = 0; // find the first device (all devices not just alarming) rslt = owFirst(portnum, TRUE, FALSE); while (rslt) { // print the device number cnt++; printf("(%d) ",cnt); // get and print the Serial Number of the device just found DisplaySerialNum(portnum); // find the next device rslt = owNext(portnum, TRUE, FALSE); } printf("-- End\r\n\r\nPress any key to continue searching\r\n"); rslt = getchar(); //printf("key pressed: %c\r\n",rslt); } while (rslt!='q'); // release the 1-Wire Net owRelease(portnum); return; }