Hello,
I have two arduinos MEGA for communication using UART. I am using as well a RF module with TX and RX pins. I want to monitor bit error, so I need to print the entire frame, not only the data. Using bitRead seems to get rid of the start and stop bits. Any clue on how to get that? Here is the code for the transmitting and receiving arduino.
int num_sent = 203; //String data
void setup() {
// Begin the Serial at 9600 Baud
Serial.begin(9600,SERIAL_8N1);
}
void loop() {
Serial.write(num_sent); //Write the serial data
delay(1000);
}
Receiving code:
const byte numPins = 10;
byte pins[] = {13, 14, 15, 16, 17, 18, 19, 20, 21, 22};
void setup() {
Serial.begin(9600,SERIAL_8N1);
}
void loop() {
while(!Serial.available()); // Do nothing until serial input is received
byte num = Serial.read(); // Get num from somewhere
for (byte i=0; i<numPins; i++) {
byte state = bitRead(num, i);
digitalWrite(pins[i], state);
Serial.print(state);
}
Serial.println();
}
Using bitRead seems to get rid of the start and stop bits. Any clue on how to get that?
Start and stop bits are handled by the hardware, your sketch won't see them. What exactly are you trying to achieve? I get the impression a scope or logic analyzer would be the better tool for the task but as I don't know the target that's just wild guessing.
1. When you execute Serial.write(203); from the sender side, the following timing frame travels to the destination.
Figure-1: ASCII frame for data byte 203 (0xCB) wih LSBit first
2. The frames arrives at the destination (Fig-2), the START and STOP bits are stripped out by the RX-Section and then only the 8-bit data (CB = 11001011) enters into FIFO Buffer from which you have got it into your variable num by executing this code: byte num = Serial.read();. So, the variable num essentially contains (assuming no bit loss) the following bit pattern:
num = 11001011.
Figure-2: UART data communicaton between two Arduinos
3. You are sending the data byte at 1-sec interval. If the receiver does not print it at 1-sec interval, then there has occurred a loss of frame either due to corruption in START/STOP bit.
4. You have read the bits and have sent them to 8 LEDs connected with 8 DPins of receiver. This helps you to visualize if there is any bit loss or not.
5. Note that START (always LOW) and STOP (always HIGH) are not information as they have the known values. Therefore, there is no meaning to monitor them.
6. Please, move your RF devices to Serial1 Ports and leave Serial Port connected with Serial Monitor/IDE.