Saturday 16 June 2012

Episode 2

Buttons and Direct Port Access

In this tutorial we will use the RB0 button and the RC0 LED as input and output respectively. The first two examples are basic programs for the use of a push button as an input. For the Direct Port Access I will use the same examples but the button and LED will be connected to different pins. This is handy when you make prototypes because you can connect the build on components of the board to any desirable pin of the MCU.



Useful Link
https://www.ccsinfo.com/forum/viewtopic.php?t=32211&view=next


Board settings

First connect the board to USB port and switch it ON. Then tutn ON the number 8 bit on SW11 and SW1.






Example Code

Button example

//Include the header for 16F877A.
#include <16F877A.h>

//We use an xtal crystal, no watchdog,
//no low-voltage protection and no code protection.
#fuses XT, NOWDT, NOLVP, NOPROTECT

//The crystal is at 4,000,000 Hz.
#use delay (clock=4M)

//Set button to pin RB0.
#define BUTTON PIN_B0

//Set LED on pin RCO.
#define LED    PIN_C0

//This is the starting point of the program.
void main()
{
   //Infinite loop.
   while(1)
   {
     //If the button is not pressed switch off the led.
     if (input(BUTTON) == 1)
     {
       output_low(LED);
     }

     //If the button is pressed switch on the led.
     else if (input (BUTTON) == 0)
     {
       output_high(LED):
     }

     //Wait for 50 milliseconds.
     delay_ms(50);
   }
}



Button state example

//Include the header for 16F877A.
#include <16F877A.h>

//We use an xtal crystal, no watchdog,
//no low-voltage protection and no code protection.
#fuses XT, NOWDT, NOLVP, NOPROTECT

//The crystal is at 4,000,000 Hz.
#use delay (clock=4M)

//Set button to pin RB0.
#define BUTTON  PIN_B0

//Set LED on pin RCO.
#define LED  PIN_C0

//This is the starting point of the program.
void main()
{  
   //Create a LED state and a Button state.
   int1 Led_State = 0, Btn_State = 0;
 
   output_high(LED);

    //Infinite loop.
    while (1)
    {
       //Read the RB0 and store it in Button state.
       Btn_State = input(BUTTON);

       //If LED is off and press the button toggle the LED.
       if ((Led_State == 0) && (Btn_State == 1) )
       {
         output_toggle(LED);
       } 

     //The current LED state is the button state.
     Led_State = Btn_State;
 
     //De-bounce delay.
     delay_ms(20);
   }
}



DPA No.1 example

For this example replace the following line of code in the Button example code


//Set button to pin RB0.
#define BUTTON  PIN_B0

to

#define BUTTON  PIN_B4

For the Direct Port Access N0.1 example you have to connect with a jumper wire the RB4 to RB0.


 

DPA No.2 example

For this example replace the following lines of code in the Button state example code

//Set button to pin RB0.
#define BUTTON  PIN_B0

to

#define BUTTON  PIN_B4

and
//Set LED on pin RCO.
#define LED  PIN_C0

to

#define LED  PIN_D4


For the Direct Port Access N0.2 example you have to connect with a jumper wire the RD4 to RC0.