View previous topic :: View next topic |
Author |
Message |
eager Guest
|
Can a 16bit (Nx8bit) variable be returned? |
Posted: Sun Jul 23, 2006 11:44 am |
|
|
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
|
|
Posted: Sun Jul 23, 2006 12:26 pm |
|
|
Of course! |
|
|
eager Guest
|
|
Posted: Sun Jul 23, 2006 2:26 pm |
|
|
I suspected this would be the answer. Now I have to figure out why it doesn't work in my case. |
|
|
eager Guest
|
|
Posted: Sun Jul 23, 2006 2:48 pm |
|
|
eager wrote: | I suspected this would be the answer. Now I have to figure out why it doesn't work in my case. |
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. |
|
|
Guest
|
|
Posted: Sun Jul 23, 2006 5:18 pm |
|
|
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
|
|
Posted: Mon Jul 24, 2006 2:56 am |
|
|
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
|
|
Posted: Mon Jul 24, 2006 7:47 am |
|
|
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
|
|
Posted: Mon Jul 24, 2006 12:35 pm |
|
|
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
|
|
Posted: Mon Jul 24, 2006 12:42 pm |
|
|
Where are the definitions for voltage_ , voltage_sum, voltage_min, and voltage_max? |
|
|
eager
Joined: 24 Jul 2006 Posts: 6
|
|
Posted: Mon Jul 24, 2006 12:56 pm |
|
|
Mark wrote: | Where are the definitions for voltage_ , voltage_sum, voltage_min, and voltage_max? |
Also global, forgot to mention it, sorry. _________________ Best regards. |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Mon Jul 24, 2006 1:17 pm |
|
|
And again your forgot to show them! I want to see where you declared them |
|
|
eager
Joined: 24 Jul 2006 Posts: 6
|
|
Posted: Mon Jul 24, 2006 1:32 pm |
|
|
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
|
|
Posted: Mon Jul 24, 2006 2:22 pm |
|
|
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
|
|
Posted: Mon Jul 24, 2006 2:36 pm |
|
|
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. _________________ Best regards. |
|
|
Ttelmah Guest
|
|
Posted: Mon Jul 24, 2006 3:37 pm |
|
|
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 |
|
|
|