CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Getting odd values in Interrupt service routine

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Harry Mueller



Joined: 17 Oct 2005
Posts: 116

View user's profile Send private message

Getting odd values in Interrupt service routine
PostPosted: Wed Oct 26, 2005 10:31 am     Reply with quote

This is the program that I'm trying to write for a 6 legged walker. The code has just one servo right now that I'm trying to rotate through a walking cycle within an Interrupt service routine. To try to figure out whats going on, I've printed out values from the ISR to an LCD. They are all off the wall. This is what pops up on the LCD.

flag=20
kount=20
tilt_servo_delay=61312

"flag" shouldn't go over 4, "kount" shouldn't go over 50 and "tilt_servo_delay" shouldn't go over 750.

I've checked out all my books and resources but am totally stumped.

Thanks for any help....Harry

Code:
#include <16F628.h>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#include <LCD_4LINE.c>   // this file set up to use port B for LCD

#define TILT_SERVO PIN_A0
#define GP2D02_VIN PIN_B3
#define GP2D02_VOUT PIN_A2

long tilt_servo_delay;
int flag = 0;
int kount = 0;
int reading=0;

//Long_delay_us function==================================

void long_delay_us(long count)
{
char i;
i = (char)(count >> 8);
while(i >= 1)
{
i--;
delay_us(253);
restart_wdt();
}
delay_us((char)count);
}

//Interrupt Service Routine (ISR)=========================

#int_TIMER0
servo_isr()
{
int no_of_interrupts;
kount++;
set_timer0 (100);   //Preloader to achieve 20 ms period

//Set "walking" servo positions in the ISR===================

switch (flag) // Select case based on value of "flag"========
{
 case '0': no_of_interrupts= 50 ; tilt_servo_delay= 500 ; break;
 case '1': no_of_interrupts= 50 ; tilt_servo_delay= 250 ; break;
 case '2': no_of_interrupts= 15 ; tilt_servo_delay= 500 ; break;
 case '3': no_of_interrupts= 50 ; tilt_servo_delay= 750 ; break;
 default : flag = 0; kount = 0;  // If "flag" > 3 then start over
}

// Create desired servo pulse based on case info=============

output_high(TILT_SERVO);
delay_ms(1);
long_delay_us(tilt_servo_delay);
output_low(TILT_SERVO);
if (kount==no_of_interrupts) flag++; // increment "flag" when
}                                    // kount is equal to
                                     // no_of_interrupts
//Collect the IR reading==================================

Collect_IR_Reading()
{
   int counter=9;
   output_low(GP2D02_VIN);         // start cycle with Vin low
   delay_ms(1);                    // give the sensor a little time before we bug it
   while (!input(GP2D02_VOUT));    //wait for Vout to go high

   do {
      disable_interrupts(GLOBAL);
      delay_cycles(4);             // minimum wait is 2us, max is 170us, 4 worked
      output_low(GP2D02_VIN);      // tell the sensor that we want a bit
      delay_cycles(2);             // might be as low as 1 (maybe), 2 worked
      reading=reading<<1;          // left shift the reading value
      reading=reading|(input(GP2D02_VOUT)); // put the newest reading into the
                                            // LSB of reading
      output_high(GP2D02_VIN);     // we read bit, and get ready for the next one
      counter--;
      } while (counter>0);
      enable_interrupts(GLOBAL);
}

//LCD print statements====================================

LCD_Print_Routine()
{
      lcd_gotoxy(1,1);
      printf(lcd_putc,"Flag = 2%u", flag);
      lcd_gotoxy(1,2);
      printf(lcd_putc,"kount = 2%u", kount);
      lcd_gotoxy(1,3);
      printf(lcd_putc,"Value = %3u", reading);
      lcd_gotoxy(1,4);
      printf(lcd_putc,"delay=%lu", tilt_servo_delay);
}

// Main Routine========================================

main()
{
lcd_init();
setup_comparator(NC_NC_NC_NC);
setup_timer_0(RTCC_INTERNAL | RTCC_DIV_128);
enable_interrupts(INT_TIMER0);
enable_interrupts(GLOBAL);
while(true)
{
Collect_IR_Reading();
LCD_Print_Routine();
}
}
Ttelmah
Guest







PostPosted: Wed Oct 26, 2005 10:55 am     Reply with quote

There is a simple error causing this. You are testing for '0', not 0 (and the same for the other cases).
The former is the ASCII character representing 0, and has a value of 0x30.

Best Wishes
Harry Mueller



Joined: 17 Oct 2005
Posts: 116

View user's profile Send private message

PostPosted: Wed Oct 26, 2005 11:52 am     Reply with quote

Thanks Ttelmah, that was quite obvious....in hindsight. Embarassed

It's changed the output but it's still weird.

flag cycles between 20 and 23 (approximately)
kount cycles between 2000 and 2900 (approximately)
servo_delay seems right now.

Thanks....Harry
cmdrdan



Joined: 08 Apr 2005
Posts: 25
Location: Washington

View user's profile Send private message

PostPosted: Wed Oct 26, 2005 12:09 pm     Reply with quote

Harry --

Your format statements seem strange for flag and kount. As I read it, you're putting a '2' in front of the variables when you print them out....

Dan
Harry Mueller



Joined: 17 Oct 2005
Posts: 116

View user's profile Send private message

PostPosted: Wed Oct 26, 2005 8:50 pm     Reply with quote

cmdrdan wrote:
As I read it, you're putting a '2' in front of the variables when you print them out....


You're absolutely right! The servo cycles now, there are just some timing issues to work out.

Thanks for your help.....Harry
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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