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

WDT Sleep problem with loop
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
simat



Joined: 05 Feb 2008
Posts: 7

View user's profile Send private message Visit poster's website

WDT Sleep problem with loop
PostPosted: Wed Aug 06, 2008 10:50 am     Reply with quote

Hi,

I'm having a weird problem, I think it may have something to do with a stack getting full, the code below prints 'POL' three times before getting stuck in a do while(true) loop used in MAIN (the code is part of main) How are variables handling during sleeping.
If I don't check the value of radiostatus in IF statement at the top of the code then everything is fine. radiostatus is set to 2 after an event.

Basically after sleep the if(cycle > 1 && radiostatus > 1) doesn't execute even though the conditions are met.

It also looks like the interrupts stop working, INT_RDA, INT_TBE, INT_TIMER1

Any ideas ?

Cheers

Simon.

Code:


//NEAR the bottom of MAIN()

if(cycle > 1 && radiostatus > 1)
      {                                           // no sleep until radiostatus is 2
         
         
         
         delay_ms(10);      // Enought time for xmit of 5 bytes at 19200 BPS
         
         cycle = 2;     // for some weird reason we need this else it gets stuck in sleep
         
         while (cycle > 0) {                 
            sleep();
            delay_cycles(1);
            cycle--;
         }
             
         strcpy(m_buffer,"POL");
         bprintf(m_buffer);             // Print POL to serial port.

          }
     
      cycle++;                // For above WDT control and Main loop control whilst out of sleep,
                              // we use this variable to count up as well as down
     
      restart_wdt();   // feed the dog - resets PIC only if we're not sleep and the dog doesn't get fed.

//back to top of MAIN() 
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Aug 06, 2008 5:53 pm     Reply with quote

Post a complete test program. Post the #include, #fuses, and
#use delay() statements. Add the minimum amount of additional code
to demontrate the problem. For example, if you can show the problem
by adding a very small #int_rda routine (and no other interrupt routines)
then do so. The code must be compilable with no errors, and it must
show the problem when it's run on a PIC. (Test it first).

Make it short. Don't put in any unnecessary lines of code. In other words,
don't setup the ADC ports, etc., if they are not needed to demonstrate
the problem.

Also post your compiler version.
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Thu Aug 07, 2008 8:52 am     Reply with quote

Having to add
cycle = 2; // for some weird reason we need this else it gets stuck in sleep

Implies that at some point cycle is much larger when entering this routine causing a delay of
cycles * WDT_timeout * delay_cycles(1)

If cycles is a 16 bit value and is at it's max 65535 and your WDT is set to 4 sec then your loop will take 65535 * 4 * delay_cycles(1) (sec) to execute!
at least 72 hours.

NOW you see why we need more information to help you with this problem Smile
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Thu Aug 07, 2008 8:52 am     Reply with quote

Having to add
cycle = 2; // for some weird reason we need this else it gets stuck in sleep

Implies that at some point cycle is much larger when entering this routine causing a delay of
cycles * WDT_timeout * delay_cycles(1)

If cycles is a 16 bit value and is at it's max 65535 and your WDT is set to 4 sec then your loop will take 65535 * 4 * delay_cycles(1) (sec) to execute!
at least 72 hours.

NOW you see why we need more information to help you with this problem Smile
simat



Joined: 05 Feb 2008
Posts: 7

View user's profile Send private message Visit poster's website

PostPosted: Thu Aug 07, 2008 9:34 am     Reply with quote

Hi, heres some more code, compiler is 4.057

Code:

#include <16f88.h>
#include <string.h>

#fuses HS,WDT,NOPROTECT,PUT,NOBROWNOUT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=19200, xmit=PIN_B5, rcv=PIN_B2, parity=N, bits=8, stop=1, ERRORS, UART) // 16F88

#define DQ PIN_B0          // One Wire Bus pin assignment
#define PWR_EN PIN_B3      // One Wire PWR pin assignment
#define JN5139_EN PIN_B4   // JN5139 Module enable
#define JN5139_WAKE PIN_B1 // JN5139 Interrupt to wakeup from sleep

#define DEBUG PIN_A1       // Debug LED

#define uint8 char
#define uint16 long

