Solutions for the UART experiments for LPC812 MAX

Dependencies:   mbed

Committer:
embeddedartists
Date:
Fri Nov 22 08:40:56 2013 +0000
Revision:
0:0ad38420d207
First version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
embeddedartists 0:0ad38420d207 1 #include "mbed.h"
embeddedartists 0:0ad38420d207 2
embeddedartists 0:0ad38420d207 3 Serial pc(USBTX, USBRX); // tx, rx
embeddedartists 0:0ad38420d207 4
embeddedartists 0:0ad38420d207 5 DigitalIn button(D0);
embeddedartists 0:0ad38420d207 6
embeddedartists 0:0ad38420d207 7 Timer timer;
embeddedartists 0:0ad38420d207 8
embeddedartists 0:0ad38420d207 9 static void experiment1()
embeddedartists 0:0ad38420d207 10 {
embeddedartists 0:0ad38420d207 11 unsigned int variableToHoldValue; //declare a 32-bit variable
embeddedartists 0:0ad38420d207 12 unsigned char *pBytePointer; //declare a byte-pointer
embeddedartists 0:0ad38420d207 13
embeddedartists 0:0ad38420d207 14 pc.printf("\nThis is a first test...\n");
embeddedartists 0:0ad38420d207 15 pc.printf("that printf() works - and it does!");
embeddedartists 0:0ad38420d207 16
embeddedartists 0:0ad38420d207 17 //store value in 32-bit variable
embeddedartists 0:0ad38420d207 18 variableToHoldValue = 0x0AC0FFEE;
embeddedartists 0:0ad38420d207 19
embeddedartists 0:0ad38420d207 20 //set the byte-pointer to the beginning (first byte) of the 32-bit variable
embeddedartists 0:0ad38420d207 21 pBytePointer = (unsigned char *)&variableToHoldValue;
embeddedartists 0:0ad38420d207 22
embeddedartists 0:0ad38420d207 23 //Test if big-endian
embeddedartists 0:0ad38420d207 24 if ((*pBytePointer == 0x0A) &&
embeddedartists 0:0ad38420d207 25 (*(pBytePointer+1) == 0xC0) &&
embeddedartists 0:0ad38420d207 26 (*(pBytePointer+2) == 0xFF) &&
embeddedartists 0:0ad38420d207 27 (*(pBytePointer+3) == 0xEE)) {
embeddedartists 0:0ad38420d207 28 pc.printf("\nThe CPU is a Big-endian system\n");
embeddedartists 0:0ad38420d207 29 }
embeddedartists 0:0ad38420d207 30
embeddedartists 0:0ad38420d207 31 //Test if little-endian instead
embeddedartists 0:0ad38420d207 32 else if ((*pBytePointer == 0xEE) &&
embeddedartists 0:0ad38420d207 33 (*(pBytePointer+1) == 0xFF) &&
embeddedartists 0:0ad38420d207 34 (*(pBytePointer+2) == 0xC0) &&
embeddedartists 0:0ad38420d207 35 (*(pBytePointer+3) == 0x0A)) {
embeddedartists 0:0ad38420d207 36 pc.printf("\nThe CPU is a Little-endian system\n");
embeddedartists 0:0ad38420d207 37 }
embeddedartists 0:0ad38420d207 38 //Neither if the tests was correct – something is VERY wrong here!!!
embeddedartists 0:0ad38420d207 39 else {
embeddedartists 0:0ad38420d207 40 pc.printf("\nSomething is VERY wrong here!!!\n");
embeddedartists 0:0ad38420d207 41 }
embeddedartists 0:0ad38420d207 42
embeddedartists 0:0ad38420d207 43 while(1)
embeddedartists 0:0ad38420d207 44 ;
embeddedartists 0:0ad38420d207 45 }
embeddedartists 0:0ad38420d207 46
embeddedartists 0:0ad38420d207 47 static void experiment2()
embeddedartists 0:0ad38420d207 48 {
embeddedartists 0:0ad38420d207 49 // Enable button
embeddedartists 0:0ad38420d207 50 button.mode(PullUp);
embeddedartists 0:0ad38420d207 51
embeddedartists 0:0ad38420d207 52 // Enter forever loop
embeddedartists 0:0ad38420d207 53 while(1) {
embeddedartists 0:0ad38420d207 54 // Wait for button to be pressed and then released
embeddedartists 0:0ad38420d207 55 while(button) {
embeddedartists 0:0ad38420d207 56 }
embeddedartists 0:0ad38420d207 57 pc.printf("Button was pressed...\n");
embeddedartists 0:0ad38420d207 58 while(!button) {
embeddedartists 0:0ad38420d207 59 }
embeddedartists 0:0ad38420d207 60 pc.printf("and released\n");
embeddedartists 0:0ad38420d207 61 }
embeddedartists 0:0ad38420d207 62 }
embeddedartists 0:0ad38420d207 63
embeddedartists 0:0ad38420d207 64 static void experiment3_alt1()
embeddedartists 0:0ad38420d207 65 {
embeddedartists 0:0ad38420d207 66 pc.printf("This is a test of getc()...\n");
embeddedartists 0:0ad38420d207 67
embeddedartists 0:0ad38420d207 68 // Enter forever loop
embeddedartists 0:0ad38420d207 69 while(1) {
embeddedartists 0:0ad38420d207 70 int rxChar = pc.getc();
embeddedartists 0:0ad38420d207 71 pc.printf("Got %c\n", rxChar);
embeddedartists 0:0ad38420d207 72 }
embeddedartists 0:0ad38420d207 73 }
embeddedartists 0:0ad38420d207 74
embeddedartists 0:0ad38420d207 75 static void experiment3_alt2()
embeddedartists 0:0ad38420d207 76 {
embeddedartists 0:0ad38420d207 77 int val = 0;
embeddedartists 0:0ad38420d207 78 int numChars = 0;
embeddedartists 0:0ad38420d207 79 pc.printf("This is a test of getc() and converts to a number...\n");
embeddedartists 0:0ad38420d207 80
embeddedartists 0:0ad38420d207 81 // Enter forever loop
embeddedartists 0:0ad38420d207 82 while(1) {
embeddedartists 0:0ad38420d207 83 int rxChar = pc.getc();
embeddedartists 0:0ad38420d207 84 numChars++;
embeddedartists 0:0ad38420d207 85 if ((rxChar == '\r') || (rxChar == '\n') || (rxChar == ' ')) {
embeddedartists 0:0ad38420d207 86 // Found a separator character, print number and start over
embeddedartists 0:0ad38420d207 87 if (numChars > 1) {
embeddedartists 0:0ad38420d207 88 pc.printf("Found %d\n", val);
embeddedartists 0:0ad38420d207 89 }
embeddedartists 0:0ad38420d207 90 numChars = 0;
embeddedartists 0:0ad38420d207 91 val = 0;
embeddedartists 0:0ad38420d207 92 }
embeddedartists 0:0ad38420d207 93 else if ((rxChar < '0') || (rxChar > '9')) {
embeddedartists 0:0ad38420d207 94 // Not a number, reset and start looking again
embeddedartists 0:0ad38420d207 95 pc.printf("Not a number: '%c'\n", rxChar);
embeddedartists 0:0ad38420d207 96 numChars = 0;
embeddedartists 0:0ad38420d207 97 val = 0;
embeddedartists 0:0ad38420d207 98 }
embeddedartists 0:0ad38420d207 99 else {
embeddedartists 0:0ad38420d207 100 // A valid number was found
embeddedartists 0:0ad38420d207 101 val = (val * 10) + (rxChar - '0');
embeddedartists 0:0ad38420d207 102 if (numChars == 9) {
embeddedartists 0:0ad38420d207 103 // Almost reached maximum size, print and continue
embeddedartists 0:0ad38420d207 104 pc.printf("Found %d\n", val);
embeddedartists 0:0ad38420d207 105 numChars = 0;
embeddedartists 0:0ad38420d207 106 val = 0;
embeddedartists 0:0ad38420d207 107 }
embeddedartists 0:0ad38420d207 108 }
embeddedartists 0:0ad38420d207 109 }
embeddedartists 0:0ad38420d207 110 }
embeddedartists 0:0ad38420d207 111
embeddedartists 0:0ad38420d207 112 static void experiment4()
embeddedartists 0:0ad38420d207 113 {
embeddedartists 0:0ad38420d207 114 #define BYTES_IN_BUFF 512
embeddedartists 0:0ad38420d207 115 #define NUM_BUFFS_TO_SEND 10
embeddedartists 0:0ad38420d207 116
embeddedartists 0:0ad38420d207 117 // Change baudrate here to see what difference it makes. Don't forget
embeddedartists 0:0ad38420d207 118 // to change to the same value on the Host PC
embeddedartists 0:0ad38420d207 119 //pc.baud(115200);
embeddedartists 0:0ad38420d207 120
embeddedartists 0:0ad38420d207 121 pc.printf("This is a performance test...\n");
embeddedartists 0:0ad38420d207 122
embeddedartists 0:0ad38420d207 123 char buff[BYTES_IN_BUFF + 1];
embeddedartists 0:0ad38420d207 124
embeddedartists 0:0ad38420d207 125 // initialize the buffer with some characters to print
embeddedartists 0:0ad38420d207 126 for (int i = 0; i < BYTES_IN_BUFF; i+=10) {
embeddedartists 0:0ad38420d207 127 for (int c = 0; c < 10; c++) {
embeddedartists 0:0ad38420d207 128 buff[i+c] = '0' + c;
embeddedartists 0:0ad38420d207 129 }
embeddedartists 0:0ad38420d207 130 }
embeddedartists 0:0ad38420d207 131
embeddedartists 0:0ad38420d207 132 // puts expects the string to be null terminated
embeddedartists 0:0ad38420d207 133 buff[BYTES_IN_BUFF] = '\0';
embeddedartists 0:0ad38420d207 134
embeddedartists 0:0ad38420d207 135 timer.start();
embeddedartists 0:0ad38420d207 136 int begin = timer.read_ms();
embeddedartists 0:0ad38420d207 137 for (int num = 0; num < NUM_BUFFS_TO_SEND; num++) {
embeddedartists 0:0ad38420d207 138 pc.puts(buff);
embeddedartists 0:0ad38420d207 139 }
embeddedartists 0:0ad38420d207 140 int end = timer.read_ms();
embeddedartists 0:0ad38420d207 141 timer.stop();
embeddedartists 0:0ad38420d207 142
embeddedartists 0:0ad38420d207 143 // print information about how much time passed and the bitrate
embeddedartists 0:0ad38420d207 144 printf("\n--- Sent %d bytes in %dms\n", BYTES_IN_BUFF * NUM_BUFFS_TO_SEND, end-begin);
embeddedartists 0:0ad38420d207 145
embeddedartists 0:0ad38420d207 146 while(1)
embeddedartists 0:0ad38420d207 147 ;
embeddedartists 0:0ad38420d207 148 }
embeddedartists 0:0ad38420d207 149
embeddedartists 0:0ad38420d207 150 int main()
embeddedartists 0:0ad38420d207 151 {
embeddedartists 0:0ad38420d207 152 //experiment1(); // Detects endian
embeddedartists 0:0ad38420d207 153 //experiment2(); // Button
embeddedartists 0:0ad38420d207 154 //experiment3_alt1(); // getc
embeddedartists 0:0ad38420d207 155 //experiment3_alt2(); // getc with digits
embeddedartists 0:0ad38420d207 156 experiment4(); // performance
embeddedartists 0:0ad38420d207 157 }