Dump an ascii table using the Serial device

Dependencies:   mbed

Committer:
Rickta59
Date:
Wed Feb 19 16:41:06 2014 +0000
Revision:
1:26d6906308ad
Parent:
0:4a6162db30d7
update comments;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Rickta59 1:26d6906308ad 1 // Print "ascii table" to the serial port
Rickta59 0:4a6162db30d7 2
Rickta59 0:4a6162db30d7 3 #include "mbed.h"
Rickta59 0:4a6162db30d7 4
Rickta59 0:4a6162db30d7 5 Serial serial(USBTX, USBRX);
Rickta59 0:4a6162db30d7 6
Rickta59 0:4a6162db30d7 7 int thisByte = 33;
Rickta59 0:4a6162db30d7 8
Rickta59 0:4a6162db30d7 9 char *int2bin(unsigned value, char *buffer, const int buf_size) {
Rickta59 0:4a6162db30d7 10 buffer += (buf_size);
Rickta59 0:4a6162db30d7 11 int i = 8;
Rickta59 0:4a6162db30d7 12 do {
Rickta59 0:4a6162db30d7 13 *--buffer = (value & 1) + '0';
Rickta59 0:4a6162db30d7 14 value >>= 1;
Rickta59 0:4a6162db30d7 15 }
Rickta59 0:4a6162db30d7 16 while(--i);
Rickta59 0:4a6162db30d7 17
Rickta59 0:4a6162db30d7 18 return buffer;
Rickta59 0:4a6162db30d7 19 }
Rickta59 0:4a6162db30d7 20
Rickta59 0:4a6162db30d7 21 int main()
Rickta59 0:4a6162db30d7 22 {
Rickta59 0:4a6162db30d7 23 serial.printf("ASCII Table ~ Character Map\r\n");
Rickta59 0:4a6162db30d7 24
Rickta59 0:4a6162db30d7 25 char buff[34];
Rickta59 0:4a6162db30d7 26 buff[33] = 0;
Rickta59 0:4a6162db30d7 27 do
Rickta59 0:4a6162db30d7 28 {
Rickta59 0:4a6162db30d7 29 // prints value unaltered, i.e. the raw binary version of the
Rickta59 0:4a6162db30d7 30 // byte. The serial monitor interprets all bytes as
Rickta59 0:4a6162db30d7 31 // ASCII, so 33, the first number, will show up as '!'
Rickta59 0:4a6162db30d7 32 serial.printf("%c:", thisByte);
Rickta59 0:4a6162db30d7 33 serial.printf(", dec: %d", thisByte);
Rickta59 0:4a6162db30d7 34 serial.printf(", hex: 0x%x", thisByte);
Rickta59 0:4a6162db30d7 35 serial.printf(", oct: \\0%o", thisByte);
Rickta59 0:4a6162db30d7 36 serial.printf(", bin: %s\r\n", int2bin(thisByte,buff,33));
Rickta59 0:4a6162db30d7 37 } while(++thisByte < 127);
Rickta59 0:4a6162db30d7 38 }