Shop our Airplanes Products Drone Products Sales
Thread Tools
Old Dec 20, 2013, 03:16 PM
Motions is offline
Find More Posts by Motions
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
Old Dec 20, 2013, 03:43 PM
jpparisy is offline
Find More Posts by jpparisy
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 ?
Old Dec 20, 2013, 03:47 PM
jazzjohn is offline
Find More Posts by jazzjohn
Registered User
Can we see the script?
Old Dec 20, 2013, 03:51 PM
Acetronics is offline
Find More Posts by Acetronics
"MAYONNAISE"
Acetronics's Avatar
see your circuit design and eventually know which compiler you use ...
Old Dec 20, 2013, 04:45 PM
Motions is offline
Find More Posts by Motions
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.
Old Dec 20, 2013, 04:55 PM
Motions is offline
Find More Posts by Motions
Registered User
Motions's Avatar
Here's a flow chart of what it does.
Old Dec 20, 2013, 05:54 PM
ttabbal is offline
Find More Posts by ttabbal
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?
Old Dec 20, 2013, 06:12 PM
Motions is offline
Find More Posts by Motions
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.
Old Dec 20, 2013, 07:24 PM
jpparisy is offline
Find More Posts by jpparisy
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.
Old Dec 20, 2013, 09:11 PM
Motions is offline
Find More Posts by Motions
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.
Old Dec 21, 2013, 04:31 AM
Bruce Abbott is offline
Find More Posts by Bruce Abbott
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();
Old Dec 21, 2013, 04:35 AM
jpparisy is offline
Find More Posts by jpparisy
Registered User
jpparisy's Avatar
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.
Old Dec 21, 2013, 04:40 AM
jakestew is offline
Find More Posts by jakestew
Registered User
Doesn't the 675 have a hardware CCP/PWM module?
Old Dec 21, 2013, 04:47 AM
jpparisy is offline
Find More Posts by jpparisy
Registered User
jpparisy's Avatar
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.
Old Dec 21, 2013, 07:19 AM
AndyOne is offline
Find More Posts by AndyOne
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

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 10:44 AM
Discussion need HELP with I86 controller aka S1 multicopter board jaas75 Multirotor Electronics 2 Oct 30, 2013 01:50 PM
Sold Hardly used Futaba 7CAP with R168 & Servos/C~pics! stikyh8r Aircraft - General - Radio Equipment (FS/W) 1 Aug 10, 2010 02:15 AM
Discussion Need help with servo control for a jig. asennad DIY Electronics 8 Oct 31, 2008 05:37 PM
Discussion Radio controlled cable cam, need help with servos team08 Radios 0 Apr 24, 2006 08:54 PM