Hello everyone!
So what I am trying to do is receive a command using UART and when that command is received send a message out on a RS485 module I have. So basically right now I'm using a terminal program to communicate with the board via. USB. I have tested these things separately and they were working i.e. I was able to send a string to the program and then send it back to the terminal to be displayed. I was also able to send the RS485 message and observe it on a scope. However when I put these things together I'm not getting the results I expected.
I have initialized the UART as follows:
// Initialize UART
UBRR0H = (uint8_t)(UBBR_VALUE >> 8); // Setting baud rate
UBRR0L = (uint8_t)UBBR_VALUE; // Setting baud rate
UCSR0C |= (1<<UCSZ01)|(1<<UCSZ00); // Setup framing
UCSR0B |= (1<<RXEN0)|(1<<RXCIE0); // Enable receiver and RX interrupt
In order to implement the RS485 I used Nick Gammons RS485 library on his website there: http://www.gammon.com.au/forum/?id=11428
This is how the RS485 is initalized:
const byte ENABLE_TX = 13; // Write high to enable transmission
const byte ENABLE_RX = 10; // Write low to enable receiver
#define RXpin 0
#define TXpin 1
SoftwareSerial rs485(RXpin, TXpin); // Receive data on D0 and transmit data on D1
...
// Initialize RS485 pins
rs485.begin(9600);
pinMode(ENABLE_TX, OUTPUT);
pinMode(ENABLE_RX, OUTPUT);
pinMode(RXpin, INPUT);
pinMode(TXpin, OUTPUT);
And lastly this is how the data is being sent which is inside a pin change interrupt. The pin change interrupt gets set off in the RX_complete interrupt when it detects a specific string telling it to start the RS485 communication.
byte msg [] = "hello world";
//Enable transmitter and disable the receiver
digitalWrite(ENABLE_TX, HIGH);
digitalWrite(ENABLE_RX, LOW);
sendMsg(fWrite, msg, sizeof msg);
while (!(UCSR0A & (1 << UDRE0))) // Wait for empty transmit buffer
UCSR0A |= 1 << TXC0; // mark transmission not complete
while (!(UCSR0A & (1 << TXC0))); // Wait for the transmission to complete
digitalWrite(ENABLE_TX, LOW);
digitalWrite(ENABLE_RX, HIGH);
The problem I'm having is when I send a character to the board from the terminal program the terminal is receiving a bunch of garbage characters. I've tried disabling the transmitter bit in the UCSR0B register but that doesn't work. All I'm trying to do right now is send "hello world" out on RS485 when the appropriate command is sent to the arduino on a USB, from there I can develop my project further..
I think I have provided enough code...if you would like to see/know anything else don't hesitate to ask. I appreciate the insight and look forward to your responses!