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 CCS Technical Support

Timer Interrupt - not working
Goto page Previous  1, 2, 3  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

PostPosted: Fri Aug 03, 2012 5:35 am     Reply with quote

Tried changing as suggested plus the 'current read' and 'old read' to the same signed int32.
Code:
signed int tic, input_lsb, input_sb, input_msb = 0;
signed int32 ans;
int count_to_use=8;                          // NOT USED FOR NOW
static long int count=1;
signed int32 fullbyte_read_current, fullbyte_read_old;


It did not make any differnce.
I will try the unions - although I do not think this is the cause of teh problem?
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

PostPosted: Fri Aug 03, 2012 6:06 am     Reply with quote

Hi Sseidman,

I did check with a scope - but only the signal inputs into the PIC. Did not think of using it to check the time-out for the 'tic' I will do it and see. thanks.



Quote:
4) Is the problem possibly due to erronous reads when I carry out the snap-shot?? noise or a glitch?? I checked with a scope on some of the channels and the signal looks OK - but I would need a logic analyser to be sure - and I dont have that!

carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

PostPosted: Fri Aug 03, 2012 6:28 am     Reply with quote

Set the tic - to toggle a spare I/O pin.

The interrupt could clearly be seen at 170uS as expected. And it took 40uS to complete the whole code - therefore well within the 170uS.

So the code 'time' is not to blame.

Thanks Sseidman - I can now rule this out - and have also proven that the timer works.
sseidman



Joined: 14 Mar 2005
Posts: 159

View user's profile Send private message

PostPosted: Fri Aug 03, 2012 6:33 am     Reply with quote

carl wrote:
Hi Sseidman,

I did check with a scope - but only the signal inputs into the PIC. Did not think of using it to check the time-out for the 'tic' I will do it and see. thanks.



When you program on an embedded system, it's often tougher to debug than it is with a non-embedded program. Even with good hardware debugging tools, it's not like you can just open up a window and see what the processor is doing. Waggling an output bit is one of those key tools in your toolbox that can help you see, and it's a ton less intrusive than a printf to a terminal or an LCD. Developing would be much less pleasant it I couldn't waggle an output bit.

I find I have a faster and easier time through my development path if as I'm writing pseudo-code and laying out hardware, I constantly ask myself "What am I going to need to do to debug this if it doesn't work?" and build this in. Sometimes it turns into something as simple as leaving myself a test point to conveniently attach a ground on a PCB. Other times its making sure I have generated the right signal to trigger the scope off of. Sometimes it even involves adding a DAC if I think an analog out would help me confirm good operation of something like an accelerometer when a printf is just too slow.

Thinking from the "how will I know this works, and how can I debug it?" point of view from a very early stage will usually save you time and other resources, and it's quite mandatory if you need to maintain design controls for some regulatory body, like the FDA. In fact, add "document" to the list for design controls. I just added a second NIC to my development computer to facilitate record-keeping from my oscilloscope!
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

PostPosted: Fri Aug 03, 2012 6:37 am     Reply with quote

Totally agree - 'deign for test' I think it is called. Making sure that everything can be tested at different stages to prove it works.

However, I am only slightly forward with this - as the fault still remains.
thanks
anyway
Ttelmah



Joined: 11 Mar 2010
Posts: 20161

View user's profile Send private message

PostPosted: Fri Aug 03, 2012 7:51 am     Reply with quote

sseidman wrote:
Oscilloscope. A key piece of bench gear that everyone beyond novice hobbyist should have.

"Beyond novice hobbyist".
Below this, they should be building one!....

I must admit I thought the problem was going to be the need for int32, perhaps resulting in 'wrapped' values when movements were quicker.

Obviously, the 'delay_ms(1000)', would potentially cause update problems if the code goes this way, which is increasingly likely to happen with faster movements.

Best Wishes
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

PostPosted: Fri Aug 03, 2012 8:20 am     Reply with quote

Hi Ttelmah,

The delay is just there so that I can see on the LCD when an error occurs. Once I get this thing working obviously this will be removed.

I tried implementing your code, but got lots of compile errors - because I am not implementing it correctly.

Please you you have a look and see how I am doing it wrong. I need to implement it in main first - to ensure I get the first reading (called old).

And then in the while (1) loop the code continuously checks for 'new'.
My attempt is below:
Code:

/////// MASTER PROGRAM ////////
///*
#include <18F4550.H>
#fuses EC, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP, NOPBADEN, NOVREGEN
#use delay(clock=48000000)
#use standard_io(a)     
#use standard_io(b)
#use standard_io(c)     
#use standard_io(d)
#use standard_io(e)


#byte UCFG = getenv("SFR:UCFG") //USB Registors
#bit UTRDIS = UCFG.3         //USB Registors

