[chronojump] Chronopic-firmware 20MHz encoder: Added triggerIfPushed, removed option 0 stuff, cleaned code.



commit 15fa8c678750fe2568225b109550ac34650f6112
Author: Xavier de Blas <xaviblas gmail com>
Date:   Wed Mar 1 13:06:46 2017 +0100

    Chronopic-firmware 20MHz encoder: Added triggerIfPushed, removed option 0 stuff, cleaned code.

 .../chronopic-firmware-20MHz.c                     | 1142 +++++++++-----------
 .../chronopic-firmware-20MHz.hex                   |  226 ++---
 2 files changed, 575 insertions(+), 793 deletions(-)
---
diff --git a/chronopic-firmware/chronopic-firmware-c/chronopic-firmware-20MHz.c 
b/chronopic-firmware/chronopic-firmware-c/chronopic-firmware-20MHz.c
index 70cbaae..4a036fd 100644
--- a/chronopic-firmware/chronopic-firmware-c/chronopic-firmware-20MHz.c
+++ b/chronopic-firmware/chronopic-firmware-c/chronopic-firmware-20MHz.c
@@ -1,660 +1,482 @@
-/*
-Software for the microcontroller PIC16F876A used by the Chronopic chronometer on Chronojump project          
  
-
-Translation of firmware from assembler to C (SDCC):
-source: 
http://git.gnome.org/browse/chronojump/plain/chronopic-firmware/chronopic-firmware-assembler/chronopic-firmware.asm
-
-LICENSE: GPL
-
-encoder:
-  * brown: VCC   
-  * blue : GND   
-  * black: CLK   pin 21
-  * white: VATA          pin 23
-  
-PIC16F87
-  * RB5 : option  pin 26
-
-History:
-  2005 Original Firmware from Juan González <juan iearobotics com>
-  2010 Translated comments to english by Xavi de Blas <xaviblas gmail com>
-  Since 2011 all changes are made by Teng Wei Hua <wadegang gmail com>
-  2011-01-01 Rewritten Firmware on C. complete TMR0 interrupt
-  2011-05-13 ERROR: TIME = 04 ED
-             modify configuration Bits: close WDT
-             modify ISR and MAIN LOOP's if --> else if
-             limit COUNTDEBOUNCE overflow in INTERRUPT(isr)
-            assembler is more efficient than C
-  2011-05-25 change crystal from 4 to 20 MHz
-             let SPBRG = 0X81
-  2011-07-23 add new function of encoder.
-  2011-07-30 complete every 1 ms send 1 byte to PC, detect if encoder turn clockwise or counterclockwise
-  2011-08-09 version 5 set baud rate = 625000.
-  2012-04-02 change the baud rate = 115200, set default function = encoder.
-  2012-04-19 if PC send command 'J' for porti scanning, Chronopic will return 'J'
-*/
-
-//-- this PIC is going to be used:
-//#include <pic16f876A.h>
-#include <pic16f876a.h>
-
-
-//*****************************************
-//*         CONSTANTS                     *
-//*****************************************
-//-- Logical status of the input (not physical status)
-//-- It's the information sent to the frame of changing
-unsigned char INPUT_OFF = 0;  //-- Push button not pushed
-unsigned char INPUT_ON  = 1;  //-- Push button pushed
-
-//-- Header of the asyncronous frame of "changing"
-unsigned char FCHANGE = 'X';
-
-//-- Header of the "status" frame
-unsigned char FSTATUS = 'E';  // Petition of status of the platform
-unsigned char RSTATUS = 'E';  // Response of the status frame
-
-//-- Initialization value of the TIMER0 to have TICKS with a duration of 10ms
-//-- It's used for the debouncing time
-//4M:D9   20M:3D
-unsigned char TICK = 0x3D;
-
-//-- Value of the debouncing time (in units of 10 milliseconds)
-//-- This value can be changed, in order to select the most suitable
-//-- Signals with a duration lower than this value are considered spurious
-//unsigned char DEBOUNCE_TIME = 0x05;
-//0x14
-unsigned char DEBOUNCE_TIME = 0x14;
-
-//-- Status of main automaton
-unsigned char STAT_WAITING_EVENT = 0x00;
-unsigned char STAT_DEBOUNCE = 0x01;
-unsigned char STAT_FRAMEX = 0x02;
-
-//*******************************************
-//*               VARIABLES                 *
-//*******************************************
-
-//-- Save the context when interruptions arrive
-unsigned char savew;   // Temporal storage of W register
-unsigned char saves;   // temporal storage of the STATUS register
-
-//-- Extension of timer 1
-unsigned char TMR1HH;
-
-//-- Counter of the debouncer
-unsigned char COUNTDEBOUNCE;
-
-//-- Timestamp of an event
-unsigned char TIMESTAMP_HH; //-- high-high part
-unsigned char TIMESTAMP_H;  //-- high part
-unsigned char TIMESTAMP_L;  //-- low part
-
-//-- Pushbutton status
-unsigned char input;         //-- Current stable input Value: (0-1)
-unsigned char input_new;     //-- New stable input Value: (0-1)
-
-//-- Automaton status
-unsigned char status;
-
-//-- Reset: Shows if has been done a reset of events
-//-- Reset=1, means that next incoming event will be considered the first
-//-- that means that it's timestamp has no sense, and a 0 must be returned
-//-- Reset=0, it's normal status.
-unsigned char reset;
-
-//-- 16 bits variable to do a pause
-//-- (used by the pause routine)
-unsigned char pause_h;
-unsigned char pause_l;
-
-//-- Store a character of the received frame
-unsigned char my_char;
-
-//-- wade's addition variables
-unsigned char i = 0, j = 0;
-unsigned char option = 1;     // option: 0 button enable, 1 encoder enable
-unsigned char command_port_scanning = 'J';
-
-//-- encoder's valus
-char encoder_count = 0;
-
-
-// Interruptions routine
-// Find the interruption cause
-void isr(void) __interrupt 0
-{
-    //while (!TXIF);
-       //TXREG = 0xaa;
-    if (option == 0)
-    {
-       //******************************************************
-       //* Routine of interruption of timer0
-       //* timer0 is used to control debouncing time
-       //* A change on input signal is stable if time it's at minimum equal to debouncing time
-       //* This timer is working all the time.
-       //* Main automaton know when there's valid information
-       //******************************************************  
-       // Cause by timer0
-       if (T0IF == 1)
-       {       
-           T0IF = 0;            // Remove overflow flag
-           TMR0 = TICK;                 //-- Execute timer again inside a click
-           if (COUNTDEBOUNCE > 0)       // wade : limit COUNTDEBOUNCE overflow
-               COUNTDEBOUNCE--;         //-- Decrese debouncing counter
-           while (!TXIF);
-               TXREG = COUNTDEBOUNCE;
-       }
-       //****************************************************
-       // Routine of port B interruption
-       // Called everytime that's a change on bit RB4
-       // This is the main part.
-        // Everytime that's a change on input signal,
-       // it's timestamp is recorded on variable (TIMESTAMP, 3 bytes)
-        // and we start debouncing time phase
-       //****************************************************    
-        // Caused by a change on B port
-       else if (RBIF == 1)
-        {
-           if (reset == 1)
-           {
-               //-- It's the first event after reset
-               //-- Put counter on zero and go to status reset=0
-               TMR1HH = 0;
-               TMR1H = 0;
-               TMR1L = 0;
-               reset = 0;
-               }
-           //-- Store the value of chronometer on TIMESTAMP
-           //-- This is the timestamp of this event
-           TIMESTAMP_HH = TMR1HH;
-           TIMESTAMP_H = TMR1H;
-           TIMESTAMP_L = TMR1L;
-           //-- Initialize timer 1
-           TMR1HH = 0;
-           TMR1H = 0;
-           TMR1L = 0;
-           //-- Initialize debouncing counter
-           COUNTDEBOUNCE =  DEBOUNCE_TIME;
-
-           //-- start debouncing status
-           status = STAT_DEBOUNCE;
-           //-- start debouncing timer on a tick
-           TMR0 = TICK;
-           //-- Remove interruption flag
-           RBIF = 0;
-           //-- Inhabilite B port interruption
-           // wade : take care
-           RBIE = 0;   
-       }
-       //********************************************************
-       //* Routine of interruption of timer1
-       //* timer 1 controls the cronometring
-       //* This routine is invoked when there's an overflow
-       //* Timer 1 gets extended with 1 more byte: TMR1HH
-       //********************************************************
-       // Caused by timer1
-       else if (TMR1IF == 1)
-       {
-           TMR1IF = 0;  // Remove overflow flag
-           if (TMR1HH != 0xFF)
-           {
-               //-- Overflow control
-               //-- Check if counter has arrived to it's maximum value
-               //-- If it's maximum, then not increment
-               TMR1HH++;
-           }   
-       }
-    } // end of (option == 0)
-
-    if (option == 1)
-    {
-       //******************************************************
-       //* Routine of interruption of timer0
-       //* This timer is working all the time.
-       //******************************************************  
-       // Cause by timer0
-       if (T0IF == 1)
-       {   
-           T0IF = 0;                   // Remove overflow flag
-           TMR0 = 255 - 18;            //-- Execute timer again inside a click
-           //wade : start
-           while (!TXIF);
-           TXREG = encoder_count;
-           encoder_count = 0;
-           // wade : end
-       }
-       if (INTF == 1)
-       {
-           INTF = 0;
-           if (RB2 == 1)
-           {
-               //if (encoder_count < 255)
-               encoder_count++;
-           }
-           else if (RB2 == 0)
-           {
-               //if (encoder_count > 0)
-               encoder_count--;
-           }
-           else 
-           {
-           }
-       }
-       else
-       {
-           //while (!TXIF);
-           //TXREG = 0xcc;
-       }
-    } // end of if (option == 1)
-}
-
-//---------------------------------------
-//-- CONFIGURATION OF SERIAL PORT
-//-- 9600 BAUD, N81
-//---------------------------------------
-void sci_configuration()
-{
-    // wade : start
-    // formula: Baud = Frequency / ( 16(x+1) )
-    // SPBRG = 0X19;   // crystal:  4MHz Speed: 9600 baud 
-    // SPBRG = 0X81;   // crystal: 20MHz Speed: 9600 baud
-    // SPBRG = 0X0A;   // crystal: 20MHz Speed: 115200 baud
-    // SPBRG = 0X01;   // crystal: 20MHz Speed: 625000
-    if (option == 0)
-       SPBRG = 0X81;   // Speed: 9600 baud
-    else
-       SPBRG = 0X0A;   // Speed: 115200 baud
-
-    // wade : end
-    TXSTA = 0X24;   // Configure transmitter
-    RCSTA = 0X90;   // Configure receiver
-}
-
-//**************************************************
-//* Receive a character by the SCI
-//-------------------------------------------------
-// OUTPUTS:
-//    Register W contains received data
-//**************************************************
-unsigned char sci_readchar()
-{
-    while (!RCIF);     // P1R1:RCIF  Receive Interrupt Flag
-    return RCREG;
-}
-
-//*****************************************************
-//* Transmit a character by the SCI
-//*---------------------------------------------------
-//* INPUTS:
-//*    Register W:  character to send
-//*****************************************************
-//-- Wait to Flag allows to send it to 1 (this comment may be bad translated)
-void sci_sendchar(unsigned char my_w)
-{
-    while (!TXIF);
-    TXREG = my_w;
-}
-
-//*******************************************
-//* Routine of pause, by active waiting
-//*******************************************
-void pause()
-{
-    unsigned char i,j;
-    for (i = 0; i < 0xff; i++)
-    {
-       __asm
-       CLRWDT  
-       __endasm;
-       for (j = 0; j < 0xff; j++);
-    }
-}
-
-// wade : long delay
-/*
-void pause1()
-{
-    int i;int j;
-    for (i = 0; i < 2; i++)
-       for (j = 0; j < 10000; j++)
-}
-*/
-
-//***************************************************************************
-//* Read input on RB4 (push button status)
-//* INPUTS: None
-//* OUTPUTS: None
-//* RETURNS: W contains input status (INPUT_ON, INPUT_OFF)
-//***************************************************************************
-unsigned char read_input()
-{
-    //-- Check status of bit RB4
-    if (RB4 == 0)
-       return INPUT_ON;
-    return INPUT_OFF;
-}
-
-//*************************************************************
-//* Update led with stable status of input
-//* Stable status is on variable: input
-//* INPUTS: None
-//* OUTPUTS: None
-//* RETURNS: Nothing
-//*************************************************************
-void update_led()
-{
-    //-- Led is on bit RB1. Input variable contains
-    //-- only an information bit (1,0) on less signficant bit
-    RB1 = !input;
-    // 2012-04-02
-    if (option == 1)
-    {
-       RB1 = 1;
-    }
-}
-
-//*****************************************************
-//* Activate interruption of changing of port B
-//* INPUT: None
-//* OUTPUT:  None
-//* RETURN: Nothing
-//*****************************************************
-void portb_int_enable()
-{
-    // Remove inerruption flag, just in case it's activated
-    RBIF = 0;
-    // Activate interruption of change
-    RBIE = 1;
-}
-
-//****************************************************************
-//* Status service. Returns the status of the platform           *
-//****************************************************************
-void status_serv()
-{
-    //--Deactivate interruption of change while frame is sent
-    RBIE = 0;
-    //-- Send response code
-    sci_sendchar(RSTATUS);
-    //-- Send status of the push button
-    sci_sendchar(input);
-    //-- Activate interruption of change
-    RBIE = 1;
-}
-
-/*
-static void asm_ledon()
-{
-    __asm
-    BANKSEL PORTC
-    MOVLW   0XFF
-    MOVWF PORTC
-    __endasm;
-}
-*/
-
-void main(void)
-{
-    // =========
-    //   START
-    // =========
-    
-    //-----------------------------
-    //- Detect option pin 26
-    //-----------------------------
-    
-    //-----------------------------
-    //- CONFIGURE PORT B
-    //-----------------------------
-    //-- Pins I/O: RB0,RB4 inputs, all the other are outputs
-    // 2012-04-02 wade: start
-    //if (option == 0)
-       //TRISB = 0x11;
-    
-    // encoder Mode
-    //-- Pins I/O: RB0,RB2 inputs, all the other are outputs
-    // 2012-04-02 wade:start
-    TRISB = 0x05;
-    // 2012-04-02 wade:end
-    if (option == 1)
-    {
-        // wade : for testing
-       TRISA = 0x00;
-       RA0 = 1;
-       // wade : for testing
-       TRISB = 0x05;
-       RBIE = 0;
-       RB1 = 1;
-    }
-    // 2012-04-02 wade: end
-    //-- Pull-ups of port B enabled
-    //-- Prescaler of timer0 at 256
-    //--   RBPU = 0, INTEDG=0(1:rising edge, 0:falling edge), T0CS=0, T0SE=0, PSA=0, [PS2,PS1,PS0]=111
-    OPTION_REG = 0x07;
-
-    //----------------------------------------------
-    //  CONFIGURATION OF SERIAL COMMUNICATIONS
-    //----------------------------------------------
-    sci_configuration();
-
-    //----------------------------------------------
-    //- CONFIGURATION OF TIMER 0
-    //----------------------------------------------
-    //-- Remove interruption flag, just in case it's activated
-    T0IF = 0;  //  Remove overflow flag
-    //-- Activate timer. Inside an interruption tick
-    // wade : start
-    if (option == 0)
-       TMR0 = TICK;
-    if (option == 1)
-       TMR0 = 0xFF;
-    // wade : end
-
-    //----------------------------------------------
-    //- CONFIGURATION OF TIMER 1
-    //----------------------------------------------
-    //-- Activate timer 
-    //   set prescaler
-    // wade : start
-    if (option == 0)
-       T1CON = 0X31;
-    // wade : end
-    //-- Zero value
-    TMR1HH = 0;
-    TMR1H = 0;
-    TMR1L = 0;
-    //-- Enable interruption
-    // wade : sart
-    if (option == 0)
-       TMR1IE = 1;
-    // wade : end
-
-    //----------------------------
-    //- Interruptions port B
-    //----------------------------
-    //-- Wait to port B get's stable
-    pause();
-    //-- Enable interruption of change on port B
-    // wade : start
-    if (option == 0)
-       portb_int_enable();
-    // wade : end
-
-    //------------------------------
-    //- INITIALIZE VARIABLES
-    //------------------------------
-    //-- Initialize extended counteri
-    TMR1HH = 0;
-    //-- Initialize debounce counter
-    COUNTDEBOUNCE = DEBOUNCE_TIME;
-    //-- Initialize automaton
-    status = STAT_WAITING_EVENT;
-    //-- At start, system is on Reset
-    reset = 1;
-    //-- Read input status and update input variable   
-    input = read_input();
-    
-    //----------------------
-    //- INITIAL LED STATUS
-    //----------------------
-    //-- Update led with the stable status of input variable
-    update_led();
-
-    //--------------------------
-    //- Interruption TIMER 0
-    //--------------------------
-    //T0IE = 1;        // Activate interruption overflow TMR0
-    /// wade : start
-    T0IE = 1;  // Activate interruption overflow TMR0
-
-    // wade : end
-   
-    //--------------------------
-    //- Interruption INT RB0
-    //-------------------------- 
-    // wade : start
-    if (option == 1)
-       INTE = 1;
-    // wade : end
-    // wade : for testing
-    /*
-    for (i = 0; i <= 0x77; i++)
-    {
-       TXEN = 1;
-       for(j = 0; j < 0xFF; j++)
-       {
-           while (!TXIF);
-           TXREG = i;
-           while (!TXIF);
-           TXREG = j;
-       }
-       TXEN = 0;       
-    }
-    */
-    // wade : for testing
-
-    //------------------------------------------
-    //- ACTIVATE GLOBAL INTERRUPTIONS
-    //- The party starts, now!!!
-    //------------------------------------------
-    //-- Activate peripheral interruptions
-    PEIE = 1;
-    //-- Activate global interruptions
-    GIE = 1;
-
-    //****************************
-    //*   MAIN LOOP
-    //**************************** 
-    while(1)
-    {
-        //-- used on the simulation
-       __asm
-       CLRWDT
-       __endasm;
-        // wade : start
-        // sci_sendchar(FCHANGE);
-        //sci_sendchar(0xFF);
-        //sci_sendchar(0xFF);
-       // wade : end
-
-       if (option == 0)
-       {
-           //-- Analize serial port waiting to a frame
-           if (RCIF == 1)      // Yes--> Service of platform status
-           {           
-               my_char = sci_readchar();
-               if (my_char == FSTATUS)
-                   status_serv();
-           }
-
-           //------------------------------------------------------
-           //- DEPENDING AUTOMATON STATUS, GO TO DIFFERENT ROUTINES
-           //------------------------------------------------------
-           if (status == STAT_WAITING_EVENT)   // status = STAT_WAITING_EVENT?
-           {
-           }
-           else if (status == STAT_DEBOUNCE)   // status = DEBOUNCE?
-           {
-               //----------------------------
-               //- STATUS DEBOUNCING
-               //----------------------------
-               if (COUNTDEBOUNCE == 0)
-               {
-                   //-- End of debounce
-                   //-- Remove interruption flag of port B: to clean. We do not want or need
-                   //-- what came during that time
-                   RBIF = 0;
-                   //-- input_new = input status
-                   input_new = read_input();
-                   //-- Compare new input with stable input
-                   if (input_new == input)
-                   {
-                       //-- It came an spurious pulse (change with a duration
-                       //-- lower than debounce time). It's ignored.
-                       //-- We continue like if nothing happened
-                       //-- The value of the counter should be: actual + TIMESTAMP
-                       //-- TMR1 = TIMR1 + TIMESTAMP
-                       TMR1L = TMR1L + TIMESTAMP_L;
-                       //-- Add carry, if any
-                       if (C == 1)         //-- Yes--> Add it to high part
-                           TMR1H++;
-                       TMR1H = TMR1H + TIMESTAMP_H;
-                       //-- Add carry, if any
-                       if (C == 1)         //-- Yes--> Add it to "higher weight" part
-                           TMR1HH++;
-                       TMR1HH = TMR1HH + TIMESTAMP_HH;
-                       //-- Change status to waiting event
-                       status = STAT_WAITING_EVENT;
-                       //-- Activate interruption port B
-                       portb_int_enable();
-                   }
-                   else
-                   { 
-                       //-- input!=input_new: Happened an stable change
-                       //-- Store new stable input
-                       input = input_new;
-                       //-- Change to status FRAMEX to send frame with the event
-                       status = STAT_FRAMEX;
-                   }
-               }
-           }
-           else if (status == STAT_FRAMEX)         // status = FRAMEX?
-           {
-               //----------------------------
-               //- STATUS FRAMEX
-               //----------------------------
-               //-- Send frame of changing input
-               //-- First the frame identifier
-               sci_sendchar(FCHANGE);
-               //-- Send push button status
-               sci_sendchar(input);
-               //-- Send timestamp
-               sci_sendchar(TIMESTAMP_HH);
-               sci_sendchar(TIMESTAMP_H);
-               sci_sendchar(TIMESTAMP_L);
-               //-- Change to next status
-               status = STAT_WAITING_EVENT;
-               //-- Update led status, depending on stable input status
-               update_led();
-               //-- Activate port B interruption
-               portb_int_enable();
-           }
-       } // end of if (option == 0)
-       if (option == 1)
-       {       
-           if (RCIF == 1)      // if PC send a command from serial port.
-           {
-               T0IE = 0;
-               my_char = sci_readchar();
-               if (my_char == command_port_scanning)
-               {
-                   sci_sendchar(command_port_scanning);
-               }
-               T0IE = 1;
-           }
-       }
-    } // end of while
-
-}
-
+/*
+Software for the microcontroller PIC16F876A used by the Chronopic chronometer on Chronojump project
+
+Translation of firmware from assembler to C (SDCC):
+source: 
http://git.gnome.org/browse/chronojump/plain/chronopic-firmware/chronopic-firmware-assembler/chronopic-firmware.asm
+
+LICENSE: GPL
+
+encoder:
+  * brown: VCC
+  * blue : GND
+  * black: CLK   pin 21
+  * white: VATA          pin 23
+
+PIC16F87
+  * RB5 : option  pin 26
+
+History:
+  2005 Original Firmware from Juan González <juan iearobotics com>
+  2010 Translated comments to english by Xavi de Blas <xaviblas gmail com>
+  Since 2011 all changes are made by Teng Wei Hua <wadegang gmail com>
+  2011-01-01 Rewritten Firmware on C. complete TMR0 interrupt
+  2011-05-13 ERROR: TIME = 04 ED
+             modify configuration Bits: close WDT
+             modify ISR and MAIN LOOP's if --> else if
+             limit COUNTDEBOUNCE overflow in INTERRUPT(isr)
+            assembler is more efficient than C
+  2011-05-25 change crystal from 4 to 20 MHz
+             let SPBRG = 0X81
+  2011-07-23 add new function of encoder.
+  2011-07-30 complete every 1 ms send 1 byte to PC, detect if encoder turn clockwise or counterclockwise
+  2011-08-09 version 5 set baud rate = 625000.
+  2012-04-02 change the baud rate = 115200, set default function = encoder.
+  2012-04-19 if PC send command 'J' for porti scanning, Chronopic will return 'J'
+  2017-02.28 Xavier Padullés and Xavi de Blas: added triggerIfPushed, removed option 0 stuff, cleaned code.
+*/
+
+//-- this PIC is going to be used:
+//#include <pic16f876A.h>
+#include <pic16f876a.h>
+
+
+//*****************************************
+//*         CONSTANTS                     *
+//*****************************************
+//-- Logical status of the input (not physical status)
+//-- It's the information sent to the frame of changing
+unsigned char INPUT_OFF = 0;  //-- Push button not pushed
+unsigned char INPUT_ON  = 1;  //-- Push button pushed
+
+//-- Header of the asyncronous frame of "changing"
+unsigned char FCHANGE = 'X';
+
+//-- Header of the "status" frame
+unsigned char FSTATUS = 'E';  // Petition of status of the platform
+unsigned char RSTATUS = 'E';  // Response of the status frame
+
+//RCA push (RB4) will activate trigger and this will be sent to Chronojump
+unsigned char TRIGGER = 'T';  // Trigger (RCA has been pushed)
+unsigned char RB4Previous = 0; //previous RB4 (RCA) to see if there's a change
+
+//-- Initialization value of the TIMER0 to have TICKS with a duration of Nms
+//-- It's used for the debouncing time
+
+//unsigned char TICK = 0xD9; //For a duration of 10ms and clock at 4Mhz-->0xD9
+unsigned char TICK = 0x3D; //For a duration of 10ms and clock at 20Mhz-->0x3D
+//unsigned char TICK = 0xFC; //For a duration of 1ms and clock at 4Mhz-->0xFC
+//unsigned char TICK = 0xEC; //For a duration of 1ms and clock at 20Mhz-->0xEC
+
+
+//-- Value of the debouncing time (in units of TICK )
+//-- This value can be changed, in order to select the most suitable
+//-- Signals with a duration lower than this value are considered spurious
+//unsigned char DEBOUNCE_TIME = 0x05; //If TICK is 10ms and DEBOUNCE_TIME = 0x05 means 50ms
+//0x14
+unsigned char DEBOUNCE_TIME = 0x14;
+
+//-- Status of main automaton
+unsigned char STAT_WAITING_EVENT = 0x00;
+unsigned char STAT_DEBOUNCE = 0x01;
+unsigned char STAT_FRAMEX = 0x02;
+
+//*******************************************
+//*               VARIABLES                 *
+//*******************************************
+
+//-- Save the context when interruptions arrive
+unsigned char savew;   // Temporal storage of W register
+unsigned char saves;   // temporal storage of the STATUS register
+
+//-- Extension of timer 1
+unsigned char TMR1HH;
+
+//-- Counter of the debouncer
+unsigned char COUNTDEBOUNCE;
+
+//-- Timestamp of an event
+unsigned char TIMESTAMP_HH; //-- high-high part
+unsigned char TIMESTAMP_H;  //-- high part
+unsigned char TIMESTAMP_L;  //-- low part
+
+//-- Pushbutton status
+unsigned char input;         //-- Current stable input Value: (0-1)
+unsigned char input_new;     //-- New stable input Value: (0-1)
+
+//-- Automaton status
+unsigned char status;
+
+//-- Reset: Shows if has been done a reset of events
+//-- Reset=1, means that next incoming event will be considered the first
+//-- that means that its timestamp has no sense, and a 0 must be returned
+//-- Reset=0, its normal status.
+unsigned char reset;
+
+//-- 16 bits variable to do a pause
+//-- (used by the pause routine)
+unsigned char pause_h;
+unsigned char pause_l;
+
+//-- Store a character of the received frame
+unsigned char my_char;
+
+//-- wade's addition variables
+unsigned char i = 0, j = 0;
+unsigned char option = 1;     // option: 0 button enable, 1 encoder enable
+unsigned char command_port_scanning = 'J';
+
+
+//-- encoder's valus
+char encoder_count = 0;
+
+//*****************************************************
+//* Transmit a character by the SCI
+//*---------------------------------------------------
+//* INPUTS:
+//*    Register W:  character to send
+//*****************************************************
+//-- Wait to Flag allows to send it to 1 (this comment may be bad translated)
+void sci_sendchar(unsigned char my_w)
+{
+    while (!TXIF);
+    TXREG = my_w;
+}
+
+//*************************************************************
+//* Update led with stable status of input
+//* Stable status is on variable: input
+//* INPUTS: None
+//* OUTPUTS: None
+//* RETURNS: Nothing
+//*************************************************************
+void update_led()
+{
+    //-- Led D1 is on bit RB1. Input variable contains
+    //-- only an information bit (1,0) on less signficant bit
+    RB1 = ! RB4;
+}
+
+
+void triggerIfPushed()
+{
+       if(RB4 == 1)
+       {
+               if(RB4Previous == 0)
+               {
+                       sci_sendchar(TRIGGER);
+               }
+       }
+       if(RB4Previous != RB4)
+       {
+               update_led();
+       }
+
+       RB4Previous = RB4;
+}
+
+// Interruptions routine
+// Find the interruption cause
+void isr(void) __interrupt 0
+{
+    //while (!TXIF);
+       //TXREG = 0xaa;
+
+    if (option == 1)
+    {
+       //******************************************************
+       //* Routine of interruption of timer0
+       //* This timer is working all the time.
+       //******************************************************
+       // Cause by timer0
+       if (T0IF == 1)
+       {
+           T0IF = 0;                   // Remove overflow flag
+           TMR0 = 255 - 18;            //-- Execute timer again inside a click
+           //wade : start
+           while (!TXIF);
+           TXREG = encoder_count;
+           encoder_count = 0;
+           // wade : end
+
+           triggerIfPushed();
+       }
+       if (INTF == 1)
+       {
+           INTF = 0;
+           if (RB2 == 1)
+           {
+               //if (encoder_count < 255)
+               encoder_count++;
+           }
+           else if (RB2 == 0)
+           {
+               //if (encoder_count > 0)
+               encoder_count--;
+           }
+           else
+           {
+           }
+       }
+       else
+       {
+           //while (!TXIF);
+           //TXREG = 0xcc;
+       }
+    } // end of if (option == 1)
+}
+
+//---------------------------------------
+//-- CONFIGURATION OF SERIAL PORT
+//-- 9600 BAUD, N81
+//---------------------------------------
+void sci_configuration()
+{
+    // wade : start
+    // formula: Baud = Frequency / ( 16(x+1) )
+    // SPBRG = 0X19;   // crystal:  4MHz Speed: 9600 baud
+    // SPBRG = 0X81;   // crystal: 20MHz Speed: 9600 baud
+    // SPBRG = 0X0A;   // crystal: 20MHz Speed: 115200 baud
+    // SPBRG = 0X01;   // crystal: 20MHz Speed: 625000
+    //if (option == 0): jumps
+    // SPBRG = 0X81;   // Speed: 9600 baud
+    //else
+       SPBRG = 0X0A;   // Speed: 115200 baud //option == 1: encoder
+
+    // wade : end
+    TXSTA = 0X24;   // Configure transmitter
+    RCSTA = 0X90;   // Configure receiver
+}
+
+//**************************************************
+//* Receive a character by the SCI
+//-------------------------------------------------
+// OUTPUTS:
+//    Register W contains received data
+//**************************************************
+unsigned char sci_readchar()
+{
+    while (!RCIF);     // P1R1:RCIF  Receive Interrupt Flag
+    return RCREG;
+}
+
+//*******************************************
+//* Routine of pause, by active waiting
+//*******************************************
+void pause()
+{
+    unsigned char i,j;
+    for (i = 0; i < 0xff; i++)
+    {
+       __asm
+       CLRWDT
+       __endasm;
+       for (j = 0; j < 0xff; j++);
+    }
+}
+
+// wade : long delay
+/*
+void pause1()
+{
+    int i;int j;
+    for (i = 0; i < 2; i++)
+       for (j = 0; j < 10000; j++)
+}
+*/
+
+//***************************************************************************
+//* Read input on RB4 (push button status)
+//* INPUTS: None
+//* OUTPUTS: None
+//* RETURNS: W contains input status (INPUT_ON, INPUT_OFF)
+//***************************************************************************
+unsigned char read_input()
+{
+    //-- Check status of bit RB4
+    if (RB4 == 0)
+       return INPUT_ON;
+    return INPUT_OFF;
+}
+
+//*****************************************************
+//* Activate interruption of changing of port B
+//* INPUT: None
+//* OUTPUT:  None
+//* RETURN: Nothing
+//*****************************************************
+void portb_int_enable()
+{
+    // Remove inerruption flag, just in case it's activated
+    RBIF = 0;
+    // Activate interruption of change
+    RBIE = 1;
+}
+
+void portb_int_disable(unsigned char clearInterruptionFlag)
+{
+    if(clearInterruptionFlag == 1)
+    {
+      // Remove interruption flag, just in case it's activated
+      RBIF = 0;
+    }
+    // Activate interruption of change
+    RBIE = 0;
+}
+
+//****************************************************************
+//* Status service. Returns the status of the platform           *
+//****************************************************************
+void status_serv()
+{
+    //--Deactivate interruption of change while frame is sent
+    portb_int_disable(0);
+    //-- Send response code
+    sci_sendchar(RSTATUS);
+    //-- Send status of the push button
+    sci_sendchar(input);
+    //-- Activate interruption of change
+    RBIE = 1;
+}
+
+/*
+static void asm_ledon()
+{
+    __asm
+    BANKSEL PORTC
+    MOVLW   0XFF
+    MOVWF PORTC
+    __endasm;
+}
+*/
+
+void main(void)
+{
+    // =========
+    //   START
+    // =========
+
+    //-----------------------------
+    //- Detect option pin 26
+    //-----------------------------
+
+    //-----------------------------
+    //- CONFIGURE PORT B
+    //-----------------------------
+
+    // encoder Mode
+
+    if (option == 1)
+    {
+        // wade : for testing
+       //TRISA = 0x00;
+       //RA0 = 1;
+       // wade : for testing
+
+        //2017-02-27 XaviP:start
+        TRISB = 0x15;        //RB0,RB2,RB4 inputs, all other outputs
+        //2017-02-27 XaviP:end
+
+       portb_int_disable(0);
+
+
+       RB1 = 1; //LED D1 activated
+       TMR0 = 0xFF;       //-- Activate timer. TODO: check it
+       pause();           //Wait for port B getting stable
+       INTE = 1;          //-- Activate the interruptions
+    }
+    // 2012-04-02 wade: end
+    //-- Pull-ups of port B enabled
+    //-- Prescaler of timer0 at 256
+    //--   RBPU = 0, INTEDG=0(1:rising edge, 0:falling edge), T0CS=0, T0SE=0, PSA=0, [PS2,PS1,PS0]=111
+    OPTION_REG = 0x07;
+
+    //----------------------------------------------
+    //  CONFIGURATION OF SERIAL COMMUNICATIONS
+    //----------------------------------------------
+    sci_configuration();
+
+    //----------------------------------------------
+    //- CONFIGURATION OF TIMER 0
+    //----------------------------------------------
+    //-- Remove interruption flag, just in case it's activated
+    T0IF = 0;  //  Remove overflow flag
+
+    //----------------------------------------------
+    //- CONFIGURATION OF TIMER 1
+    //----------------------------------------------
+    //-- Activate timer
+    //   set prescaler
+    // wade : start
+    // wade : end
+    //-- Zero value
+    TMR1HH = 0;
+    TMR1H = 0;
+    TMR1L = 0;
+
+    //------------------------------
+    //- INITIALIZE VARIABLES
+    //------------------------------
+
+    //-- Initialize debounce counter
+    COUNTDEBOUNCE = DEBOUNCE_TIME;
+    //-- Initialize automaton
+    status = STAT_WAITING_EVENT;
+    //-- At start, system is on Reset
+    reset = 1;
+    //-- Read input status and update input variable
+    input = read_input();
+
+    //----------------------
+    //- INITIAL LED STATUS
+    //----------------------
+    //-- Update led with the stable status of input variable
+    update_led();
+
+    //--------------------------
+    //- Interruption TIMER 0
+    //--------------------------
+    //T0IE = 1;        // Activate interruption overflow TMR0
+    /// wade : start
+    T0IE = 1;  // Activate interruption overflow TMR0
+
+    // wade : end
+
+    //------------------------------------------
+    //- ACTIVATE GLOBAL INTERRUPTIONS
+    //- The party starts, now!!!
+    //------------------------------------------
+    //-- Activate peripheral interruptions
+    PEIE = 1;
+    //-- Activate global interruptions
+    GIE = 1;
+
+    //****************************
+    //*   MAIN LOOP
+    //****************************
+    while(1)
+    {
+        //-- used on the simulation
+       __asm
+       CLRWDT
+       __endasm;
+        // wade : start
+        // sci_sendchar(FCHANGE);
+        //sci_sendchar(0xFF);
+       // wade : end
+
+       if (option == 1)
+       {
+           if (RCIF == 1)      // if PC send a command from serial port.
+           {
+               T0IE = 0;
+               my_char = sci_readchar();
+               if (my_char == command_port_scanning)
+               {
+                   sci_sendchar(command_port_scanning);
+               }
+               T0IE = 1;
+           }
+       }
+    } // end of while
+
+}
diff --git a/chronopic-firmware/chronopic-firmware-c/chronopic-firmware-20MHz.hex 
b/chronopic-firmware/chronopic-firmware-c/chronopic-firmware-20MHz.hex
index a51d3bb..24b1eed 100644
--- a/chronopic-firmware/chronopic-firmware-c/chronopic-firmware-20MHz.hex
+++ b/chronopic-firmware/chronopic-firmware-c/chronopic-firmware-20MHz.hex
@@ -1,134 +1,94 @@
-:1000000000008A110A12ED2AF200030E8301F100AA
-:100010000A088A01F000040883120313CD0000309F
-:10002000831203134A04031DA62883120313AC0191
-:10003000831203130B1D1F2883120313AC0A8312B0
-:1000400003132C08013A031D3F28831203130B11DD
-:1000500083120313430883120313810000308312B9
-:1000600003133304031DB303831203130C1E34283C
-:10007000831203133308831203139900A6288312F3
-:100080000313AC01831203130B1C492883120313BF
-:10009000AC0A831203132C08013A031D8D28831226
-:1000A00003133A08013A031D5F2883120313B201B8
-:1000B000831203138F018E0183120313BA0183127B
-:1000C0000313320883120313B400831203130F08BF
-:1000D00083120313B500831203130E088312031354
-:1000E000B60083120313B201831203138F018E0132
-:1000F00083120313440883120313B3008312031300
-:10010000460883120313B9008312031343088312B2
-:10011000031381000B108B11A62883120313AC016B
-:10012000831203130C1C972883120313AC0A831247
-:1001300003132C08013A031DA628831203130C1085
-:10014000831203133208FF3A031DB20A831203130A
-:100150004A08013A031DFC2883120313AC018312E1
-:1001600003130B1DB62883120313AC0A8312031367
-:100170002C08013A031DCE28831203130B11ED3016
-:100180008100831203130C1EC128831203134C0831
-:1001900083120313990083120313CC0183120313F8
-:1001A000AC01831203138B1CD82883120313AC0AEF
-:1001B000831203132C08013A031DFC288312031336
-:1001C0008B1083120313AC0183120313061DEB285B
-:1001D00083120313AC0A831203132C08013A031D84
-:1001E000F52883120313CC0AFC2883120313061983
-:1001F000FC2883120313CC03831203134D088400DD
-:1002000070088A008301710E8300F20E720E0900DD
-:100210000530831603138600831203134A08013A3C
-:10022000031D20298316031385018312031305146C
-:100230000530831603138600831203138B11861473
-:100240000730831603138100D522831203130B1189
-:100250000030831203134A04031D3429831203134D
-:100260004308831203138100831203134A08013ADF
-:10027000031D3E29FF3083120313810000308312D7
-:1002800003134A04031D48293130831203139000DD
-:1002900083120313B201831203138F018E01003006
-:1002A000831203134A04031D5829831603130C14E5
-:1002B000AC220030831203134A0403197D228312F7
-:1002C0000313B20183120313440883120313B30010
-:1002D00083120313450883120313B90001308312FC
-:1002E0000313BA00A02283120313B70082228312E1
-:1002F00003138B16831203134A08013A031D832943
-:10030000831203130B16831203130B178B1764004E
-:100310000030831203134A04031D402A831203137F
-:10032000AF01831203138C1E982983120313AF0AA3
-:10033000831203132F08013A031DAA29CF22831227
-:100340000313BD00831203134108831203133D06F8
-:1003500003196E228312031345088312031339060F
-:100360000319402A83120313460883120313390624
-:10037000031D1C2A0030831203133304031D402A7B
-:10038000831203130B10A02283120313B8008312ED
-:1003900003133708831203133806031D0F2A831231
-:1003A00003133608831203138E0783120313AF015E
-:1003B00083120313031CDF2983120313AF0A831272
-:1003C00003132F08013A031DE829831203138F0A30
-:1003D000831203133508831203138F078312031349
-:1003E000AF0183120313031CF82983120313AF0A0E
-:1003F000831203132F08013A031D012A83120313EA
-:10040000B20A83120313340883120313B207831250
-:100410000313450883120313B9007D22402A831277
-:100420000313380883120313B7008312031347081A
-:1004300083120313B900402A83120313470883125F
-:1004400003133906031D402A831203134008C122F7
-:10045000831203133708C122831203133408C12205
-:10046000831203133508C122831203133608C122F5
-:1004700083120313450883120313B90082227D22DD
-:10048000831203134A08013A031D872983120313B9
-:10049000AF01831203138C1E502A83120313AF0A79
-:1004A000831203132F08013A031D872983120313B4
-:1004B0008B12CF2283120313BD00831203134B0848
-:1004C000831203133D06031D692A831203134B088D
-:1004D000C122831203138B1687290800831203138A
-:1004E0008B11831203134208C122831203133708AE
-:1004F000C122831203138B150800831203130B1000
-:100500008B150800831203133708003003190130DC
-:1005100083120313AD002D0C0318912A83120313C9
-:100520008610031C962A8312031386148312031366
-:100530004A08013A031D9F2A8312031386140800F8
-:1005400083120313061AA82A831203133F08AB2A47
-:10055000831203133E08080083120313AD01FF301A
-:10056000831203132D020318C02A6400FF30831284
-:100570000313AE0083120313AE0BBA2AAD0AAF2ADF
-:10058000080083120313AD00831203130C1EC42A48
-:10059000831203132D08831203139900080083129A
-:1005A00003138C1ECF2A1A0808000030831203138D
-:1005B0004A04031DE02A8130831603139900E42ABC
-:1005C0000A3083160313990024308316031398000E
-:1005D000903083120313980008008130FE0003302E
-:1005E000FF0080308A110A12F6238A110A12831240
-:1005F0000313A1007F08A0008330A2000330A300F2
-:10060000831203132008A4002108A500FF30A007CF
-:10061000031CA1032408250403197D2B04302207A1
-:10062000A400A501A50D2308A5072408FE002508A0
-:10063000FF0080308A110A12F6238A110A128312EF
-:100640000313A7007F08A6002208FE002308FF006E
-:1006500080308A110A12F6238A110A1283120313B8
-:10066000A900A5007F08A800A40002302207A4006A
-:10067000A501A50D2308A5072408FE002508FF00F5
-:1006800080308A110A12F6238A110A128312031388
-:10069000A500AB007F08A400AA008312031326085C
-:1006A000AA002708AB00FF30A607031CA7032A08EF
-:1006B0002B040319762B2808FE002908FF00803040
-:1006C0008A110A12E3238A110A1283120313AA0061
-:1006D000240884008313251883172A088000A80A99
-:1006E0000319A90AA40A0319A50A4D2B063083127F
-:1006F0000313A2070318A30A002B8A110A12082960
-:10070000080010340034173404343E34003401340B
-:100710000034183404343F340034013400341934C4
-:10072000043440340034013400341A340434413485
-:100730000034013400341B340434423400340134B6
-:1007400000341C34043443340034013400341D3488
-:10075000043444340034013400341E340434453449
-:100760000034013400341F3404344634003401347E
-:10077000003420340434473400340134003421344C
-:10078000043448340034013400342234043449340D
-:10079000003401340034233404344A340034013446
-:1007A0000034243404344B34003401340034253410
-:1007B00004344C34003401340034263404344E34D0
-:1007C000003401340034003A0319EA2B803A03194B
-:1007D000F12B00347E08840083137F1883170008F0
-:1007E00008007F088A007E0882000800003A03198A
-:1007F000FE2B803A0319082CFF0100347E08840088
-:1008000083137F1883170008FF00840A000808007C
-:100810008A110A12F123FD00FE0FFF03FF0A8A115D
-:100820000A12F123FC007D08FF007C080800003458
-:1008300001345834453445343D34143400340134E3
-:0E08400002340034003401344A3400340034F1
+:1000000000008A110A129A29F200030E8301F100FE
+:100010000A088A01F000040883120313D000831237
+:1000200003134D08013A031D6A2883120313AF011D
+:10003000831203130B1D1F2883120313AF0A8312AD
+:1000400003132F08013A031D3C28831203130B11DD
+:10005000ED308100831203130C1E2A288312031330
+:100060004F0883120313990083120313CF018A11DF
+:100070000A1232218A110A1283120313AF0183126A
+:1000800003138B1C462883120313AF0A8312031336
+:100090002F08013A031D6A28831203138B10831261
+:1000A0000313AF0183120313061D59288312031390
+:1000B000AF0A831203132F08013A031D632883122A
+:1000C0000313CF0A6A288312031306196A288312BE
+:1000D0000313CF03831203135008840070088A00AF
+:1000E0008301710E8300F20E720E09008312031356
+:1000F0004D08013A031D8B28153083160313860023
+:100100000030F120831203138614FF3081000C218C
+:10011000831203130B160730831603138100272164
+:10012000831203130B1183120313B30183120313FE
+:100130008F018E0183120313470883120313B40047
+:1001400083120313480883120313BA000130831289
+:100150000313BB00002183120313B8006F21831225
+:1001600003138B160B178B176400831203134D08B0
+:10017000013A031DB42883120313B0018312031341
+:100180008C1EC52883120313B00A83120313300890
+:10019000013A031DB428831203138B122121831209
+:1001A0000313BE00831203134E08831203133E068B
+:1001B000031DDE28831203134E088C2183120313C0
+:1001C0008B16B42808000030F12083120313430873
+:1001D0008C218312031338088C21831203138B158F
+:1001E0000800013A031DF728831203130B10831232
+:1001F00003138B110800831203130B108B150800D7
+:1002000083120313061A08298312031340080B29CB
+:10021000831203133F08080083120313AC01FF305D
+:10022000831203132C02031820296400FF30831269
+:100230000313AD0083120313AD0B1A29AC0A0F2967
+:100240000800831203138C1E21291A0808000A30A3
+:1002500083160313990024309800903083120313FF
+:100260009800080083120313AE0183120313061EC5
+:100270003C2983120313AE0A831203132E08013A9A
+:10028000031D4C290030831203134504031D4C2920
+:100290008312031344088C2183120313AE018312CB
+:1002A0000313061E562983120313AE0A8312031387
+:1002B0004508831203132E06031D6F2183120313B7
+:1002C000AE0183120313061E682983120313AE0ABC
+:1002D000831203132E0883120313C5000800831230
+:1002E0000313AC0183120313061E7929831203132F
+:1002F000AC0A831203132C08003003190130AD003F
+:100300002D0C03188629831203138610031C8B29D6
+:10031000831203138614080083120313AC008312A4
+:1003200003130C1E8F29831203132C08831203134B
+:10033000990008002E30FE000230FF0080308A1144
+:100340000A12AF228A110A1283120313A1007F0836
+:10035000A0003030A2000230A30083120313200853
+:10036000A4002108A500FF30A007031CA103240856
+:10037000250403192A2A04302207A400A501A50D8B
+:100380002308A5072408FE002508FF0080308A11F5
+:100390000A12AF228A110A1283120313A7007F08E0
+:1003A000A6002208FE002308FF0080308A110A12EE
+:1003B000AF228A110A1283120313A900A5007F0835
+:1003C000A800A40002302207A400A501A50D23085F
+:1003D000A5072408FE002508FF0080308A110A12B4
+:1003E000AF228A110A1283120313A500AB007F0803
+:1003F000A400AA00831203132608AA002708AB0052
+:10040000FF30A607031CA7032A082B040319232A7D
+:100410002808FE002908FF0080308A110A129C2259
+:100420008A110A1283120313AA002408840083137A
+:10043000251883172A088000A80A0319A90AA40A04
+:100440000319A50AFA29063083120313A207031819
+:10045000A30AAD298A110A12762808001234003442
+:10046000D03402343F34003401340034D134023407
+:100470004034003401340034D23402344134003486
+:1004800001340034D33402344234003401340034B3
+:10049000D43402344334003401340034D5340234CB
+:1004A0004434003401340034D6340234453400344A
+:1004B00001340034D734023446340034013400347B
+:1004C000D83402344734003401340034D93402348F
+:1004D0004834003401340034DA340234493400340E
+:1004E00001340034DB3402344A3400340134003443
+:1004F000DC3402344B34003401340034DD34023453
+:100500004C34003401340034DE3402344D340034D1
+:1005100001340034DF3402344E340034013400340A
+:10052000E03402344F34003401340034E134023416
+:100530005134003401340034003A0319A32A803ABC
+:100540000319AA2A00347E08840083137F188317B6
+:10055000000808007F088A007E0882000800003A30
+:100560000319B72A803A0319C12AFF0100347E0813
+:10057000840083137F1883170008FF00840A000893
+:1005800008008A110A12AA22FD00FE0FFF03FF0ACB
+:100590008A110A12AA22FC007D08FF007C080800CC
+:1005A00000340134583445344534543400343D3437
+:1005B00014340034013402340034003401344A3439
+:0405C00000340034CF
 :00000001FF



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]