0
votes

Im trying to create an embedded c code to control a dc motor with the PIC32MX460F512L microcontroller. Ive Configured the system clock at 80MHz, and the peripheral clock at 10MHz, Am using Timer 1 for pulsing the PWM with a given duty cycle, and Timer 2 for measuring the motor run time. I have a header file(includes.h) that contains system configuration information eg clock. Ive created most of the functions but some are a bit challenging. For example, initializing the LEDS and the functions for forward, backward movements and stop, I wanted the dc motor to run in forward direction for 4 sec at 70% duty cycle, then stop for 1 sec then reverse for 3 sec at 50% duty cycle and then stop for 1 sec and then forward again for 3 sec at 40% duty cycle, stop for 1 sec and finally forward for 5 sec at 20% duty cycle. Any suggestions for the forward, stop, and reverse functions

#include <stdio.h>
#include <stdlib.h>
#include <includes.h>

void main()
{
    // Setting up PIC modules such as Timers, IOs OCs,Interrupts, ...
    InitializeIO();
    InitializeLEDs();
    InitializeTimers();

    while(1) {
        WaitOnBtn1();
        Forward(4.0,70);
        Stop(1.0);
        Backward(3.0,50);
        Stop(2);
        Forward(3.0,40);
        Stop(1.0);
        Backward(2.0,20);
        LEDsOFF();
    }
    return;
}

void InitializeIO(){
    TRISAbits.TRISA6 = 1;
    TRISAbits.TRISA7 = 1;
    TRISGbits.TRISG12 = 0;
    TRISGbits.TRISB13 = 0;
    LATGbits.LATB12 = 0;
    LATGbits.LATB13 = 0;
    return;
}

void InitializeLEDs(){
    //code to initialize LEDS
}

void InitializeTimers(){
    // Initialize Timer1
    T1CON = 0x0000; // Set Timer1 Control to zeros
    T1CONbits.TCKPS=3; // prescale by 256
    T1CONbits.ON = 1; // Turn on Timer
    PR1= 0xFFFF; // Period of Timer1 to be full
    TMR1 = 0; // Initialize Timer1 to zero
    // Initialize Timer2
    T2CON = 0;
    T2CONbits.TCKPS = 7; // prescale by 256
    T2CONbits.T32 = 1; // use 32 bits timer
    T2CONbits.ON = 1;
    PR2 = 0xFFFFFFFF; // Period is set for 32 bits
    TMR2 = 0;
}

void WaitOnBtn1(){
    // wait on Btn1 indefinitely
    while(PORTAbits.RA6 == 0);

    // Turn On LED1 indicating it is Btn1 is Pushed
    LATBbits.LATB10 = 1;
    return;
}

void Forward(float Sec, int D){
    int RunTime = (int)(Sec*39000); // convert the total
    time to number of Tics
    TMR2 = 0;
    //LEDs
    LATGbits.LATG12 = 1; // forward Direction
    LATBbits.LATB12 = 0;
    LATBbits.LATB13 = 0;
    LATBbits.LATB11 = 1;
    // Keep on firing the PWM as long as Run time is not
    elapsed
    while (TMR2 < RunTime){
        PWM(D);
    }
    return;
}

void PWM(int D){
    TMR1 = 0;
    int Period = 400;
    while (TMR1< Period) {
        if (TMR1 < Period*D/100){
            LATGbits.LATG13 = 1;
        }
        else{
        LATGbits.LATG13 = 0;
    }
}
1
Suggestions? First ask a question. - Eugene Sh.
1) C does not support methods. 2) Learn How to Ask 3) Format&indent your code properly. 4) Use prototype-style function declarators. - too honest for this site
Im sorry about that, I meant functions; I will try to format my code better in future but meanwhile any suggestions - Chogo
It's impossible to tell anything without a schematic. And with the schematic it will fit more the SE EE - Eugene Sh.
I'm actually doing computer science and the code is simulated in mplab x ide and proteus - Chogo

1 Answers

0
votes

Functions, not methods, to be precise.

So what is exactly the question?

What I can say from a quick look on a source code:

LEDs initialisation should be done as you did in InitializeIO() function. Simply set proper TRISx bits to 0 to configure LED pins as output.

For the PWM and motor control functions you should take some time and try to understand how builtin PWM peripheral works. It is a part of OC (Output Compare) and it is very easy to use. Please, take look on following link http://ww1.microchip.com/downloads/en/DeviceDoc/61111E.pdf

and this one for the minimal implementation using builtin peripheral libraries https://electronics.stackexchange.com/questions/69232/pic32-pwm-minimal-example

Basically you need to set up OC registers to "make" OC module acts like PWM. You need to allocate one of the timers to work with OC module (it will be used for base PWM frequency) and that's it.

All you need after that is to set PWM duty cycle value by setting timer PRx register, you don't need to swap bits like in your PWM routine.

To stop it simple stop it simply disable the timer.

To run it again run the timer.

To change direction (it depends of your driver for the motor) I guess you need just to toggle direction pin.

I hope it helps...