Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Warning message from a line of code

Status
Not open for further replies.

P.Copper

Member level 5
Member level 5
Joined
Mar 18, 2013
Messages
82
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Visit site
Activity points
1,807
Hi all,

i wrote this line of code

Code:
ADCHSbits.CH0SA0 = 0x0004; // Connect RB4/AN4 as CH0 input

and I'm getting this warning message

warning: large integer implicitly truncated to unsigned type

can anyone please explain to me what does that mean
 
Last edited by a moderator:

Code:
#include <p30F4013.h>
#include <libpic30.h>



//****** Device configuration register macros for building the hex file *******//
_FOSC(CSW_FSCM_OFF & FRC);
_FWDT(WDT_OFF);                         /* Watchdog timer disabled */
_FBORPOR(PBOR_OFF & MCLR_EN);   /*Brown-out reset disabled, MCLR reset enabled*/
_FGS(CODE_PROT_OFF);

int main(void)
{
    int ADC_voltage,ADC_current;
// conrigure adc port pins
    ADPCFG = 0xFFCF; // all PORTB = Digital; RB4 & RB5 = analog
    ADCON1 = 0x00E0; // SSRC bit = 111 implies internal

//***************************CONVERTING VOLTAGE and Current*****************************//

    ADCHSbits.CH0SA0 = 0x0004; // Connect RB4/AN4 as CH0 input
    ADCHSbits.CH0SA0 = 0x0005;  // connect RB5/AN5 as CH0 input
    ADCSSL = 0;
    ADCON3 = 0x0113;    // Sample time = 1Tad, Tad = 333.33 ns @ 30 MIPS
                        // which will give 1 / (15 * 333.33 ns) = 200 ksps
    ADCON2 = 0x6000;    // Select external VREF+ and VREF- pins
                        // Interrupts at the completion of conversion for each sample/convert sequence

    ADCON1bits.ADON = 1; // turn ADC ON
  while (1) // repeat continuously
    {
    ADC_voltage = 0;        // clear value
    ADC_current = 0;
    IFS0bits.ADIF = 0;   // clear ADC interrupt flag
    ADCON1bits.ASAM = 1; // auto start sampling
                         // for 31Tad then go to conversion

  while (!IFS0bits.ADIF); // conversion done?
    ADCON1bits.ASAM = 0;  // yes then stop sample/convert
    ADC_voltage =  ADCBUF0;  // yes then get ADC value
     ADC_current =  ADCBUF1;  // yes then get ADC current value
    }  
}
 

Code:
ADCHSbits.CH0SA0 = 0x0004; // Connect RB4/AN4 as CH0 input

Most likely the warning is due to an implicit conversion from integer to unsigned char of the 0x0004 value before beginning masked to set the bits of ADCHSbits.CH0SA0.

If you reduce the length of the Rvalue 0x0004 to 0x04, is the warning still issued?


BigDog

- - - Updated - - -

Also the following code snippet would most likely not yield the expected results:

Code:
    ADCHSbits.CH0SA0 = 0x0004; // Connect RB4/AN4 as CH0 input
    ADCHSbits.CH0SA0 = 0x0005;  // connect RB5/AN5 as CH0 input

The first assignment statement is irrelevant as the second assignment statement overwrites the first assignment statement.

You may also need to reduce the length of the value 0x0005 to 0x05 to alleviate the aforementioned warning.

BigDog
 

Also the following code snippet would most likely not yield the expected results:

Code:
    ADCHSbits.CH0SA0 = 0x0004; // Connect RB4/AN4 as CH0 input
    ADCHSbits.CH0SA0 = 0x0005;  // connect RB5/AN5 as CH0 input

The first assignment statement is irrelevant as the second assignment statement overwrites the first assignment statement.

You may also need to reduce the length of the value 0x0005 to 0x05 to alleviate the aforementioned warning.

BigDog

yes the warning still appear.
as for the 2 lines of code not yielding proper results, how then would i write it so not to overwrite the previous line?
 

After referencing the dsPIC30F4013 Datasheet, the dsPIC30F Family Reference Manual and device specific header file, I found the following.

If you examine the device specific header file, p30F4013.h, found in the directory tree, support/dsPIC30F/h within the compiler installation directory:

Code:
extern volatile unsigned int  ADCHS __attribute__((__sfr__));
__extension__ typedef struct tagADCHSBITS {
  union {
    struct {
      [COLOR="#FF0000"]unsigned CH0SA:4;[/COLOR]
      unsigned CH0NA:1;
      unsigned :3;
      unsigned CH0SB:4;
      unsigned CH0NB:1;
    };
    struct {
      [COLOR="#FF0000"]unsigned CH0SA0:1;[/COLOR]
      unsigned CH0SA1:1;
      unsigned CH0SA2:1;
      unsigned CH0SA3:1;
      unsigned :4;
      unsigned CH0SB0:1;
      unsigned CH0SB1:1;
      unsigned CH0SB2:1;
      unsigned CH0SB3:1;
    };
  };
} ADCHSBITS;
extern volatile ADCHSBITS ADCHSbits __attribute__((__sfr__));

