I am trying to interface the 8bit/8channel serial output ADC converter (ADC838) to AT89s8253 microcontroller but I couldnt get it to work with the mikroC for 8051 built-in SPI library.
Is the ADC838 compatible with the SPI library? if no is there an alternative way of communicating with the converter?
Here is the example code that I modified (It was originally for MC3204)
Code: Select all
// LCD module connections
sbit LCD_RS at P2_0_bit;
sbit LCD_EN at P2_1_bit;
sbit LCD_D4 at P2_2_bit;
sbit LCD_D5 at P2_3_bit;
sbit LCD_D6 at P2_4_bit;
sbit LCD_D7 at P2_5_bit;
// End LCD module connections
// ADC module connections
sbit ADC_CS at P1_4_bit;
// End ADC module connections
unsigned short measurement;
void Init() {
LCD_Init(); // init LCD
LCD_Cmd(_LCD_CLEAR);
LCD_Cmd(_LCD_CURSOR_OFF);
SPI1_Init(); // init SPI
ADC_CS = 1; // deselect ADC838
}
unsigned short getADC() { // returns 0..255
unsigned short result;
ADC_CS = 0; // select ADC838
SPI1_Write(0xC0); // send command byte (11000) Start bit-odd-Single-select0-select1
// meaning: select channel 0 as single ended input
result = SPI1_Read(0x00);
ADC_CS = 1; // deselect ADC838
return result;
}
void DisplayValue(unsigned short value) { // Writes Value to LCD
char i, lcdRow, lcdCol;
lcdRow=1;
lcdCol=4;
i = value /100 + 48;
LCD_Chr(lcdRow, lcdCol+1, i);
value %= 100;
i = value /10 + 48;
LCD_Chr(lcdRow, lcdCol+2, i);
value %= 10;
i = value + 48;
LCD_Chr(lcdRow, lcdCol+3, i); // Put number on display
}
void main(){
Init(); // initialize SPI and LCD
LCD_Out(1,1,"V0=");
while (1) {
measurement = getADC(); // get ADC result from Channel 0
DisplayValue(measurement); // display measurement
Delay_ms(100); // wait for a while
}
}
Also I followed a link in the net http://www.sixca.com/micro/mcs51/adc51/index.html explaining how to do that manually,
I modified the code and it didnt work either
Code: Select all
// LCD module connections
sbit LCD_RS at P2_0_bit;
sbit LCD_EN at P2_1_bit;
sbit LCD_D4 at P2_2_bit;
sbit LCD_D5 at P2_3_bit;
sbit LCD_D6 at P2_4_bit;
sbit LCD_D7 at P2_5_bit;
// End LCD module connections
// ADC module connections
sbit ADC_CS at P1_4_bit;
sbit ADC_DI at P1_5_bit;
sbit ADC_DO at P1_6_bit;
sbit ADC_CLK at P1_7_bit;
// End ADC module connections
unsigned short measurement;
void Init() {
LCD_Init(); // init LCD
LCD_Cmd(_LCD_CLEAR);
LCD_Cmd(_LCD_CURSOR_OFF);
ADC_CS = 1; // deselect ADC838
}
unsigned short getADC() { // returns 0..255
unsigned short result;
char i;
char cont_word;
ADC_CS = 0; // select ADC838
Delay_us(1);
ADC_CLK = 0;
Delay_us(2);
cont_word = 0b11000000;
// Send the control word to the ADC838 DI line
for(i=1; i<=5; i++) {
ADC_DI = (cont_word & 0x80);
cont_word=cont_word<<1;
ADC_CLK=1;
Delay_us(2);
ADC_CLK=0;
}
// Read converted data from the ADC838 DO line and save it to result
result = 0;
for(i=1; i<=8; ++i) {
ADC_CLK=1;
Delay_us(2);
ADC_CLK=0;
Delay_us(2);
result=result<<1;
result |= (ADC_DO & 0x01);
/*ADC_CLK=1;
Delay_us(2);
ADC_CLK=0;
Delay_us(2);*/
}
ADC_CS = 1; // deselect ADC838
return result;
}
void DisplayValue(unsigned short value) { // Writes Value to LCD
char i, lcdRow, lcdCol;
lcdRow=1;
lcdCol=4;
i = value /100 + 48;
LCD_Chr(lcdRow, lcdCol+1, i);
value %= 100;
i = value /10 + 48;
LCD_Chr(lcdRow, lcdCol, i);
value %= 10;
i = value + 48;
LCD_Chr(lcdRow, lcdCol+1, i); // Put number on display
}
void main(){
Init(); // initialize SPI and LCD
LCD_Out(1,1,"R=");
while (1) {
measurement = getADC(); // get ADC result from Channel 0
DisplayValue(measurement); // display measurement
Delay_ms(100); // wait for a while
}
}
thanks.