RC Servo Motor

Discussion about projects that used PIC Microcontroller, Hardware Interface, Programming Algorithm and etc......

RC Servo Motor

Postby DanielWu » Fri Mar 18, 2011 11:24 pm

Hi,
Below is the code for my project. The attachment is the schematic.

CODE: SELECT_ALL_CODE
//Project: Servo Motor
//PIC: PIC16F877A
//Crystal Frequency: 20MHz

#include <pic.h>
#include <htc.h>
__CONFIG(0x3F32);
#define SERVO    RC0
#define   min    10
#define   max    50
#define _XTAL_FREQ 20000000

void pic_init(void);
void timer_init(void);
int read_a2d(unsigned char channel);

int counter,position;

static void interrupt isr(void)
{
if(TMR0IF==1){
TMR0IF=0;
counter++;
if(counter==position) SERVO=0;
if(counter==390){
counter=0;
SERVO=1;}
}
}

main()
{int i;
pic_init();    //initialize PIC
timer_init();    //initialize Timer Module
for(;;){
position=0.9775*(max-min)*read_a2d(0)/1000+min; //change ADC voltage value to position
}}


void pic_init(void)
{
TRISA=0b00000001;
TRISB=0b00000000;
TRISC=0b00000000;
TRISD=0b00000000;
TRISE=0b00000000;
ADCON1=0b00000110;    //configures the functions of the port pins
PORTA=0b00000000;
PORTB=0b00000000;
PORTC=0b00000000;
PORTD=0b00000000;
PORTE=0b00000000;
INTCON=0b10100000;    //Enable Global, TMR0 Interrupt
PIE1=0b00000000;    //Disable TMR1, TMR2 Interrupt
PIE2=0b00000000;
}


void timer_init(void)
{
OPTION_REG=0b00001000;    //Internal instruction clock
T1CON=0b00000000;    //Timer1 OFF
T2CON=0b00000000;    //Timer2 OFF
PR2=255;
TMR0=0;
TMR1H=0;
TMR1L=0;
TMR2=0;
}


int read_a2d(unsigned char channel)
{ADCON0=0b00000001;    //Turn on A/D module
ADCON1=0b10000000;    //configures analog and voltage reference pins
ADCON0=(ADCON0&0xC7)|(channel<<3);   //select analog input channel
__delay_ms(2);
ADON=1;    //initiate conversion on the selected channel
while(ADON==1) continue;    //wait until conversion done
return(256*ADRESH+ADRESL);}


The code has been successfully compiled and loaded into PIC. The potentiometer is connected to ground, 5V and RA0. Servo is connected to 5v, ground and RC0. According to my code, the servo should be rotating while I rotate the potentiometer. However, the servo doesn't rotate. The servo rotates once I connect it to 5V and then stop. Could someone please help me to check my code.
Thanks!!
Attachments
PIC-Schematic.jpg
DanielWu
Greenhorn
 
Posts: 2
Joined: Fri Mar 18, 2011 11:19 pm

Re: RC Servo Motor

Postby robosang » Sun Mar 20, 2011 10:45 am

Can you please explain on your code? What is the ISR doing, Timer 0 setting, ADC, etc. If you wait for forumers to understand your coding from the register itself, it might that weeks, months. And your schematic does not show the connection from potential meter and Servo motor. That is basic circuit only.
robosang
Expert
 
Posts: 1239
Joined: Wed Jun 10, 2009 5:37 pm

Re: RC Servo Motor

Postby shahrul » Sun Mar 20, 2011 6:21 pm

This code is running because this is same as my Servo Lesson code.
I don't know which Hi-Tech version you use that changed 'OPTION' to 'OPTION_REG' and also 'ADGO' to 'ADON'
Also re-check your hardware and make sure your Crystal value is 20MHz. If different, you need to change XTAL define.
User avatar
shahrul
Professional
 
Posts: 812
Joined: Sat May 16, 2009 9:54 pm
Location: Selangor

Re: RC Servo Motor

Postby DanielWu » Mon Mar 21, 2011 12:54 am