#define SPI_MODE_0 (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_1 (SPI_L_TO_H)
#define SPI_MODE_2 (SPI_H_TO_L)
#define SPI_MODE_3 (SPI_H_TO_L | SPI_XMIT_L_TO_H)

#include "flex_lcd_16x1.c"
#define SPI_SS      PIN_E2
#define TIC_CHECK   PIN_E1

#define LSB_LATCH_LE_CL3  PIN_B7
#define LSB_LATCH_OE_CL8  PIN_B2
#define SB_LATCH_LE_CL4   PIN_B6
#define SB_LATCH_OE_CL7   PIN_B3
#define MSB_LATCH_LE_CL5  PIN_B5
#define MSB_LATCH_OE_CL6  PIN_B4


//============================

signed int tic, input_lsb, input_sb, input_msb = 0;
signed int32 ans, my_bytes;
int count_to_use=8;                          // NOT USED FOR NOW
static long int count=1;
signed int32 fullbyte_read_current, fullbyte_read_old;

#int_RTCC
void RTCC_isr(void)
{
//    if (--count==0) {
//        count=count_to_use;                                                                                                       
        tic = 1;
output_high(TIC_CHECK);           // USED FOR TIMING CHECK (SCOPE)
                        // SET INTERRUPT HAS OCCURRED FLAG                           
//    }
//    else  {                       
//        tic = 2;                                                                                         
// }
}
void main()
{
setup_timer_0(RTCC_INTERNAL|RTCC_8_BIT|RTCC_DIV_8);  // this should generate a 170.6uS interrupt.
enable_interrupts(INT_TIMER0);
enable_interrupts(global);
output_high(SPI_SS);                             // Initial Slave Select to a high level
output_low(TIC_CHECK);
setup_spi(SPI_MASTER | SPI_MODE_1 | SPI_CLK_DIV_4);
SETUP_ADC_PORTS(NO_ANALOGS);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
setup_psp(PSP_DISABLED);
setup_ccp1(CCP_OFF);
output_low(LSB_LATCH_LE_CL3);                     //Disable all latch's
output_high(LSB_LATCH_OE_CL8);
output_low(SB_LATCH_LE_CL4);
output_high(SB_LATCH_OE_CL7);
output_low(MSB_LATCH_LE_CL5);
output_high(MSB_LATCH_OE_CL6);

////////////////////////////////////////INITIAL 24-BIT CATCH (OLD VALUE)//////////////////////////

typedef struct {
    int8 input_lsb;
    int8 input_sb;
    int8 input_msb;
    int8 TOP;
} my_bytes;
   
union to_int32 {
    my_bytes part;
    int32 whole;
} fullbyte_read_current, fullbyte_read_old;


   output_high(LSB_LATCH_LE_CL3);   //LSB Latch Enable
   output_high(SB_LATCH_LE_CL4);    //SB Latch Enable
   output_high(MSB_LATCH_LE_CL5);   //MSB Latch Enable
   delay_us(5);                //More than required
 
   output_low(LSB_LATCH_OE_CL8);   //LSB Latch Output Enable
   delay_us(5);                //More than required
   //input_lsb = input_d();         //Read LSB Byte
   fullbyte_read_old.part.input_lsb=input_d();
   delay_us(1);                  //More than required???
   output_low(LSB_LATCH_LE_CL3);   //Disable Latch
   output_high(LSB_LATCH_OE_CL8);
   delay_us(1);                  //More than required

   output_low(SB_LATCH_OE_CL7);      //SB Latch Output Enable
   delay_us(5);                  //More than required
   //input_sb = input_d();         //Read SB Byte
   fullbyte_read_old.part.input_sb=input_d();
   delay_us(1);                  //More than required???
   output_low(SB_LATCH_LE_CL4);      //Disable Latch
   output_high(SB_LATCH_OE_CL7);
   delay_us(1);                //More than required

   output_low(MSB_LATCH_OE_CL6);    //MSB Latch Output Enable
   delay_us(5);                  //More than required
   //input_msb = input_d();         //Read SB Byte
   fullbyte_read_old.part.input_msb=input_d();
   delay_us(1);                  //More than required???
   output_low(MSB_LATCH_LE_CL5);   //Disable Latch
   output_high(MSB_LATCH_OE_CL6);
   delay_us(1);                  //More than required

//   fullbyte_read_old = make32(0, input_msb, input_sb, input_lsb); //Take initial 24-bit Snap-Shot
fullbyte_read_current.part.input_top=0;

//////////////////////////////////////////////////////////////////////////////////////////////////
 
while(1)
  {
if (tic == 1){   ////////////////////INTERRUPT HAS OCCURRED - WE NOW HAVE 170.4uS TO DO EVERYTHING BEFORE THE NEXT INTERRUPT///////////////////////////
                              //RESET INTERRUPT FLAG
 
   output_high(LSB_LATCH_LE_CL3);   //LSB Latch Enable
   output_high(SB_LATCH_LE_CL4);    //SB Latch Enable
   output_high(MSB_LATCH_LE_CL5);   //MSB Latch Enable
   delay_us(5);                //More than required
 
   output_low(LSB_LATCH_OE_CL8);   //LSB Latch Output Enable
   delay_us(5);                //More than required
   //input_lsb = input_d();         //Read LSB Byte
   fullbyte_read_current.part.input_lsb=input_d();
   delay_us(1);                  //More than required???
   output_low(LSB_LATCH_LE_CL3);   //Disable Latch
   output_high(LSB_LATCH_OE_CL8);
   delay_us(1);                  //More than required

   output_low(SB_LATCH_OE_CL7);      //SB Latch Output Enable
   delay_us(5);                  //More than required
   //input_sb = input_d();         //Read SB Byte
   fullbyte_read_current.part.input_sb=input_d();
   delay_us(1);                  //More than required???
   output_low(SB_LATCH_LE_CL4);      //Disable Latch
   output_high(SB_LATCH_OE_CL7);
   delay_us(1);                //More than required

   output_low(MSB_LATCH_OE_CL6);    //MSB Latch Output Enable
   delay_us(5);                  //More than required
   //input_msb = input_d();         //Read SB Byte
   fullbyte_read_current.part.input_msb=input_d();
   delay_us(1);                  //More than required???
   output_low(MSB_LATCH_LE_CL5);   //Disable Latch
   output_high(MSB_LATCH_OE_CL6);
   delay_us(1);                  //More than required


/////////////////////////////////////ERROR DETECTION CODE///////////////////////////////
//   fullbyte_read_current = make32(0, input_msb, input_sb, input_lsb);    //TAKE CURRENT READ
//   ans = (fullbyte_read_current - fullbyte_read_old);               //TAKE ANWAY FORM PREVIOUS READ            
//   fullbyte_read_old = fullbyte_read_current;                      //OLD = NEW READ
//////////////////////////////////////////////////////////////////////////////////////////

ans=fullbyte_read_current.whole-fullbyte_read_old.whole;
fullbyte_read_old.whole=fullbyte_read_current.whole;


////////////////////////////////////CHECK IF AN ERROR HAS OCCURRED AND SEND VALUE//////////
         if ((( ans >=-1) && (ans <=1)) || (ans == 0)) {
//         if (true){
          output_low(SPI_SS);              // NO ERRORS DETECTED - SEND 24 BIT VALUE TO SLAVE
          delay_cycles(7);
          spi_write(ans); 
         delay_cycles(7);
       // spi_write(input_sb);   
         // delay_us(10);
         // spi_write(input_msb); 
         // delay_us(10);
            output_high(SPI_SS);
         //input_lsb = 0;                   //RESET
         //input_sb = 0;
         //input_msb = 0;
           delay_cycles(7);
            ans = 0;                   // RESET ANS
            tic=0;                      // RESET TIC               
         output_low(TIC_CHECK);          // USED FOR TIMING CHECK (SCOPE)
}
   
     else if (( ans <-1) || (ans >1)){
           output_low(SPI_SS);           //  ERROR DETECTED - SEND ERROR MESSAGE
          delay_cycles(7);
          spi_write(ans); 
         delay_cycles(7);
       // spi_write(input_sb);   
         // delay_us(10);
         // spi_write(input_msb); 
         // delay_us(10);
            output_high(SPI_SS);
         //input_lsb = 0;                   //RESET
         //input_sb = 0;
         //input_msb = 0;
           delay_ms(1000);}
}
   
////////////////////////////////////////////////////////////////////////////////////////////////////

                                   
//else if (tic == 2){      ////////////////////INTERRUPT HAS NOT OCCURRED - DO SOMETHING ELSE///////////////////////////
//   tic=0;
//   output_low(SPI_SS);
//   spi_write(10);
//   delay_cycles(7);
//   spi_write(0);
//   delay_us(10);
//   spi_write(0);
//   delay_us(10);
//   output_high(SPI_SS);
//   delay_cycles(7);
//   delay_ms(1000);
//}

}
}



