Pages

Wednesday, October 7, 2020

Blinking the PIC16F887 in MikroC

Starting to program a micro-controller is usually writing a code with blinking an LED to make sure that the introductory coding works correctly. Most of embedded C compiler makes a ready to use examples include all the features of their compiler and the target MCU.

However I make an easy beginning to code the PIC16F887 8-bit MCU with MikroC for PIC. A free version of this compiler offers a free firmware building of the coding size that does not exceed 2 Kb of MCU program memory.

Using  PORTD of the PIC16F887, the CPU regularly toggle pin RD0 for every one second. The delay_ms() function of the MikroC create an acceptable delay time in milli-seconds. 

  1. #define LED PORTD.RD0 // LED IS AT RD0
  2. #define rate 1000 // rate is 1000ms
  3.  
  4. void main() {
  5. LED=0; // CLEAR LED
  6. TRISD=0x00; // PORTD AS OUTPUT
  7. while (1)
  8. {
  9. LED=0; // LED OFF
  10. delay_ms(rate); // wait for 1000ms
  11. LED=1; // LED ON
  12. delay_ms(rate); // wait for 1000ms
  13. }
  14. }

The input parameter to the delay_ms() function is a unsigned long data type. However it is a constant rather than a variable.

Blinking the PIC16F887 in MikroC
Schematic Diagram


No comments:

Post a Comment