Servo control using PIC - Need help with C - RC Groups
Thread Tools
Dec 20, 2013, 03:16 PM
Registered User
Motions's Avatar
Discussion

Servo control using PIC - Need help with C


I'm working on a project that is killing me and I finally succumbed to posting on a forum. I need some help with a VERY simple script in C which I can't seem to compile.

If any PIC experts can help, I would be greatly appreciative.
Sign up now
to remove ads between posts
Dec 20, 2013, 03:43 PM
Registered User
jpparisy's Avatar
What do you want to do ?

Which PIC would you be to use ?

Do you know how works a servo ?
Dec 20, 2013, 03:47 PM
Registered User
Can we see the script?
Dec 20, 2013, 03:51 PM
"MAYONNAISE"
Acetronics's Avatar
see your circuit design and eventually know which compiler you use ...
Dec 20, 2013, 04:45 PM
Registered User
Motions's Avatar
Wow. quick responses.

It's a very simple project. I want to toggle a servo left-right using a push button switch.
1. Push button - position servo one way
2. Push button - position servo the other way

I'm using a 12F675. I do a lot of PIC projects but never done a servo using PIC. I do all my programming in Flowcode so I struggle like hell with C, ASM, BAS

Anyway, here's some code I found and altered a little.

Code:
#include <pic12f6x.h> 
#define BUTTON  PIN_A3 
#define Right		0 
#define Left		1 

void CCW(void) { 
	output_high(PIN_A2); 
	delay_us(2000); 
	output_low(PIN_A2); 
} 
void CW(void) {
	output_high(PIN_A2); 
	delay_us(700);
	output_low(PIN_A2); 
}

void main() { 

	int1  OUTPUT = 0; 
	
	CCW(); 
	delay_ms(60);				// 55 msec typical deadband max for servo 
	CCW(); 

	while (1) { 					// loop forever 

		// Button press
		if (input(button) == 1) {

			// Toggle OUTPUT value
			OUTPUT = !OUTPUT;

			// What is OUTPUT set at?
			if (OUTPUT == 0) {

				CCW(); 
				delay_ms(60); // deadband between any possible next move

			} else {

				CW(); 
				delay_ms(60);

			}

			// Wait for button release
			while (1) {

				//Delay: 50 ms
				delay_ms(50);

				if (input(button) == 0) break;
			}

		} // end Button press

	} // end while(1) 

}// end main
Last edited by Motions; Dec 21, 2013 at 09:44 AM.
Dec 20, 2013, 04:55 PM
Registered User
Motions's Avatar
Here's a flow chart of what it does.
Dec 20, 2013, 05:54 PM
Registered User
ttabbal's Avatar
You mentioned trouble compiling. What compiler and what errors are you getting? Or do you get a .hex and it just doesn't work properly?
Dec 20, 2013, 06:12 PM
Registered User
Motions's Avatar
I tried MPLAB 8.92 and MPLAB X and 3 different compilers. This is definitely not my thing and no idea what I'm doing.

I was hoping someone could guide me through it. I know this is a very simple project for someone that actually does it. I could use Flowcode but it only supports certain PICs which are 40 pin. Little over kill for toggling a servo. And I have to build 10 of them.
Dec 20, 2013, 07:24 PM
Registered User
jpparisy's Avatar
Here is the code, to be used with the HI-TECH C complier:

Code:
#include <htc.h>
#include <pic.h>
#include <pic12f675.h>
#define _XTAL_FREQ  4000000

#define BUTTON GP3 
#define Right	 0 
#define Left	 1 

// set fuse bits
__CONFIG(FOSC_INTRCIO & WDTE_OFF & BOREN_OFF & CP_OFF & CPD_OFF & MCLRE_OFF);
void CCW(void) { 
	GP2 = 1; 
	__delay_us(2000); 
	GP2 = 0; 
} 
void CW(void) {
	GP2 = 1; 
	__delay_us(1000);
	GP2 = 0; 
}
void main() { 
	int OUTPUT = 0; 

	OPTION_REG = 0b10011000;					// set OPTION register
	TRISIO = 0b001000;							// GP3 as input, all other as outputs

	CCW(); 
	__delay_ms(60);	 // 55 msec typical deadband max for servo 
	CCW(); 

	while (1) { // loop forever 
		// Button press
		if (BUTTON == 1) {
			// Toggle OUTPUT value
			OUTPUT = ~OUTPUT;

			// What is OUTPUT set at?
			if (OUTPUT == 0) {
				CCW(); 
				__delay_ms(60); // deadband between any possible next move
			} else {
				CW(); 
				__delay_ms(60);
			}
			// Wait for button release
			while (1) {
				//Delay: 50 ms
				__delay_ms(50);
				if (BUTTON == 0) break;
			}
		} // end Button press
	} // end while(1) 
}// end main
Save it in a .c source file, create a project, using the Project Wizard, and compile it.
Dec 20, 2013, 09:11 PM
Registered User
Motions's Avatar
You are freakin AWESOME!!! Thank you so much for the help. It actually compiles now.

I just have to see if it will work now. I might be back if it doesn't.
Dec 21, 2013, 04:31 AM
We want... Information!
Bruce Abbott's Avatar
You probably won't get any output, because the 12F675 defaults to analog input on GP0~GP3. To fix this you need to reset bits 0~3 of the ANSEL register, eg.

Code:
void main() { 
 int OUTPUT = 0; 

 OPTION_REG = 0b10011000; // set OPTION register
 TRISIO = 0b001000; // GP3 as input, all other as outputs
 ANSEL = 0; // enable digital I/O on GP0~3

 CCW();
Dec 21, 2013, 04:35 AM
Registered User
jpparisy's Avatar
Quote:
Originally Posted by Bruce Abbott
You probably won't get any output, because the 12F675 defaults to analog input on GP0~GP3. To fix this you need to reset bits 0~3 of the ANSEL register, eg.

Code:
void main() { 
 int OUTPUT = 0; 

 OPTION_REG = 0b10011000; // set OPTION register
 TRISIO = 0b001000; // GP3 as input, all other as outputs
 ANSEL = 0; // enable digital I/O on GP0~3

 CCW();
You're right, I forgot it.
Dec 21, 2013, 04:40 AM
Registered User
Doesn't the 675 have a hardware CCP/PWM module?
Dec 21, 2013, 04:47 AM
Registered User
jpparisy's Avatar
Quote:
Originally Posted by jakestew
Doesn't the 675 have a hardware CCP/PWM module?
No, it has not, but the PIC12F683, very similar, has a 10-bit PWM, using timer 2.
Dec 21, 2013, 07:19 AM
Oxford Panic
AndyOne's Avatar
The PWM output of a PIC is good for controlling digital servos since they don't need a 20ms refresh time like analogue servos. This means you can use most the resolution of the PWM. The PIC will have to be run at a clock rate of no more than 4MHz to achieve the correct frequency. Doing it this way allows all of the other activities in the control program to run virtually concurrently so there are no glitches. This only allows for one output on most low-end PICs but the 16F883 range has 2 PWMs.

A.


Thread Tools