Also this is probably a 'red-herring' but a lot of the time when the error occurrs, the value of 'ANS' is '0'. This doesn't make sense, because '0' is within the detection value of the no error condition of : ((( ans >=-1) && (ans <=1)) || (ans == 0)) .

And secondly a '0' is outside of the error detection code: (( ans <-1) || (ans >1)).

I dont understand this. How can it possibly error on zero????


Thanks for all your help
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

PostPosted: Fri Aug 03, 2012 9:16 am     Reply with quote

managed it with the union - but still the same error is occurring
Code:
/////// MASTER PROGRAM ////////
///*
#include <18F4550.H>
#fuses EC, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP, NOPBADEN, NOVREGEN
#use delay(clock=48000000)
#use standard_io(a)     
#use standard_io(b)
#use standard_io(c)     
#use standard_io(d)
#use standard_io(e)


#byte UCFG = getenv("SFR:UCFG") //USB Registors
#bit UTRDIS = UCFG.3         //USB Registors

#define SPI_MODE_0 (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_1 (SPI_L_TO_H)
#define SPI_MODE_2 (SPI_H_TO_L)
#define SPI_MODE_3 (SPI_H_TO_L | SPI_XMIT_L_TO_H)

#include "flex_lcd_16x1.c"
#define SPI_SS      PIN_E2
#define TIC_CHECK   PIN_E1

