Relable data transmission
connect to Arduino nano and associate LCD & breadboard showing how data transmission works
hardware
- Arduino Board * 2
- LCD * 2
- LED * 2
- Potentiometier & 2
- 220 ohm resister * 2
- wires
Wires
- LCD (TX) to Arduino
- K(Cathode) GND
- A(Anode) VCC (with 330 ohm)
- D7 7
- D6 6
- D5 5
- D4 4
- E 9
- RW (Read Write) GND (always wirte)
- RS(Register Select) 8
- V0(Setting sthe contrast) variable resistor (potentiometer)
- VDD VCC
- VSS GND
- Arduino nano
- 4~9 LCD
- 3 LED( with 220 ohm) tx-rx line
- 2 LED( with 220 ohm) clock line
- VCC VCC
- GND GND
code
- TX
#include <LiquidCrystal.h>
// Transmit rate in bps
#define TX_RATE 5
// Pin assignments
#define TX_CLOCK 2
#define TX_DATA 3
#define LCD_D4 4
#define LCD_D5 5
#define LCD_D6 6
#define LCD_D7 7
#define LCD_RS 8
#define LCD_EN 9
const char *message = "Hello, world!";
void setup() {
pinMode(TX_CLOCK, OUTPUT);
pinMode(TX_DATA, OUTPUT);
// Initialize the LCD screen;
LiquidCrystal lcd(LCD_RS, LCD_EN, LCD_D4, LCD_D5, LCD_D6, LCD_D7);
lcd.begin(16, 2); // 16 byte, 2 lines
lcd.setCursor(0, 0); // top left corner
lcd.print(message);
for (int byte_idx = 0; byte_idx < strlen(message); byte_idx++) {
char tx_byte = message[byte_idx];
// Clear the second line of the display
lcd.noCursor(); // turn off cursor display;
lcd.setCursor(0, 1);
lcd.print(" "); // 8 spaces
lcd.setCursor(byte_idx, 0);
lcd.cursor(); // show the cursor
for (int bit_idx = 0; bit_idx < 8; bit_idx++) {
bool tx_bit = tx_byte & (0x80 >> bit_idx);
digitalWrite(TX_DATA, tx_bit);
delay((1000 / TX_RATE) / 2);
// Update the LCD
lcd.noCursor(); // turn off cursor display;
lcd.setCursor(bit_idx, 1); // second line of lcd
lcd.print(tx_bit ? "1" : "0");
lcd.setCursor(byte_idx, 0);
lcd.cursor(); // show the cursor
// pulse the clock
digitalWrite(TX_CLOCK, HIGH);
delay((1000 / TX_RATE) / 2);
digitalWrite(TX_CLOCK, LOW);
}
}
digitalWrite(TX_DATA, LOW);
}
void loop() {
// put your main code here, to run repeatedly:
}
- RX
#include <LiquidCrystal.h>
// Transmit rate in bps
#define TX_RATE 5
// Pin assignments
#define RX_CLOCK 2
#define RX_DATA 3
#define LCD_D4 4
#define LCD_D5 5
#define LCD_D6 6
#define LCD_D7 7
#define LCD_RS 8
#define LCD_EN 9
// Initialize the LCD screen;
LiquidCrystal lcd(LCD_RS, LCD_EN, LCD_D4, LCD_D5, LCD_D6, LCD_D7);
char message[16];
volatile byte rx_byte = 0;
volatile int bit_position = 0;
volatile bool update_lcd = true;
void setup() {
pinMode(RX_DATA, INPUT);
strcpy(message, "");
lcd.begin(16, 2); // 16 byte, 2 rows
attachInterrupt(digitalPinToInterrupt(RX_CLOCK), onClockPulse, RISING);
}
void onClockPulse() {
bool rx_bit = digitalRead(RX_DATA);
if (bit_position == 8) {
rx_byte = 0;
bit_position = 0;
}
if (rx_bit) {
rx_byte |= (0x80 >> bit_position);
}
bit_position += 1;
if (bit_position == 8) {
strncat(message, &rx_byte, 1);
}
update_lcd = true;
}
void loop() {
// use clock interrupt instead
// delay(196); // delay(200); in sync with rx because of extra calc down below
if (update_lcd) {
update_lcd = false;
lcd.noCursor();
lcd.setCursor(0, 0);
lcd.print(message);
lcd.setCursor(0, 1);
for (int i = 0; i < 8; i += 1) {
if (i < bit_position) {
lcd.print(rx_byte & (0x80 >> i) ? "1" : "0");
} else {
lcd.print(" ");
}
}
lcd.setCursor(strlen(message), 0);
}
}