// Global Variables
uint8   crc8 = 0;
uint16 crc16 = 0;
uint8  ROMbit[8];
uint8  lastDiscrep = 0;
short  doneFlag = 0;
uint8  FoundROM[9][8];
uint8  maxDevices = 10;
uint8  numROMs = 0;       
uint8  message[4];         
uint8  NTU[8];
uint8  tickCount = 0;     


#define r_BUFFER_SIZE 32      // Receive Buffer
BYTE r_buffer[r_BUFFER_SIZE];
BYTE r_next_in = 0;
BYTE r_next_out = 0;

#define t_BUFFER_SIZE 16      // Transmit Buffer
BYTE t_buffer[t_BUFFER_SIZE];
BYTE t_next_in = 0;
BYTE t_next_out = 0;

#define m_BUFFER_SIZE 64
uint8  m_buffer[m_BUFFER_SIZE];
uint8  m_bufferPtr = 0;


#int_TBE
void t_serial_isr(void)
{
   putc(t_buffer[t_next_out]);
   t_next_out = (t_next_out + 1) % t_BUFFER_SIZE;
   if (t_next_in == t_next_out)
   {
      disable_interrupts(int_tbe);
   }
}


void bputc(char c)
{
   short restart;
   int ni;
   restart = t_next_in == t_next_out;
   t_buffer[t_next_in] = c;
   ni = (t_next_in + 1) % t_BUFFER_SIZE;
   
   while (ni == t_next_out)
   {
      enable_interrupts(int_tbe);
   }
   
   t_next_in = ni;
   
   if (restart)
   {
      enable_interrupts(int_tbe);
   }
}


#int_RDA
void r_serial_isr(void)
{
   BYTE t;
     
   r_buffer[r_next_in] = getc();
   
   t=r_next_in;
   r_next_in = (r_next_in + 1) % r_BUFFER_SIZE;
   if(r_next_in == r_next_out)
      r_next_in = t;                     
}


#define bkbhit (r_next_in != r_next_out)


BYTE bgetc() {
   BYTE c;

   while(!bkbhit) ;   
   
   c = r_buffer[r_next_out];
   r_next_out = (r_next_out+1) % r_BUFFER_SIZE;
   return(c);
}


#int_TIMER1
void t_ticker(void)     // For timing events, tickCount increments every 100ms @ 8MHz CLK - depends on T1 setup.
{
   tickCount++;
}


void main()
{
   uint8 radiostatus = 0;
   uint8 cycle = 0;
   
   disable_interrupts(GLOBAL);
   disable_interrupts(INT_RDA);
   disable_interrupts(INT_TIMER1);
   
   setup_wdt(WDT_2304MS);     // Setup WDT for 2.3 seconds
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);   // Timer 1 for awake time monitoring   
       
   restart_wdt();
   
   enable_interrupts(GLOBAL);
   enable_interrupts(INT_RDA);
 
   set_timer1(0);
   tickCount = 0;
   enable_interrupts(INT_TIMER1);
   
   
   do    // Main Program Loop
   {
      output_toggle(DEBUG);   // Toggle LED to indicate life.
       
      if(bkbhit)     // We have data to decode
      {
            m_buffer[m_bufferPtr]=(bgetc());   
            m_bufferPtr++;                     
           
            // parse m_buffer section
            // do operations on 1-wire devices.
           
     
      }
     
      if(cycle > 1 && radiostatus > 1)      // FYI - cycle incremented below
      {                                       
         
         delay_ms(10);      // Enought time for xmit of 5 bytes at 19200 BPS
         
         cycle = 2;     // for some weird reason we need this.
         
         while (cycle > 0) {
            sleep();
            delay_cycles(1);
            cycle--;
         }
         
         tickCount = 0;
             
         //strcpy(m_buffer,"POL");
         //bprintf(m_buffer);
         bputc(64);   // print @ just for this test example still use INT_RBE interrupt.
         
      }
     
      cycle++;                // For above WDT control and Main loop control whilst out of sleep.
     
      restart_wdt();                 
                                                                           
   } while (TRUE);
}
simat



Joined: 05 Feb 2008
Posts: 7

View user's profile Send private message Visit poster's website

PostPosted: Thu Aug 07, 2008 9:35 am     Reply with quote

Hi, heres some more code, compiler is 4.057

Code:

#include <16f88.h>
#include <string.h>

