Controlling LED by using Switch with C program | applied electronics engineering

Latest

Controlling LED by using Switch with C program

By Applied Electronics - Tuesday, June 17, 2014 No Comments
Using Proteus, MPLAB and CCS C compiler we can test C programs with microcontroller interactively. Here is an example of how a LED can be switched ON and OFF with the microcontroller by writing the program for doing this in C language.

In the schematic diagram below, Port GP0(or A0) of PIC12F675 microcontroller is pulled to +5V via the 10k resistor. That is the signal read by the port A0 is 1. When the switch is closed all the signals flows into the ground and the input state read by port A0 becomes 0.



In the above schematic, when the switch is closed the LED should turn ON. To do this, we read the value at port A0. When the value is high(1) then LED is turned OFF and when the value is low(0) then the LED should turn ON.

To read the input signal in port GP0 the CCS C function is,

input(PIN_A0)

where, GP0 as in datasheet corresponds to A0 port

To turn the LED ON if the input is high and turn LED OFF if the input is low the C program is,

 if(input(PIN_A0) ==0)
       output_high(PIN_A1);
     else
        output_low(PIN_A1);

The complete C program is,

#include "12F675.h"
#use delay(clock=4M)
#use rs232(baud=9600, xmit=PIN_A1, rcv=PIN_A2)

void main()                       
{

while(1)

    {
   
     if(input(PIN_A0) ==0)
       output_high(PIN_A1);
     else
        output_low(PIN_A1);

    }
}

The program can be build with MPLAB IDE software with build-in CCS C compiler. The picture shows this process in MPLAB.


Once the program is written and successfully compiled it should be burned into the microcontroller. In proteus this corresponds to uploading the generated .hex or .cof file into the microcontroller. Once this is done the simulation can be run. After running the program the switch should be closed to see whether the LED is turned ON or not.

The picture shows LED ON state when the input is low that is when switch is closed.


See more Microcontroller Tutorials

No Comment to " Controlling LED by using Switch with C program "