![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Simple UART0 usage with SimpleLib
main.cpp@2:d367033913a2, 2010-11-13 (annotated)
- Committer:
- Alkorin
- Date:
- Sat Nov 13 14:48:05 2010 +0000
- Revision:
- 2:d367033913a2
- Parent:
- 1:6a9f5827b72e
Now working with UART0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Alkorin | 1:6a9f5827b72e | 1 | #include "mbed.h" |
Alkorin | 1:6a9f5827b72e | 2 | |
Alkorin | 1:6a9f5827b72e | 3 | #include "mbed_globals.h" |
Alkorin | 1:6a9f5827b72e | 4 | #include "interrupts.h" |
Alkorin | 1:6a9f5827b72e | 5 | #include "serial.h" |
Alkorin | 1:6a9f5827b72e | 6 | #include "leds.h" |
Alkorin | 1:6a9f5827b72e | 7 | |
Alkorin | 1:6a9f5827b72e | 8 | void serialOut(unsigned char c) { |
Alkorin | 1:6a9f5827b72e | 9 | SERIAL_PUTCHAR(c); |
Alkorin | 1:6a9f5827b72e | 10 | } |
Alkorin | 1:6a9f5827b72e | 11 | |
Alkorin | 1:6a9f5827b72e | 12 | void serialOutString(const char * c) { |
Alkorin | 1:6a9f5827b72e | 13 | while (*c) { |
Alkorin | 1:6a9f5827b72e | 14 | SERIAL_PUTCHAR(*c); |
Alkorin | 1:6a9f5827b72e | 15 | c++; |
Alkorin | 1:6a9f5827b72e | 16 | } |
Alkorin | 1:6a9f5827b72e | 17 | } |
Alkorin | 1:6a9f5827b72e | 18 | |
Alkorin | 2:d367033913a2 | 19 | void serialOutHex32(unsigned int i) |
Alkorin | 2:d367033913a2 | 20 | { |
Alkorin | 2:d367033913a2 | 21 | for(int j = 0; j < 8; j++) |
Alkorin | 2:d367033913a2 | 22 | { |
Alkorin | 2:d367033913a2 | 23 | unsigned char c = (i & 0xF0000000) >> 28; |
Alkorin | 2:d367033913a2 | 24 | if(c < 10) |
Alkorin | 2:d367033913a2 | 25 | serialOut('0' + c); |
Alkorin | 2:d367033913a2 | 26 | else |
Alkorin | 2:d367033913a2 | 27 | serialOut('A' + c - 10); |
Alkorin | 2:d367033913a2 | 28 | i = i << 4; |
Alkorin | 2:d367033913a2 | 29 | } |
Alkorin | 2:d367033913a2 | 30 | } |
Alkorin | 2:d367033913a2 | 31 | |
Alkorin | 1:6a9f5827b72e | 32 | SERIAL_INTERRUPT_HANDLER(void) { |
Alkorin | 1:6a9f5827b72e | 33 | // Check if interrupt is pending |
Alkorin | 1:6a9f5827b72e | 34 | if(!SERIAL_CHECK_INTERRUPT()) |
Alkorin | 1:6a9f5827b72e | 35 | return; |
Alkorin | 1:6a9f5827b72e | 36 | |
Alkorin | 1:6a9f5827b72e | 37 | // While some data to read |
Alkorin | 1:6a9f5827b72e | 38 | while (SERIAL_DATA_TO_READ()) { |
Alkorin | 1:6a9f5827b72e | 39 | char c = SERIAL_GETCHAR(); |
Alkorin | 1:6a9f5827b72e | 40 | serialOut('['); |
Alkorin | 1:6a9f5827b72e | 41 | serialOut(c); |
Alkorin | 1:6a9f5827b72e | 42 | serialOut(']'); |
Alkorin | 1:6a9f5827b72e | 43 | } |
Alkorin | 1:6a9f5827b72e | 44 | } |
Alkorin | 1:6a9f5827b72e | 45 | |
Alkorin | 1:6a9f5827b72e | 46 | int main() { |
Alkorin | 1:6a9f5827b72e | 47 | // Hardware Init |
Alkorin | 2:d367033913a2 | 48 | SERIAL_INIT(); |
Alkorin | 2:d367033913a2 | 49 | SERIAL_SETBAUD(9600); |
Alkorin | 1:6a9f5827b72e | 50 | SERIAL_ENABLE_INTERRUPT(SERIAL_INT_RX); |
Alkorin | 2:d367033913a2 | 51 | |
Alkorin | 1:6a9f5827b72e | 52 | serialOutString("Simple UART Sample Code\r\n"); |
Alkorin | 2:d367033913a2 | 53 | |
Alkorin | 1:6a9f5827b72e | 54 | while (1); |
Alkorin | 0:60e8d7bb5545 | 55 | } |