Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define TX_PIN 2
- #define BAUD_RATE 9600
- #define BIT_DURATION_MICROS (1000000 / BAUD_RATE)
- //T = 1/f = 1/baudrate seconds = 1000000 / baudrate microseconds
- void setup() {
- pinMode(TX_PIN, OUTPUT);
- digitalWrite(TX_PIN, HIGH); // Idle state
- }
- void loop() {
- sendByte('H');
- sendByte('e');
- sendByte('l');
- sendByte('l');
- sendByte('o');
- sendByte('\n');
- delay(1000);
- }
- void sendByte(byte b) {
- // Start bit (LOW)
- digitalWrite(TX_PIN, LOW);
- delayMicroseconds(BIT_DURATION_MICROS);
- // Data bits (LSB first)
- for (int i = 0; i < 8; i++) {
- digitalWrite(TX_PIN, (b >> i) & 1);
- delayMicroseconds(BIT_DURATION_MICROS);
- }
- // Stop bit (HIGH)
- digitalWrite(TX_PIN, HIGH);
- delayMicroseconds(BIT_DURATION_MICROS);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement