|
|
View previous topic :: View next topic |
Author |
Message |
funmix
Joined: 27 Mar 2007 Posts: 33
|
Some weird character in rs232 |
Posted: Sat Apr 28, 2007 6:41 am |
|
|
i would like to transmit certain amount of character act like a communication protocol.
protocol format:||BUFFER[0] == Node_Address||BUFFER[1]...BUFFER[6]=DATA||BUFFER[7]==ETX||
Code: |
#int_TBE
void Serial_TX_isr(void)
{
if(tx_counter!=0)//if there are characters to be transmitted...
{
putc(TX_BUFFER[TX_rd_index]);
//send char out port
//test and wrap the pointer
if(++TX_rd_index > TX_SIZE)
{
TX_rd_index = 0;
}
tx_counter--;//keep track of the counter
if(tx_counter ==0)
{
disable_interrupts(INT_TBE);
}
}
}
//write a character to the serial transmit buffer
void putc_buffer(int8 cmd)
{
int restart = 0;
while (tx_counter>(TX_SIZE - 1));//wait ! BUFFER is getting full
if(tx_counter ==0)
{//if buffer empty, setup for interrupt
restart = 1;
}
TX_BUFFER[TX_wr_index++] = cmd;//jam the character in the buffer
if(TX_wr_index> TX_SIZE)
{//wrap the pointer
TX_wr_index = 0;
}
//keep track of buffered characters
tx_counter++;
if(restart ==1)
{
enable_interrupts(INT_TBE);
}
}
void main()
{
enable_interrupts(global);
enable_interrupts(INT_RDA);
stream_complete = FALSE;
data_valid = FALSE;
while(1){
putc_buffer(Node_Address);
putc_buffer("0");
putc_buffer("70");
putc_buffer("-1");
putc_buffer("-1");
putc_buffer(ETX);
delay_ms(1000);
delay_ms(1000);
delay_ms(1000);
putc_buffer(Node_Address);
putc_buffer("0");
putc_buffer("100");
putc_buffer("-1");
putc_buffer("-1");
putc_buffer(ETX);
delay_ms(1000);
delay_ms(1000);
delay_ms(1000);
}
}
|
my problem is when i transmit the value "100" , the computer display some weird character. it works fine if i am transmitting value less than '100'. What wrong with my code? I would like to send value more than 100.
Appreciate if someone point out my error. |
|
|
funmix
Joined: 27 Mar 2007 Posts: 33
|
|
Posted: Sat Apr 28, 2007 8:07 am |
|
|
output in terminal:
1070-1-110 100-1-11070-1-110 100-1-11070-1-110 100-1-110100-1-
11070-1-110 100-1-11070-1-110 100-1-11070-1-110 100-1-11070-1-1
10 100-1-11070-1-110 100-1-11070-1-110 100-1-11070-1-110 1
00-1-11070-1-110100-1-11070-1-110 100-1-11070-1-110 100-1-11070-1-1
10 100-1-11070-1-110 100-1-11070-1-110 100-1-11070-1-1
as you can see(brown colour protocol), there is a gap between 10 and 100
the first number ('1') representing the slave address
the second number ('0') representing zero mode
follow by data value (100). |
|
|
Ttelmah Guest
|
|
Posted: Sat Apr 28, 2007 10:20 am |
|
|
Note that the last entry on the first line, does not show the space.
Now there are some faults in the buffer handling code. For instance, if the buffer goes empty, between the test at the start of the putc routine, and 'restart' test at the end, the interrupt will not restart, until another character is sent.
As a comment, how big is your buffer?. As shown, it needs to be TX_SIZE+1 characters long, or it'll be overflowing. Your two index tests, test for the counters being greater than TX_SIZE, but a buffer 'TX_SIZE' characters long, can only accept addresses from 0 to TX_SIZE-1. So the test should use 'greater than or equal', not just 'greater than'.
It may be that there is nothing wrong with the 'main' code, but that the second problem in particular, is resulting in a memory value being destroyed resulting in the extra space being sent.
I'd re-code like this, and see what happens:
Code: |
#int_TBE
void Serial_TX_isr(void) {
//There is no point in testing if counter==0, the interrupts are disabled
//if it is.
putc(TX_BUFFER[TX_rd_index]);
//send char out port
//test and wrap the pointer
if(++TX_rd_index >= TX_SIZE) {
TX_rd_index = 0;
}
tx_counter--;//keep track of the counter
if (tx_counter ==0) {
disable_interrupts(INT_TBE);
}
}
//write a character to the serial transmit buffer
void putc_buffer(int8 cmd) {
while (tx_counter>(TX_SIZE - 1));//wait ! BUFFER is getting full
disable_interrupts(INT_TBE);
//It is safer not to add characters to the buffer, at the same time as
//the interrupt routine removes them.
TX_BUFFER[TX_wr_index++] = cmd;//jam the character in the buffer
if(TX_wr_index> TX_SIZE) {//wrap the pointer
TX_wr_index = 0;
}
//keep track of buffered characters
tx_counter++;
enable_interrupts(INT_TBE);
}
|
Best Wishes |
|
|
funmix
Joined: 27 Mar 2007 Posts: 33
|
|
Posted: Sat Apr 28, 2007 9:11 pm |
|
|
Code: |
//VARIABLES GLOBALS
//Receive Variables*************************************************************
static int8 char_rcved,data_valid,rx_in;
static int8 buffer_overflow,stream_complete;
//******************************************************************************
//Transmit Variables************************************************************
static int8 TX_rd_index=0;
static int8 TX_wr_index = 0;
static int8 TX_counter=0;
//******************************************************************************
//Buffer Size*******************************************************************
#define RX_SIZE 20
#define TX_SIZE 20
signed int16 RX_BUFFER[RX_SIZE];
signed int16 TX_BUFFER[TX_SIZE];
//interrupts once the mcu receive character
//protocol format:
//||BUFFER[0] == Node_Address||BUFFER[1]...BUFFER[6]==DATA||BUFFER[7]==ETX||
#int_RDA
void Serial_RX_isr()
{
int8 char_rcved;
int8 i;
char_rcved = getc();
if(char_rcved == Node_Address)
{
rx_in = 0;
RX_BUFFER[rx_in] = char_rcved;
data_valid = TRUE;
buffer_overflow = FALSE;
}
if(data_valid)
{
RX_BUFFER[rx_in] = char_rcved;
rx_in++;
if(rx_in>RX_SIZE)
{
data_valid = FALSE;
rx_in = RX_SIZE;
buffer_overflow = TRUE;
}
}
if(char_rcved == ETX)
{
data_valid = FALSE;
rx_in = 0;
RX_BUFFER[rx_in] = 0;
stream_complete = TRUE;
}
}
//interrupts once the mcu enable transmit character
//protocol format:
//||BUFFER[0] == Node_Address||BUFFER[1]...BUFFER[6]==DATA||BUFFER[7]==ETX||
#int_TBE
void Serial_TX_isr(void)
{
if(tx_counter!=0)//if there are characters to be transmitted...
{
putc(TX_BUFFER[TX_rd_index]);
//send char out port
//test and wrap the pointer
if(++TX_rd_index >= TX_SIZE)
{
TX_rd_index = 0;
}
tx_counter--;//keep track of the counter
if(tx_counter ==0)
{
disable_interrupts(INT_TBE);
}
}
}
//write a character to the serial transmit buffer
void putc_buffer(int8 cmd)
{
int restart = 0;
while (tx_counter>(TX_SIZE - 1));//wait ! BUFFER is getting full
if(tx_counter ==0)
{//if buffer empty, setup for interrupt
restart = 1;
}
TX_BUFFER[TX_wr_index++] = cmd;//jam the character in the buffer
if(TX_wr_index> TX_SIZE)
{//wrap the pointer
TX_wr_index = 0;
}
//keep track of buffered characters
tx_counter++;
if(restart ==1)
{
enable_interrupts(INT_TBE);
}
}
void main()
{
enable_interrupts(global);
enable_interrupts(INT_RDA);
stream_complete = FALSE;
data_valid = FALSE;
while(1){
putc_buffer(Node_Address);
putc_buffer("0");
putc_buffer("70");
putc_buffer("-1");
putc_buffer("-1");
putc_buffer("-1");
putc_buffer(ETX);
delay_ms(1000);
delay_ms(1000);
delay_ms(1000);
putc_buffer(Node_Address);
putc_buffer("0");
putc_buffer("100");
putc_buffer("-1");
putc_buffer("-1");
putc_buffer("-1");
putc_buffer(ETX);
delay_ms(1000);
delay_ms(1000);
delay_ms(1000);
}
}
|
The Result doesn't seems to be better. my buffer size is 20 for tx and rx.
Besides, can you help to check my receive interrupts. I fail to make two pic communicate by sending the protocol that i created myself. thanks for your help again |
|
|
funmix
Joined: 27 Mar 2007 Posts: 33
|
|
Posted: Sun Apr 29, 2007 12:25 am |
|
|
I solved the problem!! i able to transmit what i want now.
modified code:
Code: |
void putc_buffer(int8 cmd)
{
int restart = 0;
while (tx_counter>(TX_SIZE - 1));//wait ! BUFFER is getting full
if(tx_counter ==0)
{//if buffer empty, setup for interrupt
restart = 1;
}
TX_BUFFER[TX_wr_index++] = cmd;//jam the character in the buffer
if(TX_wr_index>= TX_SIZE)
{//wrap the pointer
TX_wr_index = 0;
}
//keep track of buffered characters
tx_counter++;
if(restart ==1)
{
enable_interrupts(INT_TBE);
}
}
|
By the way, Can you check my int_rda...
my slave not able to response when i sent |node address||0||100||-1||-1||ETX|
Thanks for your attention anyway. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|