Saturday, 4 July 2015

Lesson2 Switch Controlled LED (MikroC)

Switch Controlled LED

In this tutorial we're going to show how a push button switch is used to turn ON and OFF an LED. The target MCU is PIC 16F877A. We'll introduce 2 registers here together with the I/O registers discussed in Lesson 1.

  •  It is ADCON1 register and CMCON register.
  •  In PIC 16F877A, PORTA is a 6 bit wide bi-directional port. Apart from RA4 (PORTA BIT 4), Other PORTA pins are multiplexed with analog inputs and the analog VREF input for both the A/D converters and the comparators.

Switches accepts digital inputs, so we'll set the bits of ADCON1 to digital input by writing 0x06 (6 in decimal) to the register

**ADCON1 - Analogue/Digital Control Register 1 ; used to make a PORT Pin with A/D module either digital or analogue
**CMCON - Comparator Control Register ; controls the comparator input and output multiplexers

*** For ADC operation we don't need CMCON Register so we are turning it OFF by writing 0x07 (7        in decimal) to the register.

You can read more about the ADCON1 and CMCON register from the device data sheet, www.microchip.com

CIRCUIT DIAGRAM

The switch can be connected in 2 ways :

  • In Active High Mode - In this mode , the PORT pin is High (Logic 1) when the button is pressed. Here the button is connected between the supply voltage and the PORT pin, a pull down resistor is connected between the PORT pin and ground.                                                         
    PUSH BUTTON ON PORTA BIT 1 CONNECTED IN ACTIVE HIGH MODE


  • In Active Low Mode - When the button is pressed the PORT pin is low (Logic 0). Here the PORT pin is connected through a pull up resistor to the supply voltage, and the button is connected between the PORT pin and ground .
    PUSH BUTTON ON PORTA BIT 1 CONNECTED IN ACTIVE LOW MODE
    **The button connected to the Master Clear Pin (MCLR) is the RESET BUTTON. It is used to RESET the MCU.                                                                                                                             
**Switch Debounce : when a switch closes, the switch makes several contacts quickly(open and closes several times). This contacts can be read by the MCU, thereby causing the wrong switch state to be read. One way of eliminating switch bounce is to delay reading the input after the switch changes its state, say 10ms.
Here is the MikroC code:

/**********************************
Project Name :
   SWITCH CONTROLLED LED
Description :
   This project demonstrate how to controll an LED using a push button switch,the LED is connected to PORTB BIT ZERO
Test configuration:
   MCU:  PIC 16F877A
   Oscillator: XT 8.0000 MHz, 8.0000 MHz Crystal
***********************************/

void main() {
TRISB.B0 = 0;     // PORTB Bit Zero as OUTPUT
TRISA = 0xFF;   // PORTA set as input.FF is hexadecimal equivalent of 255(decimal), 11111111(binary)
CMCON = 0x07;    // Turn OFF Comparator
ADCON1 = 0x06;  // configure  PORTA as digital input
PORTB.B0 = 0;
while(1){
if(PORTA.B1){
delay_ms(10);   // Switch debounce
if(PORTA.B1){
PORTB.B0 = 1;   //turn ON PORTB.B0
}
}
else  {
PORTB.B0 = 0; }   //turn OFF PORTB.B0
}
}

No comments:

Post a Comment