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. 


 

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)