#fuses HS,WDT,NOPROTECT,PUT,NOBROWNOUT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=19200, xmit=PIN_B5, rcv=PIN_B2, parity=N, bits=8, stop=1, ERRORS, UART) // 16F88

#define DQ PIN_B0          // One Wire Bus pin assignment
#define PWR_EN PIN_B3      // One Wire PWR pin assignment
#define JN5139_EN PIN_B4   // JN5139 Module enable
#define JN5139_WAKE PIN_B1 // JN5139 Interrupt to wakeup from sleep

#define DEBUG PIN_A1       // Debug LED

#define uint8 char
#define uint16 long

// Global Variables
uint8   crc8 = 0;
uint16 crc16 = 0;
uint8  ROMbit[8];
uint8  lastDiscrep = 0;
short  doneFlag = 0;
uint8  FoundROM[9][8];
uint8  maxDevices = 10;
uint8  numROMs = 0;       
uint8  message[4];         
uint8  NTU[8];
uint8  tickCount = 0;     


#define r_BUFFER_SIZE 32      // Receive Buffer
BYTE r_buffer[r_BUFFER_SIZE];
BYTE r_next_in = 0;
BYTE r_next_out = 0;

#define t_BUFFER_SIZE 16      // Transmit Buffer
BYTE t_buffer[t_BUFFER_SIZE];
BYTE t_next_in = 0;
BYTE t_next_out = 0;

#define m_BUFFER_SIZE 64
uint8  m_buffer[m_BUFFER_SIZE];
uint8  m_bufferPtr = 0;


#int_TBE
void t_serial_isr(void)
{
   putc(t_buffer[t_next_out]);
   t_next_out = (t_next_out + 1) % t_BUFFER_SIZE;
   if (t_next_in == t_next_out)
   {
      disable_interrupts(int_tbe);
   }
}


void bputc(char c)
{
   short restart;
   int ni;
   restart = t_next_in == t_next_out;
   t_buffer[t_next_in] = c;
   ni = (t_next_in + 1) % t_BUFFER_SIZE;
   
   while (ni == t_next_out)
   {
      enable_interrupts(int_tbe);
   }
   
   t_next_in = ni;
   
   if (restart)
   {
      enable_interrupts(int_tbe);
   }
}


#int_RDA
void r_serial_isr(void)
{
   BYTE t;
     
   r_buffer[r_next_in] = getc();
   
   t=r_next_in;
   r_next_in = (r_next_in + 1) % r_BUFFER_SIZE;
   if(r_next_in == r_next_out)
      r_next_in = t;                     
}


#define bkbhit (r_next_in != r_next_out)


BYTE bgetc() {
   BYTE c;

   while(!bkbhit) ;   
   
   c = r_buffer[r_next_out];
   r_next_out = (r_next_out+1) % r_BUFFER_SIZE;
   return(c);
}


#int_TIMER1
void t_ticker(void)     // For timing events, tickCount increments every 100ms @ 8MHz CLK - depends on T1 setup.
{
   tickCount++;
}


void main()
{
   uint8 radiostatus = 0;
   uint8 cycle = 0;
   
   disable_interrupts(GLOBAL);
   disable_interrupts(INT_RDA);
   disable_interrupts(INT_TIMER1);
   
   setup_wdt(WDT_2304MS);     // Setup WDT for 2.3 seconds
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);   // Timer 1 for awake time monitoring   
       
   restart_wdt();
   
   enable_interrupts(GLOBAL);
   enable_interrupts(INT_RDA);
 
   set_timer1(0);
   tickCount = 0;
   enable_interrupts(INT_TIMER1);
   
   
   do    // Main Program Loop
   {
      output_toggle(DEBUG);   // Toggle LED to indicate life.
       
      if(bkbhit)     // We have data to decode
      {
            m_buffer[m_bufferPtr]=(bgetc());   
            m_bufferPtr++;                     
           
            // parse m_buffer section
            // do operations on 1-wire devices.
           
     
      }
     
      if(cycle > 1 && radiostatus > 1)      // FYI - cycle incremented below
      {                                       
         
         delay_ms(10);      // Enought time for xmit of 5 bytes at 19200 BPS
         
         cycle = 2;     // for some weird reason we need this.
         
         while (cycle > 0) {
            sleep();
            delay_cycles(1);
            cycle--;
         }
         
         tickCount = 0;
             
         //strcpy(m_buffer,"POL");
         //bprintf(m_buffer);
         bputc(64);   // print @ just for this test example still use INT_RBE interrupt.
         
      }
     
      cycle++;                // For above WDT control and Main loop control whilst out of sleep.
     
      restart_wdt();                 
                                                                           
   } while (TRUE);
}
simat



