PR11, Help for understanding C code!!

LED Blinking, Walking with Cytron Servo, Displaying RFID, Multi-function Mobile Robot......

PR11, Help for understanding C code!!

Postby bartsvns » Wed Feb 23, 2011 11:11 pm

Hi,

I have study the circuit of PR11 and C code fro PR11. Currently, I have 2 question for the sample C code that supply by Cytron for PR11;

1) Why we need to shift 8 bit. (refer to below c code, in red). Also what the meaning of c code below (green font).

void read_adc(void)
{
unsigned short i;
unsigned long result_temp=0;
for(i=2000;i>0;i-=1) //looping 2000 times for getting average value
{
ADGO = 1; //ADGO is the bit 2 of the ADCON0 register
while(ADGO==1); //ADC start, ADGO=0 after finish ADC progress
result=ADRESH;
result=result<<8; //shift to left for 8 bit
result=result|ADRESL; //10 bit result from ADC

result_temp+=result;
}
result = result_temp/2000; //getting average value

}

2) What are the function of below subroutine. I can't found in the C code, where we call this subroutine.

void increment(unsigned long data)
{
unsigned short j;
for(j=10;j>0;j--)
{ lcd_goto(32);
data=data+1;
dis_num(data);
delay(10000);
}

}

Thanks.
bartsvns
Greenhorn
 
Posts: 2
Joined: Wed Feb 23, 2011 10:54 pm

Re: PR11, Help for understanding C code!!

Postby ober » Thu Feb 24, 2011 9:03 am

bartsvns WROTE:void read_adc(void)
{
unsigned short i;
unsigned long result_temp=0;
for(i=2000;i>0;i-=1) //looping 2000 times for getting average value
{
ADGO = 1; //ADGO is the bit 2 of the ADCON0 register
while(ADGO==1); //ADC start, ADGO=0 after finish ADC progress
result=ADRESH;
result=result<<8; //shift to left for 8 bit
result=result|ADRESL; //10 bit result from ADC

result_temp+=result;
}
result = result_temp/2000; //getting average value
}


Shifting ADRESH 8 bit to left into a new 16-bit register, further bitwise OR with ADRESL is to make 10 bit ADC value. Because all register is in 8-bit value. If read ADRESH only, you will read higher 8 bit (MSB) only; while taking ADRESL is lower 2 bit(LSB). In order to combine these bit in 16-bit register, one of the method is by doing so.

void increment(unsigned long data)
{
unsigned short j;
for(j=10;j>0;j--)
{ lcd_goto(32);
data=data+1;
dis_num(data);
delay(10000);
}
}


Sometime the programmer create function/s thinking of using it in future, but in the end it is not. So you can ignore it if it is not called. Basically it will display character at LCD, slowly increase the ASCII value. Maybe this function is being call to test some code, to test the ASCII code.
Ober Choo
Cytron Technologies Sdn Bhd
www.cytron.com.my
User avatar
ober
Moderator
 
Posts: 1486
Joined: Wed Apr 15, 2009 1:03 pm

Re: PR11, Help for understanding C code!!

Postby bartsvns » Thu Feb 24, 2011 10:50 am

Dear Ober Choo,

Thanks for your explanation. I'm just a beginner with this Microcontroller. Just start few weeks ago.

Thanks.
bartsvns
Greenhorn
 
Posts: 2
Joined: Wed Feb 23, 2011 10:54 pm


Return to DIY Project Set

Who is online

Users browsing this forum: No registered users and 13 guests

cron