Continue to Site

# controlling R/C servo C languange

Status
Not open for further replies.

#### Beng123

##### Newbie level 4

Im new in edaboard and now has a question to ask .
As i know when the analogue convert to digital,the result wil store in ADRESH AND ADRESL.
Can someone tell me the what the different about them?
if i jz wan send one bit,
on and off only.
how i get the result 1 or 0 from ADCON stored place?

#### masud58

##### Full Member level 2

u may see data sheet pic 16F877a.

'(*
' * Project name:
' (c) mikroElektronika, 2005 - 2006
' * Revision History:
' 20050312:
' - initial release.
' * Description:
' This code demonstrates using library function for ADC conversion
' (10 bits) result is displayed on portd and portb
' (two most significant bits are displayed on portb).
' * Test configuration:
' MCU: PIC16F877A
' Dev.Board: EasyPIC3
' Oscillator: HS, 8.000 MHz
' Ext. Modules: None
' SW: mikroBasic v5.0 or higher
' * NOTES:
' - If you are using EasyPIC3, use provided jumper on top right to connect potentiometer to RA2
' *)

dim temp_res as word

main:
ADCON1 = $80 ' configure analog inputs and Vref TRISA =$FF ' designate PORTA as input
TRISB = $3F ' designate RB7, RB6 pins as outputs TRISD =$0 ' designate PORTD as output
while true
'now you can use temp_res ...'
PORTD = temp_res ' send lower 8 bits to PORTD
PORTB = word(temp_res >> 2)
' send two most significant bits to PORTB, pins RB7,RB6
wend
end.

#### waseem

##### Full Member level 5

Hi!
Since the adc result is 10 bit wide therefore it is stored in two different registers ADRESH and ADRESL. The ADRESH contains the most significant bits where as the ADRESL contains the least significant bits.

I don't understand why u want to get just 1 or 0 from the ADC result registers.
If u want to make a decision based on the result from the ADC then compare the result with some fixed level to get the decision e.g

unsigned int resut;
#define Max_value 177;

result now contains total ADC CONVERSION result. U can compare it with anything to get a decision e.g

if(result>Max_value)
LED=1;
else
LED=0;

Hope it helps.
Regards.

Sorry the actual line is

i donot know how the smiley entered the equation?

Regards.

### Beng123

Points: 2

#### Beng123

##### Newbie level 4

waseem said:
Hi!
Since the adc result is 10 bit wide therefore it is stored in two different registers ADRESH and ADRESL. The ADRESH contains the most significant bits where as the ADRESL contains the least significant bits.

I don't understand why u want to get just 1 or 0 from the ADC result registers.
If u want to make a decision based on the result from the ADC then compare the result with some fixed level to get the decision e.g

unsigned int resut;
#define Max_value 177;

result now contains total ADC CONVERSION result. U can compare it with anything to get a decision e.g

if(result>Max_value)
LED=1;
else
LED=0;

Hope it helps.
Regards.

Sorry the actual line is

i donot know how the smiley entered the equation?

Regards.

after Analogue conversion are complete,
if i use

and the result=(result<<8|ADRESL); <<8is wat meaning?

#### Buriedcode

##### Full Member level 6

The values are automatically stored in the ADRESH and ADRESL after the conversion is complete. You don't have to move data IN to those registers, only read them.
You can also configure the ADC module to be left or right justified. With left, the ADC MSB is bit 7 of ADRESH, and the 2 LSB's are stored in bits 7,6 or ADRESL. Right justified is simply the other way around.

and 'result=(result<<8|ADRESL);' is C or basic I believe.

BuriedCode.

### Beng123

Points: 2

#### Beng123

##### Newbie level 4

Buriedcode said:
The values are automatically stored in the ADRESH and ADRESL after the conversion is complete. You don't have to move data IN to those registers, only read them.
You can also configure the ADC module to be left or right justified. With left, the ADC MSB is bit 7 of ADRESH, and the 2 LSB's are stored in bits 7,6 or ADRESL. Right justified is simply the other way around.

and 'result=(result<<8|ADRESL);' is C or basic I believe.

BuriedCode.

as u say 'result=(result<<8|ADRESL);' is C or basic then if i wan use in mplab what should i write?

#### waseem

##### Full Member level 5

Hi!

I thought that you were writing ur code in C language. If u r using assembly then the code will be totally different. Moreover as already posted that u donot have to store the result in ADRESH and ADRESL registers. The ADC automatically places the result in these registers after the conversion is complete. U have to read the result from these registers and place it in some variables before starting a new conversion.
If u are using assembly then i can give u a code that will not only save the result but also convert it to BCD. which can be easily displayed.

Regards.

### Beng123

Points: 2

#### Beng123

##### Newbie level 4

waseem said:
Hi!

I thought that you were writing ur code in C language. If u r using assembly then the code will be totally different. Moreover as already posted that u donot have to store the result in ADRESH and ADRESL registers. The ADC automatically places the result in these registers after the conversion is complete. U have to read the result from these registers and place it in some variables before starting a new conversion.
If u are using assembly then i can give u a code that will not only save the result but also convert it to BCD. which can be easily displayed.

Regards.

OK,then can u give me the code as reference.
tqtq...

#### Tahmid

Hi Beng123,
Since Adc of Pic micro is of 10 bit, the result is also 10 bits and hence to keep the result, Pic is having 2x 8 bit register. Lower 8 bits are stored in Adresl and higher 2 bits are stored in Adresh register. Other 6 bits of of Adresh register remains blank. If you require the 10 bit result, you have to use both the registers. If you want 8 bit result, you can set the ADFM bit in Adcon1 register to make it right justification and then use only Adresl register to get the lower 8 bit result. Or you can make the ADDM bit 0 and hence the result will be left justified and you then use only Adresh register to get the higher 8 bits and leaving lower 2 bits in Adresl register.
Please follow the ADC portion of the Datasheet of the PIC you intend to use and you will be clear. It is very easy to understand. Thanks.

Status
Not open for further replies.