Interfacing ACD838 with 8051 MCU?

General discussion on mikroC PRO for 8051.
Post Reply
Author
Message
riadh2002
Posts: 4
Joined: 08 Apr 2010 20:09

Interfacing ACD838 with 8051 MCU?

#1 Post by riadh2002 » 08 Apr 2010 20:28

Hello everybody,
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

  }
}
Any help would be appreaciated
thanks.

User avatar
tihomir.losic
mikroElektronika team
Posts: 2138
Joined: 02 Dec 2009 14:16
Location: Serbia
Contact:

Re: Interfacing ACD838 with 8051 MCU?

#2 Post by tihomir.losic » 09 Apr 2010 11:15

Hello,

I am sending you pin-out of our MCP3204 - 12-bit Analog-to-Digital (A/D) Converters with on-board sample and hold circuitry,
and ADC0838 - 8-Bit Serial I/O A/D Converters with Multiplexer Options.
- MCP3204
- ADC0838

I would like to help you, but our support team is very busy trying to get to every single support request on forum, as well as on our support tickets.
I recommend you to read your chip's datasheet and see how to manage your issue and try writing the code by yourself.
Then, if there are still some problems, you can explain it to us and paste the problematic block of code, and we'll be glad to help.

Best regards,

Losic Tihomir
mikroElektronika [Support team]

riadh2002
Posts: 4
Joined: 08 Apr 2010 20:09

Re: Interfacing ACD838 with 8051 MCU?

#3 Post by riadh2002 » 09 Apr 2010 12:42

Before I start solving my problem, i would like to ask if the SPI library in MikroC for 8051 is only for MCP3204 or it can be used with any device supporting serial communication? if not I will be wasting my time trying to figure out how to get it to work with ADC838.
Thanks in advance.

User avatar
tihomir.losic
mikroElektronika team
Posts: 2138
Joined: 02 Dec 2009 14:16
Location: Serbia
Contact:

Re: Interfacing ACD838 with 8051 MCU?

#4 Post by tihomir.losic » 12 Apr 2010 16:43

riadh2002 wrote:Before I start solving my problem, i would like to ask if the SPI library in MikroC for 8051 is only for MCP3204 or it can be used with any device supporting serial communication? if not I will be wasting my time trying to figure out how to get it to work with ADC838.
Thanks in advance.
Hello,

mikroC PRO for 8051 provides a library for comfortable with SPI work in Master mode.
Number of SPI modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.
In your case, ADC0838 has SPI (pins 14(DO) and 17(DI)) and you can use our library.

Best regards,

Losic Tihomir
mikroElektronika [Support team]

riadh2002
Posts: 4
Joined: 08 Apr 2010 20:09

Re: Interfacing ACD838 with 8051 MCU?

#5 Post by riadh2002 » 12 Apr 2010 19:36

thank you, I'll try to get it to work.

Post Reply

Return to “mikroC PRO for 8051 General”