Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
09/26/09 14:31
Read: times


 
#169164 - Re: Hello, World!
Responding to: ???'s previous message
Thanks Andy. I am quite OK to:

1. Initial the serial port with Timer 3 Generated BAUD rate(ADuC848):

e.g. 
void serial_init(void)
{						//Configure UART
  T3CON = 0x83;         			//configure timer 3...
  T3FD  = 0x12;          			//...for 9600 baud
  SCON  = 0x52;
}
 

2. Get a single character and also send a single character or string:

e.g
unsigned char c;
  printf ("Press a key.rn");
  c = getchar ();
       delay(1000);
  printf ("rn");
  	delay(1000);
  printf ("You pressed '%c'.rnrn", c);
 

But in my project, I need to read a string command from PC then reply with:

e.g. A. If PC send S1 + cr --> reply with MCU's status (e.g. 0,1,2 where 2 means MCU has finished the test and ADC result is ready to send back to PC) + cr

B. If PC send V1 + cr --> send ADC channel 1 measurement result (PC will ask for from V1 to V10) + cr

My problem is to use buffer I probably have to use Timer1, mode 1, 8 bit variable BAUD rate similar to "intsio" project (see below).

So I initialise the serial with timer 1. But I could not get it work. When I send a string as in step 2 above, I could not see any message on the terminal.

I don't know what crystal value should I put in to this formula for generating the BAUD rate: TH1 = (unsigned char) (256 - (Crystal / (16L * 12L * baudrate)));
so I put the Crystal value as 12.582912MHz (384 x 32.768KHz, page 90 "Clock Oscillator" ADUc848 datasheet).

Note that the ADuC848 use 32.768KHz external crystal.

/*------------------------------------------------------------------------------
SIO.C:  Serial Communication Routines.

Copyright 1995-2002 KEIL Software, Inc.
------------------------------------------------------------------------------*/

//#include <reg51.h>
//#include <ADuC848.h>    //To use this with an ADuC847 or ADuC848, simply change the
#include <string.h>
#include "sio.h"

/*------------------------------------------------------------------------------
------------------------------------------------------------------------------*/
#define TBUF_SIZE	256		/* DO NOT CHANGE */
#define RBUF_SIZE	256		/* DO NOT CHANGE */

static xdata unsigned char tbuf [TBUF_SIZE];
static xdata unsigned char rbuf [RBUF_SIZE];

static xdata unsigned char t_in = 0;
static xdata unsigned char t_out = 0;
static xdata unsigned char t_disabled = 0;

static xdata unsigned char r_in = 0;
static xdata unsigned char r_out = 0;

/*------------------------------------------------------------------------------
------------------------------------------------------------------------------*/
static void com_isr (void) interrupt 4 using 2
{
/*------------------------------------------------
Received data interrupt.
------------------------------------------------*/
if (RI != 0)
  {
  RI = 0;

  if ((r_in + 1) != r_out)
    rbuf [r_in++] = SBUF;
  }

/*------------------------------------------------
Transmitted data interrupt.
------------------------------------------------*/
if (TI != 0)
  {
  TI = 0;

  if (t_in != t_out)
    SBUF = tbuf [t_out++];
  else
    t_disabled = 1;
  }

}

/*------------------------------------------------------------------------------
------------------------------------------------------------------------------*/
void com_initialize (void)
{
/*------------------------------------------------
Setup TIMER1 to generate the proper baud rate.
------------------------------------------------*/
//com_baudrate (1200);
com_baudrate (9600);

/*------------------------------------------------
Clear com buffer indexes.
------------------------------------------------*/
EA = 0;                         /* Disable Interrupts */

t_in = 0;
t_out = 0;
t_disabled = 1;

r_in = 0;
r_out = 0;

/*------------------------------------------------
Setup serial port registers.
------------------------------------------------*/
SM0 = 0; SM1 = 1;		/* serial port MODE 1 */
SM2 = 0;
REN = 1;			/* enable serial receiver */

TI = 0;				/* clear transmit interrupt */
RI = 0;				/* clear receiver interrupt */

ES = 1;				/* enable serial interrupts */
PS = 0;				/* set serial interrupts to low priority */

EA = 1;                         /* Enable Interrupts */

}

