caramoan tour package

caramoan tour package

Author Topic: how to utilize Analog-to-Digital Converter Module of a Microcontroller ?  (Read 10155 times)

Offline 'yus

  • Technical People
  • Nuclear Reactor
  • *****
  • Posts: 4251
  • Pogi/Ganda Points: 299
  • Gender: Male
  • hw -> fw -> sw
    • yus' projects
Re: how to utilize Analog-to-Digital Converter Module of a Microcontroller ?
« Reply #60 on: November 02, 2008, 09:46:37 PM »
sir bobot,
i think, aside pa dun sa dalawang register (ADRESL and ADRESH),
kelangan mo rin tingnan yung ADFM(: A/D Result Format Select bit)  ng ADCON1 (or ADCON2).
join  - Philippine Electronics and Robotics Enthusiasts Club - www.philrobotics.com

Philippine Electronics Forum

Re: how to utilize Analog-to-Digital Converter Module of a Microcontroller ?
« Reply #60 on: November 02, 2008, 09:46:37 PM »

Offline marcelino

  • Moderator
  • Solar Power Satellite
  • *****
  • Posts: 6016
  • Pogi/Ganda Points: 258
  • ...keep moving forward! - Robinson's
Re: how to utilize Analog-to-Digital Converter Module of a Microcontroller ?
« Reply #61 on: November 02, 2008, 10:02:38 PM »
ito ang code na nakita ko

Code: [Select]

void
adc_read_hummidity(void)
{
int humidity_h, humidity_l;
ADCON1=128;
HUMIDITY = 1;
adc_read(3); // read channel 3
humidity_h = ADRESH;
humidity_l = ADRESL;
HUMIDITY = 0;
ADCON1=7; // Set all the input/output to digital
}


meron naman ADRESH & ADRESL

kaya nga, pero sa main(), yung humidity_l nalang ang ginagamit nya...  dyan ako nalito.

di ba pagtinawag ko yan function na yan sa main, at kung idineclare mo pa din ang same na "humidity_l" sa main din. magiging equal yung laman di ba?

dyan ako naguluhan.

sir bobot,
i think, aside pa dun sa dalawang register (ADRESL and ADRESH),
kelangan mo rin tingnan yung ADFM(: A/D Result Format Select bit)  ng ADCON1 (or ADCON2).

ADFM? alin yan?
"Don't take life seriously. After all, no one has ever come out of it alive. -Bugs Bunny"

Philippine Electronics Forum

Re: how to utilize Analog-to-Digital Converter Module of a Microcontroller ?
« Reply #61 on: November 02, 2008, 10:02:38 PM »

Offline marcelino

  • Moderator
  • Solar Power Satellite
  • *****
  • Posts: 6016
  • Pogi/Ganda Points: 258
  • ...keep moving forward! - Robinson's
Re: how to utilize Analog-to-Digital Converter Module of a Microcontroller ?
« Reply #62 on: November 02, 2008, 10:05:00 PM »
sir bobot,
i think, aside pa dun sa dalawang register (ADRESL and ADRESH),
kelangan mo rin tingnan yung ADFM(: A/D Result Format Select bit)  ng ADCON1 (or ADCON2).
ah ok... yung format pala yan. (nasa datasheet.. nanaman!)
ano ba ang default sa CCS? at pano babaguhin?

sa tingin ko, left justified... as in, adresh:adresl. kasi di ko pa naman yan pinapakiaalaman, nagiging tama naman yung mga readings ko.
"Don't take life seriously. After all, no one has ever come out of it alive. -Bugs Bunny"

Philippine Electronics Forum

Re: how to utilize Analog-to-Digital Converter Module of a Microcontroller ?
« Reply #62 on: November 02, 2008, 10:05:00 PM »

Offline 'yus

  • Technical People
  • Nuclear Reactor
  • *****
  • Posts: 4251
  • Pogi/Ganda Points: 299
  • Gender: Male
  • hw -> fw -> sw
    • yus' projects
Re: how to utilize Analog-to-Digital Converter Module of a Microcontroller ?
« Reply #63 on: November 02, 2008, 10:10:33 PM »
^ yup, output format nga..
join  - Philippine Electronics and Robotics Enthusiasts Club - www.philrobotics.com

Philippine Electronics Forum

Re: how to utilize Analog-to-Digital Converter Module of a Microcontroller ?
« Reply #63 on: November 02, 2008, 10:10:33 PM »

Offline marcelino

  • Moderator
  • Solar Power Satellite
  • *****
  • Posts: 6016
  • Pogi/Ganda Points: 258
  • ...keep moving forward! - Robinson's
Re: how to utilize Analog-to-Digital Converter Module of a Microcontroller ?
« Reply #64 on: November 02, 2008, 10:15:11 PM »
^^hence, right justified pala... sa tingin ko, yan na din ang default sa CCS...
"Don't take life seriously. After all, no one has ever come out of it alive. -Bugs Bunny"

Philippine Electronics Forum

Re: how to utilize Analog-to-Digital Converter Module of a Microcontroller ?
« Reply #64 on: November 02, 2008, 10:15:11 PM »

Offline zer0w1ng

  • Technical People
  • Gas Turbine
  • *****
  • Posts: 2179
  • Pogi/Ganda Points: 305
  • Gender: Male
  • Enter any 11-digit prime number to continue...
    • The Cebuano Geek
Re: how to utilize Analog-to-Digital Converter Module of a Microcontroller ?
« Reply #65 on: November 03, 2008, 08:03:38 AM »
CCS named this as 16-bit or 10-bit results.
The Cebuano Geek

Offline marcelino

  • Moderator
  • Solar Power Satellite
  • *****
  • Posts: 6016
  • Pogi/Ganda Points: 258
  • ...keep moving forward! - Robinson's
Re: how to utilize Analog-to-Digital Converter Module of a Microcontroller ?
« Reply #66 on: November 03, 2008, 01:54:23 PM »
CCS named this as 16-bit or 10-bit results.

what do you mean sir? is that the declaration at the preprocessor - "adc=10"?

may I also ask for your guidance in this code...
http://www.piclist.com/images/boards/temp-hum/code/lcd.c
it come from this project: http://www.piclist.com/images/boards/temp-hum/index.htm

I am confused with how the humidity level is being gathered.

looking at the "adc_read_hummidity" function, I can see that "humidity_l" receives "ADRESL" and humidity_h" receives "ADRESH". However at the the main(), only the "humidity_l" is being used. I am greatly confused with that fact. I see that the 2-msb is not being used.
"Don't take life seriously. After all, no one has ever come out of it alive. -Bugs Bunny"

Offline zer0w1ng

  • Technical People
  • Gas Turbine
  • *****
  • Posts: 2179
  • Pogi/Ganda Points: 305
  • Gender: Male
  • Enter any 11-digit prime number to continue...
    • The Cebuano Geek
Re: how to utilize Analog-to-Digital Converter Module of a Microcontroller ?
« Reply #67 on: November 04, 2008, 07:49:27 AM »
what do you mean sir? is that the declaration at the preprocessor - "adc=10"?
Yes. It is adc=10 or adc=16.

looking at the "adc_read_hummidity" function, I can see that "humidity_l" receives "ADRESL" and humidity_h" receives "ADRESH". However at the the main(), only the "humidity_l" is being used. I am greatly confused with that fact. I see that the 2-msb is not being used.

The code is *NOT* well written.  The author uses adc_read_function() but does not use the passed value, instead at the end read again ADRESL.   I think it is written fast and for hobby purposes only.

My guess is that the reading of the humidity sensor does not go beyond 255 thus does not need to read the upper 2 bits of the result. 
The Cebuano Geek

Offline marcelino

  • Moderator
  • Solar Power Satellite
  • *****
  • Posts: 6016
  • Pogi/Ganda Points: 258
  • ...keep moving forward! - Robinson's
Re: how to utilize Analog-to-Digital Converter Module of a Microcontroller ?
« Reply #68 on: November 04, 2008, 09:42:31 AM »
I also believe that it is not well written... hehehe ;D
I know I don't know much about programming, but I can also say that... there are variables that has been declared but never been used. Maybe, it was just submitted because of the PCB contest. ;D

I have already constructed the circuit and read the measurements, it always exceeds 255... I have set my reference voltage to VDD(5V), and I typically see from 700 to 850 reading (this is a raw value of adc, 0-1024). I believe this reading because I bought a commercial hygrometer that show a typical value of 60%-75% of relative humidity... so I comparing the 700 - 850 adc value to a 60%-75% RH, I guess my readings are in range.

Thank you sir and God Bless. :)
"Don't take life seriously. After all, no one has ever come out of it alive. -Bugs Bunny"

Offline motion55

  • Technical People
  • Diesel Generator
  • *****
  • Posts: 1878
  • Pogi/Ganda Points: 243
  • Gender: Male
  • Been at this longer than you've been alive.
Re: how to utilize Analog-to-Digital Converter Module of a Microcontroller ?
« Reply #69 on: November 04, 2008, 11:48:19 AM »
We are in tropical country, relative humidity is always above 50%. Maybe that device was developed in a dryer climate.

Secondly, there is no preprocessor directive in the source code. Instead there is a line in code, ADCON1 = 128. This is equivalent to ADFM = 1 (right justified result).
"Set your mind free!"

Offline marcelino

  • Moderator
  • Solar Power Satellite
  • *****
  • Posts: 6016
  • Pogi/Ganda Points: 258
  • ...keep moving forward! - Robinson's
Re: how to utilize Analog-to-Digital Converter Module of a Microcontroller ?
« Reply #70 on: November 04, 2008, 01:32:01 PM »
We are in tropical country, relative humidity is always above 50%. Maybe that device was developed in a dryer climate.

Yes sir, so I guess i will have to use the whole ADRESH:ADRESL readings. I was really confused with that code...

Secondly, there is no preprocessor directive in the source code. Instead there is a line in code, ADCON1 = 128. This is equivalent to ADFM = 1 (right justified result).

And yes sir, i have already verified it with my PIC's datasheet...
it is placed in the ADCON2 of PIC18F2520.

Thank you sir... :)
"Don't take life seriously. After all, no one has ever come out of it alive. -Bugs Bunny"

Offline agg_asd

  • Size AAA Battery
  • ***
  • Posts: 58
  • Pogi/Ganda Points: 0
Re: how to utilize Analog-to-Digital Converter Module of a Microcontroller ?
« Reply #71 on: November 04, 2008, 02:03:05 PM »
ito example from mikroc, for 10 bit adc

Code: [Select]

unsigned int temp_res;

void main() {
  ADCON1 = 0x80;              // Configure analog inputs and Vref
  TRISA  = 0xFF;              // PORTA is input
  TRISB  = 0x3F;              // Pins RB7, RB6 are outputs
  TRISD  = 0;                 // PORTD is output

  do {
    temp_res = ADC_Read(2);   // Get results of AD conversion
    PORTD = temp_res;         // Send lower 8 bits to PORTD
    PORTB = temp_res >> 2;    // Send 2 most significant bits to RB7, RB6
  } while(1);
}


ADC_Read() is a built-in function.

If you dont want to use the built-in function, you can make your own function for reading the 10-bit adc

Code: [Select]

unsigned int read_ADC(void)
{
unsigned int adc_temp; //this variable will hold the 16-bit adc value

ADCON0 |= 0x04; //start conversion
while(ADCON0 & 0x04) //wait while conversion is no finished
;

adc_temp = ADRESL; //read the lower 8-bit
adc_temp += (ADRESH << 8); //read the higher 8-bit
//and form a 16-bit adc value

return(adc_temp); //return
}





Boss tnx po... kaya pala di gumana, kailangan ko palang burahin ang hex file sa DIR nya, di pala sya nagover write kapag nag cocompile.

patulong a din po sa pagset ng ADC clock medyo nalilito po ako dito.
sa software din po ba ito sineset? ang gamit ko po ay PIC16F877A at my 20MHz ako na Xtal at 22nF.

ito po ang error ko na nakukuha.
ADC conversion clock period(1e-07) is invalid for device frequency

my sleep feature din daw po itong IC na ito, paano po ito ginagamit?

Offline paranz

  • Moderator
  • Nuclear Reactor
  • *****
  • Posts: 4525
  • Pogi/Ganda Points: 177
  • Gender: Male
  • 1/4W resistor specialist
    • RapidSignal Electronics
Re: how to utilize Analog-to-Digital Converter Module of a Microcontroller ?
« Reply #72 on: November 04, 2008, 05:54:14 PM »

patulong a din po sa pagset ng ADC clock medyo nalilito po ako dito.
sa software din po ba ito sineset? ang gamit ko po ay PIC16F877A at my 20MHz ako na Xtal at 22nF.

ito po ang error ko na nakukuha.
ADC conversion clock period(1e-07) is invalid for device frequency

my sleep feature din daw po itong IC na ito, paano po ito ginagamit?

ADC conversion clock period(1e-07) is invalid for device frequency -> you are using Fosc/2 for the adc clock, which is the default value. This is very high if you are using 20Mhz xtal, since valid adc clock should not be less than 1.6 us (1e-07 s < 1.6 us).

For a 20mhz xtal oscillator, you must use adc clock = Fosc/32. This can be set in software (during initialization) by setting the ADCS1 and ADCS0 bits of the ADCON0 register.

here is an initialization code.

Code: [Select]

//ADCON0 constants
#define     RA0_ANA0    0x00
#define     FREQ_DIV_32     0x80

//ADCON1 constants
#define     RIGHT_JUSTIFIED     0x80
#define      AN0                 0x0E

void init_ADC(void)
{
  ADCON0 = FREQ_DIV_32 | RA0_ANA0;
  ADCON1 = RIGHT_JUSTIFIED | AN0;

TRISA |= 0x01;          //RA0 is input

  ADRESL = 0x00;          //ADC Result Low Register is 0
ADRESH = 0x00;          //ADC Result High Register is 0

INTCON |= 0xC0;         //enable peripheral interrupt
ADCON0 |= 0x01;         //turn on ADC
}

PIC16 Programming Tutorial using MPLAB and Hi-Tech C
www.rapidsignalph.com/tutorials/pic16-tutorials

Arduino & gizDuino Tutorials
www.rapidsignalph.com/tutorials/arduino-tutorials

Offline paranz

  • Moderator
  • Nuclear Reactor
  • *****
  • Posts: 4525
  • Pogi/Ganda Points: 177
  • Gender: Male
  • 1/4W resistor specialist
    • RapidSignal Electronics
Re: how to utilize Analog-to-Digital Converter Module of a Microcontroller ?
« Reply #73 on: November 04, 2008, 05:58:01 PM »
for more discussion on pic adc, refer to this manual from the manufacturer

http://ww1.microchip.com/downloads/en/DeviceDoc/31023a.pdf
PIC16 Programming Tutorial using MPLAB and Hi-Tech C
www.rapidsignalph.com/tutorials/pic16-tutorials

Arduino & gizDuino Tutorials
www.rapidsignalph.com/tutorials/arduino-tutorials

Offline agg_asd

  • Size AAA Battery
  • ***
  • Posts: 58
  • Pogi/Ganda Points: 0
Re: how to utilize Analog-to-Digital Converter Module of a Microcontroller ?
« Reply #74 on: November 05, 2008, 08:03:46 AM »
for more discussion on pic adc, refer to this manual from the manufacturer

http://ww1.microchip.com/downloads/en/DeviceDoc/31023a.pdf
thank you very much boss..... ;)

Offline agg_asd

  • Size AAA Battery
  • ***
  • Posts: 58
  • Pogi/Ganda Points: 0
Re: how to utilize Analog-to-Digital Converter Module of a Microcontroller ?
« Reply #75 on: November 05, 2008, 08:06:51 AM »
for more discussion on pic adc, refer to this manual from the manufacturer

http://ww1.microchip.com/downloads/en/DeviceDoc/31023a.pdf

Same codes din po ba ito sa MikroC?

Offline agg_asd

  • Size AAA Battery
  • ***
  • Posts: 58
  • Pogi/Ganda Points: 0
Re: how to utilize Analog-to-Digital Converter Module of a Microcontroller ?
« Reply #76 on: November 05, 2008, 08:08:33 AM »
Same codes din po ba ito sa MikroC?
Pasensya na po begginer lang po sa PIC. hehehehe

Offline agg_asd

  • Size AAA Battery
  • ***
  • Posts: 58
  • Pogi/Ganda Points: 0
Re: how to utilize Analog-to-Digital Converter Module of a Microcontroller ?
« Reply #77 on: November 05, 2008, 08:10:28 AM »


Initialization narin po ba ito para sa lahat ng ADC input?

Offline agg_asd

  • Size AAA Battery
  • ***
  • Posts: 58
  • Pogi/Ganda Points: 0
Re: how to utilize Analog-to-Digital Converter Module of a Microcontroller ?
« Reply #78 on: November 05, 2008, 08:51:42 AM »
ADC conversion clock period(1e-07) is invalid for device frequency -> you are using Fosc/2 for the adc clock, which is the default value. This is very high if you are using 20Mhz xtal, since valid adc clock should not be less than 1.6 us (1e-07 s < 1.6 us).

For a 20mhz xtal oscillator, you must use adc clock = Fosc/32. This can be set in software (during initialization) by setting the ADCS1 and ADCS0 bits of the ADCON0 register.

here is an initialization code.

Code: [Select]

//ADCON0 constants
#define     RA0_ANA0    0x00
#define     FREQ_DIV_32     0x80

//ADCON1 constants
#define     RIGHT_JUSTIFIED     0x80
#define      AN0                 0x0E

void init_ADC(void)
{
  ADCON0 = FREQ_DIV_32 | RA0_ANA0;
  ADCON1 = RIGHT_JUSTIFIED | AN0;

TRISA |= 0x01;          //RA0 is input

  ADRESL = 0x00;          //ADC Result Low Register is 0
ADRESH = 0x00;          //ADC Result High Register is 0

INTCON |= 0xC0;         //enable peripheral interrupt
ADCON0 |= 0x01;         //turn on ADC
}



Boss paranz
Same error parin po ako, ang ginawa ko po, copy paste ko ang code na binigay nyo.  MikroC po ang gamit ko, my suggestion po ba kayo kung alin ang magandang gamitin na compiler? How about yung Hi-tech C sa tutorial nyo?

ito po ang ginawa ko

unsigned int AN;

//ADCON0 constants
#define     RA0_ANA0    0x00
#define     FREQ_DIV_32     0x80

//ADCON1 constants
#define     RIGHT_JUSTIFIED     0x80
#define      AN0                 0x0E

void init_ADC(void)
{
    ADCON0 = FREQ_DIV_32 | RA0_ANA0;
    ADCON1 = RIGHT_JUSTIFIED | AN0;

   //TRISA |= 0x01;          //RA0 is input

    ADRESL = 0x00;          //ADC Result Low Register is 0
   ADRESH = 0x00;          //ADC Result High Register is 0

   INTCON |= 0xC0;         //enable peripheral interrupt
   ADCON0 |= 0x01;         //turn on ADC
}

void main(){
     TRISA = 0xFF;                 //init PortA as analog input
     init_ADC();
     //TRISE = 0xFF;
//     ADCON1 = 0;
     Usart_Init(57600);
     do{
        AN = Adc_Read(0);

        Usart_Write(AN);
        Delay_ms(500);
     }while(1);
}

Offline 'yus

  • Technical People
  • Nuclear Reactor
  • *****
  • Posts: 4251
  • Pogi/Ganda Points: 299
  • Gender: Male
  • hw -> fw -> sw
    • yus' projects
Re: how to utilize Analog-to-Digital Converter Module of a Microcontroller ?
« Reply #79 on: November 09, 2008, 11:15:16 AM »
pa'no ba ng tamang sequence (sa code) ng pag-read ng adc value? ???

meron kasing warning sa proteus simulation:
Quote
ADC conversion started before 'wait' time has expired following previous conversion or channel change.



ito ngayon ang gamit kong routine (Hi-Tech C):
Code: [Select]
/* return a 10-bit result */
unsigned int read_adc(unsigned char channel)
{
channel&=0x07; // truncate channel to 3 bits
ADCON0&=0xC5; // clear current channel select
ADCON0|=(channel<<3); // apply the new channel select
ADGO=1; // initiate conversion on the selected channel
while(ADGO)continue;
return(((ADRESH&0x03)<<8)+ADRESL); // return the 10-bit result
}
aling kaya ang mali? ???
join  - Philippine Electronics and Robotics Enthusiasts Club - www.philrobotics.com

Philippine Electronics Forum

Re: how to utilize Analog-to-Digital Converter Module of a Microcontroller ?
« Reply #79 on: November 09, 2008, 11:15:16 AM »

 

Privacy Policy

Contact Us: elabph@yahoo.com