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.

Simple C18 Code For Timer / PWM

Status
Not open for further replies.

Obi_X

New Member
I've been looking for any functional examples of code in C18 that will generate a simple PWM type signal that I can connect an oscilloscope to and observe...

I am using a PIC18F4550.

I've tried countless things that are 'supposed' to work, but I must be missing something in the theory, can anyone give me a functional piece of C18 code that I can measure on an o'scope? The end result I'm after is to control a stepper motor, so something with about a 20ms period and a 1.5ms DutyCycle.

I can't believe it is this difficult, must have enjoyed the weekend to much and sacrificed those brain cells :eek:

Thanks!
 
Here's some simple code to drive a servo. I think you meant servo as these require a 1-2mS pulse every 20mS.

This code uses the special event trigger and polls for the interrupt flag. It could be modified to use interrupts but you did ask for simple. This is untried but it work fine in the simulator.

Mike.
Code:
#include <p18f4550.h>

#pragma config WDT = OFF, LVP = OFF, FOSC = INTOSCIO_EC

#define ServoPin LATBbits.LATB0
#define ServoTris TRISBbits.TRISB0

void main(void){
int ServoPos;
    ADCON1=0x0f;
    CMCON=7;
    ServoTris=0;                //make bit 0 output
    ServoPin=0;
    CCP1CON=0b00001011;         //Special event trigger
    T1CON=0b10010001;           //Timer 1 on with Pre=2
    ServoPos=1500;              //set servo to mid position
    CCPR1=ServoPos;             //set CCP initial value   
    while(1){
        while(!PIR1bits.CCP1IF);    //wait for CCP interrupt bit
        ServoPin=0;                 //end pulse
        CCPR1=20000-ServoPos;       //Off time = 20mS - Servo Time
        PIR1bits.CCP1IF=0;          //clear int flag
        while(!PIR1bits.CCP1IF);    //wait for int flag
        ServoPin=1;                 //start pulse
        CCPR1=ServoPos;             //Servo time in uS
        PIR1bits.CCP1IF=0;          //clear int flag
    }
}
 
This is awesome! Thanks Pommie, I will be back at me 'workbench' tomorrow and give it another go. Currently I'm just making it work using Delay1KTCYx() but this doesn't seem like it is the best way.

What is the advantage/disadvantage to using interrupts in a servo motor configuration?
 
If you use interrupts it only takes a miniscule amount of the processor time so you can do lots of other things at the same time.

Mike.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top