Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
28 views2 pages

#Include #Define Bit9600Delay 100 #Define Halfbit9600Delay 50 #Define Bit4800Delay 188 #Define Halfbit4800Delay 94

This Arduino code defines functions for serial communication at 9600 baud: - The setup() function initializes the receive and transmit pins and prints a test message. - SWprint() sends a character by generating the start bit, data bits, and stop bit. - SWread() reads a character by waiting for the start bit and sampling the receive pin over each data bit interval. - The main loop() continuously reads characters and prints them in uppercase.

Uploaded by

Fadlila Muhammad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views2 pages

#Include #Define Bit9600Delay 100 #Define Halfbit9600Delay 50 #Define Bit4800Delay 188 #Define Halfbit4800Delay 94

This Arduino code defines functions for serial communication at 9600 baud: - The setup() function initializes the receive and transmit pins and prints a test message. - SWprint() sends a character by generating the start bit, data bits, and stop bit. - SWread() reads a character by waiting for the start bit and sampling the receive pin over each data bit interval. - The main loop() continuously reads characters and prints them in uppercase.

Uploaded by

Fadlila Muhammad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

//Created August 23 2006

//Heather Dewey-Hagborg
//http://www.arduino.cc

#include <ctype.h>

#define bit9600Delay 100


#define halfBit9600Delay 50
#define bit4800Delay 188
#define halfBit4800Delay 94

byte rx = 6;
byte tx = 7;
byte SWval;

void setup() {
pinMode(rx,INPUT);
pinMode(tx,OUTPUT);
digitalWrite(tx,HIGH);
delay(2);
digitalWrite(13,HIGH); //turn on debugging LED
SWprint('h'); //debugging hello
SWprint('i');
SWprint(10); //carriage return
}

void SWprint(int data)


{
byte mask;
//startbit
digitalWrite(tx,LOW);
delayMicroseconds(bit9600Delay);
for (mask = 0x01; mask>0; mask <<= 1) {
if (data & mask){ // choose bit
digitalWrite(tx,HIGH); // send 1
}
else{
digitalWrite(tx,LOW); // send 0
}
delayMicroseconds(bit9600Delay);
}
//stop bit
digitalWrite(tx, HIGH);
delayMicroseconds(bit9600Delay);
}

int SWread()
{
byte val = 0;
while (digitalRead(rx));
//wait for start bit
if (digitalRead(rx) == LOW) {
delayMicroseconds(halfBit9600Delay);
for (int offset = 0; offset < 8; offset++) {
delayMicroseconds(bit9600Delay);
val |= digitalRead(rx) << offset;
}
//wait for stop bit + extra
delayMicroseconds(bit9600Delay);
delayMicroseconds(bit9600Delay);
return val;
}
}

void loop()
{
SWval = SWread();
SWprint(toupper(SWval));
}

You might also like