Joined: 05 Feb 2008
Posts: 7

View user's profile Send private message Visit poster's website

PostPosted: Thu Aug 07, 2008 9:36 am     Reply with quote

Hi, heres some more code, compiler is 4.057

Code:

#include <16f88.h>
#include <string.h>

#fuses HS,WDT,NOPROTECT,PUT,NOBROWNOUT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=19200, xmit=PIN_B5, rcv=PIN_B2, parity=N, bits=8, stop=1, ERRORS, UART) // 16F88

#define DQ PIN_B0          // One Wire Bus pin assignment
#define PWR_EN PIN_B3      // One Wire PWR pin assignment
#define JN5139_EN PIN_B4   // JN5139 Module enable
#define JN5139_WAKE PIN_B1 // JN5139 Interrupt to wakeup from sleep

#define DEBUG PIN_A1       // Debug LED

#define uint8 char
#define uint16 long

// Global Variables
uint8   crc8 = 0;
uint16 crc16 = 0;
uint8  ROMbit[8];
uint8  lastDiscrep = 0;
short  doneFlag = 0;
uint8  FoundROM[9][8];
uint8  maxDevices = 10;
uint8  numROMs = 0;       
uint8  message[4];         
uint8  NTU[8];
uint8  tickCount = 0;     


#define r_BUFFER_SIZE 32      // Receive Buffer
BYTE r_buffer[r_BUFFER_SIZE];
BYTE r_next_in = 0;
BYTE r_next_out = 0;

#define t_BUFFER_SIZE 16      // Transmit Buffer
BYTE t_buffer[t_BUFFER_SIZE];
BYTE t_next_in = 0;
BYTE t_next_out = 0;

#define m_BUFFER_SIZE 64
uint8  m_buffer[m_BUFFER_SIZE];
uint8  m_bufferPtr = 0;


#int_TBE
void t_serial_isr(void)
{
   putc(t_buffer[t_next_out]);
   t_next_out = (t_next_out + 1) % t_BUFFER_SIZE;
   if (t_next_in == t_next_out)
   {
      disable_interrupts(int_tbe);
   }
}


void bputc(char c)
{
   short restart;
   int ni;
   restart = t_next_in == t_next_out;
   t_buffer[t_next_in] = c;
   ni = (t_next_in + 1) % t_BUFFER_SIZE;
   
   while (ni == t_next_out)
   {
      enable_interrupts(int_tbe);
   }
   
   t_next_in = ni;
   
   if (restart)
   {
      enable_interrupts(int_tbe);
   }
}


#int_RDA
void r_serial_isr(void)
{
   BYTE t;
     
   r_buffer[r_next_in] = getc();
   
   t=r_next_in;
   r_next_in = (r_next_in + 1) % r_BUFFER_SIZE;
   if(r_next_in == r_next_out)
      r_next_in = t;                     
}


#define bkbhit (r_next_in != r_next_out)


BYTE bgetc() {
   BYTE c;

   while(!bkbhit) ;   
   
   c = r_buffer[r_next_out];
   r_next_out = (r_next_out+1) % r_BUFFER_SIZE;
   return(c);
}


#int_TIMER1
void t_ticker(void)     // For timing events, tickCount increments every 100ms @ 8MHz CLK - depends on T1 setup.
{
   tickCount++;
}


