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.

[PIC] How define the lower and upper char(s) of an integer?

Status
Not open for further replies.

Samitha_ediriweera

Newbie level 2
Newbie level 2
Joined
Jul 2, 2010
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Colombo, Sri Lanka, Sri Lanka
Visit site
Activity points
1,295
How to define the lower and upper char(s) of an integer?

hi guys,

this is the code(s), i currently know,


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
union v{
    unsigned int vint;
    unsigned char vchar[2];
};
 
main()
{
    union v data;
 
    data.vchar[0]=ADRESL;
    data.vchar[1]=ADRESH;
    adc=data.vint;
}



Also,


Code C - [expand]
1
2
3
4
main()
{
    adc = (ADRESL | (ADRESH<<8));
}



isn't there a way i could define the adc lower & upper char(s) so that,

when ADRESL=0x34; and ADRESH=0x12; , then, adc reads 0x1234.

i.e. get the compiler to read ADRESL as the lower char & ADRESH as upper char of adc.

thanks in advance.

SAM
 
Last edited by a moderator:

You are probably working with an 8-bit CPU core, which due to its internal data bus is limited to this magnitude, all access to internal registers must be done byte per byte.
 

Re: How to define the lower and upper char(s) of an integer?

It is not clear what exactly you want.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
unsigned int adcResult = 0; //10 bit adc result fits in 16 bit type
unsigned char lowByte  = 0, highByte = 0; //holds adcResult split into bytes
 
 
 
void main() {
 
 
    while(1) {
    
            
        lowByte = ADRESL;   //ADC result right justified
        highByte = ADRESH;
 
        adcResult = (highByte << 8) | lowByte;
 
        //or
 
        adcResult = (ADRESH << 8) | ADRESL;
     
 
    }   
}

 

See if this works


Code C - [expand]
1
2
3
4
5
6
#define ADC_RESULT {(ADRESH << 8) | ADRESL}
 
 
unsigned int adcResult = 0;
 
adcResult = ADC_RSEULT;

 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top