wannaBinventor
Member
Hey all, still trying to learn C and get back into this.
I've attached a servo to the PICKIT2 devboard (the one with the 16F887).
First, my variable delay functions are not working. I'm trying to just use the "__delay_ms(#)" and "__delay_us(#)" macros inside a function with a for loop. The idea is that I'll pass the # of us or ms that I want to the function and the for loop with the __delay macro will run that many times. Let's ignore compensating for overhead for the sake of just getting it going. Please see the code and the error that this is generating below. I don't understand the error, as "_delayms" isn't even in the code.
Besides that problem, the main purpose of the code was just to move a servo from one position to another over and over again. It serves no real purpose, other than just establishing the concept. Note that in the below code, i substituted __delay_ms(#) for the function that I wrote since I am getting errors. Notice in my while(1) loop, I'm just sending the pulse once every 20ms five hundred times. The first for loop should move it 90 deg left, and the second for loop should move 90 deg right. It should remain in each position for 10,000ms (or 10 seconds).
It is not behaving as expected. The servo is moving 90 left for several seconds and then just bouncing to the right quickly and then back left. It's only staying in the 90 deg right position for probably 250ms. I can't understand that because the period and the number of iterations is the same for both for loops. I even tried a different servo, but had the same results.
Here is the code:
Here is the compiler output:
Any idea what I am getting that error, and why the servo is behaving as it is?
I've attached a servo to the PICKIT2 devboard (the one with the 16F887).
First, my variable delay functions are not working. I'm trying to just use the "__delay_ms(#)" and "__delay_us(#)" macros inside a function with a for loop. The idea is that I'll pass the # of us or ms that I want to the function and the for loop with the __delay macro will run that many times. Let's ignore compensating for overhead for the sake of just getting it going. Please see the code and the error that this is generating below. I don't understand the error, as "_delayms" isn't even in the code.
Besides that problem, the main purpose of the code was just to move a servo from one position to another over and over again. It serves no real purpose, other than just establishing the concept. Note that in the below code, i substituted __delay_ms(#) for the function that I wrote since I am getting errors. Notice in my while(1) loop, I'm just sending the pulse once every 20ms five hundred times. The first for loop should move it 90 deg left, and the second for loop should move 90 deg right. It should remain in each position for 10,000ms (or 10 seconds).
It is not behaving as expected. The servo is moving 90 left for several seconds and then just bouncing to the right quickly and then back left. It's only staying in the 90 deg right position for probably 250ms. I can't understand that because the period and the number of iterations is the same for both for loops. I even tried a different servo, but had the same results.
Here is the code:
Code:
#include <xc.h>
#include <stdlib.h>
#include <stdio.h>
#pragma config WDTE = OFF // Disable Watchdog timer
#pragma config LVP = OFF
#define _XTAL_FREQ 4000000
//__CONFIG(MCLRE_ON & CP_OFF & WDT_OFF & OSC_IntRC)
usdelay(int a);
msdelay(int b);
void main()
{
int i;
int j;
int adcresult;
long int delay;
ANSEL = 1;
TRISD = 0;
TRISB = 0;
INTCON = 0;
OPTION_REG = 128;
INTCONbits.GIE = 0;
ANSELH = 0b00000000;
PORTD = 128;
while(1){
for (i=0;i<500;i++)
{
PORTBbits.RB0 = 1;
msdelay(1);
PORTBbits.RB0 = 0;
msdelay(19);
}
PORTDbits.RD7 = ~PORTDbits.RD7;
PORTDbits.RD0 = ~PORTDbits.RD0;
for (i=0;i<500;i++)
{
PORTBbits.RB0 = 1;
delayms(2);
PORTBbits.RB0 = 0;
delayms(18);
}
}
}
usdelay(int a)
{
int r;
for (r=0;r<a;r++)
__delay_us(1);
}
msdelay(int b)
{
int s;
for (s=0;s<b;s++)
__delay_ms(1);
}
Here is the compiler output:
Code:
Warning [361] C:\Program Files (x86)\Microchip\xc8\v1.12\bin\flashy.c; 60.1 function declared implicit int
Executing: "C:\Program Files (x86)\Microchip\xc8\v1.12\bin\xc8.exe" -otestothercode.cof -mtestothercode.map --summary=default --output=default flashy.p1 --chip=16F887 -P --runtime=default --opt=default -N-1 -D__DEBUG=1 -g --asmlist "--errformat=Error [%n] %f; %l.%c %s" "--msgformat=Advisory[%n] %s" "--warnformat=Warning [%n] %f; %l.%c %s"
Microchip MPLAB XC8 C Compiler V1.12
Copyright (C) 2012 Microchip Technology Inc.
License type: Node Configuration
Warning [1273] ; . Omniscient Code Generation not available in Free mode
Error [499] ; 0. undefined symbol:
_delayms(testothercode.obj)
(908) exit status = 1
********** Build failed! **********
Any idea what I am getting that error, and why the servo is behaving as it is?