[SOLVED] Need a sample c- code for displaying register data on lcd (16x2) .

Status
Not open for further replies.

abv

Newbie level 5
Joined
Oct 9, 2012
Messages
9
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Activity points
1,335
I'm new to analog part of 16f877a .Also I didn't know how to print directly from register (ADRESL/H) because i always use pointer to display on my lcd.
I'm using MPLab with htc compiler.Please help me to get a sample C-code for displaying from register. :|
 

Code:
//configuration part
 TRISA=0x01; ADCON1=0xc0;        //port A.0 ADC input 

//adc read function
unsigned int read_adc()
{
unsigned int temp;
  ADCON0=0x81;    //read from ADC channel 1
  __delay_ms(1);    //setting time 
  ADGO=1;    while(ADGO);   // wait until ADC conversion complete 
  temp=(ADRESH<<8|ADRESL);  // combine 10 bit result 
return temp;
}

//display function

void lcd_number_display(unsigned int adc)
{
 unsigned char a[5]={0,0,0,0,0},cnt=0;   
if(adc==0) { cnt=1; }


    while(adc)
      {
		a[cnt++]=(adc%10);
		adc=(adc/10);
      }

     while(cnt--)
     {
       lcd_data((a[cnt]+'0'));    //your single character display  LCD function    [ +'0' means  add ascii value for display ]
     }
 
Reactions: abv

    abv

    Points: 2
    Helpful Answer Positive Rating


While your code example may yield the expected results when compiled using the MikroC compiler,
it typically will not yield the expected result when compiled with a compiler more compliant with the C standards.

The issue is due to the statement highlighted in red:

Code:
  temp=(ADRESH<<8|ADRESL);  // combine 10 bit result

The following part of the statement:

Code:
ADRESH<<8

Shifts the entire contents of the byte out, effectively clearing the byte.

A compiler compliant to the C standards would require you first cast ADRESH to a higher rank type, like an unsigned int, before shifting the contents eight place to the left.

Another, less used, but very powerful option would be to employ a type structure comprised of a union of a type unsigned int and two unsigned bytes.

Code:
typedef union{
unsigned int int_value;
struct{
    unsigned char hibyte;
    unsigned char lobyte;
}parts;
}int_convert;

Example Use:
Code:
     int_convert  adcvalue;

    adcvalue.parts.hibyte = ADRESH;
    adcvalue.parts.lobyte = ADRESL;

    unsigned int combinedvalues = adcvalue.int_value;


BigDog
 
Reactions: abv

    abv

    Points: 2
    Helpful Answer Positive Rating
Code:
this code working with hitech C

temp=(ADRESH<<8|ADRESL);  // combine 10 bit result 

/////////////////////////////////////////////
alternative code 

temp=ADRESH;
temp<<=8;
temp|=ADRESL;
 
Reactions: abv

    abv

    Points: 2
    Helpful Answer Positive Rating
thanks for the code..
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…