// ===================================================================== // // Function: void power_up() // // There are a few things that need to be done (once) before the main // program loop starts, these routines are pulled together in this // function, and is called from the main program // // ===================================================================== // --------------------------------------------------------------------- // Update version number at each iteration! // Stored in internal EEPROM // --------------------------------------------------------------------- #define string00 0x0000 #ROM 0xf00000 = {"Code Verson : 0.001"} // ---------------------------------------------------------- // My button routines - Kevin M0KHZ // Initial version 0.001 // 26 Feb 08 // ---------------------------------------------------------- /* // wait_for_release(PORTA,0) void button_release(char port, short pin) { char x=0; while(x != 5) { if (!bit_test(A,4)) x++; else x=0; delay_ms(50); } } // wait_for_press(PORT?,pin_??) void button_press() { /* btn_pushed = false; while(x != 5) { if (!bit_test(A,4)) x++; else x=0; btn_pushed = true; } } } */ // ---------------------------------------------------------- // End of button routines // ---------------------------------------------------------- // ============================================================================= // // Display a string on the LCD from the PIC EEPROM // //============================================================================== void string_from_PIC_EEPROM(long EEPROM_addr) { // from calling code use lcd_gotoxy(x,y); to move pointer to correct // position on LCD for text int char_value = 255; // Character to display from EEPROM while (char_value > 0) { char_value = read_EEPROM(EEPROM_addr++); if (char_value >0) { if (char_value == '#') lcd_putc(1); else lcd_putc(char_value); } } } // ------------------------------------------------------- // Function to write a float variable to EEPROM // ------------------------------------------------------- void write_float_to_eeprom(int8 addr, float data) { int8 i; for(i = 0; i < 4; i++) write_eeprom(addr + i, *((int8*)&data + i) ) ; } // -------------------------------------------------------- // Now to read this value from EEPROM // -------------------------------------------------------- float read_float_from_eeprom(int8 addr) { int8 i; float data; for(i = 0; i < 4; i++) *((int8*)&data + i) = read_eeprom(addr + i); return(data); } // --------------------------------------------------------------------------- // Main LCD display function // --------------------------------------------------------------------------- void paint_display () { // line 1 starts @ 1,1 // line 2 starts @ 1,2 // line 3 starts @ 21,1 // line 4 starts @ 21,2 } //---------------------------------------------------------------------------- // End of Scratch pad area - delete above before final release // --------------------------------------------------------------------------- #separate void power_up(){ // --------------------------------------------------------------------- // uncomment the next line if port B pullups are required // --------------------------------------------------------------------- // port_b_pullups(true); // --------------------------------------------------------------------- // Disable analog inputs // --------------------------------------------------------------------- // setup_adc_ports(NO_ANALOGS|VSS_VDD); // setup_adc(ADC_OFF); // --------------------------------------------------------------------- // Disable parallel slave port // --------------------------------------------------------------------- // setup_psp(PSP_DISABLED); // --------------------------------------------------------------------- // Disable SPI interface // --------------------------------------------------------------------- setup_spi(FALSE); // --------------------------------------------------------------------- // Disable watchdog timer // --------------------------------------------------------------------- setup_wdt(WDT_OFF); // --------------------------------------------------------------------- // Setup required timers // --------------------------------------------------------------------- setup_timer_0(RTCC_INTERNAL); //setup_timer_1(T1_DISABLED); // ******************************************************************** // clock at 16KHz, interrupt every 4*50nS * 4 * (77+1) * 16 = 1.0mS // ******************************************************************** setup_timer_2(T2_DIV_BY_4, 77, 16); //setup_timer_2(T2_DISABLED,0,1); setup_timer_3(T3_DISABLED|T3_DIV_BY_1); // --------------------------------------------------------------------- // Setup comparators as required // --------------------------------------------------------------------- setup_comparator(NC_NC_NC_NC); setup_vref(FALSE); // --------------------------------------------------------------------- // Enable interupts // --------------------------------------------------------------------- enable_interrupts(INT_TIMER2); enable_interrupts(GLOBAL); // -------------------------------------------------------------------- // Setup remaining options // -------------------------------------------------------------------- setup_low_volt_detect(FALSE); setup_oscillator(False); // -------------------------------------------------------------------- // Initialise LCD // -------------------------------------------------------------------- lcd_init(); // -------------------------------------------------------------------- // Initialise I2C support // -------------------------------------------------------------------- init_ext_eeprom(); // --------------------------------------------------------------------- // Write to RS232 port to inform of program start - NOTE this message // should only be displayed once! // --------------------------------------------------------------------- printf("\nProgramme running - you should only see this message once!"); printf("\nIf you see this again you have a serious problem!\n"); } // -------------------------------------------------------------------- // End of power_up() function // -------------------------------------------------------------------- // ==================================================================== // Finite State machine start // ==================================================================== // -------------------------------------------------------------------- // Functions to process each state of the FSM // -------------------------------------------------------------------- // -------------------------------------------------------------------- // State 00 : Entered from on power up // Not a lot happens here! Splash message displayed & // Start message to RS232 for diagnostics sent // -------------------------------------------------------------------- void state_00 () { if (state_changed) { // Reset line display flags // State transition code state_changed = false; // You may want to do something here // The code should never enter this if statement, // remember we set the condition of this ‘flag’ in // the main program before the ‘while’ loop forever // statement, so if we are here, something is wrong! // So the best option would be to jump to some // sort of error trapping function } else { // Steady state code is here // You will want to do something here // For example, bearing in mind this routine should // only run once at power up, so you might want to // log the start time of power up to a diagnostic file // send a message to an engineering panel for info // etc. // After this you will write some code here to // place the FSM into the correct start state. // --------------------------------------------------------------------- // Display Flash screen // --------------------------------------------------------------------- // Surplase display screen locations - these may need changing depending // on your chosen display, are: // // line 1 starts @ 1,1 // line 2 starts @ 1,2 // line 3 starts @ 21,1 // line 4 starts @ 21,2 lcd_putc("\f"); lcd_gotoxy(1,1); lcd_putc("PIC18F6722 Dev Board"); lcd_gotoxy(1,2); lcd_putc("Function Test code"); lcd_gotoxy(21,1); string_from_PIC_EEPROM(string00); lcd_gotoxy(21,2); lcd_putc("By Kevin - M0KHZ"); // leave these 4 lines displayed for 4 secs delay_ms(4000); // Clear display lcd_putc("\f"); delay_ms(5); // --------------------------------------------------------------------- // Write to RS232 port to inform of FSM start - NOTE this message // should only be displayed once! // --------------------------------------------------------------------- printf("\nFinite State machine running"); printf("\nIf you see this again you have a serious problem!\n"); // --------------------------------------------------------------------- // End of write to RS232 port. // --------------------------------------------------------------------- // --------------------------------------------------------------------- // Inform the user what is about to happen // --------------------------------------------------------------------- lcd_putc("\f"); lcd_gotoxy(1,1); lcd_putc("OK we are about to"); lcd_gotoxy(1,2); lcd_putc("enter a continuous "); lcd_gotoxy(21,1); lcd_putc("loop - power off"); lcd_gotoxy(21,2); lcd_putc("to exit :) - M0KHZ"); // leave these 4 lines displayed for 4 secs delay_ms(4000); // Clear display lcd_putc("\f"); delay_ms(5); Required_state = 1; } } // -------------------------------------------------------------------- // State 01 - Start sequence // State description : LCD interface test // -------------------------------------------------------------------- void state_01 () { if (state_changed) { // State transition code state_changed = false; // You may want to do something here } else { // Steady state code is here lcd_putc("\f"); lcd_gotoxy(1,1); lcd_putc("OK if you can read"); lcd_gotoxy(1,2); lcd_putc("this - the LCD "); lcd_gotoxy(21,1); lcd_putc("interface is working"); delay_ms(4000); lcd_putc("\f"); lcd_gotoxy(1,1); lcd_putc("Full character set"); lcd_gotoxy(1,2); lcd_putc("To follow ......."); lcd_gotoxy(21,1); lcd_putc("across all four"); lcd_gotoxy(21,2); lcd_putc("lines.........."); delay_ms(4000); lcd_putc("\f"); lcd_gotoxy(1,1); lcd_putc("1234567890abcdefghij"); lcd_gotoxy(1,2); lcd_putc("klmnopqrstuvwxyzABCD"); lcd_gotoxy(21,1); lcd_putc("EFGHIJKLMNOPQRSTUVWX"); lcd_gotoxy(21,2); lcd_putc("yz!@£$%^&*()?@#[]<>+"); delay_ms(4000); // Clear display lcd_putc("\f"); delay_ms(5); Required_state = 2; } } // -------------------------------------------------------------------- // State 02 // State description : I2C EEPROM A Test // -------------------------------------------------------------------- void state_02 () { if (state_changed) { // State transition code state_changed = false; // You may want to do something here lcd_putc("\f"); lcd_gotoxy(1,1); lcd_putc("R/W EEPROM_A"); init_ext_eeprom(); } else { // Steady state code is here int16 EEPROM_A = 0x0010, result; int16 loop_counter = 0; int16 value = 0x0001; While(loop_counter < 10) { write_int16_ext_eeprom(EEPROM_A, value); delay_ms(500); lcd_gotoxy(1,2); result= read_int16_ext_eeprom(EEPROM_A); delay_ms(3); printf(lcd_putc, "data: %Lu ",result); delay_ms(500); value ++; loop_counter ++; } Required_state = 3; } } // -------------------------------------------------------------------- // State 03 // State description : I2C EEPROM B Test // -------------------------------------------------------------------- void state_03 () { if (state_changed) { // State transition code state_changed = false; // You may want to do something here lcd_putc("\f"); lcd_gotoxy(1,1); lcd_putc("R/W EEPROM_B"); } else { // Steady state code is here int16 EEPROM_B = 0x0011, result; int16 loop_counter = 0; int16 value = 0x0096; While(loop_counter < 10) { write_int16_ext_eeprom(EEPROM_B, value); delay_ms(500); lcd_gotoxy(1,2); result= read_int16_ext_eeprom(EEPROM_B); delay_ms(3); printf(lcd_putc, "data: %Lu ",result); delay_ms(500); value ++; loop_counter ++; } Required_state = 4; } } // -------------------------------------------------------------------- // State 04 // State description : OK Lets flash the LED's // -------------------------------------------------------------------- void state_04 () { if (state_changed) { // State transition code state_changed = false; // You may want to do something here lcd_putc("\f"); lcd_gotoxy(1,1); lcd_putc("The on board LED's"); lcd_gotoxy(1,2); lcd_putc("Should flash now"); } else { // Steady state code is here int16 loop_counter = 0; while(loop_counter < 5){ output_low(pin_A5); delay_ms(400); output_high(pin_A5); delay_ms(400); loop_counter ++; } loop_counter = 0; while(loop_counter < 5){ output_low(pin_B4); delay_ms(400); output_high(pin_B4); delay_ms(400); loop_counter ++; } loop_counter = 0; while(loop_counter < 5){ output_low(pin_B5); delay_ms(400); output_high(pin_B5); delay_ms(400); loop_counter ++; } loop_counter = 0; Required_state = 5; } } // -------------------------------------------------------------------- // State 05 // State description : Com 1 testing // -------------------------------------------------------------------- void state_05 () { if (state_changed) { // State transition code state_changed = false; // You may want to do something here lcd_putc("\f"); lcd_gotoxy(1,1); lcd_putc("A text loop should"); lcd_gotoxy(1,2); lcd_putc("now be seen on com 1"); lcd_gotoxy(21,1); lcd_putc("Ensure comms cable"); lcd_gotoxy(21,2); lcd_putc("is attached"); delay_ms(5000); } else { // Steady state code is here long loop_count = 0; while(loop_count < 5) { fprintf(COM1,"\r\n\If you are reading this text comm1 is working, I'll repeat this message a few times so you can change com cables etc."); delay_ms(1000); loop_count ++; } lcd_putc("\f"); lcd_gotoxy(1,1); lcd_putc("A text loop should"); lcd_gotoxy(1,2); lcd_putc("now be seen on com 2"); lcd_gotoxy(21,1); lcd_putc("Ensure comms cable"); lcd_gotoxy(21,2); lcd_putc("is attached"); loop_count = 0; while(loop_count < 5) { fprintf(COM2,"\r\n\If you are reading this text comm2 is working, I'll repeat this message a few times so you can change com cables etc."); delay_ms(1000); loop_count ++; } delay_ms(5000); Required_state = 6; } } // -------------------------------------------------------------------- // State 06 // State description : Infor user we're about to loop // -------------------------------------------------------------------- void state_06 () { if (state_changed) { // State transition code state_changed = false; // You may want to do something here } else { // Steady state code is here lcd_putc("\f"); lcd_gotoxy(1,1); lcd_putc("Testing complete"); lcd_gotoxy(1,2); lcd_putc("About to jump "); lcd_gotoxy(21,1); lcd_putc("to start of loop"); lcd_gotoxy(21,2); lcd_putc("Kevin - M0KHZ"); delay_ms(4000); Required_state = 1; } } // Add as many State functions here, depending on the number of // case statements in the main program loop // This next function is an error trap - you should never enter this state // but just incase of code breakdown (sat stray nuetrons ! ) do // something here void state_error () { printf(lcd_putc, "\fCODE ERROR "); printf(lcd_putc, "\nRe-initailasing"); printf(lcd_putc, "\n & returning to "); printf(lcd_putc, "\nstate 0"); printf("\nState error !!! - This should never happen?"); delay_ms(3000); // Clear the LCD. printf(lcd_putc, "\f"); delay_ms(100); Required_state = 0; state_changed = false; } // Process any required state changes void do_state_change() { // if a new state is required and it is valid set it up and flag a // state change if (required_state != state) { if ((required_state >= 0) & (required_state <7)) { previous_state = state; state = required_state; state_changed = true; } else { state_error(); } } } // --------------------------------------------------------------------------- // End of Finite State machine // ---------------------------------------------------------------------------