|
|
View previous topic :: View next topic |
Author |
Message |
Guest
|
Suitable configuration for 18f4550 for this servo code |
Posted: Tue Oct 28, 2008 5:14 pm |
|
|
I am a newbie in embedded C programming, and so with the help of some friends who are good with AVR microcontrollers, I wrote this code in MPLAB. I copy pasted the code down below after the specficiations. This code works in MPLAB without any errors.The problem is that I am facing with this code is that I can't run it in CCS C due to the different syntax in CCS C. I get errors saying "undefined identifiers". I figured out some changes in syntax, but not the rest. Can someone please help me out compile this in CCS?
0) I still have to add suitable configuration for 18f4550 as it has been tested on 18f452
1) The code is commented
2) The code is made to interface with any output
3) I coded for 0.9ms to 2.1ms for a 20 ms time period servo
4) You have to change the value of PWMontime for changing the angle according to your servo specificaitons
5) The output is from the RD7 pin
6) The PWMontime is a global variable so changing the value will change the angle of the servo motor.
Main.h
Code: | #ifndef __MAIN_H
#define __MAIN_H
#define ON 1
#define OFF 0
#define Enable 1
#define Disable 0
#define High 1
#define Low 0
void SystemInit(void);
#endif |
Main.c
Code: |
#include<p18f452.h>
#include<delays.h>
/* // this config settings is for 18f452, add suitable config setting for 18f4550
#pragma config WDT = OFF
#pragma config OSC = HS
#pragma config OSCS = OFF
#pragma config BOR = OFF
#pragma config STVR = OFF
#pragma config LVP = OFF
#pragma config DEBUG = ON // make sure to enable when required
#pragma config PWRT = OFF
*/
#include "isr.h"
#include "timer3.h"
#include "main.h"
#include "servo.h"
#pragma code low_vector = 0x18
void interrupt_at_low_vector(void)
{
_asm GOTO LowIsr _endasm
}
#pragma code // return to the default code section
#pragma code high_vector = 0x08
void interrupt_at_high_vector(void)
{
_asm GOTO HighIsr _endasm
}
#pragma code // return to the default code section
volatile unsigned char SoftFlag = 0;
unsigned int timeOutPeriod;
void main(void)
{
SystemInit();
while(1)
{
}
}
void SystemInit(void)
{
// enable/init all input output devices
InitTimer3();
InitServo();
// enable global interrupt/ priority
IsrHighEnDis(Enable); // All High priority interrupts are enabled
IsrLowEnDis(Enable); // Int is Priority is enabled and all low priority int enabled
// enable interrupt and priority of concerned device here
Timer3Interrupt(Enable);
// set interrupt priority of concerned devices here
Timer3IntPriority(Low);
} |
Servo.h
Code: |
#ifndef SERVO_H
#define SERVO_H
extern volatile unsigned int PWMOnTime,PWMOffTime;
void InitServo(void);
void ServoPositionPWM(unsigned int onTime);
#endif |
servo.c
Code: |
#include<p18f452.h>
#include<delays.h>
#include "main.h"
#include "serial.h"
#include "timer3.h"
#include "servo.h"
/* ServoPosition function needs to be tested and calibrated
for correct angle heading
*/
volatile unsigned int PWMOnTime,PWMOffTime;
void InitServo(void)
{
//Init timer3
InitTimer3();
//Init RD7 as output
TRISDbits.TRISD7 = 0;
PWMOnTime = 1500; /*center position of the servo, sensor oriented at 0 degree */
SetTimer3(PWMOnTime);
RunTimer3(Start);
}
void ServoPositionPWM(unsigned int onTime) /* onTime in microSecond 500 to 2500, 0.9mS to 2.1mS */
{
PWMOnTime = onTime;
PWMOffTime = 20000 - onTime;
} |
timer.h
Code: |
#ifndef __TIMER3_H
#define __TIMER3_H
//#define Enable 1
//#define Disable 0
//#define Start 1
//#define Stop 0
void InitTimer3(void);
void SetTimer3(unsigned int time);
void RunTimer3(unsigned char state);
void Timer3Interrupt(unsigned char Int);
void Timer3IntPriority(unsigned char status);
void Timer3InterruptService(void);
#endif |
timer.c
Code: |
#include<p18f452.h>
#include "timer3.h"
#include "isr.h"
#include "main.h"
#include "servo.h"
void InitTimer3(void) //prescale - 1:1
{
T3CON = 0b11001100;
IPR2bits.TMR3IP = 0; // set to low priority interrupt
}
void SetTimer3(unsigned int time) //in microSeconds 0-32000
{
unsigned int count,tempW;
unsigned char tempB;
if(time > 32000) /* maximum time timer3 can count is 32768uS under the given settings */
time = 32000;
count = 2 * time; // 1 count = 0.5uSec, 1usec = 2 counts
count = 0xFFFF - count;
tempW = count & 0xFF00; // load upper byte into timer3h register
tempW = tempW >> 8;
tempB = (unsigned char)tempW;
TMR3H = tempB;
tempW = count & 0x00FF; // load lower byte into timer3l register
tempB = (unsigned char)tempW;
TMR3L = tempB;
}
void RunTimer3(unsigned char state) // Start Stop
{
T3CONbits.TMR3ON = state;
}
void Timer3Interrupt(unsigned char Int) // enable disable
{
PIE2bits.TMR3IE = Int;
}
void Timer3IntPriority(unsigned char status) // high/low
{
IPR2bits.TMR3IP = status; // set interrupt priority
}
void Timer3InterruptService(void)
{
//RunTimer3(Stop);
if(PORTDbits.RD7 == 0)
{
SetTimer3(PWMOnTime);
LATDbits.LATD7 = 1;
}
else if(PORTDbits.RD7 == 1)
{
SetTimer3(PWMOffTime);
LATDbits.LATD7 = 0;
}
} |
ISR.h
Code: | #ifndef __ISR_H
#define __ISR_H
void LowIsr(void);
void HighIsr(void);
void IsrHighEnDis(unsigned char status);
void IsrLowEnDis(unsigned char status);
#endif |
ISR.c
Code: |
#include<p18f452.h>
#include<delays.h>
#include "isr.h"
#include "main.h"
#include "timer3.h"
#include "servo.h"
#pragma interruptlow LowIsr
void LowIsr(void)
{
/*timer3 interrupt is configured as low priority interrupt this will be executed */
// Timer3 overflow interrupt
// servo pwm generator
if(PIR2bits.TMR3IF ==1)
{
//Timer3Interrupt(Disable);
PIR2bits.TMR3IF = 0;
Timer3InterruptService();
//Timer3Interrupt(Enable);
}
}
#pragma interrupt HighIsr
void HighIsr(void)
{
/* timer3 interrupt is configured as high priority interrupt this will be executed */
// Timer3 overflow interrupt
// servo pwm generator
if(PIR2bits.TMR3IF ==1)
{
//Timer3Interrupt(Disable);
PIR2bits.TMR3IF = 0;
Timer3InterruptService();
//Timer3Interrupt(Enable);
}
}
// High priority interrupt enable/disable
void IsrHighEnDis(unsigned char status)
{
RCONbits.IPEN = 1;
INTCONbits.GIEH = status;
}
// Low priority interrupt enable/disable
void IsrLowEnDis(unsigned char status)
{
RCONbits.IPEN = 1;
INTCONbits.GIEL = status;
} |
|
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|