| ??? 02/05/03 18:36 Read: times |
#38338 - RE: sending data to Uart in C Responding to: ???'s previous message |
Here is a typical UART routine written in C. Look at this as an example....you will need to change it slightly to talk to your UART becasue you may be in a different 8051 derivative. This code was from a Cygnal C8051F126 part.
/* UART 0 queue buffers */
unsigned char xdata rx0_buffer[RX0_BUFSIZE];
unsigned char xdata tx0_buffer[TX0_BUFSIZE];
/* UART 0 queue control variables */
unsigned char data rx0_rd_idx; /* receive buffer read index */
unsigned char data rx0_wr_idx; /* receive buffer write index */
unsigned char data tx0_rd_idx; /* transmit buffer read index */
unsigned char data tx0_wr_idx; /* transmit buffer write index */
unsigned char data rx0_empty; /* receive buffer empty flag */
unsigned char data tx0_empty; /* transmit buffer empty flag */
/*
**
** routine to initialize UART 0 for operation in full duplex manner
** with interrupts and buffer queues. This routine will also
** setup the baud rate utilizing timer 3 as the baud rate clock for
** both the transmit and receive sides of the UART. The initialized
** baud rate is a fixed rate.
**
*/
void uart0_init(void)
{
IE=(IE & ~0x10); /* disable uart interrupt */
/* initialize timer 3 for the baud rate clocking */
SFRPAGE=TMR3_PAGE; /* activate the timer 3 page */
TR3=0; /* stop the counter */
RCAP3L=UART0_DIVISOR & 0xFF; /* set the reload divisor */
RCAP3H=UART0_DIVISOR >> 8;
TMR3CN=0x00; /* set the configuration */
TMR3CF=0x08; /* now the mode setup */
TR3=1; /* let timer 3 run */
/* initialize uart 0 control registers */
SFRPAGE=UART0_PAGE; /* setup the SFR page for uart 0 */
SCON0=0x70; /* enable uart mode */
SSTA0=0x0A; /* enable timer 3 for baud clock */
/* initialize the buffer control variables */
rx0_rd_idx=0;
rx0_wr_idx=0;
tx0_rd_idx=0;
rx0_wr_idx=0;
rx0_empty=1;
tx0_empty=1;
IE=(IE & ~0x10) | 0x10; /* enable uart 0 interrupt */
}
/*
**
** UART 0 Transmit Data send routine. This will transfer the caller
** specified data to the transmit 0 queue. The data is the entry
** argument. The return value from the function is setup such that
** a mon zero value indicates that the data was successfully placed
** into the transmit queue. Zero return if the data could not be
** put into the transmit queue.
**
*/
char uart0_send(unsigned char chr)
{
if(tx0_empty || tx0_wr_idx != tx0_rd_idx) /* if the queue was empty or has space */
{
EA=0; /* disable interrupts during pointer updates */
tx0_buffer[tx0_wr_idx]=chr; /* put send data into queue */
tx0_wr_idx++; /* bump & wrap pointer */
tx0_wr_idx%=TX0_BUFSIZE;
if(tx0_empty) /* if queue was empty */
{
tx0_empty=0; /* no longer empty */
SFRPAGE=UART0_PAGE; /* setup the SFR page for uart 0 */
TI0=1; /* start an interrupt */
}
EA=1; /* reenable interrupts */
return(1); /* show that data was accepted */
}
return(0); /* show data not accepted */
}
/*
**
** UART 0 Receive Data read routine. This will fetch a possible
** character from the receive 0 queue and place it at the users
** specified location. user specifies the location via a pointer.
** The return value from the function is a status such that a
** non zero value is returned if the data came from the receive
** queue and was placed at the users secified location. Zero
** return if there was no data to be returned.
**
*/
char uart0_read(unsigned char *chrptr)
{
SFRPAGE=UART0_PAGE; /* setup the SFR page for uart 0 */
if(rx0_empty == 0) /* there is data available */
{
EA=0; /* disable UART 0 to prevent interrupts */
*chrptr=rx0_buffer[rx0_rd_idx]; /* get the data from the queue */
rx0_rd_idx++;
rx0_rd_idx%=RX0_BUFSIZE; /* bump / wrap queue index */
if(rx0_rd_idx == rx0_wr_idx) /* if queue has just gone empty */
{
rx0_empty=1; /* show empty coindition */
}
EA=1;
return(1); /* show data was moved @ caller pointer */
}
return(0); /* show that no data wasavailable */
}
/*
**
** UART 0 Interrupt Service routine. This will handle both a
** possible receive character and a pending transmit character
**
*/
void uart0_isr(void) interrupt 4 using 1
{
unsigned char chr;
SFRPAGE=UART0_PAGE; /* setup the SFR page for uart 0 */
if(RI0) /* if receive data interrupt */
{
chr=SBUF0; /* get the received character */
RI0=0; /* reset the receive interrupt flag */
if(rx0_empty || rx0_wr_idx != rx0_rd_idx) /* if space in receive buffer */
{ /* keep the data,,else toss it */
rx0_buffer[rx0_wr_idx]=chr; /* put the data into the queue */
rx0_wr_idx++; /* bump / wrap the queue index */
rx0_wr_idx%=RX0_BUFSIZE;
rx0_empty=0; /* mark buffer as not empty */
}
}
if(TI0) /* if transmit empty interrupt */
{
// If there are chars. left to send, send one and check for empty,
// otherwise just ack. the transmit interrupt and leave.
TI0=0; /* mark the interrupt flag as off */
if(tx0_rd_idx != tx0_wr_idx) /* if there is data in the queue */
{
SBUF0=tx0_buffer[tx0_rd_idx]; /* fetch the data from the queue */
tx0_rd_idx++; /* bump / wrap the queue index */
tx0_rd_idx%=TX0_BUFSIZE;
}
else /* queue is empty */
{
tx0_empty=1; /* show transmit queue is empty */
}
}
}
Michael Karas |
| Topic | Author | Date |
| sending data to Uart in C | 01/01/70 00:00 | |
| RE: sending data to Uart in C | 01/01/70 00:00 | |
| RE: sending data to Uart in C | 01/01/70 00:00 | |
| RE: sending data to Uart in C | 01/01/70 00:00 | |
| RE: sending data to Uart in C | 01/01/70 00:00 | |
| RE: sending data to Uart in C | 01/01/70 00:00 | |
| Get a decent Compiler! | 01/01/70 00:00 | |
| RE: Get a decent Compiler! | 01/01/70 00:00 | |
| RE: sending data to Uart in C | 01/01/70 00:00 | |
| RE: sending data to Uart in C | 01/01/70 00:00 | |
RE: sending data to Uart in C | 01/01/70 00:00 |