#define LSB_LATCH_LE_CL3  PIN_B7
#define LSB_LATCH_OE_CL8  PIN_B2
#define SB_LATCH_LE_CL4   PIN_B6
#define SB_LATCH_OE_CL7   PIN_B3
#define MSB_LATCH_LE_CL5  PIN_B5
#define MSB_LATCH_OE_CL6  PIN_B4


//============================

signed int tic, input_lsb, input_sb, input_msb = 0;
signed int32 ans, my_bytes;
int count_to_use=8;                          // NOT USED FOR NOW
static long int count=1;
signed int32 fullbyte_read_current, fullbyte_read_old;

#int_RTCC
void RTCC_isr(void)
{
//    if (--count==0) {
//        count=count_to_use;                                                                                                       
        tic = 1;
output_high(TIC_CHECK);           // USED FOR TIMING CHECK (SCOPE)
                        // SET INTERRUPT HAS OCCURRED FLAG                           
//    }
//    else  {                       
//        tic = 2;                                                                                         
// }
}
void main()
{
typedef struct {
    int8 input_lsb;
    int8 input_sb;
    int8 input_msb;
    int8 TOP;
} my_bytes;
   
union to_int32 {
    my_bytes part;
    int32 whole;
} fullbyte_read_current, fullbyte_read_old;

setup_timer_0(RTCC_INTERNAL|RTCC_8_BIT|RTCC_DIV_8);  // this should generate a 170.6uS interrupt.
enable_interrupts(INT_TIMER0);
enable_interrupts(global);
UTRDIS = 1;
output_high(SPI_SS);                             // Initial Slave Select to a high level
output_low(TIC_CHECK);
setup_spi(SPI_MASTER | SPI_MODE_1 | SPI_CLK_DIV_4);
SETUP_ADC_PORTS(NO_ANALOGS);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
setup_psp(PSP_DISABLED);
setup_ccp1(CCP_OFF);
output_low(LSB_LATCH_LE_CL3);                     //Disable all latch's
output_high(LSB_LATCH_OE_CL8);
output_low(SB_LATCH_LE_CL4);
output_high(SB_LATCH_OE_CL7);
output_low(MSB_LATCH_LE_CL5);
output_high(MSB_LATCH_OE_CL6);

////////////////////////////////////////INITIAL 24-BIT CATCH (OLD VALUE)//////////////////////////

   output_high(LSB_LATCH_LE_CL3);   //LSB Latch Enable
   output_high(SB_LATCH_LE_CL4);    //SB Latch Enable
   output_high(MSB_LATCH_LE_CL5);   //MSB Latch Enable
   delay_us(5);                //More than required
 
   output_low(LSB_LATCH_OE_CL8);   //LSB Latch Output Enable
   delay_us(5);                //More than required
   //input_lsb = input_d();         //Read LSB Byte
   fullbyte_read_old.part.input_lsb=input_d();
   delay_us(1);                  //More than required???
   output_low(LSB_LATCH_LE_CL3);   //Disable Latch
   output_high(LSB_LATCH_OE_CL8);
   delay_us(1);                  //More than required

   output_low(SB_LATCH_OE_CL7);      //SB Latch Output Enable
   delay_us(5);                  //More than required
   //input_sb = input_d();         //Read SB Byte
   fullbyte_read_old.part.input_sb=input_d();
   delay_us(1);                  //More than required???
   output_low(SB_LATCH_LE_CL4);      //Disable Latch
   output_high(SB_LATCH_OE_CL7);
   delay_us(1);                //More than required

   output_low(MSB_LATCH_OE_CL6);    //MSB Latch Output Enable
   delay_us(5);                  //More than required
   //input_msb = input_d();         //Read SB Byte
   fullbyte_read_old.part.input_msb=input_d();
   delay_us(1);                  //More than required???
   output_low(MSB_LATCH_LE_CL5);   //Disable Latch
   output_high(MSB_LATCH_OE_CL6);
   delay_us(1);                  //More than required

//   fullbyte_read_old = make32(0, input_msb, input_sb, input_lsb); //Take initial 24-bit Snap-Shot
fullbyte_read_old.part.TOP=0;

//////////////////////////////////////////////////////////////////////////////////////////////////
 
while(1)
  {
if (tic == 1){   ////////////////////INTERRUPT HAS OCCURRED - WE NOW HAVE 170.4uS TO DO EVERYTHING BEFORE THE NEXT INTERRUPT///////////////////////////
                              //RESET INTERRUPT FLAG
 
   output_high(LSB_LATCH_LE_CL3);   //LSB Latch Enable
   output_high(SB_LATCH_LE_CL4);    //SB Latch Enable
   output_high(MSB_LATCH_LE_CL5);   //MSB Latch Enable
   delay_us(5);                //More than required
 
   output_low(LSB_LATCH_OE_CL8);   //LSB Latch Output Enable
   delay_us(5);                //More than required
   //input_lsb = input_d();         //Read LSB Byte
   fullbyte_read_current.part.input_lsb=input_d();
   delay_us(1);                  //More than required???
   output_low(LSB_LATCH_LE_CL3);   //Disable Latch
   output_high(LSB_LATCH_OE_CL8);
   delay_us(1);                  //More than required

   output_low(SB_LATCH_OE_CL7);      //SB Latch Output Enable
   delay_us(5);                  //More than required
   //input_sb = input_d();         //Read SB Byte
   fullbyte_read_current.part.input_sb=input_d();
   delay_us(1);                  //More than required???
   output_low(SB_LATCH_LE_CL4);      //Disable Latch
   output_high(SB_LATCH_OE_CL7);
   delay_us(1);                //More than required

   output_low(MSB_LATCH_OE_CL6);    //MSB Latch Output Enable
   delay_us(5);                  //More than required
   //input_msb = input_d();         //Read SB Byte
   fullbyte_read_current.part.input_msb=input_d();
   delay_us(1);                  //More than required???
   output_low(MSB_LATCH_LE_CL5);   //Disable Latch
   output_high(MSB_LATCH_OE_CL6);
   delay_us(1);                  //More than required


/////////////////////////////////////ERROR DETECTION CODE///////////////////////////////
//   fullbyte_read_current = make32(0, input_msb, input_sb, input_lsb);    //TAKE CURRENT READ
//   ans = (fullbyte_read_current - fullbyte_read_old);               //TAKE ANWAY FORM PREVIOUS READ            
//   fullbyte_read_old = fullbyte_read_current;                      //OLD = NEW READ
//////////////////////////////////////////////////////////////////////////////////////////

ans=fullbyte_read_current.whole-fullbyte_read_old.whole;
fullbyte_read_old.whole=fullbyte_read_current.whole;


////////////////////////////////////CHECK IF AN ERROR HAS OCCURRED AND SEND VALUE//////////
         if ((( ans >=-1) && (ans <=1)) || (ans == 0)) {
//         if (true){
          output_low(SPI_SS);              // NO ERRORS DETECTED - SEND 24 BIT VALUE TO SLAVE
          delay_cycles(7);
          spi_write(ans); 
         delay_cycles(7);
       // spi_write(input_sb);   
         // delay_us(10);
         // spi_write(input_msb); 
         // delay_us(10);
            output_high(SPI_SS);
         //input_lsb = 0;                   //RESET
         //input_sb = 0;
         //input_msb = 0;
           delay_cycles(7);
            ans = 0;                   // RESET ANS
            tic=0;                      // RESET TIC               
         output_low(TIC_CHECK);          // USED FOR TIMING CHECK (SCOPE)
}
   
     else if (( ans <-1) || (ans >1)){
           output_low(SPI_SS);           //  ERROR DETECTED - SEND ERROR MESSAGE
          delay_cycles(7);
          spi_write(ans); 
         delay_cycles(7);
       // spi_write(input_sb);   
         // delay_us(10);
         // spi_write(input_msb); 
         // delay_us(10);
            output_high(SPI_SS);
         //input_lsb = 0;                   //RESET
         //input_sb = 0;
         //input_msb = 0;
           delay_ms(1000);}
}
   
////////////////////////////////////////////////////////////////////////////////////////////////////

                                   
//else if (tic == 2){      ////////////////////INTERRUPT HAS NOT OCCURRED - DO SOMETHING ELSE///////////////////////////
//   tic=0;
//   output_low(SPI_SS);
//   spi_write(10);
//   delay_cycles(7);
//   spi_write(0);
//   delay_us(10);
//   spi_write(0);
//   delay_us(10);
//   output_high(SPI_SS);
//   delay_cycles(7);
//   delay_ms(1000);
//}

}
}
Ttelmah



