• 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.

PIC motor control coding problem

Status
Not open for further replies.

shredder001

New Member
Hello! This is my first post. I am currently planning to be able to control a motor from a PWM input. Since I am very new to C programing, I am going in steps at a time and using LED's to test. Here is what I have so far:

#include <pic.h>
__CONFIG(FCMDIS & IESODIS & INTIO & MCLRDIS & WDTDIS & UNPROTECT);
//Initialize
unsigned char TIMER_COUNT_LOW; // Holding register for TMR0 low byte
unsigned char TIMER_COUNT_HIGH; // Holding register for TMR0 high byte

main()
{
OSCCON = 0b00000001; // Internal oscillator for system clock
PORTC = 0x00;
CMCON0 = 0b00000111;
ANSEL = 0x00;
TRISC = 0b00111100;
PR2 = 0b11111111; //Set the PWM period by loading the PR2 register
CCPR1L = 0x00;
T2CON = 0b01011000;
TMR2IF = 0; //clear bit for timer2 module interupt flag

while(TMR2IF == 0) //Wait until timer2 overflows then continue
{
;
}
TRISC = 0x00;
ECCPASE = 1; // for future part of program
PORTA = 0x00;
CMCON0 = 0b00000111; // comperators off
ANSEL = 0x00; //digital
TRISA = 0b00101000;
T1CON = 0b00010101;
while(1==1) // Start MainLoop
{
while(RA5 == 1) //Wait until RA5 goes low then continue
{
;
}
while(RA5 == 0) //Wait until RA5 goes high then continue
{
;
}
TMR1H = 0x00;
TMR1L = 0x00;
while(RA5 == 1) //Wait until RA5 goes low again
{
;
}
PORTC = TMR1L;

}
}


It starts timer1, then waits untill a low signal on RA5 (PWM input), then a high one, then it initializes timer1 and wait for a low signal. imediatly after that, it puts the timer1 value into a variable and the displays it on PORTC (LED array). I compiled and programmed it succesfully, but it does not seem to be displaying the value, any thoughts? Thanks!!
 

shredder001

New Member
Sorry forgot to mention that :D I am using a pic16f684. Birdman, does it really matter if I use the whole portc as output?
 

be80be

Well-Known Member
Theirs only 6 pins on portC of a 16f684 you only have 2 set as output

Firstly, you set PORTC as mostly output "TRISC = 0b00111100;"
So they wouldn't all be able to be outputs.
I think he been around me LOL he was saying you only have 2 pins as output on portc

should be this portc = 0b00000000;"
 
Last edited:

birdman0_o

Active Member
Your code looks like giberrish, there are a lot of loops that do nothing, could you explain it please?

It would help if you turned on the Timer2
T2CON = 0b01011100;
 

shredder001

New Member
Well, I am controling the speed and direction of the motor using a PWM input on RA3. Since I am doing it in sections, I havent made the rest of it yet. The first while loop;
while(RA5 == 1), waits until the signal on RA3 goes low then continues to the second; while(RA5 == 0), which waits until the signal on RA3 goes high then continues to reset the timer1 low and high byte registers. When the signal goes high again (third while), It throws the value of TMR1L into portc to be displayed, and then repeats.
 

be80be

Well-Known Member
try this and see what happens
Code:
main()
{

    PORTC = 0;
    CMCON0 = 7;                 //  Turn off Comparators
    ANSEL = 1 << 3;             //  RA4 (AN3) is the ADC Input

    ADCON0 = 0b00001101;        //  Turn on the ADC
                                //   Bit 7 - Left Justified Sample
                                //   Bit 6 - Use VDD
                                //   Bit 4:2 - RA4
                                //   Bit 1 - Do not Start
                                //   Bit 0 - Turn on ADC
    ADCON1 = 0b00010000;        //  Select the Clock as Fosc/8

    TMR2 = 0;                   //  TMR2 Provides PWM Period
    PR2 = 64;                   //  15 kHz PWM Frequency
    T2CON = 0b00000100;         //  Enable TMR2
    CCPR1L = 0;                 //  0 Duty Cycle to Start Off

    while(1 == 1)
    {
        NOP();
        for (Dlay = 0; Dlay < 6666; Dlay++);  //  100 ms between Samples
        NOP();

        GODONE = 1;             //  Read Pot Value
        while (GODONE);

        ADCValue = ADRESH;      //  Read in ADC Value
        if (ADCValue > 0x80)    //  go Forwards
        {
            CCPR1L = (ADCValue - 80) >> 1;
            CCP1CON = 0b01001110;
            TRISC = 0b011011;   //  RC5/RC2 Output, RC3/RC4 Input
        }
        else                    //  Go in Reverse
        {
            CCPR1L = (ADCValue  ^ 0x7F) >> 1;
            CCP1CON = 0b11001110;
            TRISC = 0b100111;   //  RC5/RC2 Output, RC3/RC4 Input
        }  //  fi
    }  //  elihw
}  //  End cMotor
it reads a pot and sets the speed by it
 
Last edited:

shredder001

New Member
Thanks guys, I got it working (turns out I forgot to turn TMR2 on). If I need any help on the next part, Ill be sure to ask. Thanks!!
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top