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] need help to explain a code

Status
Not open for further replies.

merzouk

Newbie level 4
Newbie level 4
Joined
Sep 20, 2014
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
Activity points
32
need help to explain a code


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
unsigned int measure_temp(void)
 {
   unsigned int read_value=0;
    ADCON0&=0;
    ADCON0|=1|(1<<7)|(1<<6);
    wait();
    ADCON0|=(1<<2);
    while(ADCON0&0x04);
    read_value=ADRESH;
    read_value<<=8;
    read_value+=ADRESL;
    read_value*=196;
    read_value>>=2;
    ADCON0&=0XFE;
    return read_value/100;
 
 }

but d't found a function: ADC_Read();
thank you very much:bang:
 
Last edited by a moderator:

Hi,
With high probability ADRESH and ADRESL are memory mapped register.You can check it by finding definition in header files.For more help you must specify your platform and some more information
Regards
 

thank you Miralipoor for your reply
for more information. it use mcu 16f877 for display temperature on led matrix. like following
Drawing1.png
 

Basically, that IS the ADC_Read() function.

An ADC_Read function would be made of codes to access and process the values in the ADRESL and ADRESH registers in the ADC module. The values in those registers are the results of the ADC module taking a reading.

ADRESH = A to D RESult High byte
ADRESL = A to D RESult Low Byte

However code like
Code:
ADCON0|=1|(1<<7)|(1<<6);
is pretty meaningless. It makes far more sense to use the bit names like this:
Code:
ADCON0 |= (ADON | (1 << ADCS0) | (1 << ADCS1));
which shows that the ADON bit is set and the internal RC clock is being selected.

Brian.
 

Basically, that IS the ADC_Read() function.

An ADC_Read function would be made of codes to access and process the values in the ADRESL and ADRESH registers in the ADC module. The values in those registers are the results of the ADC module taking a reading.

ADRESH = A to D RESult High byte
ADRESL = A to D RESult Low Byte

However code like
Code:
ADCON0|=1|(1<<7)|(1<<6);
is pretty meaningless. It makes far more sense to use the bit names like this:
Code:
ADCON0 |= (ADON | (1 << ADCS0) | (1 << ADCS1));
which shows that the ADON bit is set and the internal RC clock is being selected.

Brian.

thank you
what is the means of:read_value*=196;
can i use a function ADC_Read() to replace the preceding code.
 
Last edited by a moderator:

thank you
what is the means of:read_value*=196;
can i use a function ADC_Read() to replace the preceding code.
"read_value*=196;" can also be written as "read_value = read_value * 196;". In other words, it multiples whatever value is in the variable "read_value" by the constant 196.
I can't answer your 2nd question as it would all depend on what the ADC_Read() function was written to do. However, with all of the manipulations that the code does on the value extracted from the ADC, if the ADC_Read function is from some general purpose library, then I would really doubt it.
If the code works, then why bother trying to replace it?
Susan
 

Beware of a potential math fault. The largest number you can read from the ADC (= full scale reading) is 1023 so it you multiply it by 196 it equals 200508 which is larger than an unsigned int can hold. In most microcontroller languages an unsigned int can only hold values up to 65535. In any case, the very next instruction divides it by 2 and then you return the result divided by 100.

That math is extremely suspicious! Tell us what you are measuring and we can advise on the best way to convert it to a usable value.

Brian.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top