Joined: 11 Mar 2010
Posts: 20161

View user's profile Send private message

PostPosted: Fri Aug 03, 2012 3:55 pm     Reply with quote

As a comment, the || (ans==0) does nothing except waste time...
If ans is greater or equal to -1, and less than or equal to 1, it has to be -1,0, or 1.

I'd actually suspect it'd be quicker to just test 'for' the three legal values. So:

if ((ans==-1) || (ans==0) || (ans==1))

Why not simplify your code a lot. Have a little routine, or a #define to latch one byte using a given latch line, and return this. Then use this for all six of your fetches. Reducing duplication, reduces the likelyhood of errors.

However I'd really be wondering if there is some hardware problem appearing when the data change rate rises.

Consider whether you could use fixed_io (or fast_io), and latch all the latch bits at once. What would happen if a change took place between one being latched, and the next?. The likelyhood of this, obviously rises with frequency. Since you are only sending one byte from ans, ans would be seen as 'zero', when in fact the result was 256 for example.

Best Wishes
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

PostPosted: Sat Aug 04, 2012 4:10 am     Reply with quote

Hi Ttelmah thanks for getting back. See below:
Quote:
As a comment, the || (ans==0) does nothing except waste time...
If ans is greater or equal to -1, and less than or equal to 1, it has to be -1,0, or 1.

