HobbyKing.com New Products Flash Sale
Reply
Thread Tools
Old Dec 20, 2013, 02:16 PM
Registered User
Motions's Avatar
Tallahassee, FL
Joined Nov 2005
480 Posts
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.
Motions is offline Find More Posts by Motions
Reply With Quote
Sign up now
to remove ads between posts
Old Dec 20, 2013, 02:43 PM
Registered User
jpparisy's Avatar
France, Ile-de-France, Plaisir
Joined Sep 2010
461 Posts
What do you want to do ?

Which PIC would you be to use ?

Do you know how works a servo ?
jpparisy is offline Find More Posts by jpparisy
Reply With Quote
Old Dec 20, 2013, 02:47 PM
Registered User
Madison WI
Joined Jan 2009
599 Posts
Can we see the script?
jazzjohn is offline Find More Posts by jazzjohn
Reply With Quote
Old Dec 20, 2013, 02:51 PM
"MAYONNAISE"
Acetronics's Avatar
Le Treport, France
Joined Jun 2004
1,404 Posts
see your circuit design and eventually know which compiler you use ...
Acetronics is online now Find More Posts by Acetronics
Reply With Quote
Old Dec 20, 2013, 03:45 PM
Registered User
Motions's Avatar
Tallahassee, FL
Joined Nov 2005
480 Posts
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
Motions is offline Find More Posts by Motions
Last edited by Motions; Dec 21, 2013 at 08:44 AM.
Reply With Quote
Old Dec 20, 2013, 03:55 PM
Registered User
Motions's Avatar
Tallahassee, FL
Joined Nov 2005
480 Posts
Here's a flow chart of what it does.
Motions is offline Find More Posts by Motions
Reply With Quote
Old Dec 20, 2013, 04:54 PM
Registered User
ttabbal's Avatar
United States, UT, Herriman
Joined Jan 2012
2,099 Posts
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?
ttabbal is online now Find More Posts by ttabbal
Reply With Quote
Old Dec 20, 2013, 05:12 PM
Registered User
Motions's Avatar
Tallahassee, FL
Joined Nov 2005
480 Posts
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.
Motions is offline Find More Posts by Motions
Reply With Quote
Old Dec 20, 2013, 06:24 PM
Registered User
jpparisy's Avatar
France, Ile-de-France, Plaisir
Joined Sep 2010
461 Posts
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.
jpparisy is offline Find More Posts by jpparisy
Reply With Quote
Old Dec 20, 2013, 08:11 PM
Registered User
Motions's Avatar
Tallahassee, FL
Joined Nov 2005
480 Posts
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.
Motions is offline Find More Posts by Motions
Reply With Quote
Old Dec 21, 2013, 03:31 AM
We want... Information!
Bruce Abbott's Avatar
Hastings, New Zealand
Joined Jan 2001
5,182 Posts
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();
Bruce Abbott is offline Find More Posts by Bruce Abbott
Reply With Quote
Old Dec 21, 2013, 03:35 AM
Registered User
jpparisy's Avatar
France, Ile-de-France, Plaisir
Joined Sep 2010
461 Posts
Quote:
Originally Posted by Bruce Abbott View Post
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.
jpparisy is offline Find More Posts by jpparisy
Reply With Quote
Old Dec 21, 2013, 03:40 AM
Registered User
United States, ID
Joined Sep 2011
423 Posts
Doesn't the 675 have a hardware CCP/PWM module?
jakestew is offline Find More Posts by jakestew
Reply With Quote
Old Dec 21, 2013, 03:47 AM
Registered User
jpparisy's Avatar
France, Ile-de-France, Plaisir
Joined Sep 2010
461 Posts
Quote:
Originally Posted by jakestew View Post
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.
jpparisy is offline Find More Posts by jpparisy
Reply With Quote
Old Dec 21, 2013, 06:19 AM
Oxford Panic
AndyOne's Avatar
United Kingdom, Oxford
Joined Feb 2003
3,655 Posts
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.
AndyOne is offline Find More Posts by AndyOne
Reply With Quote
Reply


Thread Tools

Similar Threads
Category Thread Thread Starter Forum Replies Last Post
Discussion Need help with tilt control on Tarot T-2 acmjg Multirotor Talk 4 Dec 03, 2013 09:44 AM
Discussion need HELP with I86 controller aka S1 multicopter board jaas75 Multirotor Electronics 2 Oct 30, 2013 12:50 PM
Sold Hardly used Futaba 7CAP with R168 & Servos/C~pics! stikyh8r Aircraft - General - Radio Equipment (FS/W) 1 Aug 10, 2010 01:15 AM
Discussion Need help with servo control for a jig. asennad DIY Electronics 8 Oct 31, 2008 04:37 PM
Discussion Radio controlled cable cam, need help with servos team08 Radios 0 Apr 24, 2006 07:54 PM