Serial Communication between 2 Arduino Uno using just Registers - c

I need to transfer a string between 2 Arduino Uno, using serial communication without functions(just manipulating registers, such as UDR0).
I am able to send a string using
#define BAUD 9600
#define MYUBRR F_CPU/16/BAUD-1
void USART_Init(unsigned int ubrr)
{
UBRR0H=(unsigned char)(ubrr>>8);
UBRR0L=(unsigned char)ubrr;
UCSR0B=(1<<RXEN0)|(1<<TXEN0);
UCSR0C=(1<<USBS0)|(3<<UCSZ00);
}
void USART_Transmit(unsigned char data)
{
while(!(UCSR0A&(1<<UDRE0)));
UDR0=data;
}
void SendString(char *StringPtr)
{
while(*StringPtr !=0x00)
{
USART_Transmit(*StringPtr);
StringPtr++;
}
}
void setup()
{
USART_Init(MYUBRR);
}
void loop()
{
SendString("123456");
delay(1000);
}
but I have no idea how to copy the content of the UDR0 register at the other end. The function should be something like:
unsigned char USART_Receive(void)
{
while(!(UCSR0A&(1<<RXC0)))
return UDR0;
}
But I don't know how to actually use it; I need to save all those chars from the serial to a string, then show the string on a LCD connected to the second Arduino Uno, but every time when I attempt to receive more that one char it's all working very bad(blank spaces, a lot of zeros, invalid characters).
I read that if I have an LCD connected to the second arduino, it's much better to not use the USART_Receive() function, and implement it as an interrupt, but I don't know how to do that either.
Do you have any idea how to transfer the string to the other Arduino?