/*------------------------------------------------------------------------------
------------------------------------------------------------------------------*/
void com_baudrate (
  unsigned baudrate)
{
EA = 0;                             /* Disable Interrupts */

/*------------------------------------------------
Clear transmit interrupt and buffer.
------------------------------------------------*/
TI = 0;				    /* clear transmit interrupt */
t_in = 0;			    /* empty transmit buffer */
t_out = 0;
t_disabled = 1;			    /* disable transmitter */

/*------------------------------------------------
Set timer 1 up as a baud rate generator.
------------------------------------------------*/
TR1 = 0;			    /* stop timer 1 */
ET1 = 0;			    /* disable timer 1 interrupt */

PCON |= 0x80;			    /* 0x80=SMOD: set serial baudrate doubler */

TMOD &= ~0xF0;			    /* clear timer 1 mode bits */
TMOD |= 0x20;			    /* put timer 1 into MODE 2 */

TH1 = (unsigned char) (256 - (Crystal / (16L * 12L * baudrate)));

TR1 = 1;			    /* start timer 1 */

EA = 1;                             /* Enable Interrupts */
}

/*------------------------------------------------------------------------------
------------------------------------------------------------------------------*/
char com_putchar (
  unsigned char c)
{
/*------------------------------------------------
If the buffer is full, return an error value.
------------------------------------------------*/
if ((TBUF_SIZE - com_tbuflen ()) <= 2)
  return (-1);

/*------------------------------------------------
Add the data to the transmit buffer.  If the
transmit interrupt is disabled, then enable it.
------------------------------------------------*/
EA = 0;                         /* Disable Interrupts */

tbuf [t_in++] = c;

if (t_disabled)			/* if transmitter is disabled */
  {
  t_disabled = 0;
  TI = 1;			/* enable it */
  }

EA = 1;                         /* Enable Interrupts */

return (0);
}

/*------------------------------------------------------------------------------
------------------------------------------------------------------------------*/
int com_getchar (void)
{
int c;

if (com_rbuflen () == 0)
  return (-1);

EA = 0;                         /* Disable Interrupts */
c = rbuf [r_out++];
EA = 1;                         /* Enable Interrupts */

return (c);
}

/*------------------------------------------------------------------------------
------------------------------------------------------------------------------*/
unsigned char com_rbuflen (void)
{
return (r_in - r_out);
}

/*------------------------------------------------------------------------------
------------------------------------------------------------------------------*/
unsigned char com_tbuflen (void)
{
return (t_in - t_out);
}

/*------------------------------------------------------------------------------
------------------------------------------------------------------------------*/

 

Please give any possible advice.

Thanks,
Ralf


List of 38 messages in thread
TopicAuthorDate
RS232: Sending data to PC from 8051 MCU - Please help!            01/01/70 00:00      
   What problems have you encountered?            01/01/70 00:00      
      Re: What problems have you encountered?            01/01/70 00:00      
         Q: How can I eat a whole whale?            01/01/70 00:00      
         There are examples and serial I/O tutorial information            01/01/70 00:00      
            Re: There are examples and serial I/O tutorial information            01/01/70 00:00      
               If you're using Keil            01/01/70 00:00      
               I have only seen some in assembly            01/01/70 00:00      
                  Re: RS232 discussion on Keil & 8052 website            01/01/70 00:00      
                     Hello, World!            01/01/70 00:00      
                        Re: Hello, World!            01/01/70 00:00      
                           why on earth?            01/01/70 00:00      
                           As Erik says            01/01/70 00:00      
                              Re: As Erik says            01/01/70 00:00      
                                 Just extend single-char routine            01/01/70 00:00      
                                 pseudo code            01/01/70 00:00      
                                    bufgfers            01/01/70 00:00      
                                    Re: circular bufgfers            01/01/70 00:00      
                                    Re: pseudo code            01/01/70 00:00      
                                       what is this            01/01/70 00:00      
                                          Used size            01/01/70 00:00      
                                          Can you give me any simple code, please?            01/01/70 00:00      
                                             first            01/01/70 00:00      
                                                RE: first            01/01/70 00:00      
                                                   why not directly to where you anyhow need to be eventually            01/01/70 00:00      
                                 INTSIO?            01/01/70 00:00      
   A note on using printf            01/01/70 00:00      
      How to set BAUD rate to 19200, timer 3 for ADuCxxx?            01/01/70 00:00      
      Is that OK to use printf to send data to PC like this?            01/01/70 00:00      
         Are you listening to anything that's been said here?            01/01/70 00:00      
            Re: Are you listening to anything that's been said here?            01/01/70 00:00      
               ADC read function just stop working            01/01/70 00:00      
               far better efficient and organise            01/01/70 00:00      
                  Thanks            01/01/70 00:00      
                     The trouble I have is I am running out of time            01/01/70 00:00      
                        This is what happens when you do somebody's work for them            01/01/70 00:00      
                           Re: This is what happens when you do somebody's work for the            01/01/70 00:00      
                              Then use the resources available to you!            01/01/70 00:00      

Back to Subject List