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

Labels

ADC (10) Analog (14) Arduino (12) Atmega16 (19) Audio (2) AVR (20) Charger (1) Cortex-M0 (1) Counter (10) CPLD (25) Digital I/O (22) Display (34) EEPROM (2) Environment Sensor (1) esp8266 (2) Experiment Board (10) I2C (4) Interrupt (7) LCD (1) LDmicro (29) measurement and instrumentation (7) Microchip Studio (3) MikroC (1) One-Shot (3) OpAmp (1) PCB (31) PIC16 Microcontrollers (16) PIC16F877A (2) PIC16F887 MikroC (22) PLC (35) PWM (11) Regulator (1) RTC (2) Sensor (8) Shift Registers (5) SPI (5) Timer (34) UART (2) ultra-sonic sensor (1) USB (1) VHDL (21) xc8 (1) XC95108 (9) XC9536 (15) XC9572 (1) Xilinx (23) Xilinx ISE (22)