You will notice the identifier ADCHSbits.CH0SA0 refers to a single bit within the ADCHS register, where as ADCHSbits.CH0SA refers to four bits within the ADCHS register.

Therefore you will need to change the following assignment statement,

From:
Code:
ADCHSbits.[COLOR="#FF0000"]CH0SA0[/COLOR] = 0x0004; // Connect RB4/AN4 as CH0 input

To:
Code:
ADCHSbits.[COLOR="#FF0000"]CH0SA[/COLOR] = 0x0004; // Connect RB4/AN4 as CH0 input

When attempting to set the ADC Bank A to a particular channel.

The first assignment statement attempts to store a 16 bits value within a single bit of the register, hence the implicit conversion warning.

Alternatively, you could refer to the register as a whole by:

Code:
ADCHS = 0x0004; // Connect RB4/AN4 as CH0 input

Although, such as reference is not as specific and could easily result in other programming errors.


As your code is currently written, manual selection of a channel, only one channel can be manually select at a time.

Therefore,

Either:
Code:
ADCHSbits.[COLOR="#FF0000"]CH0SA[/COLOR] = 0x0004; // Connect RB4/AN4 as CH0 input

Or:
Code:
ADCHSbits.[COLOR="#FF0000"]CH0SA[/COLOR] = 0x0005; // Connect RB4/AN4 as CH0 input

Not Both.


Note: Although it is possible to perform simultaneous/sequential ADCs of multiple channels, your current code would need to be changed considerably.

I strong suggest reviewing:

Reference: dsPIC30F Family Reference Manual, Section 17: 10-bit A/D Converter, Page: 17-1

Then post any additional questions you have in this thread.



BigDog
 
yes they stop, but commenting them out means that RB4 and RB5 are no longer analog inputs. are you suggesting that i make all PORTB pins analog?
 

This is wrong. You are trying to assign 16 bit value to a bit.


Code C - [expand]
1
2
ADCHSbits.CH0SA0 = 0x0004; // Connect RB4/AN4 as CH0 input
ADCHSbits.CH0SA0 = 0x0005;  // connect RB5/AN5 as CH0 input



Try something like


Code C - [expand]
1
2
ADCHSbits.CH0SA0 = 1; // Connect RB4/AN4 as CH0 input
ADCHSbits.CH0SA0 = 1;  // connect RB5/AN5 as CH0 input



You have to set it like this. If you want to set the bits CH0SA<3:0>


Code C - [expand]
1
2
3
4
ADCHSbits.CH0SA0 = 1;
ADCHSbits.CH0SA1 = 1;
ADCHSbits.CH0SA2 = 1;
ADCHSbits.CH0SA3 = 1;

 
Last edited:
jayanth is correct, although there is a more practical option than to setting the bits individually.

See my previous post as I was adding this information during your replies.

BigDog

- - - Updated - - -

yes they stop, but commenting them out means that RB4 and RB5 are no longer analog inputs. are you suggesting that i make all PORTB pins analog?

No, I was attempting to ascertain whether those code statements were indeed the source of the compiler warnings.

Examine the dsPIC30F Family Reference Manual, link provided in my previous reply, as to how to perform ADCs on multiple channels.

BigDog
 

Thanks for all the replies,

I've checked the datasheet for simultaneous/sequential conversion. I'm more interested in a 12 Bit ADC, my question how would I perform the simultaneous conversion in a 12 Bit ADC. I understand that in a 10 Bit simultaneous conversion controlled by the SIMSAM bit (ADCON1<3>) but for 12 Bit, SIMSAM doesn't exist.
 

but it's possible to do sequential with 12 Bit ADC?

The dsPIC30F Family Reference Manual refers to the available features within the entire dsPIC30F family of devices, many of which are not offered features of specific devices of that family.

Actually the dsPIC30F4013 offers only a single 12-bit ADC with one sample and hold and yes sequential ADC from multiple channels is possible.

Reference: dsPIC39F4013 Product Page
Analog Features:

12-bit 200 Ksps Analog-to-Digital Converter (A/D)
A/D Conversion available during Sleep and Idle
1 Sample/Hold
Multiple Conversion Sequencing Options

Reference: dsPIC30F3014/4013 Datasheet, Section: 12-BIT ANALOG-TO-DIGITAL CONVERTER (ADC) MODULE, Page: 131
12-BIT ANALOG-TO-DIGITAL CONVERTER (ADC) MODULE

The 12-bit Analog-to-Digital Converter (ADC) allows
conversion of an analog input signal to a 12-bit digital
number. This module is based on a Successive
Approximation Register (SAR) architecture and pro-
vides a maximum sampling rate of 200 ksps. The A/D
module has up to 16 analog inputs which are multi-
plexed into a sample and hold amplifier. The output of
the sample and hold is the input into the converter
which generates the result. The analog reference volt-
age is software selectable to either the device supply
voltage (AVdd/AVss) or the voltage level on the
(Vref+/Vref-) pin. The A/D converter has a unique
feature of being able to operate while the device is in
Sleep mode with RC oscillator selection.