void main()
{
   uint8 radiostatus = 0;
   uint8 cycle = 0;
   
   disable_interrupts(GLOBAL);
   disable_interrupts(INT_RDA);
   disable_interrupts(INT_TIMER1);
   
   setup_wdt(WDT_2304MS);     // Setup WDT for 2.3 seconds
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);   // Timer 1 for awake time monitoring   
       
   restart_wdt();
   
   enable_interrupts(GLOBAL);
   enable_interrupts(INT_RDA);
 
   set_timer1(0);
   tickCount = 0;
   enable_interrupts(INT_TIMER1);
   
   
   do    // Main Program Loop
   {
      output_toggle(DEBUG);   // Toggle LED to indicate life.
       
      if(bkbhit)     // We have data to decode
      {
            m_buffer[m_bufferPtr]=(bgetc());   
            m_bufferPtr++;                     
           
            // parse m_buffer section
            // do operations on 1-wire devices.
           
     
      }
     
      if(cycle > 1 && radiostatus > 1)      // FYI - cycle incremented below
      {                                       
         
         delay_ms(10);      // Enought time for xmit of 5 bytes at 19200 BPS
         
         cycle = 2;     // for some weird reason we need this.
         
         while (cycle > 0) {
            sleep();
            delay_cycles(1);
            cycle--;
         }
         
         tickCount = 0;
             
         //strcpy(m_buffer,"POL");
         //bprintf(m_buffer);
         bputc(64);   // print @ just for this test example still use INT_RBE interrupt.
         
      }
     
      cycle++;                // For above WDT control and Main loop control whilst out of sleep.
     
      restart_wdt();                 
                                                                           
   } while (TRUE);
}
Guest








PostPosted: Thu Aug 07, 2008 9:38 am     Reply with quote

Hi, heres some more code, compiler is 4.057

Code:

#include <16f88.h>
#include <string.h>

#fuses HS,WDT,NOPROTECT,PUT,NOBROWNOUT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=19200, xmit=PIN_B5, rcv=PIN_B2, parity=N, bits=8, stop=1, ERRORS, UART) // 16F88

#define DQ PIN_B0          // One Wire Bus pin assignment
#define PWR_EN PIN_B3      // One Wire PWR pin assignment
#define JN5139_EN PIN_B4   // JN5139 Module enable
#define JN5139_WAKE PIN_B1 // JN5139 Interrupt to wakeup from sleep

#define DEBUG PIN_A1       // Debug LED

#define uint8 char
#define uint16 long

// Global Variables
uint8   crc8 = 0;
uint16 crc16 = 0;
uint8  ROMbit[8];
uint8  lastDiscrep = 0;
short  doneFlag = 0;
uint8  FoundROM[9][8];
uint8  maxDevices = 10;
uint8  numROMs = 0;       
uint8  message[4];         
uint8  NTU[8];
uint8  tickCount = 0;     


#define r_BUFFER_SIZE 32      // Receive Buffer
BYTE r_buffer[r_BUFFER_SIZE];
BYTE r_next_in = 0;
BYTE r_next_out = 0;

#define t_BUFFER_SIZE 16      // Transmit Buffer
BYTE t_buffer[t_BUFFER_SIZE];
BYTE t_next_in = 0;
BYTE t_next_out = 0;

#define m_BUFFER_SIZE 64
uint8  m_buffer[m_BUFFER_SIZE];
uint8  m_bufferPtr = 0;


#int_TBE
void t_serial_isr(void)
{
   putc(t_buffer[t_next_out]);
   t_next_out = (t_next_out + 1) % t_BUFFER_SIZE;
   if (t_next_in == t_next_out)
   {
      disable_interrupts(int_tbe);
   }
}


void bputc(char c)
{
   short restart;
   int ni;
   restart = t_next_in == t_next_out;
   t_buffer[t_next_in] = c;
   ni = (t_next_in + 1) % t_BUFFER_SIZE;
   
   while (ni == t_next_out)
   {
      enable_interrupts(int_tbe);
   }
   
   t_next_in = ni;
   
   if (restart)
   {
      enable_interrupts(int_tbe);
   }
}


#int_RDA
void r_serial_isr(void)
{
   BYTE t;
     
   r_buffer[r_next_in] = getc();
   
   t=r_next_in;
   r_next_in = (r_next_in + 1) % r_BUFFER_SIZE;
   if(r_next_in == r_next_out)
      r_next_in = t;                     
}


#define bkbhit (r_next_in != r_next_out)


BYTE bgetc() {
   BYTE c;

   while(!bkbhit) ;   
   
   c = r_buffer[r_next_out];
   r_next_out = (r_next_out+1) % r_BUFFER_SIZE;
   return(c);
}


#int_TIMER1
void t_ticker(void)     // For timing events, tickCount increments every 100ms @ 8MHz CLK - depends on T1 setup.
{
   tickCount++;
}


