/* Mock exam Q4 - Dec 9th 2025*/

#include <16F877A.h>
#use  delay (clock=4000000)//4MHz
#use  rs232(baud=9600, xmit=PIN_C6, rcv=PIN_c7)//setup for RS232 - virtual terminal

#define  LED      PIN_B0
#define  BUTTON   PIN_C0   //pull-up button

void main(){
   
   //set all pin states and start low/off. unused pins set as outputs. Best practice
   set_tris_a(0x00); //unused
   output_a(0x00);
   set_tris_b(0x00); //B0 LED
   output_b(0x00);
   set_tris_c(0x01); //Input pull-up button C0, 0b 0000 0001
   output_c(0x00);
   set_tris_d(0x00); //unused
   output_d(0x00);
   set_tris_e(0x00); //unused
   output_e(0x00);
   
   while(TRUE){//main loop
   
      if(input(BUTTON) == 0){       //when pull-upbutton pressed
         delay_ms(20);              //simple 20ms debounce
   
         if(input(BUTTON) == 0){       //if pull-upbutton still pressed - not noise
      
            output_high(LED);            //turn led on
            printf("LED ON\r\n");         //print LED ON and create a new line
            
            while(input(BUTTON) == 0){}//debounce while pull-up button pressed
         }//if button still pressedafter debounce
      }//if button pressed

   }//main loop
}//main


