Thursday, October 8, 2020

PIC16F887 toggling an output relay

In the previous post, we have discussed about using the digital input/Output of PORTA. Now let move to a typical example of turn on a relay on and off. This job is easily done by inverting the output bit of PORTA register.

PIC16F887 toggling an output relay
Schematic Diagram

Each time the switch connects to RA0 is pressed, the CPU toggle the output relay.

  1. #define RLY PORTA.F1
  2. #define SW PORTA.F0
  3.  
  4. void main() {
  5. PORTA=0x00; // CLEAR PORTA
  6. TRISA=0x01; // RA0 IS INPUT
  7. ANSEL=0x00; // Disable all analog function
  8. while(1)
  9. {
  10. if(SW==0)
  11. {
  12. while(SW==0) ; // wait for switch release
  13. RLY=RLY^1; // toggle relay on/off
  14. }
  15. }
  16. }

No comments:

Post a Comment

DIY PIC18F4550 USB Prototype Board

DIY PIC18F4550 USB Prototype Board
Prototype Board for PIC18F4550 and other 8-bit 40-Pin PIC Microcontroller Experiment Board for Hobbyists

Labels