Perhaps try to put all the chars in an big array and then send that array to your LCD. That way, the time between reading one char and the next one is minimized. (UART is asynchronous, so if you're too slow, you miss the requested data).

Related

Can't send data to ESP8266 via arduino

I have an ESP8266-01 and I want to send sensor data from Arduino to ESP8266 via serial communication but the data I receive is not correct, I tried changing baud rates but no luck, here is the code
Code for ESP8266:
#include <SoftwareSerial.h>
void setup() {
// put your setup code here, to run once:
Serial.begin(38400);
}
void loop() {
// put your main code here, to run repeatedly:
while(Serial.available()) {
Serial.println("yes");
Serial.println(Serial.read());
}
delay(5);
}
Code for Arduino
#include <SoftwareSerial.h>
SoftwareSerial esp(1, 0);
String str;
void setup(){
Serial.begin(38400);
esp.begin(38400);
delay(2000);
}
void loop() {
// put your main code here, to run repeatedly:
str = String("Hi there");
esp.println(str);
esp.println("hi there");
delay(1000);
}
Here is the serial monitor:
Serial monitor showing numbers instead of "hi there"
The wiring connections are correct.
if you are using simple wires for the connection you might need to reduce the baud rate at 9600 or lower.
For higher speed you will need coaxial cables to get a decent communication distance. Moreover I am sure that you have checked the electrical connection obvously.
On the programming side, Serial.read() returns the first byte of incoming serial data available (or -1 if no data is available). Data type: int. This is why you are getting a single number instead of your string.
I would suggest to use Serial.readString() instead.
Please see the proper documentation here:
https://www.arduino.cc/reference/en/language/functions/communication/serial/
All the best

Splitting an 8-bit Byte and assigning to a 7x1 array in Arduino

I'm using a Teensy 3.2 and Teensyduino to read in a UART signal consisting of 6 bytes of data.
For each byte, I'd like to assign each of the bits a position in a 1x7 Array.
For example, let's say I read in the 1st byte = 01110101. I would like an array that excludes the most significant bit(0) and looks like this
1110101
Below is what I've written to read the data and it works like a charm. It's the values currently stored in each element of byteArray that I would like to manipulate into a separate array as described.
I've searched high and low so if you could point me in the right direction I'd be forever grateful. I don't necessarily just want the answer, but I have no idea how to move forward at the moment. Is there a way to index specific bits in a byte that's similar to indexing positions in an Array?
#define PEESerial Serial2
byte byte1, byte2, byte3, byte4, byte5, byte6;
unsigned long period;
byte byteArray[6];
int count=0;
void setup() {
//Set the primary Serial (USB Serial)
Serial.begin(9600);
// Set the pin Serial - known value based on transmitted signal Baud Rate
PEESerial.begin(1200);
}
void loop() {
// read from port 9 (serial2 Rx) IF there
// is a signal to read.
if (Serial2.available()&& count<6) {
byteArray[count] = PEESerial.read();
Serial.println(byteArray[count],HEX);
Serial.println(byteArray[count],BIN);
count++;
}
else {
count=0;
}
}

Forwarding / passthrough UART from one to another port

I'm pretty new to programming in C, but getting used to registers and the way communication in C works. Since UART using the official Arduino read() / write() creates a high delay in passing commands through, I tried to translate this Arduino sketch into pure C. (and because I've time to play around, haha)
My ebike's controller and display are communicating using UART. I tried to read the commands and react to speed changes or brake signals, but first of all I need to get rid of this huge delay in updating the display.
I'm using an Robodyn Mega 2560 PRO (Embed), which has 4x hardware serial ports. In the first step I tried to read what's coming in and forward it to the other port. Shouldn't be to hard to implement in C, right?
void setup() {
Serial1.begin(1200); // BBSHD controller #RX18 TX19
Serial2.begin(1200); // Display DP-C18 #TX16 RX17
}
void loop() {
if (Serial2.available()) {
Serial1.write(Serial2.read());
}
if (Serial1.available()) {
Serial2.write(Serial1.read());
}
}
That's what I programmed in Atmel Studio 7 so far. I used the C example from ATmega640/1280/1281/2560/2561 - Complete Datasheet (Search for USART_Receive and USART_Transmit) and this guide Simple Serial Communications With AVR libc
Currently I can compile and flash it the 2560, but the communication is not being forwarded. I don't know what the default settings are, that Arduino with Serial uses. Which mode, number of stop bits, .. Is there anything obvious I'm missing?
#define F_CPU 16000000UL
#define BAUD 1200
#include <avr/io.h>
#include <stdio.h>
#include <util/setbaud.h>
void uart_init(void) {
/* Serial1 controller */
UBRR1H = UBRRH_VALUE;
UBRR1L = UBRRL_VALUE;
/* Serial2 display */
UBRR2H = UBRRH_VALUE;
UBRR2L = UBRRL_VALUE;
/* 8-bit data, 1stop bit*/
UCSR1C = (1<<UCSZ11) | (1<<UCSZ10);
UCSR2C = (1<<UCSZ21) | (1<<UCSZ20);
/* Enable RX and TX */
UCSR1B = (1<<RXEN1) | (1<<TXEN1);
UCSR2B = (1<<RXEN2) | (1<<TXEN2);
}
int main(void) {
uart_init();
while(1) {
if (!(UCSR1A & (1<<RXC1))) { /* Only if data is received */
while (!(UCSR1A & (1<<UDRE1))); /* Wait for empty transmit buffer */
UDR2 = UDR1; /* Put data into buffer, sends the data */
}
if (!(UCSR2A & (1<<RXC2))) { /* Only if data is received */
while (!(UCSR2A & (1<<UDRE2))); /* Wait for empty transmit buffer */
UDR1 = UDR2; /* Put data into buffer, sends the data */
}
}
}

Problems with code to send request and receive response over UART on Atmel SAM L21 Xplained Pro

