integer overflow - DEMOJM board using MC9S08JM60

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

integer overflow - DEMOJM board using MC9S08JM60

1,171 Views
schalasa
Contributor I

Hello Everyone,

 

I am using the DEMOJM Board with the 8 bit MC9S08JM60 processor mounted on it. I am using codewarrior version 6 for developing the application. I am facing a problem while performing a simple subtraction over a set of raw values read by the ADC channel.

 

I had to declare the variables as signed int and I see that, if the difference between the raw values  is either 0 or -1, an overflow occurs and I get -256 or 255 randomly.

 

I am declaring the variables as signed integers. ( say signed int diff =0).

 

Is there any other way i need to declare the variables?

 

This is the first time I am using Codewarrior and I am a beginner in Firmware developement.

 

Thank you,

schalasa

Labels (1)
0 Kudos
Reply
5 Replies

638 Views
bigmac
Specialist III

Hello Schalasa, and welcome to the forum.

 

What ADC mode are you using, 8- , 10- or 12-bit mode?  I think that you will need to post your ADC code.

 

Regards,

Mac

 

0 Kudos
Reply

638 Views
schalasa
Contributor I

Hello Mac,

 

Thank you for the reply. I am using the 10 bit mode for the ADC. Find below, the code for initializing ADC.

 

 
 ADCCFG = 0XF8;       //  low power, clk%2, long sample time, 10 bit, bus clock
 
 ADCSC2 = 0X00;       // compare fn disabled

 ADCSC1 =0X21;        // interrupt disabled, continuous conversion, channel 1
 
 APCTL1 = 0X02;       // i/o pin 1 disabled

 

schalasa

0 Kudos
Reply

638 Views
bigmac
Specialist III

Hello Schalasa,

 

I actually meant for you to post the code where the ADC result is being read and then processed - where the error is occurring.  You should also include the definition for each variable.

 

Regards,

Mac

 

0 Kudos
Reply

638 Views
schalasa
Contributor I

Hello Mac,

 

I thought you wanted to take a look at the initialization :smileyhappy:

 

Here you go, a snippet of the ADC part. The two raw values are read by ADC channel 1 and ADC channel 2.

 

if (flag == 0 ) {
 
  adc1config ( ) ;               // fn to configure ADC channel 1

    while (ADCSC1_COCO== 0) ;
 
      {
         rssi_r= ADCR;
      }
      
 ADCSC1_COCO = 0;  
 flag = flag +1;

}
 
 if ( flag == 1) {
 
  adc2config ( );               // fn to configure ADC channel 2
 
  while ( ADCSC1_COCO == 0);

{
    rssi_l = ADCR;     
  }

ADCSC1_COCO = 0;
 
  flag = 0;
}

 

diff = rssi_l  - rssi_r;

 

diff1 = ((diff/2) - 5 );  // offset

 

 

rssi_r, rssi_l, diff, diff1 are declared as signed ints.

 

when the diff1 is varying between 0 and -1, I see the overflow on the variable diff1.

 

Thank you,

schalasa

 

 

 

0 Kudos
Reply

638 Views
bigmac
Specialist III

Hello Schalasa,

 

There are a number of operational issues that need to be sorted, and then see if your original problem still persists.

 

Since you are sequentially measuring two different channels, it is quite inappropriate to select continuous conversion mode.  The usual method would be to start the first conversion, and on its completion to start the second conversion.  Additionally, the APCTL1 setting appears to disable the GPIO pin for only one of the ADC channels.

 

Some of the code you have included is quite unnecessary, for example explicitly writing zero to the COCO bit.  The flag variable also does nothing.  You might need a flag to keep track of the current channel only if you were to use interrupts.  I arrived at the following simplified code snippet.

 

int rssi_r, rssi_l;int diff, diff1;ADCSC1 = 0x01;              // Start conversion ADC channel 1while (ADCSC1_COCO == 0) ;  // Wait for completionrssi_r = ADCR;ADCSC1 = 0x02;              // Start conversion ADC channel 2while (ADCSC1_COCO == 0);   // Wait for completionrssi_l = ADCR;     diff = rssi_l - rssi_r;diff1 = (diff/2) - 5;       // offset

Did you previously get the correct signed result within diff?  With the division by 2, I can see no reason why the diff1 calculation should overflow. 

 

The ADC initialisation might be modified to the following:

 

void ADC_init( void){   ADCCFG = 0xF8; // low power, long samp, 10 bit, bus clock/8   ADCSC2 = 0x00; // compare fn disabled   ADCSC1 = 0x1F; // ADC module disabled   APCTL1 = 0x06; // GPIO disabled channels 1 & 2}

 

 Regards,

Mac

 

0 Kudos
Reply