Playing with a C Programming
                     and PIC16F877A


If you would like to play with a C programming language and PIC16F877A, it's very easy to get started. What you need is a desktop computer and CCS C compiler with PIC16F877A development kit. There are many suppler out there that can supply this at a very cheap cost.


15 easy step to make you write your first intelligent program in CCS C:

1. Select your PIC type.
#include <16f877a.h>

2. Select your oscillator speed.
#USE DELAY( CLOCK=20000000 ) //20MHz crystall

3. Select the oscillator type.
#FUSES HS,NOWDT,NOPROTECT

4. Define the Address of PORTB
#byte port_b=6 

5. Define the Address of PORTD
#byte port_d=8

6. Declare main program.
main(){

7. Set port_b0 to be input.
set_tris_b(0b00000001);  

8. Set D0,D1 as output (D0=LED, D1=Relay to Motor)    
set_tris_d( 0b00000000);

9. Initialize all PORTD output to 0 Volt.
port_d = 0;  

10. Do forever
for(;;){ 

11. Check and test PortB pin 0 voltage.   
if ( INPUT(PIN_B0) == 0 )

12. Turn LED and Motor ON when button is pressed.
{
       output_high(PIN_D0);  //LED Turn ON
       output_high(PIN_D1);  // Motor Turn ON
   }


13. Turn LED and Motor ON when button is not pressed.
    
else
      {
        output_low(PIN_D0);  //LED Turn OFF
        output_low(PIN_D1);  // Motor Turn OFF
       }


14. Close the for loop.
   } // close for loop

15. Close the main program.
} // close main



This program seems too simple that normal connection of power supply to push button and LED can do it. But make the LED blink ON for 1 seconds and OFF for 1.5 seconds repeatedly as long as you pressed the button. Then it is so difficult to do it without using PIC as you need components such as timer, counter and so on. However PIC don’t need other component to do it. All already inside the PIC chip. To blink the LED this code can be added :

if ( INPUT(PIN_B0) == 0 )
for(;;)
  {
       output_high(PIN_D0);  //LED Turn ON
             delay_MS(1000); // delay 1 seconds
       output_low(PIN_D0);  //LED Turn OFF
             delay_MS(1500); // delay 1.5 seconds
   }


                                                               
Next Page >>
<< Back to Electronics