I'd actually suspect it'd be quicker to just test 'for' the three legal values. So:

if ((ans==-1) || (ans==0) || (ans==1))


I knew all this and the redundant statement - but just wanted to be doubly sure that the logic was correct. I tried a few different methods and they were all the same - so i am happy that the way I have done it is not wrong - it's just duplication. I will use your method.

Quote:
Why not simplify your code a lot. Have a little routine, or a #define to latch one byte using a given latch line, and return this. Then use this for all six of your fetches. Reducing duplication, reduces the likelyhood of errors.



Don't quite understand what you mean. If I had a routine that can be called for like the code below (take_snapshot()). and you just called it when required? is this what you mean?, but wouldn't this just be the same as it is now in the 'while' loop:
Code:


Take_snapshot ()

(

output_high(LSB_LATCH_LE_CL3);   //LSB Latch Enable
   output_high(SB_LATCH_LE_CL4);    //SB Latch Enable
   output_high(MSB_LATCH_LE_CL5);   //MSB Latch Enable
   delay_us(5);                //More than required
 
   output_low(LSB_LATCH_OE_CL8);   //LSB Latch Output Enable
   delay_us(5);                //More than required
   //input_lsb = input_d();         //Read LSB Byte
   fullbyte_read_old.part.input_lsb=input_d();
   delay_us(1);                  //More than required???
   output_low(LSB_LATCH_LE_CL3);   //Disable Latch
   output_high(LSB_LATCH_OE_CL8);
   delay_us(1);                  //More than required

   output_low(SB_LATCH_OE_CL7);      //SB Latch Output Enable
   delay_us(5);                  //More than required
   //input_sb = input_d();         //Read SB Byte
   fullbyte_read_old.part.input_sb=input_d();
   delay_us(1);                  //More than required???
   output_low(SB_LATCH_LE_CL4);      //Disable Latch
   output_high(SB_LATCH_OE_CL7);
   delay_us(1);                //More than required

   output_low(MSB_LATCH_OE_CL6);    //MSB Latch Output Enable
   delay_us(5);                  //More than required
   //input_msb = input_d();         //Read SB Byte
   fullbyte_read_old.part.input_msb=input_d();
   delay_us(1);                  //More than required???
   output_low(MSB_LATCH_LE_CL5);   //Disable Latch
   output_high(MSB_LATCH_OE_CL6);
   delay_us(1);                  //More than required

//   fullbyte_read_old = make32(0, input_msb, input_sb, input_lsb); //Take initial 24-bit Snap-Shot
fullbyte_read_old.part.TOP=0;



Quote:
However I'd really be wondering if there is some hardware problem appearing when the data change rate rises.


Regarding the above statement, I think this is where I think the problem lies. I double checked the signals coming into the pic - to see if their was a problem on one of the the signals (shorted or not a sharp waveform), but they look good - nice clean 5uS square wave pulses. I would need someway of logging the byte output to check - ensuring that the count is correct. I need to think about this carefully. One issue that I dont fully understand yet though is when the encoder 'passes '0' and continues going anti-clockwise. When I look at the signals with a scope in this condition (negative numbers), then the siganls are inverted (so always high and then pulled low for 5uS. I think this is correct and what i should be seeing. It always fails in this condition though - but this is due to probably due to me just not understand and implementing the correct code to catch this condition. It is not where the main concern lays.
Quote:
Consider whether you could use fixed_io (or fast_io), and latch all the latch bits at once


I have thought about this, but surely this is only going to reduce the amount of time it takes to carry out the program - and as already measured with the scope - the interrupt occurrs every 170uS. the current program takes 40uS. That is loads of spare time (plus most of that 40uS is because of the 'large delays?' I have incorporated into the snap-shot code. Also everyone seems to say - steer away from fast i/o unless absolutely necassary - and going off the timings I don't this is where the problem lies.

Quote:
What would happen if a change took place between one being latched, and the next?. The likelyhood of this, obviously rises with frequency


Same as above - even with the encoder signal at 5KHz (200ms), a change should always be detected and cought. And I have not been testing it that fast either - only probably 2-3KHz so far.

Quote:
Since you are only sending one byte from ans, ans would be seen as 'zero', when in fact the result was 256 for example.



Yes I understand. Thankyou for that. But how could the 'snap-shots' be so different - it is so annoying.

If I do get this snap-shot issue sorted, please can you continue helping me with sending the three bytes over SPI - I still cannot get it to work correctly (nearly but not quite there). Again though, this is something for later - still got to understand why/how this is failing first.

Thanks
Carl[/b]
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

PostPosted: Sat Aug 04, 2012 4:11 am     Reply with quote

Hi Ttelmah thanks for getting back. See below:
Quote:
As a comment, the || (ans==0) does nothing except waste time...
If ans is greater or equal to -1, and less than or equal to 1, it has to be -1,0, or 1.

I'd actually suspect it'd be quicker to just test 'for' the three legal values. So:

if ((ans==-1) || (ans==0) || (ans==1))


I knew all this and the redundant statement - but just wanted to be doubly sure that the logic was correct. I tried a few different methods and they were all the same - so i am happy that the way I have done it is not wrong - it's just duplication. I will use your method.

Quote:
Why not simplify your code a lot. Have a little routine, or a #define to latch one byte using a given latch line, and return this. Then use this for all six of your fetches. Reducing duplication, reduces the likelyhood of errors.



Don't quite understand what you mean. If I had a routine that can be called for like the code below (take_snapshot()). and you just called it when required? is this what you mean?, but wouldn't this just be the same as it is now in the 'while' loop:
Code:


Take_snapshot ()

(

output_high(LSB_LATCH_LE_CL3);   //LSB Latch Enable
   output_high(SB_LATCH_LE_CL4);    //SB Latch Enable
   output_high(MSB_LATCH_LE_CL5);   //MSB Latch Enable
   delay_us(5);                //More than required
 
   output_low(LSB_LATCH_OE_CL8);   //LSB Latch Output Enable
   delay_us(5);                //More than required
   //input_lsb = input_d();         //Read LSB Byte
   fullbyte_read_old.part.input_lsb=input_d();
   delay_us(1);                  //More than required???
   output_low(LSB_LATCH_LE_CL3);   //Disable Latch
   output_high(LSB_LATCH_OE_CL8);
   delay_us(1);                  //More than required

   output_low(SB_LATCH_OE_CL7);      //SB Latch Output Enable
   delay_us(5);                  //More than required
   //input_sb = input_d();         //Read SB Byte
   fullbyte_read_old.part.input_sb=input_d();
   delay_us(1);                  //More than required???
   output_low(SB_LATCH_LE_CL4);      //Disable Latch
   output_high(SB_LATCH_OE_CL7);
   delay_us(1);                //More than required

   output_low(MSB_LATCH_OE_CL6);    //MSB Latch Output Enable
   delay_us(5);                  //More than required
   //input_msb = input_d();         //Read SB Byte
   fullbyte_read_old.part.input_msb=input_d();
   delay_us(1);                  //More than required???
   output_low(MSB_LATCH_LE_CL5);   //Disable Latch
   output_high(MSB_LATCH_OE_CL6);
   delay_us(1);                  //More than required

//   fullbyte_read_old = make32(0, input_msb, input_sb, input_lsb); //Take initial 24-bit Snap-Shot
fullbyte_read_old.part.TOP=0;



Quote:
However I'd really be wondering if there is some hardware problem appearing when the data change rate rises.


Regarding the above statement, I think this is where I think the problem lies. I double checked the signals coming into the pic - to see if their was a problem on one of the the signals (shorted or not a sharp waveform), but they look good - nice clean 5uS square wave pulses. I would need someway of logging the byte output to check - ensuring that the count is correct. I need to think about this carefully. One issue that I dont fully understand yet though is when the encoder 'passes '0' and continues going anti-clockwise. When I look at the signals with a scope in this condition (negative numbers), then the siganls are inverted (so always high and then pulled low for 5uS. I think this is correct and what i should be seeing. It always fails in this condition though - but this is due to probably due to me just not understand and implementing the correct code to catch this condition. It is not where the main concern lays.
Quote:
Consider whether you could use fixed_io (or fast_io), and latch all the latch bits at once


I have thought about this, but surely this is only going to reduce the amount of time it takes to carry out the program - and as already measured with the scope - the interrupt occurrs every 170uS. the current program takes 40uS. That is loads of spare time (plus most of that 40uS is because of the 'large delays?' I have incorporated into the snap-shot code. Also everyone seems to say - steer away from fast i/o unless absolutely necassary - and going off the timings I don't this is where the problem lies.

Quote:
What would happen if a change took place between one being latched, and the next?. The likelyhood of this, obviously rises with frequency


Same as above - even with the encoder signal at 5KHz (200ms), a change should always be detected and cought. And I have not been testing it that fast either - only probably 2-3KHz so far.

Quote:
Since you are only sending one byte from ans, ans would be seen as 'zero', when in fact the result was 256 for example.



Yes I understand. Thankyou for that. But how could the 'snap-shots' be so different - it is so annoying.

If I do get this snap-shot issue sorted, please can you continue helping me with sending the three bytes over SPI - I still cannot get it to work correctly (nearly but not quite there). Again though, this is something for later - still got to understand why/how this is failing first.

Thanks
Carl[/b]
Ttelmah



Joined: 11 Mar 2010
Posts: 20161

View user's profile Send private message

PostPosted: Sat Aug 04, 2012 6:38 am     Reply with quote

You are missing the point slightly.
You have three latches. You are operating their latch pins 'one after the other'.
Now, imagine that this was a counter, and you did this. Operated (say) the low byte latch, then the next byte, then the next. Problem is, what happens if at the exact moment you operate the latches, there was a 'carry' from the low byte to the next byte?. So it was counting from 0xff to 0x100. So if you operate the low byte latch 'first', then this records '0xFF', and you then latch the next byte, which at this moment has received the carry, so records 1. You get 1FF, while the actual value should be 0x100. If instead you latch the high byte first, this records 00 (the carry hasn't happened yet), and then you latch the low byte, which also gives 00 (since there has now been the carry). So in both cases, you 'miss-record' the real value...
The three bytes _must_ be latched together. Total speed is not the problem, but being 'simultaneous' is.

Best Wishes
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

PostPosted: Sat Aug 04, 2012 6:39 am     Reply with quote

hmmm...

Just thought of something.
Because the interrupt is not synchronised with the encoder 'step-change' (2^0), it could be possible for the code to change at the moment I a enablling the 'latch-enable'??

the latch enables are all done at the same time here:
Code:


   output_high(LSB_LATCH_LE_CL3);   //LSB Latch Enable
   output_high(SB_LATCH_LE_CL4);    //SB Latch Enable
   output_high(MSB_LATCH_LE_CL5);   //MSB Latch Enable


so if the step-change changed during the above enable process - an error would occur!!!

But I wonder what the chances are of this happening.

One way to check would be to use an 'interruupt on change' instead of the timer approach. But wouldn't this take longer? and also isn't the ISR on port B only? or could I connect the 2^0 to port D as normal and to RB0.

This way synchronisation will take place and he above condition can never happen??

What do you think?
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

PostPosted: Sat Aug 04, 2012 6:47 am     Reply with quote

We both wrote at the same time, and I think were talking about the same possibility.

So how to ensure they are latched at the same time?

output_high ((LSB_LATCH_LE_CL3) && (LSB_LATCH_LE_CL3) && (LSB_LATCH_LE_CL3)).


Wouldn't this more or less be the same as:
Code:
   output_high(LSB_LATCH_LE_CL3);   //LSB Latch Enable
   output_high(SB_LATCH_LE_CL4);    //SB Latch Enable
   output_high(MSB_LATCH_LE_CL5);   //MSB Latch Enable


?
Ttelmah



Joined: 11 Mar 2010
Posts: 20161

View user's profile Send private message

PostPosted: Sat Aug 04, 2012 8:20 am     Reply with quote

Neither of these operates the latches 'at the same time'.

In each case you are performing three operations, 'one after the other. The only thing that results in the three latches being changed _at the same time_, is to perform a _single_ byte output on the register, and change all the bits in one operation.
Output_high, does not allow you to '&&' together multiple bits. It is a _bit_ instruction.
Performing the three operations one after the other, means there is a total of six instruction times between setting the first latch, and setting the last one.

In this case I think you need to go to either fixed_io, or fast_io. Set the TRIS bits up, then you can use:
Code:

#define LSB_LATCH_LE_CL3  128
#define LSB_LATCH_OE_CL8  4
#define SB_LATCH_LE_CL4   64
#define SB_LATCH_OE_CL7   8
#define MSB_LATCH_LE_CL5  32
#define MSB_LATCH_OE_CL6  16

output_b(LSB_LATCH_LE_CL3 | SB_LATCH_LE_CL4  | MSB_LATCH_LE_CL5);

//sets all the latches in one operation


Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2, 3  Next
Page 2 of 3

 
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