Shahrul, I understand that the code should be working. But my problem now is the servo rotate clockwise to maximum then stop and fix there whenever I connect it to the circuit regardless of the potential meter.
May I know what errors that can cause to this problem? It is because I changed option and adgo?
Thanks!
DanielWu
Greenhorn
 
Posts: 2
Joined: Fri Mar 18, 2011 11:19 pm

Re: RC Servo Motor

Postby shahrul » Mon Mar 21, 2011 10:53 am

After you connect power to the servo, ofcourse the servo rotate to 1 position. Then it's never rotate because your program is not running.
If you want check output signal, you can use oscilloscope to see the waveform that supply to the servo. You should know how the servo input.
User avatar
shahrul
Professional
 
Posts: 812
Joined: Sat May 16, 2009 9:54 pm
Location: Selangor

Re: RC Servo Motor

Postby ABSF » Mon Mar 21, 2011 9:25 pm

DanielWu WROTE:Hi,
Below is the code for my project. The attachment is the schematic.

CODE: SELECT_ALL_CODE
//Project: Servo Motor
//PIC: PIC16F877A
//Crystal Frequency: 20MHz

#include <pic.h>
#include <htc.h>
__CONFIG(0x3F32);
#define SERVO    RC0
#define   min    10
#define   max    50
#define _XTAL_FREQ 20000000

void pic_init(void);
void timer_init(void);
int read_a2d(unsigned char channel);

int counter,position;

static void interrupt isr(void)
{
if(TMR0IF==1){
TMR0IF=0;
counter++;
if(counter==position) SERVO=0;
if(counter==390){
counter=0;
SERVO=1;}
}
}

main()
{int i;
pic_init();    //initialize PIC
timer_init();    //initialize Timer Module
for(;;){
position=0.9775*(max-min)*read_a2d(0)/1000+min; //change ADC voltage value to position
}}


void pic_init(void)
{
TRISA=0b00000001;
TRISB=0b00000000;
TRISC=0b00000000;
TRISD=0b00000000;
TRISE=0b00000000;
ADCON1=0b00000110;    //configures the functions of the port pins
PORTA=0b00000000;
PORTB=0b00000000;
PORTC=0b00000000;
PORTD=0b00000000;
PORTE=0b00000000;
INTCON=0b10100000;    //Enable Global, TMR0 Interrupt
PIE1=0b00000000;    //Disable TMR1, TMR2 Interrupt
PIE2=0b00000000;
}


void timer_init(void)
{
OPTION_REG=0b00001000;    //Internal instruction clock
T1CON=0b00000000;    //Timer1 OFF
T2CON=0b00000000;    //Timer2 OFF
PR2=255;
TMR0=0;
TMR1H=0;
TMR1L=0;
TMR2=0;
}


int read_a2d(unsigned char channel)
{ADCON0=0b00000001;    //Turn on A/D module
ADCON1=0b10000000;    //configures analog and voltage reference pins
ADCON0=(ADCON0&0xC7)|(channel<<3);   //select analog input channel
__delay_ms(2);
ADON=1;    //initiate conversion on the selected channel
while(ADON==1) continue;    //wait until conversion done
return(256*ADRESH+ADRESL);}


The code has been successfully compiled and loaded into PIC. The potentiometer is connected to ground, 5V and RA0. Servo is connected to 5v, ground and RC0. According to my code, the servo should be rotating while I rotate the potentiometer. However, the servo doesn't rotate. The servo rotates once I connect it to 5V and then stop. Could someone please help me to check my code.
Thanks!!


main()
{int i;
pic_init(); //initialize PIC
timer_init(); //initialize Timer Module
for(;;){
position=0.9775*(max-min)*read_a2d(0)/1000+min; //change ADC voltage value to position
}}


I am a beginner in C. :)

Just wondering what is the value of "position" when evaluating the formula above. Since you didn't use #include <math.h> and the variables "position, max and min" are not defined as "float" while there's a floating point number in the formula.

Allen
The next war will determine NOT who is right BUT what is left.
User avatar
ABSF
Professional
 
Posts: 810
Joined: Wed Nov 10, 2010 9:32 am
Location: E Malaysia


Return to PIC Microcontroller

Who is online

Users browsing this forum: No registered users and 2 guests

cron