ASCII really is morse code for computers. This is telegraph technology updated to the 1960s. Arduinos and Raspberry Pis still contain serial ports.
Open ASCIITable ExampleMenu File->Examples->04.Communication->ASCIITable compile and upload to your Uno board. It is a built-in Example that comes with Arduino IDE.
Click on the little magnifying glass in the corner of the Arduino screen. A sort of terminal screen pops open and fills up. The Tx transmit data lamp on the Uno board lights up.
We are looking at a digital conversation. The Uno board is talking to the PC using the ASCII protocol language. Scroll up and down to see the table of numbers and the ASCII font letter that goes with it.
Read one of the lines0x21 is a hexadecimal number equal to 33 decimal. The octal version is o41. In binary it is 0b00100001. The Arduino compiler knows this is the ASCII code for the alphanumeric symbol !
Scroll through each page of output. This is numerals, punctuation, small and capital letters. We see the alphanumeric character then decimal, hex, octal and binary versions. Printable characters are from value 32(space) to 126(~ tilde).
Big, majuscule, capital letters.
Small, miniscule letters.
Read each line beginning with a printed character from the ASCII alphabet. We can store the letter as binary in one of our Arduino's bytes in memory.
while (!Serial) {The sketch running on the board was waiting for you to open the serial monitor window. Comment out the while serial lines if you need the sketch to run automically.
Serial.begin(9600); turns on a Serial Port to work at 9600 bits per second speed. The Uno board is reading digits from pin 0(Rx) and writing bits out pin 1(Tx). The board has LED lights that blink when bits pass by.
The programmer chip on the Arduino board also carries serial data to and from the PC on USB. The pop up window is what your PC sees on its side of the Arduino connection.
CodeThe body of our code is a Serial.print command. It handles multiple arguments, formatted text and numerals. Compare these lines to how we see the tables of ASCII. These lines of code produce the output that we see.
Look at the Serial Monitor screen and click clear output. The little screen should now be blank. Press the reset button on your Uno and watch for lights flashing on the board.
This graphic shows how the bit stream from the ATmega328P controller chip is transmitted out of the board and into your computer. We are showing a close-up of the chip and which pins carry signal.
Change Baud RateBaud rate is a name for the transmission speed. We told the Arduino to open its serial port at 9600 bits per second. If we tell the PC to use a speed like 19200 baud we get a display of gibberish. Autobaud is a computer setting where devices adjust speed automatically.
Pins 0 and 1 of the Arduino board are a default serial communications port. You can still assign the pins to work as normal inputs or outputs but some applications need these leads to transmit and receive data.
The Universal Asynchronous Receiver Transmitter circuit has been around for decades. A convenient package that can handle data coming in and out, timing and handshake functions.
The same programmer circuit that reads and writes to the flash memory in the Uno processor also connects to USB on your computer. We have Arduinos with Serial Monitor comm ports. When we use external programmer/debugger devices we sometimes need to connect adapters to do this.
The Arduino IDE connects to this serial port through the USB.
Look at this header from an Orange Pi 5 SBC computer board. Notice the 3 pins labelled UART? Many computers have a default port that you can use as a console connection or serial port to communicate with other computers.
The pins are Tx-Rx-Gnd 5volt TTL level signals. We can talk between computers with ASCII.
Arduino Driver for PCWhen you setup Arduino IDE it installed a driver to talk to ArduinoCorp products. You may have a third party board that needs a serial driver to function correctly. Check device manager if you have problems.
This is a real USB serial comm port number to your PC operating system. Hyperterminal and other terminal emulators can bidirectionally connect to the Arduino using this number.
Both versions of Arduino program the flash through the same port. The computer restarts the Arduino board to put it into a flashing mode.
WaveformConnect an oscilloscope to Arduino Uno digital pin 1 which is carrying the Tx transmit signal for the UART circuit. Trigger on falling edge when the ASCII character starts.
Read the bits left to right, just like in the table. More information about the waveform at the Arduino website.
Why does the voltage start high and drop? Doing it this way allows the receiving circuit to detect the transmitter and use its power. The pin on the Arduino board can output up to 20ma at 5v.
Control CharactersWe aren't using them in this project but look at the wikipedia page on ASCII. Look at characters 0 to 31 in this diagram. CR carriage return, LF line feed even a BEL character to ring a bell. Then we see ! at 33 decimal.
Locate the declaration for our variable thisByte. The sketch says it is a 16bit integer. Our sketch has been handling an 8bit variable as if it were a much bigger number.
Change the code to read char and compile. The sketch still compiles but it takes up less flash with the smaller variable. The same happens if we use byte.
Look through the sketch for how it handles decimal and binary values. The compiler accepts ordinary numbers as decimal. 0x21 is a hexadecimal number equal to 33 decimal. The octal version is o41. In binary it is 0b00100001.
The compiler knows this is the ASCII code for the alphanumeric symbol !. When we write in our code '!' it will be read as ordinary decimal 33. That's why the sketch keeps saying it in the comments.
Where do the text strings go?Good question. Our sketch had quite a long character string to print a table header.
Our sketch stashes the Character strings in flash memory. Each ASCII character occupies a byte of flash memory so that it can be printed out on every startup. The compiler, and the processor, manage these characters in a mathematical entity called an array.
There is a way to store character strings in Arduino, the last character (or byte) in our array will be 0x00. We call this the null character because it is all zero and not on the ASCII list.
The Hex FileMenu Sketch->Export compiled binary and the Arduino IDE takes our sketch program code and generates a hex file. Look in the sketch folder. Open it with notepad and you see hexadecimal numbers. We could punch holes in paper tape and store the same data.
Go through the tables in this project and figger out what "ASCII" is in code. We know the character string is stored in the sketch. See if you can find our character string hidden in the code. Can you find the null character that signals the end of the string array?
Comments
Please log in or sign up to comment.