![](/media/cache/profiles/23d3743ad75e4470e7ff7129351a9541.jpg.50x50_q85.jpg)
ELNC-6007-19W example developed in Lecture. Enabling interrupts from DigitalIn and serial port receiver.
main.cpp@0:4741f5b28fec, 2019-03-14 (annotated)
- Committer:
- TheFella
- Date:
- Thu Mar 14 17:52:24 2019 +0000
- Revision:
- 0:4741f5b28fec
Example for lecture class
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
TheFella | 0:4741f5b28fec | 1 | #include "mbed.h" |
TheFella | 0:4741f5b28fec | 2 | |
TheFella | 0:4741f5b28fec | 3 | #define ROWS 10 |
TheFella | 0:4741f5b28fec | 4 | #define COLS 10 |
TheFella | 0:4741f5b28fec | 5 | #define BUFSIZE 30 |
TheFella | 0:4741f5b28fec | 6 | #define TRUE 1 |
TheFella | 0:4741f5b28fec | 7 | #define FALSE 0 |
TheFella | 0:4741f5b28fec | 8 | |
TheFella | 0:4741f5b28fec | 9 | PwmOut spk( p25 ); |
TheFella | 0:4741f5b28fec | 10 | DigitalIn pb1( p8 ); |
TheFella | 0:4741f5b28fec | 11 | DigitalIn pb2( p19 ); |
TheFella | 0:4741f5b28fec | 12 | DigitalIn pb3( p18 ); |
TheFella | 0:4741f5b28fec | 13 | |
TheFella | 0:4741f5b28fec | 14 | Serial pc(USBTX, USBRX, 9600); |
TheFella | 0:4741f5b28fec | 15 | |
TheFella | 0:4741f5b28fec | 16 | InterruptIn pb1Int( p8 ); |
TheFella | 0:4741f5b28fec | 17 | |
TheFella | 0:4741f5b28fec | 18 | char buffer[ BUFSIZE ]; |
TheFella | 0:4741f5b28fec | 19 | char *ptr = buffer; |
TheFella | 0:4741f5b28fec | 20 | char sentenceReady = FALSE; |
TheFella | 0:4741f5b28fec | 21 | |
TheFella | 0:4741f5b28fec | 22 | void serialISR() |
TheFella | 0:4741f5b28fec | 23 | { |
TheFella | 0:4741f5b28fec | 24 | int index = 0; |
TheFella | 0:4741f5b28fec | 25 | if( pc.readable() ) |
TheFella | 0:4741f5b28fec | 26 | { |
TheFella | 0:4741f5b28fec | 27 | char hold = pc.getc(); |
TheFella | 0:4741f5b28fec | 28 | if( hold == '$' ) |
TheFella | 0:4741f5b28fec | 29 | { |
TheFella | 0:4741f5b28fec | 30 | for( index = 0; index<BUFSIZE; index++) |
TheFella | 0:4741f5b28fec | 31 | { |
TheFella | 0:4741f5b28fec | 32 | buffer[ index ] = 0; |
TheFella | 0:4741f5b28fec | 33 | } |
TheFella | 0:4741f5b28fec | 34 | ptr = buffer; |
TheFella | 0:4741f5b28fec | 35 | |
TheFella | 0:4741f5b28fec | 36 | } |
TheFella | 0:4741f5b28fec | 37 | if( hold == '#' ) |
TheFella | 0:4741f5b28fec | 38 | { |
TheFella | 0:4741f5b28fec | 39 | sentenceReady = TRUE; |
TheFella | 0:4741f5b28fec | 40 | } |
TheFella | 0:4741f5b28fec | 41 | *ptr = hold; |
TheFella | 0:4741f5b28fec | 42 | ptr++; |
TheFella | 0:4741f5b28fec | 43 | if( ptr >= &buffer[ BUFSIZE ] ) |
TheFella | 0:4741f5b28fec | 44 | { |
TheFella | 0:4741f5b28fec | 45 | ptr = buffer; |
TheFella | 0:4741f5b28fec | 46 | } |
TheFella | 0:4741f5b28fec | 47 | } |
TheFella | 0:4741f5b28fec | 48 | } |
TheFella | 0:4741f5b28fec | 49 | |
TheFella | 0:4741f5b28fec | 50 | |
TheFella | 0:4741f5b28fec | 51 | void pb1ISR() |
TheFella | 0:4741f5b28fec | 52 | { |
TheFella | 0:4741f5b28fec | 53 | int row = 0, col = 0; |
TheFella | 0:4741f5b28fec | 54 | for( row = 0; row< ROWS; row++ ) |
TheFella | 0:4741f5b28fec | 55 | { |
TheFella | 0:4741f5b28fec | 56 | for( col = 0; col< COLS; col++ ) |
TheFella | 0:4741f5b28fec | 57 | { |
TheFella | 0:4741f5b28fec | 58 | pc.printf("|_"); |
TheFella | 0:4741f5b28fec | 59 | } |
TheFella | 0:4741f5b28fec | 60 | pc.printf("\n\r"); |
TheFella | 0:4741f5b28fec | 61 | } |
TheFella | 0:4741f5b28fec | 62 | |
TheFella | 0:4741f5b28fec | 63 | } |
TheFella | 0:4741f5b28fec | 64 | |
TheFella | 0:4741f5b28fec | 65 | int main() |
TheFella | 0:4741f5b28fec | 66 | { |
TheFella | 0:4741f5b28fec | 67 | pb1Int.rise( &pb1ISR ); |
TheFella | 0:4741f5b28fec | 68 | pc.attach( &serialISR ); |
TheFella | 0:4741f5b28fec | 69 | |
TheFella | 0:4741f5b28fec | 70 | while(1) |
TheFella | 0:4741f5b28fec | 71 | { |
TheFella | 0:4741f5b28fec | 72 | //pc.printf(".\n\r"); |
TheFella | 0:4741f5b28fec | 73 | //wait(0.5); |
TheFella | 0:4741f5b28fec | 74 | if( sentenceReady ) |
TheFella | 0:4741f5b28fec | 75 | { |
TheFella | 0:4741f5b28fec | 76 | pc.printf("%s", buffer); |
TheFella | 0:4741f5b28fec | 77 | sentenceReady = FALSE; |
TheFella | 0:4741f5b28fec | 78 | } |
TheFella | 0:4741f5b28fec | 79 | } |
TheFella | 0:4741f5b28fec | 80 | } |