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. 


 

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)