1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

ADC interfacing with AT89S52 and Hyperterminal issue

Discussion in '8051/8951' started by bitnahian, Mar 17, 2015.

  1. bitnahian

    bitnahian New Member

    Joined:
    Mar 17, 2015
    Messages:
    5
    Likes:
    0
    Hello everyone. I'm very new here and completely new to microcontrollers.
    I'm trying to interface ADC0804 with Serial port (RS232) using 8051 micocontroller (AT89S52).
    I've attached the circuit diagram I have followed to achieve my means, and I have succeeded in the Analog to Digital Conversion because my LEDs lit up the way they were supposed to.
    This is the code I programmed into my AT89S52 microcontroller:

    Code (text):
    // Program to read the values of ADC and serially tranmitting it to PC.
    #include<reg51.h>
    sbit wr= P2^0; // Write pin. It is used to start the conversion.
    sbit rd= P2^1; // Read pin. It is used to extract the data from internal register to the output pins of ADC.
    sbit intr= P2^2; // Interrupt pin. This is used to indicate the end of conversion. It goes low when conversion is complete.

    void transmit() // Function for serial tranmission of data.
    {
        SBUF=P1;
        while(TI==0);
        TI=0;
    }

    void delay(unsigned int msec ) // The delay function provides delay in msec.
    {
        int i ,j ;
        for(i=0;i<msec;i++)
            for(j=0; j<1275; j++);
    }

    void adc() //Function to read the values from ADC and tranmit serially.
    {
        rd=1;
        wr=0;
        delay(2);
        wr=1;
        while(intr==1);
        rd=0;
        transmit();
        delay(2);
        intr=1;
    }

    void init() // Intialize timer 1 in mode 2 for serial transmission. The baud rate is set to 9600bps.
    {
        TMOD=0x20;
        TH1=0xFD;
        SCON=0x50;
        TR1=1;
    }

    void main()
    {
        P1=0xff; // Declare P1 as input port.
        init();
        while(1)
        {
            adc();
        }
    }
    However, when I tried to connect my system to my PC by using a USB to RS232 converter, I could not receive any data on the hyperterminal. I selected Bit rate as 9600bps, Data bits 8, Parity as none, Stop bit 1, Flow control none, but was unable to receive anything. I also updated the latest driver for windows 7 to connect the RS232 to my USB port...
    I don't understand where I'm going wrong. PLEASE HELP. THANK YOU IN ADVANCE.
     

    Attached Files:

    • full.png
      full.png
      File size:
      80.5 KB
      Views:
      451
  2. granddad

    granddad Active Member

    Joined:
    Jan 18, 2015
    Messages:
    750
    Likes:
    75
    Location:
    Worcestershire UK
    ONLINE
    There may be a voltage issue here , is this an FTDI chip .
     
  3. bitnahian

    bitnahian New Member

    Joined:
    Mar 17, 2015
    Messages:
    5
    Likes:
    0
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,151
    Likes:
    907
    Location:
    Rochdale UK
    ONLINE

    Well!! for a start.. What do you expect to see!!!!

    SBUF = P1... To see a value in Hyperterminal you need to convert the output of the ADC to ascii text!!

    What osc speed are you using????
     
  6. granddad

    granddad Active Member

    Joined:
    Jan 18, 2015
    Messages:
    750
    Likes:
    75
    Location:
    Worcestershire UK
    ONLINE
    My concern was the RS232 levels are -9 and +9 (ish). also you may need to supply hand shaking ... RTS as well as TX . what format is the data byte ASCII ?
     
  7. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,151
    Likes:
    907
    Location:
    Rochdale UK
    ONLINE
    Your transmit code should look like this...

    Code (c):

    void transmit() // Function for serial tranmission of data.
    {
      char ascii[5];
      char *ptr = &ascii[0];
      _itoa( P1,ptr,10);
      while(*ptr != 0)
      {
      TI=0;
      SBUF=*ptr++;
      while(TI==0);
       
      }
       
    }
     
    Your compiler may need the underscore on the itoa() function, or may not.. Check your compiler docs..
    You may also want to include a line feed / carriage return " \n\r" to make it readable...
     
  8. bitnahian

    bitnahian New Member

    Joined:
    Mar 17, 2015
    Messages:
    5
    Likes:
    0
    Ian Rogers and grandad..
    Thank you for your replies.
    Well, firstly, I was expecting to see the ASCII values in hyperterminal but I guess my void transmit command did not configure that. I'll try the code. As for the iota function, I'm not sure whether Keil supports the iota function. I'll need your help again here.
    I'm using an osc frequency of 11.0592 MHz. Now I'm wondering what baud rate I should set for this. I'm currently using 9600 baud rate.
    @grandad I don't understand what you mean by the format of the data byte ASCII :(
     
  9. granddad

    granddad Active Member

    Joined:
    Jan 18, 2015
    Messages:
    750
    Likes:
    75
    Location:
    Worcestershire UK
    ONLINE
    I think the ascii was covered by Ian's post , the usb / rs232 cable may require full RS232 implementation ? ie a null modem setup ( below) . Presumably Hyper terminal can 'see' the cable port ?
    ( Feels like I'm back in the 70's... :))

    BL-819_RS-232_Pinout.jpg nullmode.gif
     
  10. bitnahian

    bitnahian New Member

    Joined:
    Mar 17, 2015
    Messages:
    5
    Likes:
    0
    Ian Rogers Tried compiling the code with the void transmit code you provided... but unfortunately, Keil doesn't recognize or support the _iota function. What other alternatives do I have?
     
  11. bitnahian

    bitnahian New Member

    Joined:
    Mar 17, 2015
    Messages:
    5
    Likes:
    0
    Should I use the sprintf() instead? :/
     
  12. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,151
    Likes:
    907
    Location:
    Rochdale UK
    ONLINE
    They should support itoa()... Its a std library.... However seeing as they don't, then just use this one..

    Courtesy of Hi-Tech...
    Code (c):

    char * itoa(char * buf, int val, int base)
    {
       char *   cp = buf;

       if(val < 0) {
         *buf++ = '-';
         val = -val;
       }
       utoa(buf, val, base);
       return cp;
    }

    char * utoa(char * buf, unsigned val, int base)
    {
       unsigned   v;
       char     c;

       v = val;
       do {
         v /= base;
         buf++;
       } while(v != 0);
       *buf-- = 0;
       do {
         c = val % base;
         val /= base;
         if(c >= 10)
           c += 'A'-'0'-10;
         c += '0';
         *buf-- = c;
       } while(val != 0);
       return ++buf;
    }

     
    Easy peasy!!
     

Share This Page