Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

TX_Buffer TO TRANSMIT 1 BYTE ONLY

Status
Not open for further replies.

embed_v

Junior Member level 1
Joined
Aug 10, 2010
Messages
18
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,432
TX_Buffer for UART serial

hii

this is about to send the sring to tx buffer but i want to send only one byte .

so any guidence to impliment this...

and the command is like this :(this is only 1 case for better understanding )

case CMD_READ_O_SENSORS: //...>0x10
Write_To_TX_Buffer((GPIO_PORTA_DATA_R & (OS1_IN | OS2_IN | OS3_IN)) >> 3);
err=ERR_SUCCESS;
break;
and the tx buffer code want to be modified in as follow:
void Write_To_TX_Buffer(unsigned char str)
{

while (str != '\0') {
/* If the TX ring buffer is full, wait until hardware FIFO has a spot
* available then put the next entry in the ring buffer into the FIFO.
* Update the head ptr. */
// if (((tx_buff_tail + 1) % TX_BUFF_LEN) == tx_buff_head)
// {
// while (UART0_FR_R & UART_FR_TXFF) {}
if (!(UART0_FR_R & UART_FR_TXFF) && (tx_buff_head == tx_buff_tail ) ) //Check tx fifo is empty
{
UART0_DR_R = tx_buff[tx_buff_head];
tx_buff_head = (tx_buff_head + 1) % TX_BUFF_LEN;
}

/* We know we have at least one spot available in the ring buffer,
* so lets place the next character into it, and update the tail ptr.
*/
tx_buff[tx_buff_tail] = str++;
tx_buff_tail = (tx_buff_tail + 1) % TX_BUFF_LEN;
}
/* Enable the UART0 TX Interrupt. */
UART0_IM_R |= UART_IM_TXIM;
 

horace1

Advanced Member level 5
Joined
Nov 18, 2008
Messages
2,123
Helped
596
Reputation
1,188
Reaction score
573
Trophy points
1,393
Location
Norwich, UK
Activity points
13,071
not sure what you are trying to do.
Unually if one is using an interrupt driven ring buffer to transmit characters using a UART it operates something along the lines of
1. if UART transmit buffer is empty load transmit buffer with next character
2. else put character in ring buffer

on UART transmit buffer empty interrupt the transmiter interrupt routine loads the next character from the ring buffer into the UART transmit buffer else (if ring buffer is empty) just return

for example using a dsPIC33FJ256GP710 (general ideas are applicable to any processor)
Code:
// UART buffer and pointers for transmitter data
#define U2txBufferSize 200
static unsigned char U2txBuffer[U2txBufferSize]={0};
static int U2txIn=0, U2txOut=0;			        // input and output index to rind buffer
static int U2txCounter=0;				// count of number of characters in ring buffer

// write characters 
void UART2PutChar( int ch )
{
    while(U2txCounter == U2txBufferSize) Nop();		// if buffer full wait
    if(!U2STAbits.UTXBF)				// if UART transmit buffer empty 
        { U2TXREG = ch; return; }			// transmit character and return		
    U2txBuffer[U2txIn++]= ch;				// character into ring buffer
    if(U2txIn == U2txBufferSize) U2txIn = 0;		// reset ring buffer index ?
    U2txCounter++;					// increment character counter
}

/*  UART2 transmit ISR */
void __attribute__((__interrupt__)) _U2TXInterrupt(void)
{
    IFS1bits.U2TXIF = 0;				// clear interrupt flag
    if(U2txCounter > 0)					// if character in ring buffer
 	{    						// transmit it
    	U2TXREG = U2txBuffer[U2txOut++];		// transmit character
    	if(U2txOut == U2txBufferSize)U2txOut=0;		// check for end of ring buffer
	U2txCounter--;					// decrement character counter
	}					
}
 

FvM

Super Moderator
Staff member
Joined
Jan 22, 2008
Messages
48,168
Helped
14,195
Reputation
28,649
Reaction score
12,886
Trophy points
1,393
Location
Bochum, Germany
Activity points
279,103
It looks like you have modified Write_To_TX_Buffer() from a working code to an erroneous construct. I guess, it has taken a string pointer originally, otherwise the "while (str != '\0')" and "tx_buff[tx_buff_tail] = str++;" iteration won't make sense.

Either you have unsigned char str, then the while loop and str++ can be simply omitted, or you have a pointer unsigned char *str, then it has to be accessed as such. You also can convert your single character to a one character string by placing a \0 delimiter behind it, and supply it's address to the original Write_To_TX_Buffer() function.

I suggest some exercises on character and string operations in C language.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top