PIC motor control code

Thread Starter

Shreder001

Joined Mar 7, 2009
22
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? BTW this was in the embedded systems forum but I thought it would be better suited here Thanks!!
 

elal

Joined Dec 19, 2008
6
Your code is hmmmm..... a bit messy , what Pic and what compiler are you using?
If you tell me what you are using i may be able to give you more specific help.
Try to use interrupts , read the pic datasheet about PWM and interrupts.
Read your compiler manual about interrupts.
 

nanovate

Joined May 7, 2007
666
Rich (BB code):
#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;

	}
}
 
Top