pia-468x60

pia-728x90

Tuesday, December 29, 2020

PIC16F887 Timer0 Works in Counter Mode MikroC

Introduction

A fundamental timer/counter module usually come with an 8-bit microcontroller. It's a set of registers composing of a free running counter with its operating control registers, and its interrupt flag.

This microcontroller inside's module has two operating modes - timer and counter mode. Timer mode is useful for creating a precise timing delay, measuring duration of incoming external pulse, etc. Counter mode store a numbers of external incoming input pulse.



PIC16F887 Timer0 Module

As it's mentioned earlier, this module could work in two modes. PIC16F887 Timer0 module is in similar way with some additional features:

  1. Switching between timer and counter mode
  2. Prescaler selection bit for both modes
  3. Interrupt control
  4. Clock edge selection for counter mode 
 
PIC16F887 Timer0 Works in Counter Mode MikroC
Timer/WDT Prescaler Block Diagram

Registers

Four registers of SFR relates to this module.

PIC16F887 Timer0 Works in Counter Mode MikroC
Summary of registers relate to Timer0

Timer0 register TMR0 is an 8-bit register for both timer and counter mode. If overflows at the top (255 or 0xFF in hexadecimal) and rolls back to bottom ( zero ) at the moment its interrupt flag is set.

Interrupt Control Register (INTCON) handles interrupt programming relates to Timer0 module (for both modes). In this post we don't discuss more about it.

Option Register OPTION_REG configures the operation for both modes. It includes prescaler selection, clock edge selection, and switching between timer and counter mode.

Switching Timer0 to Counter Mode

In this section OPTION_REG configures the process of this module.



TMR0 Clock Select bit (T0CS) switches between these two modes. Setting it to put Timer0 working in counter mode.


TMR0 Clock Clock Select bits (T0CS) select input clock edge in counter mode. Setting it to select falling edge of input pulse.

Prescaler Assignment bit (PSA) switches between Watch Dog Timer (WDT) and Timer module. Set it to assign prescaler selection to WDT.

Prescaler Rate Select bits (PS<2:0>) adjust its input rate between 1:1 and 1:256.

PIC16F887 Timer0 Works in Counter Mode MikroC
Prescaler Rate Select bits of OPTION_REG

In counter mode RA4/T0CKI pin of Port A must configured to digital input to count external input pulse.

Counter Mode Programming in MikroC

In this programming example, the controller works in counter mode showing counting value on a single 7-Segments display. The display shows up to 16 counts.

PIC16F887 Timer0 Works in Counter Mode MikroC
Schematic Diagram

 Its internal 8MHz oscillator frequency is used here.

***************************************************************************** 


*****************************************************************************

 Click here to download source file.

PIC16F887 Timer0 Works in Counter Mode MikroC
Program simulation

 



Friday, December 25, 2020

PIC16F887 IOCB in motor control example using MikroC

Introduction

We have discussed about interrupt-on-change of Port B in PIC16F887 microcontroller in previous post. Programming and prototyping were done using MikroC and Proteus VSM.

Using interrupt the program and hardware operate in a very responsive manner. Main program keeps executing its major tasks. In this example we will use the features of Port B interrupt-on-change to increase, decrease, start, and stop a DC motor drive.

Pulse Width Modulation (PWM) of a microcontroller is usually need in DC motor driving. However within this introductory PIC tutorial we will not drive deeper into PWM module of PIC16F887. PIC C program will be written to implement a software delay library in MikroC to produce a variable PWM signal with a specific frequency and adjustable duty cycle.

This is just a software emulation of PWM. Using this technique we can create a lower frequency of signal. It's not robust and advance like using a hardware PWM module of PIC microcontroller.

Circuit Design

Circuit diagram design and testing of embedded controller program are tested using Proteus VSM simulator. At my workshop I don't have any laboratory equipment like oscilloscope or function generator. It's enough to run this microcontroller in simulator.

PIC16F887 IOCB in motor control example using MikroC
Schematic Diagram designed in Proteus simulator
 

Microcontroller use its internal maximum 8MHz oscillator. Three push buttons connect to RB5, RB6, and RB7 control the rotation of DC motor. They are interrupt driven. DC motor is assumed to work in +12V with an additional transistor driver.

Programming in MikroC

C program configures the controller to access its internal 8MHz oscillator. Port B interrupt-on-change occurs at the low logic level of Port B input, as any button is being pressed. RB3 generates an software PWM to drive its output DC motor.



Since there's no hardware PWM module used in this example we use Vdelay_ms to create ON and OFF delay time. These delay times vary the output duty cycle of software PWM waveform. 

void Vdelay_ms(unsigned time_in_ms);

