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

Problem with ltc1298 and comparator interupt

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



Joined: 14 Jun 2005
Posts: 64

View user's profile Send private message

Problem with ltc1298 and comparator interupt
PostPosted: Tue Mar 21, 2006 8:09 pm     Reply with quote

Hi- I started with the following code- it is a comparator interrupt on PortA . Pins A0, A1 wired together to a pushbutton switch. It works reliably.

Code:
#include <16F877A.H>
#fuses HS,NOWDT,NOPROTECT, PUT, BROWNOUT, NOLVP    // HS for 20 mHz, fuses specific to chip
#use delay(clock=20000000)                           // adjust for resonator frequency
#use i2c(Master, SDA=PIN_C4, SCL=PIN_C3)           // set up options
#include <LCD_16f877A.c>
#include <ltc1298_sk.c>
#include <24128.c>                                 // try removing
#use fast_io(c)
#use fast_io(a)
#use fast_io(b)
#include <stdlib.h>
#byte PORTB=6

//this is the one that works
#INT_COMP
comp_isr()  {
    delay_ms(25);  //debounce
    printf(lcd_putc,"\fInterrupt loop ");
    delay_ms(500);
}
void main()   {
 init:
   set_tris_a(0b00000011);             // sets PORTA
   set_tris_b(0x00);                       
   set_tris_c(0b00110111);             // set all PORTC, pin_C1 in for marry
   lcd_init();     
   lcd_putc("\nLucky13");
   delay_ms(1000);
   setup_comparator(A0_VR_A1_VR); // Mode 2 -A0, A1 inputs to
                                                      // 2 comparators, compare with Vref
   setup_vref(VREF_HIGH|2);            // (mode|value) 5*value/32+1.25=vref
                                                    // value = 2, vref = 1.56V
   enable_interrupts(INT_COMP);
   enable_interrupts(GLOBAL);
   delay_ms(2000);   
   adc_init();
   delay_us(10);
   while(1)
   { 
         lcd_putc("\fMain loop");
         delay_ms(500);
     }
}


Then using the CCS driver for the ADLTC1298, I changed the pinouts to
Code:
#define ADC_CS   PIN_A3
#define ADC_CLK  PIN_E0 // output from MP
#define ADC_DIN  PIN_E2 // output from MP
#define ADC_DOUT PIN_E1 // input to MP

and the delays in the driver to "delay_cycles(1);"
I think my wiring is OK.

I added (tested and working) code to the one above:
Code:
#include <16F877A.H>
#fuses HS,NOWDT,NOPROTECT, PUT, BROWNOUT, NOLVP    // changed to HS for 20 mHz
#use delay(clock=20000000)                           // adjust for resonator frequency
#use i2c(Master, SDA=PIN_C4, SCL=PIN_C3)           // set up options
#include <LCD_16f877A.c>
#include <ltc1298_sk.c>
#use fast_io(a)
#use fast_io(b)
#use fast_io(c)
#use fast_io(e)
#include <stdlib.h>
#byte PORTB=6
//*****************************************************************

//function to use variable 16 bit delay...are you reading this PCM? I owe you bigtime for this function!!
void long_delay_us (long int count)
{
char i;
i = (char)(count >> 8);                          // typecast count >>8 (right shift count) as char
while(i >= 1)                                        // loop while i >= 1
{
i--;                                             
delay_us(248);                                     // delay 253 us, adjustable
restart_wdt();                                      // restart watchdog timer
}
delay_us((char)count);                             // use built-in function delay_us to typecast count
}

#INT_COMP
comp_isr(){
    delay_ms(25);  //debounce
    printf(lcd_putc,"\fInterrupt loop ");
    delay_ms(500);
}

void main()
{
   int i = 0;                                // loop count   
   float ad12, ad12temp;                     // digital output of a/d channel 0 (light)
                                             // digital output of a/d channel 1 (temp sensor)
   long int count;
   set_tris_a(0b11110011);                   // sets port a direction 0=out 1=in
   set_tris_b(0x00);                             
   set_tris_c(0b00110011);                   // set all PORTC, pin_C1 in for marry
   set_tris_e(0b11111100);                   // set all PORTE, pin_C1 in for marry
init:
   lcd_init();
   lcd_putc("\fFab 15  ");                 // welcome
   delay_ms(1000);
   setup_comparator(A0_VR_A1_VR); // Mode 2 -A0, A1 inputs to
                                                      // 2 comparators, compare with Vref
   setup_vref(VREF_HIGH|2);            // (mode|value) 5*value/32+1.25=vref
                                       // value = 2, vref = 1.56V
   enable_interrupts(INT_COMP);
   enable_interrupts(GLOBAL);
   adc_init();
   do{
      ad12 = read_analog(0) >> 4;                  // get a/d value (channel 0) and shift
      ad12temp = read_analog(1) >> 4;              // get a/d value (channel 1) and shift
      lcd_gotoxy(1,1);                             // start at 1st line
      printf(lcd_putc,"AD = %5.0f", ad12);         // print to LCD
      delay_us(1500);
        for (i=1; i<=100; i++)                     // start of light sensor action
        {
         count = ((0.244*ad12)+1000);              // y=mx+b-function to relate ad12 and servo 1000us - 2000us
                                                   // b = 1000, m = .244, for light sensor
         output_high (PIN_C2);                     // servo out high
         long_delay_us (count);                    // function call allow 16 bit us
         output_low (PIN_C2);                      // servo out (off)
         long_delay_us (22000-count);
        }//end of for
       lcd_gotoxy(2,2);                            // start at 2nd line
       printf(lcd_putc,"\nTemp = %5.0f", ad12temp);// print temperature sensor reading to LCD
       delay_us(750);

      }while(1);                                   // while true
      }


On startup, the code goes straight to the interrupt loop. When I reset, the software runs in the main loop and interrupts when I push the button. But then it hangs in the interrupt...forever....until I push the button again and it goes back to main. I can push the button over and over and flip back between threads.
I have no experience with interrupts and am at a total loss for what is wrong.
Another wierd thing is that the A/D works great but there is no signal on the clock pin (7 on A/D, 8 on MP). Unless I have a bad probe...

Thanks muchly for any help-
SophE
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Mar 21, 2006 8:17 pm     Reply with quote

Read this thread, which talks about clearing the "mismatch"
condition in the comparator interrupt routine:
http://www.ccsinfo.com/forum/viewtopic.php?t=22641
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