Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PWM Using C18

Status
Not open for further replies.

mrgobis

New Member
Hi All

Do anyone have a clue how to set a PWM using C18 compiler. I had a good look in C18 user guide. it doesnt help in a graet deal. I am sure some one would help me with a sample code.

regards,
nathan

Plz see my code below.......it compiles with no error..cant see any output.
**********************************************************
#include <p18f4520.h>
#include <timers.h> //header file for Timers
#include <pwm.h> //header file for PWM


void main (void)
{

TRISC = 0b10000000; //All bits output except 7 (mode switch),

PORTC = 0;

OpenTimer2 (TIMER_INT_OFF & T2_PS_1_1 & T2_POST_1_1);
OpenPWM1 (0xFF); //Enable PWM1 and set period
OpenPWM2 (0xFF); //Enable PWM2 and set period
}

**********************************************************
 
Do anyone have a clue how to set a PWM using C18 compiler.

Here is a working code (18F4520)

Connect the "enable" pins to ccp1 and ccp2 (pin 16 and 17)
Connect the "motor drive" to
LEFT: C0, C3
RIGHT: E0, E1

SPEED_* macro's define DC for different speeds (0 - off, 1023 - on, between values are 0-100% dc)

delay functions are tuned to 10MHz crystal (uC running on 40MHz)

Code:
#include <p18cxxx.h>
#include <delays.h>
#include <pwm.h>

#pragma config OSC = HSPLL
#pragma config FCMEN = OFF
#pragma config IESO = OFF
#pragma config WDT = OFF
#pragma config MCLRE = ON
#pragma config PBADEN = OFF
#pragma config CCP2MX = PORTC
#pragma config LVP = OFF
#pragma config XINST = OFF

#define MLF LATCbits.LATC0 = 1; LATCbits.LATC3 = 0
#define MLR LATCbits.LATC0 = 0; LATCbits.LATC3 = 1
#define MLS LATCbits.LATC0 = 0; LATCbits.LATC3 = 0

#define MRF LATEbits.LATE0 = 1; LATEbits.LATE1 = 0
#define MRR LATEbits.LATE0 = 0; LATEbits.LATE1 = 1
#define MRS LATEbits.LATE0 = 0; LATEbits.LATE1 = 0

#define SPEED_MAX 1023
#define SPEED_FAST 800
#define SPEED_NORM 650
#define SPEED_SLOW 620
#define SPEED_TURN 620

#define GO_F() MRF; MLF
#define GO_R() MRR; MLF
#define GO_L() MRF; MLR
#define GO_S() MRS; MLS


volatile unsigned char dElAy;
void delay_10ms(unsigned char x){
  for (dElAy = 0; dElAy < x; dElAy++) Delay1KTCYx(100);
}
void delay_s(unsigned char x){
  for (dElAy = 0; dElAy < x; dElAy++) delay_10ms(100); 
}


void pwmInit(){
  //start pwm
  // 1/Fpwm = (period + 1) * 4 * 1/Fosc * TMR2 prescaler
  // 100KHz : 
  // 1/100000 = (period +1) *4/40000000
  // period = 40000000 / 400000 -1 = 99
  // 40KHz, period = 249
  // OpenPWM1(249);
  // OpenPWM2(249);

  T2CON = 0;
  OpenPWM1(0xde);
  OpenPWM2(0xde);


  //DC 0-1023 (10bit number)
  SetDCPWM1(SPEED_NORM);
  SetDCPWM2(SPEED_NORM);

}

void portInit(){
  ADCON0 = 0;
  ADCON1 = 0x0F;
  TRISA  = 0;
  TRISB  = 0;
  TRISC  = 0x80;
  TRISD  = 0;
  TRISE  = 0;

  LATA = 0;
  LATB = 0;
  LATC = 0;
  LATD = 0;
  LATE = 0;

}

void main(){
  signed char dx;

  portInit();
  pwmInit();

  dx = -50;

  while(1){
     GO_S();Delay1KTCYx(10);
     SetDCPWM1(SPEED_FAST +dx);
     SetDCPWM2(SPEED_FAST);
     GO_F();
     delay_s(5);

     GO_S();Delay1KTCYx(10);
     SetDCPWM1(SPEED_TURN +dx);
     SetDCPWM2(SPEED_TURN);
     GO_R();
     delay_s(1);

     GO_S();Delay1KTCYx(10);
     SetDCPWM1(SPEED_FAST +dx);
     SetDCPWM2(SPEED_FAST);
     GO_F();
     delay_s(5);


     GO_S();Delay1KTCYx(10);
     SetDCPWM1(SPEED_TURN +dx);
     SetDCPWM2(SPEED_TURN);
     GO_L();
     delay_s(1);

  }


}
 
Last edited:
How to set up PWM for LED dimming control on PIC18F8722?

Can someone please give some sample code on how to set up the PWM module on a pic18F8722 to control brightness of LEDs? I have connected the LEDs to portH bit 7,6,5 which has enhanced pwm featured.

I'm quite new to this and have not used the pwm module before, I've also just started experimenting with MPLAB. Any advise would be useful. Thanks!
 
Hi there WYJ,

I am also new to PIC programming. Like you, a couple of months ago I also was looking for some sample PWM code...

I have attached some very simple code to get you started.

I'd start by getting one LED working on one pin.

The easiest should be pin RC2 - CCP1 pin

#include <PIC18F8722.h> // Processor specific
#include <pwm.h> // Located in the h folder of MCC18
#include <timers.h> // Located in the h folder of MCC18

#pragma config DEBUG = ON // Turn off if you do not have a compatible debugger ICD2?
#pragma config OSC = HS // HS assumes external High Speed crystal has been used
#pragma config WDT = OFF
#pragma config LVP = OFF

void main (void)
{

TRISC=0;

OpenTimer2(TIMER_INT_ON & T2_PS_1_1 & T2_POST_1_1);

OpenPWM1(0x3f); // Period - frequency
while(1)
{
SetDCPWM1(255);
}
}

According to your PIC's datasheet this should produce a PWM frequency 156.25KHz in eight bit resolution. the SetDCPWM1(255) is where you specify the duty cycle for the pulse. As we selected 8 bit resolution, there are a possible 255 steps to the PWM.

If you change the 255 to a lower number between 255 and 0, you should notice the LED getting dimmer.

Hope this helps.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top