This MikroC function creates a software delay in duration of variable time_in_ms milliseconds.

  1. #define MOTOR PORTB.RB5 // Motor Connected to RB5
  2. #define Period 20 // MAX TIME IS 20ms
  3. bit run_stop;
  4. char speed;
  5.  
  6. void interrupt() {
  7. if(INTCON.RBIF)
  8. {
  9. if(PORTB.RB2^1) { run_stop^=1; delay_ms(200); }
  10. if(PORTB.RB1^1) if(speed>0) speed--;
  11. if(PORTB.RB0^1) if(speed<20) speed++;
  12. }
  13. INTCON.RBIF=0;
  14. }
  15.  
  16. void motor()
  17. { char ON,OFF;
  18. ON=Period-speed;
  19. OFF=Period-ON;
  20. if(run_stop) {
  21. MOTOR=1;
  22. Vdelay_ms(ON);
  23. MOTOR=0;
  24. Vdelay_ms(OFF);
  25. }
  26. }
  27.  
  28. void main() {
  29. speed=20;
  30. run_stop=0;
  31. PORTB=0x00; // CLEAR PORTB
  32. TRISB=0x07; // RB0-RB2 ARE INPUTS
  33. ANSELH=0x00; // DISABLE ALL ANALOG FUNCTION
  34. OPTION_REG.F7=0; // ENABLE WPUB
  35. WPUB=0x07; // TURN ON WPUB FOR RB0-RB2
  36. OSCCON|=0x70; // SELECT INTRCIO 8MHz
  37. INTCON.GIE=1; // TURN ON GLOBAL INTERRUPT
  38. INTCON.RBIE=1; // TURN ON IOCB
  39. IOCB=0x07; // RB0-RB2 USE TO CREATE INTERRUPT
  40. INTCON.RBIF=0; // CLEAR INTERRUPT FLAG
  41. while(1)
  42. motor();
  43. }

Simulation of Program

Proteus VSM simulator works well for this embedded controller program.



PIC16F887 IOCB in motor control example using MikroC
Program simulation in Proteus

Output software PWM waveform has a frequency of 50Hz measured by virtual frequency meter. Click here to download this example in zip file package. 


 

Monday, November 2, 2020

PIC16F887 Interrupt-On-Change in MikroC

Introduction

Port B of PIC16F887 is individually able to trigger interrupt signal to the controller. It's called INTERRUPT-ON-CHANGE. It also cover the external interrupt on RB0. However this interrupt source comes with most of new version of PIC microcontroller.

There are some registers relate to this interrupt source, IOCB, and INTCON.

Interrupt Control Register - INTCON

This register contains control and flag bit of interrupt-on-change.

PIC16F887 External Interrupt Example With 7-Segments Display In MikroC
Interrupt Control Register (INTCON)

Set the Global Interrupt Enable bit (GIE) to turn on interrupt for PIC16F887 microcontroller. Bit 3 is called PORTB Change Interrupt Enable bit (RBIE). This bit must be set to turn on the PortB-Interrupt-On-Change. RBIF is PORTB Change Interrupt Flag bit. In the ISR the program must test this bit for its interrupt request.

Interrupt-On-Change PORTB Register - IOCB

This 8-bit wide special function register configures the present of PortB-Interrupt-On-Change.

PIC16F887 Interrupt-On-Change in MikroC
Interrupt-On-Change PortB Register - IOCB
It corresponds to all 8 pins of PortB. It can be individually turn on and off up to the user.

IOCB of PIC16F887 Programming in MikroC

Introduction

There are some more register to make this programming work well. Port B weak pull up resistors must be turned on to save component counts. This job will be done with the OPTION_REGand the WBPU register.

Its internal oscillator with a frequency of 8MHz could fit this simple task. Program will need to configure the OSCCON register to select its 8MHz internal oscillator.



Unlike external interrupt, this interrupt source doesn't have edge selection control bit. Its interrupt flag RBIF is set at the instance of port reading mismatch the previous port value.

Hardware

I made its schematic in Proteus due to its ease of design.

PIC16F887 Interrupt-On-Change in MikroC
Schematic Diagram

SW1 connects to RB7 creates interrupt when it's pressed (shorted to ground). D1 toggles each time the interrupt occurs.

Programming in MikroC

The controller is configured to clock from its internal 8MHz oscillator. Port B input doesn't need external pull up resistor since it's internally connected to its weak pull up resistor by software.

Interrupt-On-Change occurs when the input logic to RB7 change from high to low.

  1. void interrupt() {
  2. if(INTCON.RBIF)
  3. if(PORTB.RB7==0) // check if RB7 is shorted to GND
  4. PORTB.RB0^=1;
  5. INTCON.RBIF=0;
  6. }
  7.  
  8. void main() {
  9. PORTB=0x00; // CLEAR PORTB
  10. TRISB=0x80; // RB7 IS INPUT
  11. ANSELH=0x00; // DISABLE ALL ANALOG FUNCTION
  12. OPTION_REG.F7=0; // ENABLE WPUB
  13. WPUB=0x80; // TURN ON WPUB FOR RB7
  14. OSCCON|=0x70; // SELECT INTRCIO 8MHz
  15. INTCON.GIE=1; // TURN ON GLOBAL INTERRUPT
  16. INTCON.RBIE=1; // TURN ON IOCB
  17. IOCB=0x80; // RB7 IS USE TO CREATE INTERRUPT
  18. INTCON.RBIF=0; // CLEAR INTERRUPT FLAG
  19. while(1); // STAY HERE
  20. }

Both MikroC and Proteus simulation were recorded in this video.


 

Labels