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

Can a 16bit (Nx8bit) variable be returned?
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
eager
Guest







Can a 16bit (Nx8bit) variable be returned?
PostPosted: Sun Jul 23, 2006 11:44 am     Reply with quote

Hello.

Is it possible to return a 16bit value?

unsigned int16 variable = 12345;
unsigned int16 some_other_variable;



void main(void){

some_other_variable = my_function();

}




unsigned int16 my_function(void){

return variable;

}


Thank you!
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Sun Jul 23, 2006 12:26 pm     Reply with quote

Of course!
eager
Guest







PostPosted: Sun Jul 23, 2006 2:26 pm     Reply with quote

I suspected this would be the answer. Now I have to figure out why it doesn't work in my case. Confused
eager
Guest







PostPosted: Sun Jul 23, 2006 2:48 pm     Reply with quote

eager wrote:
I suspected this would be the answer. Now I have to figure out why it doesn't work in my case. Confused



Forgot the code... Hope it will be posted in one piece.

unsigned int16 voltage(void){

unsigned char i;
unsigned int16 voltage_16bit;

measure16bit=0x0000;
voltage_sum = 0x0000;
voltage_ = 0x0000;
voltage_max = 0x0000;
voltage_min = 0xFFFF;

for(i=0;i<66;i++){

GO=1;
while(ADCON0&0x04){}

voltage_16bit = ADRESH;
voltage_16bit = voltage_16bit <<8> voltage_max) voltage_max = voltage_16bit;
if(voltage_16bit <voltage_min>> 6;
measure16bit = voltage_;
return voltage_;
}


As I return the value of voltage_ to a 16bit variable it gets only the first 8bits. Rolling Eyes
Guest








PostPosted: Sun Jul 23, 2006 5:18 pm     Reply with quote

You never define voltage_

At least you don't show it in your code.



Also, ending a variable with _ is kinda dumb. It only decreases readability and its not like your going to use 'voltage' and 'voltage_'. So leave off the _, otherwise you'll confuse yourself. I've used _ at the start of a variable before if its a number like _4XR, but I only do that when I can't think of a better variable name.
Ttelmah
Guest







PostPosted: Mon Jul 24, 2006 2:56 am     Reply with quote

Making a few comments.
First, use the 'code' button to post code. It retains the indentation, and makes it much easier to read.
Second, the line after reading ADRESH, is garbage.
Third, the next line is too...
Fourth, how would you ever expect it to get more than 8bits of data. You never read the ADRESL register.

Best Wishes
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Mon Jul 24, 2006 7:47 am     Reply with quote

Ttelmah wrote:
Making a few comments.
First, use the 'code' button to post code. It retains the indentation, and makes it much easier to read.
Second, the line after reading ADRESH, is garbage.
Third, the next line is too...
Fourth, how would you ever expect it to get more than 8bits of data. You never read the ADRESL register.

Best Wishes


I assumed this was screwed up because HTML is turned on. In any case, it looked like garbage so I didn't even look at it.
eager



Joined: 24 Jul 2006
Posts: 6

View user's profile Send private message

PostPosted: Mon Jul 24, 2006 12:35 pm     Reply with quote

Hello.

I am sorry for the scrambled code. I was not a registered user when I posted and I could not edit nor delete that messed up post. For some reason I was also unable to register. Got a "over quota" or something like that message.

For comments regarding the _ in the name of the variable: it works for me. And that's all that matters.

Here is the code that does not pass all the 16 bits. Let me add I doublechecked that I pass it to a 16bit variable.

Code:
unsigned int16 voltage(void){

//voltage_ is a global variable defined elsewhere, measure16bit
// is a global variable used elswhere

unsigned char i;
unsigned int16 voltage_16bit;

measure16bit=0x0000;
voltage_sum = 0x0000;
voltage_ = 0x0000;
voltage_max = 0x0000;
voltage_min = 0xFFFF;

for(i=0;i<66;i++){

GO=1;
while(ADCON0&0x04){}


voltage_16bit = ADRESH;
voltage_16bit = voltage_16bit << 8;
voltage_16bit = voltage_16bit | ADRESL;

voltage_sum=voltage_sum + voltage_16bit;

if(voltage_16bit > voltage_max) voltage_max = voltage_16bit;
if(voltage_16bit < voltage_min) voltage_min = voltage_16bit;

}

voltage_ = voltage_sum - voltage_max - voltage_min;
voltage_ = voltage_ >> 6;      //6bit right divide by 64
measure16bit = voltage_;      //10bit for the LCD

return voltage_;

}


Best regards and thank you.
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Mon Jul 24, 2006 12:42 pm     Reply with quote

Where are the definitions for voltage_ , voltage_sum, voltage_min, and voltage_max?
eager



Joined: 24 Jul 2006
Posts: 6

View user's profile Send private message

PostPosted: Mon Jul 24, 2006 12:56 pm     Reply with quote

Mark wrote:
Where are the definitions for voltage_ , voltage_sum, voltage_min, and voltage_max?


Also global, forgot to mention it, sorry. Embarassed
_________________
Best regards.
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Mon Jul 24, 2006 1:17 pm     Reply with quote

And again your forgot to show them! I want to see where you declared them
eager



Joined: 24 Jul 2006
Posts: 6

View user's profile Send private message

PostPosted: Mon Jul 24, 2006 1:32 pm     Reply with quote

It looks a little bit like this:

Code:


#include <the MCU.h>
#fuses

pin definitions etc.


//function defined

unsigned int16 voltage(void);

//variables defined

unsigned int16 voltage_sum;
unsigned int16 voltage_;
unsigned int16 voltage_max;
unsigned int16 voltage_min;

unsigned int16 measure16bit;
unsigned int16 some_other_variable;


void main(void){

      while(1){
             some_other_variable = voltage();
                  }

}




unsigned int16 voltage(void){

unsigned char i;
unsigned int16 voltage_16bit;

measure16bit=0x0000;
voltage_sum = 0x0000;
voltage_ = 0x0000;
voltage_max = 0x0000;
voltage_min = 0xFFFF;


for(i=0;i<66;i++){

GO=1;
while(ADCON0&0x04){}


voltage_16bit = ADRESH;
voltage_16bit = voltage_16bit << 8;
voltage_16bit = voltage_16bit | ADRESL;

voltage_sum=voltage_sum + voltage_16bit;

if(voltage_16bit > voltage_max) voltage_max = voltage_16bit;
if(voltage_16bit < voltage_min) voltage_min = voltage_16bit;

}

voltage_ = voltage_sum - voltage_max - voltage_min;
voltage_ = voltage_ >> 6;     
measure16bit = voltage_;
     
return voltage_;

}


Is that what were you looking for?
_________________
Best regards.
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Mon Jul 24, 2006 2:22 pm     Reply with quote

Code:

#include <16F876>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)

//function defined

unsigned int16 voltage(void);

//variables defined

unsigned int16 voltage_sum;
unsigned int16 voltage_;
unsigned int16 voltage_max;
unsigned int16 voltage_min;

unsigned int16 measure16bit;
unsigned int16 some_other_variable;

#byte ADRESH = 0x1E
#byte ADRESL = 0x9E
#byte ADCON0 = 0x1F
#bit GO = ADCON0.2

unsigned int16 voltage(void)
{

  unsigned char i;
  unsigned int16 voltage_16bit;

  measure16bit=0x0000;
  voltage_sum = 0x0000;
  voltage_ = 0x0000;
  voltage_max = 0x0000;
  voltage_min = 0xFFFF;


  for(i=0;i<66;i++)
  {

    GO=1;
    while(ADCON0&0x04)
    {
    }

    ADRESH = 0x02;
    ADRESL = 0x45;
    voltage_16bit = ADRESH;
    voltage_16bit = voltage_16bit <<8> voltage_max)
      voltage_max = voltage_16bit;
    if(voltage_16bit <voltage_min>> 6;     
  measure16bit = voltage_;
     
  return voltage_;

}

void main(void)
{
  setup_adc_ports (ALL_ANALOG);
  setup_adc (ADC_CLOCK_INTERNAL);
  set_adc_channel(0);
  while(1)
  {
    some_other_variable = voltage();
  }
}

This works just fine. Maybe its your ADC.
eager



Joined: 24 Jul 2006
Posts: 6

View user's profile Send private message

PostPosted: Mon Jul 24, 2006 2:36 pm     Reply with quote

Well, I think everyting is ok with the code too. The only difference between your code and mine is that I use the next bigger MCU and I defined the registers (#byte) in a *.h file which I include right after the MCU.h file. I really do not understand where did I f* up. The intresting thing is that the code fails to perform in MPLABSIM too. Strange. Confused
_________________
Best regards.
Ttelmah
Guest







PostPosted: Mon Jul 24, 2006 3:37 pm     Reply with quote

The obvious comment, is 'maths overflow'. You are doing a 0 to 65 _inclusive_ loop. 66 samples. Each sample can be up to 1023 in value. 1023 * 66 = 67518. Too large to fit in an int16. You can only sum a maximum of 64*10 bit values in a int16.
Second comment. You seem to be trying to 'simulate', by writing to the ADRES registers. This won't work. The registers behave as a _pair_, and should really be treated as read only. In fact you can write to them, but writing to the low register wll clear the high register. I suspect this is why you are seeing an 8bit value...

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 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