Friday, June 28, 2013

Using Analog to Digital Converter of AT89C51CC01 Microcontroller

Using AT89STK-06 starter kit, I have written a few C code to read an analog to digital converter (ADC) input of AT89C51CC01 which is an 8051 microcontroller. It has 8 multiplexed ADC inputs with 10 bit resolution. As an example, ADC input pin 7 which is connected to a variable resister is read.
void SysADCInit()//Initialize AD7
ui16 SysADCRead()//Read AD7
 ui16 v;
 while((ADCON & 0x10)==0);
 v= (ADDH << 2)+(ADDL);
 return v;
Firstly, the ADC inputs must be enabled in the ADC configuration register (ADCF). Set bit 0 for ADC input 0, bit 1 for ADC input 1, and so on. Only the input 7 is used in this example and 0x80 is used to enable it. Since the ADC inputs are multiplexed, only one input can be read at a time. The input pin for ADC reading must be defined in ADCON before reading. In this example, best precision and ADC enable bits are set and input pin to be read is defined as 7. We use the ADC input 7 only and it is not necessary to redefine the input pin in ADCON register until we change the input.
In the procedure to read ADC input, the analog to digital conversion process is started by setting the start bit, bit 3. Then, a while loop is used to check the flag and it waits until the process is finished. The conversion takes 16 microseconds only and, consequently, the use of interrupt is not recommended. Becasue it cannot improve the performance in such a low end microcontroller while the conversion time is not so long. After that, the flag is cleared. The result of the ADC conversion is 10 bits. The least significant 8 bits are in ADDL and the most significant 2 bits are at position 7 and 6 of ADDH. These two values are combined into 16 integer, v.

No comments:

Post a Comment

Comments are moderated and don't be surprised if your comment does not appear promptly.