• 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.

ADC interfacing with AT89S52 and Hyperterminal issue

bitnahian

New Member
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:
// 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.
 

Attachments

granddad

Active Member
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 ?
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
Your transmit code should look like this...

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...
 

bitnahian

New Member
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 :(
 

granddad

Active Member
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
 

bitnahian

New Member
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?
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
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?
They should support itoa()... Its a std library.... However seeing as they don't, then just use this one..

Courtesy of Hi-Tech...
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!!
 

EE World Online Articles

Loading

 
Top