CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

RS232 problems

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
sandy wilson



Joined: 25 Feb 2004
Posts: 28

View user's profile Send private message

RS232 problems
PostPosted: Mon Jun 13, 2011 4:52 am     Reply with quote

Hello

Can anybody help me with this.

I am using the following code for transmitting a packet on the RS232 line using UART1

In transmit function:-

disable_interrupts(INT_RDA);
output_high(RS485_DRIVER);

while(i < Lengths)
{
TxtmpHead = (USART_TxHead + 1) & USART_TX_BUFFER_MASK;

while(TxtmpHead == USART_TxTail);

USART_TxBuffer[TxtmpHead] = REPLY_MESSAGE[i];

USART_TxHead = TxtmpHead;

i++;

enable_interrupts(INT_TBE);
}

output_low(RS485_DRIVER);
enable_interrupts(INT_RDA);

In TX interrupt:-

#int_TBE
void USART_TX_interrupt(void)
{
byte tmpTail;

if(USART_TxHead != USART_TxTail)
{
tmpTail = (USART_TxTail + 1) & USART_TX_BUFFER_MASK;

USART_TxTail = tmpTail;

TXREG = USART_TxBuffer[tmpTail];
}
else
{
disable_interrupts(INT_TBE);
}
}

RS485_DRIVER is defined as pin c4.
TXREG is defined as #byte TXREG = 0xFAD
PIC type is 18F8722
Compiler version is 4.114


What is meant to happen is the the packet to be sent is transfered from array REPLY_MESSAGE into array USART_TxBuffer[tmpTail] and then the TBE interrupt is enabled. When there is still data in the USART_TxBuffer to be sent it is shipped out otherwise the interrup is disabled.

Whats happening is that the RS485_DRIVER pin goes high as reguired but as soon as the first bit of data is sent from the UART it is set to low again. It happens before the command output_low(RS485_DRIVER);
is reached and is occuring somewhere in the while loop or in the interrupt.

Has anybody got any idea why the pin c4 should be set to low when transmitting on the uart?


Thanks for any help

SandyW Question
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Mon Jun 13, 2011 9:36 am     Reply with quote

1) Learn to use the 'code' button. Makes reading your code 100* easier......

2) Of course it does. You are buffering the data to send, then dropping the RS485_DRIVER line in the main, as sooon as you have stored the data, while the TX interrupt is still sending. You need to drop this line _at the same point where you disable the TX interrupt - where the buffer is empty_. Or, don't use TXREG directly, use the CCS putc function, and add the enable line to the #USE RS232 setup.

3) Don't bother disabling the RS232 receive. You are presumably doing this because your RS485 hardware will receive the bytes being sent?. Turning off the interrupt will still leave a couple of characters in the hardware receive buffer. Have the RS232 receive routine, read characters and throw them away (rather than storing them), if RS485_DRIVER is low. Leave the interrupt enabled, and the characters transmitted will then automatically be read and the buffer will remain clear.

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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