Wednesday 19 September 2012

Switch Input












This post looks at lesson 4 of the Pic tutorial.


/** D E F I N I T I O N S ****************************************************/
#define Switch_Pin      PORTBbits.RB0
#define DetectsInARow   5

The # define in the .h file can be used to define meaningful names to the SFR register.



#pragma udata   // declare statically allocated uinitialized variablesunsigned char LED_Display;  // 8-bit variable
#pragma code    // declare executable instructions
void main (void)
{
    unsigned char Switch_Count = 0;
    LED_Display = 1;            // initialize
    TRISD = 0b00000000;     // PORTD bits 7:0 are all outputs (0)     INTCON2bits.RBPU = 0; // enable PORTB internal pullups     WPUBbits.WPUB0 = 1; // enable pull up on RB0      ANSELH = 0x00;              // AN8-12 are digital inputs (AN12 on RB0)      TRISBbits.TRISB0 = 1;       // PORTB bit 0 (connected to switch) is input (1)
    while (1)
    {
        LATD = LED_Display;     // output LED_Display value to PORTD LEDs
        LED_Display <<= 1;      // rotate display by 1
        if (LED_Display == 0)
            LED_Display = 1;    // rotated bit out, so set bit 0

        while (Switch_Pin != 1);    // wait for switch to be released
        Switch_Count = 5;
        do
        { // monitor switch input for 5 lows in a row to debounce
            if (Switch_Pin == 0)
            { // pressed state detected                Switch_Count++;
            }
            else
            {
                Switch_Count = 0;
            }
            Delay10TCYx(25);    // delay 250 cycles or 1ms.        } while (Switch_Count < DetectsInARow);
    }
}

Some of the pins are shared with the analog input to the pic and hence need to be configured as digital. The C code above is also used to debonce the mechanical switch.

---

I decided to try my hand at some simple refactoring to make the code more readable.


/** V A R I A B L E S *************************************************/
#pragma udata   // declare statically allocated uinitialized variables
unsigned char LED_Display;  // 8-bit variable
unsigned char Switch_Count = 0;
/** D E C L A R A T I O N S *******************************************/
#pragma code    // declare executable instructions
void main (void)
{
    InitOutputs();
    InitInputs();
    while (1)
    {
        SetOutputLedAndRotate();
        DebounceSwitchDetect();
    }
}

void InitOutputs(void)
{
    LED_Display = 0b10101010;            // initialize
    TRISD = 0b00000000;     // PORTD bits 7:0 are all outputs (0)
}
 

void InitInputs(void)
{
    INTCON2bits.RBPU = 0; // enable PORTB internal pullups
WPUBbits.WPUB0 = 1; // enable pull up on RB0
    ANSELH = 0x00;              // AN8-12 are digital inputs (AN12 on RB0)
    TRISBbits.TRISB0 = 1;       // PORTB bit 0 (connected to switch) is input (1)
}


void SetOutputLedAndRotate(void)
{
     LATD = LED_Display;     // output LED_Display value to PORTD LEDs
     LED_Display <<= 1;      // rotate display by 1
     if (LED_Display == 0)
        LED_Display = 3;    // rotated bit out, so set bit 0
}
 
void DebounceSwitchDetect(void)
{
  while (Switch_Pin != 1);  // wait for switch to be released
        Switch_Count = 5;
        do
        { // monitor switch input for 5 lows in a row to debounce
            if (Switch_Pin == 0)
            { // pressed state detected
                Switch_Count++;
            }
            else
            {
                Switch_Count = 0;
            }
            Delay10TCYx(25);    // delay 250 cycles or 1ms.
        } while (Switch_Count < DetectsInARow);
}



No comments:

Post a Comment