Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@0:765cf978c3e5, 2019-02-16 (annotated)
- Committer:
- hi1000
- Date:
- Sat Feb 16 02:32:43 2019 +0000
- Revision:
- 0:765cf978c3e5
- Child:
- 1:eb499e2a1b9b
Can bus with button OK
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hi1000 | 0:765cf978c3e5 | 1 | #include "mbed.h" |
hi1000 | 0:765cf978c3e5 | 2 | |
hi1000 | 0:765cf978c3e5 | 3 | CAN can1(PD_0, PD_1); |
hi1000 | 0:765cf978c3e5 | 4 | CAN can2(PB_5, PB_6); |
hi1000 | 0:765cf978c3e5 | 5 | |
hi1000 | 0:765cf978c3e5 | 6 | int a = 0; |
hi1000 | 0:765cf978c3e5 | 7 | int b = 0; |
hi1000 | 0:765cf978c3e5 | 8 | |
hi1000 | 0:765cf978c3e5 | 9 | void print_char(char c = '*') |
hi1000 | 0:765cf978c3e5 | 10 | { |
hi1000 | 0:765cf978c3e5 | 11 | printf("%c\r\n", c); |
hi1000 | 0:765cf978c3e5 | 12 | fflush(stdout); |
hi1000 | 0:765cf978c3e5 | 13 | } |
hi1000 | 0:765cf978c3e5 | 14 | |
hi1000 | 0:765cf978c3e5 | 15 | Thread thread; |
hi1000 | 0:765cf978c3e5 | 16 | |
hi1000 | 0:765cf978c3e5 | 17 | DigitalOut led1(LED1); |
hi1000 | 0:765cf978c3e5 | 18 | DigitalOut led2(LED2); |
hi1000 | 0:765cf978c3e5 | 19 | CANMessage msg; |
hi1000 | 0:765cf978c3e5 | 20 | |
hi1000 | 0:765cf978c3e5 | 21 | |
hi1000 | 0:765cf978c3e5 | 22 | InterruptIn button1(USER_BUTTON); |
hi1000 | 0:765cf978c3e5 | 23 | volatile bool button1_pressed = false; // Used in the main loop |
hi1000 | 0:765cf978c3e5 | 24 | volatile bool button1_enabled = true; // Used for debouncing |
hi1000 | 0:765cf978c3e5 | 25 | Timeout button1_timeout; // Used for debouncing |
hi1000 | 0:765cf978c3e5 | 26 | |
hi1000 | 0:765cf978c3e5 | 27 | // Enables button when bouncing is over |
hi1000 | 0:765cf978c3e5 | 28 | void button1_enabled_cb(void) |
hi1000 | 0:765cf978c3e5 | 29 | { |
hi1000 | 0:765cf978c3e5 | 30 | button1_enabled = true; |
hi1000 | 0:765cf978c3e5 | 31 | } |
hi1000 | 0:765cf978c3e5 | 32 | |
hi1000 | 0:765cf978c3e5 | 33 | // ISR handling button pressed event |
hi1000 | 0:765cf978c3e5 | 34 | void button1_onpressed_cb(void) |
hi1000 | 0:765cf978c3e5 | 35 | { |
hi1000 | 0:765cf978c3e5 | 36 | if (button1_enabled) { // Disabled while the button is bouncing |
hi1000 | 0:765cf978c3e5 | 37 | button1_enabled = false; |
hi1000 | 0:765cf978c3e5 | 38 | button1_pressed = true; // To be read by the main loop |
hi1000 | 0:765cf978c3e5 | 39 | button1_timeout.attach(callback(button1_enabled_cb), 0.3); // Debounce time 300 ms |
hi1000 | 0:765cf978c3e5 | 40 | } |
hi1000 | 0:765cf978c3e5 | 41 | } |
hi1000 | 0:765cf978c3e5 | 42 | |
hi1000 | 0:765cf978c3e5 | 43 | void print_thread() |
hi1000 | 0:765cf978c3e5 | 44 | { |
hi1000 | 0:765cf978c3e5 | 45 | while (true) { |
hi1000 | 0:765cf978c3e5 | 46 | #if 1 |
hi1000 | 0:765cf978c3e5 | 47 | if(can1.read(msg)) { |
hi1000 | 0:765cf978c3e5 | 48 | print_char(); |
hi1000 | 0:765cf978c3e5 | 49 | printf("got message id=%d 0x%08x\r\n", msg.id, msg.id); |
hi1000 | 0:765cf978c3e5 | 50 | // b = *reinterpret_cast<int*>(msg.data); |
hi1000 | 0:765cf978c3e5 | 51 | b = msg.data[0]; |
hi1000 | 0:765cf978c3e5 | 52 | printf("got data %d 0x%08x \r\n", b, b); |
hi1000 | 0:765cf978c3e5 | 53 | if(msg.id == 1337) { |
hi1000 | 0:765cf978c3e5 | 54 | led2 = !led2; |
hi1000 | 0:765cf978c3e5 | 55 | |
hi1000 | 0:765cf978c3e5 | 56 | b = *reinterpret_cast<int*>(msg.data); |
hi1000 | 0:765cf978c3e5 | 57 | printf("got message %d\r\n", b); |
hi1000 | 0:765cf978c3e5 | 58 | if(b % 5 == 0) |
hi1000 | 0:765cf978c3e5 | 59 | led2 = !led2; |
hi1000 | 0:765cf978c3e5 | 60 | } |
hi1000 | 0:765cf978c3e5 | 61 | } |
hi1000 | 0:765cf978c3e5 | 62 | // wait(0.2); |
hi1000 | 0:765cf978c3e5 | 63 | #endif |
hi1000 | 0:765cf978c3e5 | 64 | } |
hi1000 | 0:765cf978c3e5 | 65 | } |
hi1000 | 0:765cf978c3e5 | 66 | |
hi1000 | 0:765cf978c3e5 | 67 | int main() |
hi1000 | 0:765cf978c3e5 | 68 | { |
hi1000 | 0:765cf978c3e5 | 69 | printf("\n\n*** RTOS basic example ***\n"); |
hi1000 | 0:765cf978c3e5 | 70 | |
hi1000 | 0:765cf978c3e5 | 71 | thread.start(print_thread); |
hi1000 | 0:765cf978c3e5 | 72 | // can1.reset(); |
hi1000 | 0:765cf978c3e5 | 73 | // can2.reset(); |
hi1000 | 0:765cf978c3e5 | 74 | // can1.frequency(100000); |
hi1000 | 0:765cf978c3e5 | 75 | // can2.frequency(100000); |
hi1000 | 0:765cf978c3e5 | 76 | //button1.mode(PullUp); // Activate pull-up |
hi1000 | 0:765cf978c3e5 | 77 | button1.fall(callback(button1_onpressed_cb)); // Attach ISR to handle button press event |
hi1000 | 0:765cf978c3e5 | 78 | |
hi1000 | 0:765cf978c3e5 | 79 | int idx = 0; // Just for printf below |
hi1000 | 0:765cf978c3e5 | 80 | |
hi1000 | 0:765cf978c3e5 | 81 | while(1) { |
hi1000 | 0:765cf978c3e5 | 82 | if (button1_pressed) { // Set when button is pressed |
hi1000 | 0:765cf978c3e5 | 83 | button1_pressed = false; |
hi1000 | 0:765cf978c3e5 | 84 | printf("Button pressed %d\n", idx++); |
hi1000 | 0:765cf978c3e5 | 85 | can1.write(CANMessage(1337, reinterpret_cast<char*>(&a), 1)); |
hi1000 | 0:765cf978c3e5 | 86 | led1 = !led1; |
hi1000 | 0:765cf978c3e5 | 87 | a++; |
hi1000 | 0:765cf978c3e5 | 88 | } |
hi1000 | 0:765cf978c3e5 | 89 | } |
hi1000 | 0:765cf978c3e5 | 90 | #if 0 |
hi1000 | 0:765cf978c3e5 | 91 | while(1) { |
hi1000 | 0:765cf978c3e5 | 92 | // can1.write(CANMessage(1337, reinterpret_cast<char*>(&a), sizeof(a))); |
hi1000 | 0:765cf978c3e5 | 93 | #if |
hi1000 | 0:765cf978c3e5 | 94 | can1.write(CANMessage(1337, reinterpret_cast<char*>(&a), 1)); |
hi1000 | 0:765cf978c3e5 | 95 | #endif |
hi1000 | 0:765cf978c3e5 | 96 | printf("loop a=%d\n", a); |
hi1000 | 0:765cf978c3e5 | 97 | led1 = !led1; |
hi1000 | 0:765cf978c3e5 | 98 | a++; |
hi1000 | 0:765cf978c3e5 | 99 | wait(0.2); |
hi1000 | 0:765cf978c3e5 | 100 | } |
hi1000 | 0:765cf978c3e5 | 101 | #endif |
hi1000 | 0:765cf978c3e5 | 102 | } |