Wednesday, April 28, 2021

PIC16F887 PWM Programming in MikroC

Introduction

Pulse Width Modulation (PWM) generate an analog output waveform from microcontroller output pin. Enhanced Capture/Compare/PWM (CCP1) peripheral of PIC16F887 is able to generate analog output signal.

PIC16F887 PWM Programming in MikroC
Block diagram of PWM module of CCP1
There are some related registers of SFR to configure in program to make PWM work. However in MikroC, there is a PWM library for this peripheral. In this post we will make this module work first without making any complication with SFR.

MikroC PWM Library

PWM library could be found in Hardware Library of MikroC compiler for PIC. There are four function in this library to access PWM module.  

1- void PWMx_Init(long freq);

This function initializes PWM module with 0% duty cycle. The frequency parameter "freq" is specified in Hz. This function returns nothing. For example,

PWM1_Init(5000);

which initialize PWM of CCP1 module with a frequency of 5kHz.

2- void PWMx_Set_Duty(unsigned short duty_ratio);

Set any PWM module duty cycle. Parameter duty_ratio varies between 0 and 255. Actual duty cycle of PWM output is in % which is 

(Percent*255)/100 (in %).

This function returns nothing.



3- void PWMx_Start(void);

This function starts PWM waveform generation. For example,

PWM1_Start();

generates output PWM waveform on its corresponding pin.

4- void PWMx_Stop(void);

This functions stop PWM output generation, for example, PWM1_Stop(); .

We will discuss about the details of PWM peripheral here. We will use this library functions to program PWM of PIC16F887.

PWM Programming in MikroC

In this introductory programming example, we will use two analog input channels to adjust two PWM outputs via CCP1 and CCP2. Two variable resistors connect to AN5 and AN6 vary input analog values.

PIC16F887 PWM Programming in MikroC
Schematic diagram

Two DC motors connect to CCP1 and CCP2 output of PIC16F887, driven by their corresponding transistors. Output frequency of these two PWM output are fixed of 5kHz. Their output speeds vary by variation of PWM duty cycle.

MikroC program of this example is shown below.

  1. unsigned ch1,ch2;
  2.  
  3. void PORT_Init() {
  4. PORTE=0x00; // clear porta
  5. TRISE=0x03; // RA0:RA1 are input
  6. ANSEL|=0x60; // select AN5:AN6
  7. OSCCON|=0x70; // select INTRC
  8. }
  9.  
  10. void System_Init() {
  11. ADC_Init(); // Initalize ADC with default
  12. PWM1_Init(5000); // Initalize CCP1 with 5kHz
  13. PWM2_Init(5000); // Initalize CCP2 with 5kHz
  14. PWM1_Start(); // start PWM1 with 0%
  15. PWM2_Start(); // start PWM2 with 0%
  16. }
  17.  
  18. void analog_speed() {
  19. ch1=ADC_Get_Sample(5); // read POT at AN0
  20. ch1=255.0*ch1/1024; // scale it to 0:255
  21. delay_ms(10);
  22. ch2=ADC_Get_Sample(6); // read POT at AN1
  23. ch2=255.0*ch2/1024; // scale it to 0:255
  24. delay_ms(10);
  25. PWM1_Set_Duty(ch1); // signal output
  26. PWM2_Set_Duty(ch2);
  27. }
  28.  
  29. void main() {
  30.  
  31. PORT_Init();
  32. System_Init();
  33. while(1)
  34. analog_speed();
  35. }

Click here to download this example 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)