void main()
{
   uint8 radiostatus = 0;
   uint8 cycle = 0;
   
   disable_interrupts(GLOBAL);
   disable_interrupts(INT_RDA);
   disable_interrupts(INT_TIMER1);
   
   setup_wdt(WDT_2304MS);     // Setup WDT for 2.3 seconds
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);   // Timer 1 for awake time monitoring   
       
   restart_wdt();
   
   enable_interrupts(GLOBAL);
   enable_interrupts(INT_RDA);
 
   set_timer1(0);
   tickCount = 0;
   enable_interrupts(INT_TIMER1);
   
   
   do    // Main Program Loop
   {
      output_toggle(DEBUG);   // Toggle LED to indicate life.
       
      if(bkbhit)     // We have data to decode
      {
            m_buffer[m_bufferPtr]=(bgetc());   
            m_bufferPtr++;                     
           
            // parse m_buffer section
            // do operations on 1-wire devices.
           
     
      }
     
      if(cycle > 1 && radiostatus > 1)      // FYI - cycle incremented below
      {                                       
         
         delay_ms(10);      // Enought time for xmit of 5 bytes at 19200 BPS
         
         cycle = 2;     // for some weird reason we need this.
         
         while (cycle > 0) {
            sleep();
            delay_cycles(1);
            cycle--;
         }
         
         tickCount = 0;
             
         //strcpy(m_buffer,"POL");
         //bprintf(m_buffer);
         bputc(64);   // print @ just for this test example still use INT_RBE interrupt.
         
      }
     
      cycle++;                // For above WDT control and Main loop control whilst out of sleep.
     
      restart_wdt();                 
                                                                           
   } while (TRUE);
}
Guest








PostPosted: Thu Aug 07, 2008 9:39 am     Reply with quote

Hi, heres some more code, compiler is 4.057

Code:

#include <16f88.h>
#include <string.h>

#fuses HS,WDT,NOPROTECT,PUT,NOBROWNOUT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=19200, xmit=PIN_B5, rcv=PIN_B2, parity=N, bits=8, stop=1, ERRORS, UART) // 16F88

#define DQ PIN_B0          // One Wire Bus pin assignment
#define PWR_EN PIN_B3      // One Wire PWR pin assignment
#define JN5139_EN PIN_B4   // JN5139 Module enable
#define JN5139_WAKE PIN_B1 // JN5139 Interrupt to wakeup from sleep

#define DEBUG PIN_A1       // Debug LED

#define uint8 char
#define uint16 long

// Global Variables
uint8   crc8 = 0;
uint16 crc16 = 0;
uint8  ROMbit[8];
uint8  lastDiscrep = 0;
short  doneFlag = 0;
uint8  FoundROM[9][8];
uint8  maxDevices = 10;
uint8  numROMs = 0;       
uint8  message[4];         
uint8  NTU[8];
uint8  tickCount = 0;     


#define r_BUFFER_SIZE 32      // Receive Buffer
BYTE r_buffer[r_BUFFER_SIZE];
BYTE r_next_in = 0;
BYTE r_next_out = 0;

#define t_BUFFER_SIZE 16      // Transmit Buffer
BYTE t_buffer[t_BUFFER_SIZE];
BYTE t_next_in = 0;
BYTE t_next_out = 0;

#define m_BUFFER_SIZE 64
uint8  m_buffer[m_BUFFER_SIZE];
uint8  m_bufferPtr = 0;


#int_TBE
void t_serial_isr(void)
{
   putc(t_buffer[t_next_out]);
   t_next_out = (t_next_out + 1) % t_BUFFER_SIZE;
   if (t_next_in == t_next_out)
   {
      disable_interrupts(int_tbe);
   }
}


void bputc(char c)
{
   short restart;
   int ni;
   restart = t_next_in == t_next_out;
   t_buffer[t_next_in] = c;
   ni = (t_next_in + 1) % t_BUFFER_SIZE;
   
   while (ni == t_next_out)
   {
      enable_interrupts(int_tbe);
   }
   
   t_next_in = ni;
   
   if (restart)
   {
      enable_interrupts(int_tbe);
   }
}


#int_RDA
void r_serial_isr(void)
{
   BYTE t;
     
   r_buffer[r_next_in] = getc();
   
   t=r_next_in;
   r_next_in = (r_next_in + 1) % r_BUFFER_SIZE;
   if(r_next_in == r_next_out)
      r_next_in = t;                     
}


#define bkbhit (r_next_in != r_next_out)


