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

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
11/26/04 16:02
Read: times


 
#82025 - Sdcc serial code not working
Hello i wanted to use a serial buffer program in C with SDCC .I downloaded one and was trying to run it.But the problem is that the transmission part gets into some loop but how i don know can somebody please help.

#include <at89S8252.h>
// ---------------------------------------------------------------------------
// Defines.
// ---------------------------------------------------------------------------
#define FALSE 0
#define TRUE 1
#define LOW 0
#define HIGH 1

#define SER_RX_PORT P3_0 // define the pins for the serial port.
#define SER_TX_PORT P3_1

#define BUFFER_SIZE 16 // Transmit and Receive buffer size

// ---------------------------------------------------------------------------
// Function prototypes.
// ---------------------------------------------------------------------------
void init_ser( void );
void ser_write_byte( unsigned char Buf );
char ser_byte_avail( void );
unsigned char ser_read_byte( void );

// ---------------------------------------------------------------------------
// Global variable definitions.
// ---------------------------------------------------------------------------
/*xdata at 0x9020*/ char code acTestString[] = "Hello World...nrThis is a test string.nr";
xdata unsigned char * data pcStr; // Pointer to splash text.
// volatile lets you see the variable in sdcdb/ddd.
volatile unsigned char data tx_tail; // Transmit interrupt index.
volatile unsigned char data rx_head; // Receive interrupt index.
volatile unsigned char data rx_tail; // Receive read index.
volatile unsigned char data tx_head; // Transmit write index.
static bit is_txing; // True when transmitting a character.
xdata at 0x9000 unsigned char rx_buf[ BUFFER_SIZE ]; // Receive queue.
xdata at 0x9010 unsigned char tx_buf[ BUFFER_SIZE ]; // Transmit queue.


//------------------------------------------------------------------
// Main routine here
//------------------------------------------------------------------
void main( void )
{
init_ser(); // Initialize serial ops.
EA = 1; // Enable interrupts.

while( *pcStr ) // Display splash
ser_write_byte( *pcStr++ );

while( 1 ) // forever.
{
if( ser_byte_avail()) // if data coming in...
ser_write_byte(ser_read_byte()); // get char and send it back out.
}
}

// ---------------------------------------------------------------------------
// Function definitions.
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// Initialize serial operations.
// ---------------------------------------------------------------------------
void init_ser( void )
{
rx_head = 0; // Default head/tail pointers.
rx_tail = 0;
tx_tail = 0;
tx_head = 0;
is_txing = FALSE; // Not transmitting.

SER_RX_PORT = HIGH; // Set Txd & Rxd to high
SER_TX_PORT = HIGH;

SCON = 0x50; // Mode 1.
TMOD = (TMOD & 0x0F) | 0x20; // Timer 1, mode 2, 8-bit reload.
PCON |= 0x80; // Double baud rate.
TH1 = 0xFD; // 9600 baud * 2 = 19200 baud.
TR1 = 1; // Timer 1 run.
REN = TRUE; // Receive characters.
PS = TRUE; // Low priority.
ES = TRUE; // Enable serial interrupt.
}


// ---------------------------------------------------------------------------
// Serial interrupt handler.
// ---------------------------------------------------------------------------
void SerInt( void ) interrupt 4 using 2
{
if( RI ) // Receive character?
{
RI = 0; // clear receive flag
rx_buf[ rx_head++ ] = SBUF; // Get character from serial port and put into fifo.
if( rx_head >= BUFFER_SIZE) // Wrap pointer to beginning of buffer if at end.
rx_head = 0;
}

if( TI ) // Transmit character?
{
TI = 0; // Clear transmitter flag.
if( tx_head == tx_tail ) // Check to see if anymore characters to send?
is_txing = FALSE; // No, indicate to ser_write_byte to set TI next time.
else
{
is_txing = TRUE; // TI interrupt will occur at end of this character.
SBUF = tx_buf[ tx_tail++ ]; // Transmit character out serial port.
if( tx_tail >= BUFFER_SIZE) // Wrap pointer to beginning of buffer if at end.
tx_tail = 0;
}
}
}

// ---------------------------------------------------------------------------
// Transmits the character in buf out the serial port.
// ---------------------------------------------------------------------------
void ser_write_byte( unsigned char buf )
{
unsigned char next_head;

tx_buf[ tx_head ] = buf;
next_head = tx_head + 1;

if( next_head >= BUFFER_SIZE)
next_head = 0;

// Wait until we can stick the next character into the queue.
// prevent buffer over write
while( next_head == tx_tail );

tx_head = next_head;
if( is_txing == FALSE )
TI = TRUE;
}

// ---------------------------------------------------------------------------
// Checks to see if any characters are available to be read from the
// receive queue.
// ---------------------------------------------------------------------------
char ser_byte_avail( void )
{
return(rx_head != rx_tail);
/*
if( rx_head == rx_tail ) // return(rx_head != rx_tail);
return( FALSE );
else
return( TRUE );
*/
}

// ---------------------------------------------------------------------------
// Gets one character from the receive queue.
// if we get here we know we have a character available.
// ---------------------------------------------------------------------------
unsigned char ser_read_byte( void )
{
unsigned char buf;

buf = rx_buf[ rx_tail++ ];

if( rx_tail >= BUFFER_SIZE) // rx_tail %= BUFFER_SIZE;
rx_tail = 0;

return( buf );
}

Thanks and Regards
Gopal


List of 7 messages in thread
TopicAuthorDate
Sdcc serial code not working            01/01/70 00:00      
   OT: TRUE, FALSE again            01/01/70 00:00      
   But Andy            01/01/70 00:00      
      TRUE and FALSE            01/01/70 00:00      
         The code Worked            01/01/70 00:00      
   Reason for not working            01/01/70 00:00      
   Reentrancy Issue            01/01/70 00:00      

Back to Subject List