Hello World for DigitalOut

Committer:
elelthvd
Date:
Thu Aug 27 16:15:27 2020 +0800
Revision:
12:6a944faf5dd8
Parent:
11:759df8b71f32
Use "\r\n", add three other methods of blinking

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbedAustin 4:340f9fb00d71 1 /* mbed Example Program
mbedAustin 4:340f9fb00d71 2 * Copyright (c) 2006-2014 ARM Limited
mbedAustin 4:340f9fb00d71 3 *
mbedAustin 4:340f9fb00d71 4 * Licensed under the Apache License, Version 2.0 (the "License");
mbedAustin 4:340f9fb00d71 5 * you may not use this file except in compliance with the License.
mbedAustin 4:340f9fb00d71 6 * You may obtain a copy of the License at
mbedAustin 4:340f9fb00d71 7 *
mbedAustin 4:340f9fb00d71 8 * http://www.apache.org/licenses/LICENSE-2.0
mbedAustin 4:340f9fb00d71 9 *
mbedAustin 4:340f9fb00d71 10 * Unless required by applicable law or agreed to in writing, software
mbedAustin 4:340f9fb00d71 11 * distributed under the License is distributed on an "AS IS" BASIS,
mbedAustin 4:340f9fb00d71 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbedAustin 4:340f9fb00d71 13 * See the License for the specific language governing permissions and
mbedAustin 4:340f9fb00d71 14 * limitations under the License.
mbedAustin 4:340f9fb00d71 15 */
mbed_official 0:b5a9e0614efd 16 #include "mbed.h"
mbed_official 0:b5a9e0614efd 17
mbed_official 0:b5a9e0614efd 18 DigitalOut myled(LED1);
mbed_official 0:b5a9e0614efd 19
elelthvd 12:6a944faf5dd8 20 // For approach Three
elelthvd 12:6a944faf5dd8 21 Ticker blinkticker;
elelthvd 12:6a944faf5dd8 22 void blink()
elelthvd 12:6a944faf5dd8 23 {
elelthvd 12:6a944faf5dd8 24 myled = !myled;
elelthvd 12:6a944faf5dd8 25 printf("myled = %d (blinkticker)\r\n", (uint8_t) myled );
elelthvd 12:6a944faf5dd8 26 }
elelthvd 12:6a944faf5dd8 27
elelthvd 12:6a944faf5dd8 28 // For approach Four: // A class for blink()-ing a DigitalOut
elelthvd 12:6a944faf5dd8 29 class Blinker {
elelthvd 12:6a944faf5dd8 30 public:
elelthvd 12:6a944faf5dd8 31 Blinker(PinName pin) : _pin(pin)
elelthvd 12:6a944faf5dd8 32 {
elelthvd 12:6a944faf5dd8 33 _pin = 0;
elelthvd 12:6a944faf5dd8 34 }
elelthvd 12:6a944faf5dd8 35 void blink()
elelthvd 12:6a944faf5dd8 36 {
elelthvd 12:6a944faf5dd8 37 _pin = !_pin;
elelthvd 12:6a944faf5dd8 38 printf("_pin = %d (Blinker::blink)\r\n", (uint8_t) _pin );
elelthvd 12:6a944faf5dd8 39 }
elelthvd 12:6a944faf5dd8 40 private:
elelthvd 12:6a944faf5dd8 41 DigitalOut _pin;
elelthvd 12:6a944faf5dd8 42 };
elelthvd 12:6a944faf5dd8 43 Blinker myblinker(LED2);
elelthvd 12:6a944faf5dd8 44
elelthvd 12:6a944faf5dd8 45 // For approach Five
elelthvd 12:6a944faf5dd8 46 PwmOut mypwm(PWM_OUT);
elelthvd 12:6a944faf5dd8 47 InterruptIn myint(D10); // Manually Wire D10 to PWM_OUT (D9)
elelthvd 12:6a944faf5dd8 48 void blinkinterrupt()
elelthvd 12:6a944faf5dd8 49 {
elelthvd 12:6a944faf5dd8 50 myled = !myled;
elelthvd 12:6a944faf5dd8 51 printf("myled = %d (blinkinterrupt)\r\n", (uint8_t) myled );
elelthvd 12:6a944faf5dd8 52 }
elelthvd 12:6a944faf5dd8 53
mbedAustin 7:21a83a7b8f94 54 int main()
mbedAustin 7:21a83a7b8f94 55 {
mbedAustin 6:4b14e87b65e9 56 // check that myled object is initialized and connected to a pin
mbedAustin 7:21a83a7b8f94 57 if(myled.is_connected()) {
elelthvd 12:6a944faf5dd8 58 printf("myled is initialized and connected!\r\n");
mbedAustin 5:e17647bbb68e 59 }
mbedAustin 7:21a83a7b8f94 60
mbedAustin 6:4b14e87b65e9 61 // Blink LED
elelthvd 12:6a944faf5dd8 62 for(int i=0; i<3; i++) {
elelthvd 12:6a944faf5dd8 63
elelthvd 12:6a944faf5dd8 64 // one way
mbedAustin 3:29debdbea629 65 myled = 1; // set LED1 pin to high
elelthvd 12:6a944faf5dd8 66 printf("myled = %d (assign)\r\n", (uint8_t) myled );
elelthvd 11:759df8b71f32 67 ThisThread::sleep_for(500ms);
mbedAustin 7:21a83a7b8f94 68
elelthvd 12:6a944faf5dd8 69 // another method
mbedAustin 3:29debdbea629 70 myled.write(0); // set LED1 pin to low
elelthvd 12:6a944faf5dd8 71 printf("myled = %d (write())\r\n",myled.read() );
elelthvd 11:759df8b71f32 72 ThisThread::sleep_for(500ms);
mbed_official 0:b5a9e0614efd 73 }
elelthvd 12:6a944faf5dd8 74
elelthvd 12:6a944faf5dd8 75 // A third blinker
elelthvd 12:6a944faf5dd8 76 blinkticker.attach(&blink, 500ms); // call function every 500ms
elelthvd 12:6a944faf5dd8 77 ThisThread::sleep_for(2500ms); // Let the main thread rest ...
elelthvd 12:6a944faf5dd8 78 blinkticker.detach();
elelthvd 12:6a944faf5dd8 79
elelthvd 12:6a944faf5dd8 80 // A fourth blinker
elelthvd 12:6a944faf5dd8 81 // the address of the object, member function, and interval
elelthvd 12:6a944faf5dd8 82 blinkticker.attach(callback(&myblinker, &Blinker::blink), 500ms);
elelthvd 12:6a944faf5dd8 83 ThisThread::sleep_for(2500ms); // Let the main thread rest ...
elelthvd 12:6a944faf5dd8 84 blinkticker.detach();
elelthvd 12:6a944faf5dd8 85
elelthvd 12:6a944faf5dd8 86 // A fifth blinker
elelthvd 12:6a944faf5dd8 87 blinkinterrupt(); // Test function works
elelthvd 12:6a944faf5dd8 88 myint.rise(callback(&blinkinterrupt)); // Start listening on rise events
elelthvd 12:6a944faf5dd8 89 mypwm.period_ms(500); // PWM period is 500ms
elelthvd 12:6a944faf5dd8 90 mypwm.write(0.5); // Duty Cycle: 50% HIGH time.
elelthvd 12:6a944faf5dd8 91
elelthvd 12:6a944faf5dd8 92 printf("Good Bye.\r\n");
elelthvd 12:6a944faf5dd8 93 while (true) {
elelthvd 12:6a944faf5dd8 94 ThisThread::sleep_for(1h); // Zzz
elelthvd 12:6a944faf5dd8 95 }
elelthvd 12:6a944faf5dd8 96
mbed_official 0:b5a9e0614efd 97 }