BYTE bgetc() {
   BYTE c;

   while(!bkbhit) ;   
   
   c = r_buffer[r_next_out];
   r_next_out = (r_next_out+1) % r_BUFFER_SIZE;
   return(c);
}


#int_TIMER1
void t_ticker(void)     // For timing events, tickCount increments every 100ms @ 8MHz CLK - depends on T1 setup.
{
   tickCount++;
}


void main()
{
   uint8 radiostatus = 0;
   uint8 cycle = 0;
   
   disable_interrupts(GLOBAL);
   disable_interrupts(INT_RDA);
   disable_interrupts(INT_TIMER1);
   
   setup_wdt(WDT_2304MS);     // Setup WDT for 2.3 seconds
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);   // Timer 1 for awake time monitoring   
       
   restart_wdt();
   
   enable_interrupts(GLOBAL);
   enable_interrupts(INT_RDA);
 
   set_timer1(0);
   tickCount = 0;
   enable_interrupts(INT_TIMER1);
   
   
   do    // Main Program Loop
   {
      output_toggle(DEBUG);   // Toggle LED to indicate life.
       
      if(bkbhit)     // We have data to decode
      {
            m_buffer[m_bufferPtr]=(bgetc());   
            m_bufferPtr++;                     
           
            // parse m_buffer section
            // do operations on 1-wire devices.
           
     
      }
     
      if(cycle > 1 && radiostatus > 1)      // FYI - cycle incremented below
      {                                       
         
         delay_ms(10);      // Enought time for xmit of 5 bytes at 19200 BPS
         
         cycle = 2;     // for some weird reason we need this.
         
         while (cycle > 0) {
            sleep();
            delay_cycles(1);
            cycle--;
         }
         
         tickCount = 0;
             
         //strcpy(m_buffer,"POL");
         //bprintf(m_buffer);
         bputc(64);   // print @ just for this test example still use INT_RBE interrupt.
         
      }
     
      cycle++;                // For above WDT control and Main loop control whilst out of sleep.
     
      restart_wdt();                 
                                                                           
   } while (TRUE);
}
simat



Joined: 05 Feb 2008
Posts: 7

View user's profile Send private message Visit poster's website

PostPosted: Thu Aug 07, 2008 9:45 am     Reply with quote

can a moderator please clean up this reply, the BBS has got a bit screwed and keeps telling me DEBUG MODE post failed.
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Thu Aug 07, 2008 9:57 am     Reply with quote

It will never enter
if(cycle > 1 && radiostatus > 1) // FYI - cycle incremented below

because radiostatus is initialised to 0 and never changes!

Unless I missed something Smile
simat



Joined: 05 Feb 2008
Posts: 7

View user's profile Send private message Visit poster's website

PostPosted: Thu Aug 07, 2008 9:59 am     Reply with quote

cycle increments at near the bottom of the code.

I'm using the variable twice, for count up to enter the sleep section and then counting down to zero to exit the sleep section.
simat



Joined: 05 Feb 2008
Posts: 7

View user's profile Send private message Visit poster's website

PostPosted: Thu Aug 07, 2008 10:02 am     Reply with quote

derr I'm not being to bright am I, radiostatus is set to 2 in MAIN but I've missed that out, sorry.
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Thu Aug 07, 2008 10:05 am     Reply with quote

Yes BUT radiostatus does not change!
You if statement relies on radiostatus being > 0!

That is not entirely true. You have a bug in your code.
You do not check for m_buffer overrun, When this happens

m_buffer[m_bufferPtr]=(bgetc());
m_bufferPtr++;

and m_bufferPtr > m_BUFFER_SIZE then memory corruption will occur.
As radiostatus is almost next inline for the definitions then this is most certainly being corrupted causing you code to enter the if statement, BUT as this is random cycle will now be a random value causing your code to remain in the while loop for sometime.
simat



Joined: 05 Feb 2008
Posts: 7

View user's profile Send private message Visit poster's website

PostPosted: Thu Aug 07, 2008 10:11 am     Reply with quote

Hi, but surely m_bufferPtr will just overflow back to zero after hit 255 ?

Also m_buffer is 64 bytes large and we don't put that much data in each time, after the code is parse (not shown) we reset m_bufferPtr to zero ready for next time.
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 1, 2  Next
Page 1 of 2

 
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