Kryten
New Member
Hi
I have a litte troubble with this code. Icant get it to work properly.
Most of the code is from a turtorial where youre supposed to use adc (ch 1 and 2) for input to control the servo (for LDR, turing left or right).
I was thinking it could be adapted for buttons for left and right but i guess I changed/ added worng thing
If somebody that knows a bit more (or a lot more) than me about ISR. I would be rally thankful for some tips/help on how to get it working properly..
Im using 20MHz clk so Im thinking I might be off
I have a litte troubble with this code. Icant get it to work properly.
Most of the code is from a turtorial where youre supposed to use adc (ch 1 and 2) for input to control the servo (for LDR, turing left or right).
I was thinking it could be adapted for buttons for left and right but i guess I changed/ added worng thing
Code:
/////////////////////////////////////////////////////
// //
// Servo with Interupt //
/////////////////////////////////////////////////////
#include <p18f4550.h>
//#include <adc.h>
#include <delays.h>
///////////////////////////////////////////////////////////////
// Config bits //
///////////////////////////////////////////////////////////////
#pragma config PLLDIV = 5 // This value has to be 5 for 20Mhz xtal as PLL accepts fixed input of 4MHz only
#pragma config USBDIV = 2 // Clock source from 96MHz PLL/2, therefore USB clock is 48MHz in full speed
#pragma config FOSC = HSPLL_HS // HS oscillator, PLL enabled
#pragma config CPUDIV = OSC4_PLL6 // 96MHz PLL Src/6 => a CPU clock of 16MHz achieved for Vdd=3.3V
#pragma config PWRT = ON // PowerUp timer ON
#pragma config WDT = OFF // watchdog timer off
#pragma config LVP = OFF // low level voltage progamming OFF
#pragma config BOR = OFF // brown out detect off
#pragma config DEBUG = ON // Debug ON
#pragma config PBADEN = OFF // PORTB<4:0> as digital IO on reset
#pragma config STVREN = ON // Stack overflow reset ON
#pragma config VREGEN = OFF // Disable internal 3.3V regulator!
#pragma config MCLRE = ON // Enable MCLRE for reset function
///////////////////////////////////////////////////////////
// Defines //
///////////////////////////////////////////////////////////
#define TRUE (1)
#define FALSE (0)
#define MAX_TIMER_SERVO (64536) // Far left position
#define MIN_TIMER_SERVO (62086) // Far right position
#define CENTER_TIMER_SERVO (63456) // Center servo position
#define SERVO_STEP (10) // Servo precision
#define PORT_TURN_LEFT (PORTCbits.RC1) // Button for left
#define PORT_TURN_RIGHT (PORTCbits.RC2) // Button for right
#define PORT_RED_LED (PORTCbits.RC6) // Red led to test input
#define PORT_GREEN_LED (PORTCbits.RC7) // Green led to test input
unsigned int timerServo; // global variable
void YourHighPriorityISRCode ();
void YourLowPriorityISRCode ();
////////////////////////////////////////////////////
// ISR routine //
////////////////////////////////////////////////////
#pragma code high_vector = 0x08
void high_vector ()
{
_asm
goto YourHighPriorityISRCode
_endasm
}
#pragma code
#pragma code low_vector = 0x18
void low_vector()
{
_asm
goto YourLowPriorityISRCode
_endasm
}
#pragma code
// interupt handel routines
#pragma interrupt YourHighPriorityISRCode
void YourHighPriorityISRCode ()
{
if(PIR1bits.TMR1IF == TRUE) // check interrupt flag from timer0
{
if(PORTBbits.RB5 == 0)
{
TMR1H = (timerServo & 0xFF00) >> 8; // Keep MSB
TMR1L = timerServo & 0x00FF; // Keep LSB
PORTBbits.RB5 = 1; // high pulse
}
else
{
TMR1H = 0x8A; // Timer interups after 83.3ns*8*(65536-35536) = +/- 20 ms
PORTBbits.RB5 = 0; // low pulse
}
PIR1bits.TMR1IF = FALSE; //reenable TMR1 interrupt
}
}
#pragma interruptlow YourLowPriorityISRCode
void YourLowPriorityISRCode()
{
}
////////////////////////////////////////////////////////
// Main routine //
////////////////////////////////////////////////////////
void main (void)
{
TRISBbits.TRISB5 = 0; //set RB5 as output
PORTBbits.RB5 = 0; //Initialize with 0
TRISCbits.TRISC1 = 1; //Set RC1 as input
PORTCbits.RC1 = 0;
TRISCbits.TRISC2 = 1; //Set RC2 as input
PORTCbits.RC2 = 0;
TRISCbits.TRISC6 = 0; //Set RC6 as output
PORTCbits.RC6 = 0;
TRISCbits.TRISC7 = 1; //Set RC6 as output
PORTCbits.RC7 = 0;
timerServo = CENTER_TIMER_SERVO; // Max right
PIR1bits.TMR1IF = 0; // Setting timer to 0
RCONbits.IPEN = 1; //Activating High prioity interrupt
//RCONbits.SBOREN = 0; // BOR = off
T1CON = 0b0011001; // 48Mhz/4, prescaler 1/8
INTCON = 0b10000000; // GIEH = TRUE
IPR1bits.TMR1IP = 1;
PIE1bits.TMR1IE = 1;
while (1)
{
if (PORT_TURN_LEFT = 1)
{
PORT_RED_LED = 1;
PORT_GREEN_LED = 0;
timerServo = (timerServo - SERVO_STEP);
}
if (PORT_TURN_RIGHT = 1)
{
PORT_GREEN_LED = 1;
PORT_RED_LED = 0;
timerServo =(timerServo + SERVO_STEP);
}
/* if (timerServo > MAX_TIMER_SERVO)
{
timerServo = MAX_TIMER_SERVO;
}
if (timerServo < MIN_TIMER_SERVO)
{
timerServo = MIN_TIMER_SERVO;
}*/
else
{
timerServo = CENTER_TIMER_SERVO;
}
Delay10KTCYx(6); // Wait 5ms
}
}
If somebody that knows a bit more (or a lot more) than me about ISR. I would be rally thankful for some tips/help on how to get it working properly..
Im using 20MHz clk so Im thinking I might be off
Last edited: