you don't need to write to other registers , that's the beauty of that library call.
read the compiled code's assembly listing.
and try it in your manual way too. It's not magic. Library is a tool waiting to be used.
All those above 3 points are misguiding. I'll first point it out and then come to Nandika's questions.
Sem.. stated Using libraries is a good thing. But for a simple thing such as ADC, I wonder what's the use of it. It is just a few registers you need to setup and when you do it, you know completely what is happening.
Calling a library routine and see assembly listing to see what the hell the library call has inserted is a crazy idea. This is not a practice in embedded world. I'm working in the field for last many years
If you have to write something in C and and have to see the assembly listing to see what's going on, that is simply time wasting. Take my word, MikroC is not professionally used. It is for hobbyists in the age of 10 to 15 in foreign countries.
HI-TECH C and MPLab C are the ones used for professional embedded development. And from those two, even Microchip recommends HI-TECH C because of its massive optimisation ability.
If you are a person who love electronics, I'm sure you will enjoy the HI-TECH C way. MikroC is good for hobbyists and PC programmers. "Ease of use and beauty of library calls" are something belong to PC world. Not the embedded.
However if you have to work on an embedded OS such as Android, RTOS, Micro Linux, Win CE, etc... you will have to use their API (set of libraries) to do certain things. Especially if you are moving to ARM processors, then it is advantageous to move to OS based embedded development.
But for these little micros where even a tiny OS can't be involved (no use even), it is good to write everything by your own. In few months time, you will have your own written functions to do everything. Then you can directly use these.
I'm coming to your questions now.
The answer Semi has given your questions... I don't know for what questions he has answered
Looks like he doesn't simply have hands on experience with uCs
bit 7-6: ADC Conversion Clock
ADC can be thought of a co-unit which can take input from several pins and convert to a digital value. You need to set a clock to this unit so that it can do the processing. ADC can convert a set of pins to their digital values in a single cycle of this clock you set. If you have put a 8MHz oscillator to your uC, then with first setting you can set the ADC to work in 4Mhz. Likewise you can workout the other values. The last one is for the internal RC oscillator built-in to the A/D module. This is not required when you already have a good oscillator fixed to the uC.
10 - Fosc/32 - if main clock is 8 Mhz, this is 250 KHz. I think this is very good.
Some uCs define a lower and upper limit for these. In my experience it is good to have the ADC clock in the range of 100KHz to 200KHz.
2nd question:
bit 7: ADFM
This is to set the justification (right aligned or left aligned) of the converted digital value on 2 registers.
Say the converted value is 11 0101.
If you set this left justified, you will get it as 00 0011 0101.
If you set this right justified, you will get it as 1010 1100 00.
Simple isn't it? When you learn things perfectly, it is very easy.
Here are two of my PIC codes to read ADC. It would be better if you could write your own codes.
Init code. You need to call this function one time at the main().
Code: Select all
// Setup A2D module
void init_a2d(void){
TRISA = 0x2F; //PORTA all pins are input
ADCON0 = 0x41; // select Fosc/2 and ADC on
ADCON1 = 0xC1; // select left justify result. A/D port configuration 0
ADON = 1; // turn on the A2D conversion module
}
Read the channel you want. So if you want to read channel 2, call it as read_a2d(2). Simple as that.
Code: Select all
/* Return an 10-bit bit result */
unsigned short read_a2d(unsigned char channel){
unsigned short tmpShort;
channel &= 0x07; // truncate channel to 3 bits
ADCON0 &= 0xC5; // clear current channel select
ADCON0 |= (channel<<3); // apply the new channel select
// Wait till the conversion is completed
GODONE = 1;
while (GODONE);
tmpShort = ADRESH;
DelayMs(10);
return ((tmpShort<<8)+ADRESL);
}
The good thing with this code is, you can see what exactly is happening inside rather than wasting on looking at assembly listing by calling 3rd party libraries