Posts

Showing posts from January, 2018

How to Program Interrupts in PIC16F877A

Image
There are 15 interrupts available in PIC16F877A controller. I will try to demonstrate individual interrupt so that you can easily understand. The list is the following. 1. External Interrupt 2. RB Port Change Interrupt 3. Timer 0 Interrupt 4. Timer 1 Interrupt 5. Parallel Slave Port Read/Write Interrupt 6. A/D Converter Interrupt 7. USART Receive Interrupt 8. USART Transmit Interrupt 9. SPI (Slave) Interrupt 9. Synchronous Serial Port Interrupt 10. CCP1 (Capture, Compare, PWM) Interrupt 11. CCP2  (Capture, Compare, PWM) Interrupt 12. TMR2 to PR2 Match Interrupt 13. Comparator Interrupt 14. EEPROM Write Operation Interrupt 15. Bus Collision Interrupt 1. External Interrupt (RB0) The following code demonstrates how to use external interrupt (RB0). A push button is connected to RB0/INT, when push button is pressed a high to low signal is generated. As in option register external interrupt is configured on falling edge so interrupt is generated and interrupt serv

How to Program CCP2 Module in PIC16F877A (10-Bit PWM Mode)

Image
The following code demonstrate how to use CCP2 module in PWM mode with 10-bit of resolution. On initialization PWM duty cycle is set to zero, when "DUTY INCREASE" button is pressed pulse width start increasing and by pressing "DUTY DECREASE" button, pulse width start decreasing. The code is written in “mikroC PRO for PIC v.6.6.3” IDE and simulation is done with Proteus 8.0 SP0. At the end of code, you can find complete project files for download. Code in mikroC // define crystal frequency #define _XTAL_FREQ 20000000 // timer-2 prescalar #define TMR2_PRESCALE 4 sbit DUTY_INC_Dir at TRISB.B0; sbit DUTY_DEC_Dir at TRISB.B1; sbit DUTY_INC at PORTB.B0; sbit DUTY_DEC at PORTB.B1; long pwm_freq = 5000; PWM2_Initialize(void); PWM2_Duty(unsigned int duty); void main(void) { unsigned int count = 0; // make as input DUTY_INC_Dir = 1; DUTY_DEC_Dir = 1; // init pwm2 PWM2_Initialize(); PWM2_Duty(0); while(1) { if(DUTY_INC ==

How to Program CCP1 Module in PIC16F877A (10-Bit PWM Mode)

Image
The following code demonstrate how to use CCP1 module in PWM mode with 10-bit of resolution. On initialization PWM duty cycle is set to zero, when "DUTY INCREASE" button is pressed pulse width start increasing and by pressing "DUTY DECREASE" button, pulse width start decreasing. The code is written in “mikroC PRO for PIC v.6.6.3” IDE and simulation is done with Proteus 8.0 SP0. At the end of code, you can find complete project files for download.   Code in mikroC // define crystal frequency #define _XTAL_FREQ 20000000 // timer-2 prescalar #define TMR2_PRESCALE 4 sbit DUTY_INC_Dir at TRISB.B0; sbit DUTY_DEC_Dir at TRISB.B1; sbit DUTY_INC at PORTB.B0; sbit DUTY_DEC at PORTB.B1; long pwm_freq = 5000; PWM1_Initialize(void); PWM1_Duty(unsigned int duty); void main(void) { unsigned int count = 0; // make as input DUTY_INC_Dir = 1; DUTY_DEC_Dir = 1; // init pwm1 PWM1_Initialize(); PWM1_Duty(0); while(1) { if

How to Program Parallel Slave Port (PSP) in PIC16F877A

Image
The following code demonstrate how to use parallel slave port module. The controller offers a mechanism by which an 8-bit parallel bidirectional data transfer can be achieved between a PIC16F877A and other PSP supporting devices. The PIC16F877A's Port-D and Port-E are used in this data transfer. For this data transfer, Port-D is configured as a parallel slave port (PSP) by setting bit-4 of TRISE Register. The pins of Port-E function as control pins for data transfer. The code is written in “mikroC PRO for PIC v.6.6.3” IDE and simulation is done with Proteus 8.0 SP0. At the end of code, you can find complete project files for download. UUT Code in mikroC // function proto-type void PSP_Setting(void); void main(void) { // portb as input port TRISB = 0xFF; // portc as output port TRISC = 0x00; // portc init to 0x00 PORTC = 0x00; PSP_Setting(); while(1) { // write request to PSP if(TRISE.IBF == 1) { PORTC = PORTD; // auto clear