The A/D module has six 16-bit registers:

• A/D Control Register 1 (ADCON1)
• A/D Control Register 2 (ADCON2)
• A/D Control Register 3 (ADCON3)
• A/D Input Select Register (ADCHS)
• A/D Port Configuration Register (ADPCFG)
• A/D Input Scan Selection Register (ADCSSL)

The ADCON1, ADCON2 and ADCON3 registers
control the operation of the A/D module. The ADCHS
register selects the input channels to be converted. The
ADPCFG register configures the port pins as analog
inputs or as digital I/O. The ADCSSL register selects
inputs for scanning.


Note:
The SSRC<2:0>, ASAM, SMPI<3:0>,
BUFM and ALTS bits, as well as the
ADCON3 and ADCSSL registers, must
not be written to while ADON = 1. This
would lead to indeterminate results.

To configure the dsPIC30F4013 for sequential ADC of multiple inputs, the ADPCFG, TRIS and ADCSSL registers will need to be properly configured for the channels required for ADC.

I'll see if I can find a few good examples of how to properly configure the ADC for scanning of multiple analog inputs.

BigDog

- - - Updated - - -

I had difficultly finding these, however I believe the following webinars were some of the best available covering dsPIC30F 12-bit ADC and its different configurations:

dsPIC30F 12-bit ADC Part 1

dsPIC30F 12-bit ADC Part 2

BigDog
 
I had difficultly finding these, however I believe the following webinars were some of the best available covering dsPIC30F 12-bit ADC and its different configurations:

dsPIC30F 12-bit ADC Part 1

dsPIC30F 12-bit ADC Part 2

BigDog
The links that you posted loads up to 100% and then stays there, nothing happens, I've tried different web browsers too but to no avail. If possible could you kindly open them and post them if their size is not a concern.

- - - Updated - - -

Will this now work? the only thing that I changed is that i set the ALTS bit of ADCON2 to alternate between MUXA and MUXB during conversions
see the code below

Code:
#include "p30F4013.h"
#include <libpic30.h>
#include "dsp.h"


//****** Device configuration register macros for building the hex file ******//
_FOSC(CSW_FSCM_OFF & FRC);
_FWDT(WDT_OFF);                         /* Watchdog timer disabled */
_FBORPOR(PBOR_OFF & MCLR_EN);   /*Brown-out reset disabled, MCLR reset enabled*/
_FGS(CODE_PROT_OFF);



 int ADC_voltage(), Volt_process();
 int ADC_current(), Current_process();
 unsigned int ADC_volt_value;
 unsigned int  ADC_curr_value;
 void PORT_INITIAL (void)
int main()
{
            //----configure Oscillator-----------//
    OSCCONbits.COSC0 =1;        //internal fast oscillator
    OSCCONbits.COSC1 =0;

}
//***************************PORT INITIALISATION******************************//
  void PORT_INITIAL (void)
  {
    LATB = 0xFFFF;
    ADPCFG = 0xFFCF;        // RB4 & RB5 pins are analog
    TRISB = 0xFFFF;         //all PORTB pins are inputs
  }
//***************************CONVERTING VOLTAGE AND CURRENT*******************************//
int ADC_voltage()
{
   ADCHSbits.CH0SA = 0x0004; // Connect RB4/AN4 as CH0 input for MUXA
   ADCHSbits.CHOSB = 0x0005; // Connect RB5/AN5 as CHO input for MUXB
    ADCSSL = 0;
    ADCON1 = 0x00E0;        // SSRC bit = 111 implies internal
    ADCON3 = 0x0113;        // Sample time = 1Tad, Tad = 333.33 ns @ 30 MIPS
                            // which will give 1 / (15 * 333.33 ns) = 200 ksps
    ADCON2 = 1;             // Select AVdd and AVss as reference voltage
                            // Interrupts at the completion of conversion for each sample/convert sequence
                            // alternates between MUXA and MUXB
    
    ADCON1bits.ADON = 1;    // turn ADC ON

while (1) // repeat continuously
  {
    ADC_volt_value = 0;        // clear value
    ADC_curr_value = 0;       // clear value
    IFS0bits.ADIF = 0;         // clear ADC interrupt flag
    ADCON1bits.ASAM = 1;       // auto start sampling for 31Tad then go to conversion

  while (!IFS0bits.ADIF);       // conversion done?
   {
    ADCON1bits.ASAM = 0;        // yes then stop sample/convert
    ADC_volt_value =  ADCBUF4;  // yes then get ADC voltage value from buffer4
    ADC_curr_value =  ADCBUF5;  // yes then get ADC current value from buffer5
   }
  }
}
 
Last edited:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top