I'm currently developing a system which involves sending a request string to a sensor device connected via UART to an Atmel SAML21 Xplained Pro board. I'm testing with an Arduino board as the "sensor device", but eventually, it'll be for a Rotronic HC-2 sensor.
The process goes something like this:
MCU sends string { 99RDD} over UART to sensor
-> delay of up to 500ms
-> Response string of 99 bytes sent back via UART
-> Response transmitted to virtual com port on embedded debugger
My issue is that for some reason, I'm either not getting anything sent back, or it's sending back the variable request_msg
I know that the response from the sensor should be 99 bytes of ASCII, and I've tested both the actual sensor, and the Arduino test board over serial connectors to ensure that the readings are coming back correctly.
The software is using Atmel ASF v4.0, which is great when it works, but the documentation is fairly flaky, so I was hoping someone with more experience could point me as to where I'm going wrong in the code.
I have the following code for my main application:
#include "atmel_start.h"
#include "atmel_start_pins.h"
#include <string.h>
static uint8_t example_hello_world[14] = "Hello World!\n";
static uint8_t example_error_msg[13] = "UART Error!\n";
static uint8_t request_msg[24] = "Sending Sensor Request\n";
static uint8_t rotronic_ascii[8] = "{ 99RDD}";
volatile static uint32_t data_arrived = 0;
volatile static uint32_t reading_received = 0;
static void tx_cb_EDBG_COM(const struct usart_async_descriptor *const io_descr)
{
/* Transfer completed */
gpio_toggle_pin_level(LED0);
}
static void rx_cb_EDBG_COM(const struct usart_async_descriptor *const io_descr)
{
/* Receive completed */
data_arrived = 1;
}
static void err_cb_EDBG_COM(const struct usart_async_descriptor *const io_descr)
{
/* error handle */
io_write(&EDBG_COM.io, example_error_msg, 13);
}
static void tx_cb_COM1(const struct usart_async_descriptor *const io_descr)
{
/* Transfer completed */
gpio_toggle_pin_level(LED0);
}
static void rx_cb_COM1(const struct usart_async_descriptor *const io_descr)
{
/* Receive completed */
reading_received = 1;
}
static void err_cb_COM1(const struct usart_async_descriptor *const io_descr)
{
/* error handle */
io_write(&COM1.io, example_error_msg, 13);
}
int main(void)
{
volatile uint8_t recv_char[99];
atmel_start_init();
// Setup the EDBG Serial Port
usart_async_register_callback(&EDBG_COM, USART_ASYNC_TXC_CB, tx_cb_EDBG_COM);
usart_async_register_callback(&EDBG_COM, USART_ASYNC_RXC_CB, rx_cb_EDBG_COM);
usart_async_register_callback(&EDBG_COM, USART_ASYNC_ERROR_CB, err_cb_EDBG_COM);
usart_async_enable(&EDBG_COM);
// Send a test string to ensure EDBG Serial is working
io_write(&EDBG_COM.io, example_hello_world, 14);
// Setup the Rotronic [Arduino] Serial Port
usart_async_register_callback(&COM1, USART_ASYNC_TXC_CB, tx_cb_COM1);
usart_async_register_callback(&COM1, USART_ASYNC_RXC_CB, rx_cb_COM1);
usart_async_register_callback(&COM1, USART_ASYNC_ERROR_CB, err_cb_COM1);
usart_async_enable(&COM1);
while (1) {
if (reading_received == 0)
{
// Delay for a Bit
delay_ms(5000);
// Notify the EDBG COM Port
io_write(&EDBG_COM.io, request_msg, 24);
// Send the Rotronic ASCII
io_write(&COM1.io, rotronic_ascii, 8);
}
// Check if Reading has been Received
if (reading_received == 1)
{
while (io_read(&COM1.io, &recv_char, 99) == 99)
{
// Write what's on the buffer from the receiver
io_write(&EDBG_COM.io, recv_char, 99);
}
// Reset the flag
reading_received = 0;
}
}
}
You seem to be coding for ASFv3 - v4 will trigger your receive callback for any incoming bytes, not only once when your buffer is full (and you have received every 99 characters).
That means that io_read will most probably never return 99 (because it was only a partial read of your message) and you will most probably never send anything back.
Note the docs say (Scroll down to "different read function behavior..."):
In ASFv4 a data reception type callback in a driver with a ring buffer is triggered for every received data.
The UART apparently is a driver with a ring buffer.
You need to repeatedly call io_read and sum up the number of received bytes until you have got 99. Only then proceed. The ASF docs have an example for that. Make sure you copy code from there that fits your version.

Sharing text through XBee on Arduino

How do I share text through an XBee module?
I tried, but instead of a word, I am getting some numbers every time. I want to exchange text data from both sides. I am using an Arduino for communication. How can I fix this problem?
I've been through a similar problem and was able to work around it. I assume you're using a MEGA 2560 and have a set of XBEE modules connected to two computers through the SPARKFUN XBEE USB explorer.
I've included my code that you can upload to both of the XBEEs and use them as a simple walkie-talkie pair. The program is meant to read an entire incoming word/string terminated with a defined character.
//xbee walkie-talkies
#define EndOfInput '#'//define a terminating character
void setup() {
Serial1.begin(9600); //serial thru pin 19
Serial.begin(9600); //Serial monitor
}
String incomingWord=""; //initialize to NULL
char input; //to read the incoming character
void loop() {
if (Serial.available()>0) {
// send out whatever is typed at the serial
//monitor thru the XBEE
Serial1.write(Serial.read());
}
//read the entire incoming word
while(Serial1.available()>0){
input = Serial1.read();
if (input != EndOfInput) incomingWord+=input;
else break;
}
//print out the word received on the serial monitor
Serial.println(incomingWord);
incomingWord = ""; //reset the string
}
It should be pretty self-explanatory.

Resources