First laboratory uses each of the five routines with your modifications. These routines illustrate digital output, PWM output, and Analog input and output.

Committer:
billcooke51
Date:
Mon Oct 01 01:51:02 2018 +0000
Revision:
68:61bde79fc489
Parent:
67:9015fa015c73
Child:
69:9c1bb616a393
Four working routines. Only first compiles unless you change the commenting to compile a different routine.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Jonathan Austin 0:2757d7abb7d9 1 #include "mbed.h"
Jonathan Austin 0:2757d7abb7d9 2
billcooke51 68:61bde79fc489 3 // In the sections below, change
billcooke51 68:61bde79fc489 4 // /* to // and */ to // to make a program active
Jonathan Austin 0:2757d7abb7d9 5
billcooke51 68:61bde79fc489 6 // PROGRAM #1
billcooke51 68:61bde79fc489 7 //
billcooke51 68:61bde79fc489 8 // This controls two digital out lines connected to LEDs
billcooke51 68:61bde79fc489 9 // and two others that go outon pins 21 and 22
billcooke51 68:61bde79fc489 10 // The program alternates these lines with a 2 second delay;
billcooke51 68:61bde79fc489 11 DigitalOut led1(LED1); // Controls LED1
billcooke51 68:61bde79fc489 12 DigitalOut led2(LED2); // Controls LED2
billcooke51 68:61bde79fc489 13 DigitalOut d1(p21); // Controls pin 21
billcooke51 68:61bde79fc489 14 DigitalOut d2(p22); // Controls pin 22
billcooke51 68:61bde79fc489 15
billcooke51 68:61bde79fc489 16 bool valOut = false; //logical variable to flip on and off
Tyari21 67:9015fa015c73 17
Tyari21 67:9015fa015c73 18 int main(void){
billcooke51 68:61bde79fc489 19 while(1){
billcooke51 68:61bde79fc489 20 led1 = valOut;
billcooke51 68:61bde79fc489 21 d1 = valOut;
billcooke51 68:61bde79fc489 22 led2 = !valOut;
billcooke51 68:61bde79fc489 23 d2 = !valOut;
billcooke51 68:61bde79fc489 24 valOut = !valOut;
billcooke51 68:61bde79fc489 25 wait(2.); // wait 2 seconds. Use wait_ms or wait_us using integers of milli or micro seconds
billcooke51 68:61bde79fc489 26 }
billcooke51 68:61bde79fc489 27 }
billcooke51 68:61bde79fc489 28 //
billcooke51 68:61bde79fc489 29
billcooke51 68:61bde79fc489 30 // PROGRAM #2
billcooke51 68:61bde79fc489 31 /*
billcooke51 68:61bde79fc489 32 // This program uses PWM to vary the LED brightness
billcooke51 68:61bde79fc489 33 PwmOut led1(LED1); // PWM output will control LED1
billcooke51 68:61bde79fc489 34 PwmOut led2(LED2); // PWM output will control LED2
billcooke51 68:61bde79fc489 35 // Could use PwmOut a1(p21) through p(26)
billcooke51 68:61bde79fc489 36 float brightness1 = 0.1;
billcooke51 68:61bde79fc489 37 float brightness2 = 0.5;
billcooke51 68:61bde79fc489 38
billcooke51 68:61bde79fc489 39 int main() {
billcooke51 68:61bde79fc489 40 // Initialize
billcooke51 68:61bde79fc489 41 led1.period(0.01); // period in seconds both outputs use the same period
billcooke51 68:61bde79fc489 42 // Could use led.period_ms (for milliseconds) or led.period_us (for microseconds)
billcooke51 68:61bde79fc489 43 // Could use led.pulsewidth (or led.pulsewidth_ms, led.pulsewidth_us) to set the on width for a given frequency
billcooke51 68:61bde79fc489 44
billcooke51 68:61bde79fc489 45 while(1) {
billcooke51 68:61bde79fc489 46 led1 = brightness1; // set the fraction of period on
billcooke51 68:61bde79fc489 47 led2 = brightness2; // set the fraction of period on
billcooke51 68:61bde79fc489 48 // Could use led.write(brightness);
Jonathan Austin 0:2757d7abb7d9 49 }
Jonathan Austin 0:2757d7abb7d9 50 }
billcooke51 68:61bde79fc489 51 */
Jonathan Austin 1:846c97078558 52
billcooke51 68:61bde79fc489 53 // PROGRAM #3
billcooke51 68:61bde79fc489 54 /*
billcooke51 68:61bde79fc489 55 // This program writes out a sine wave
billcooke51 68:61bde79fc489 56 AnalogOut aout(p18);
billcooke51 68:61bde79fc489 57 int main(void){
billcooke51 68:61bde79fc489 58 while(1){
billcooke51 68:61bde79fc489 59 for (int k = 0; k < 32; k++){
billcooke51 68:61bde79fc489 60 aout = (1+sin(3.14159*k/16.))/2.;
billcooke51 68:61bde79fc489 61 }
billcooke51 68:61bde79fc489 62 }
billcooke51 68:61bde79fc489 63 }
billcooke51 68:61bde79fc489 64 */
billcooke51 68:61bde79fc489 65
billcooke51 68:61bde79fc489 66
billcooke51 68:61bde79fc489 67 // PROGRAM #4
billcooke51 68:61bde79fc489 68 /*
billcooke51 68:61bde79fc489 69 // This program writes out a sine wave quickly
billcooke51 68:61bde79fc489 70 int main(void){
billcooke51 68:61bde79fc489 71 // Define an array to hold the function values
billcooke51 68:61bde79fc489 72 // Change the 32 if you want more or fewer values
billcooke51 68:61bde79fc489 73 float temp[32];
billcooke51 68:61bde79fc489 74 // If you calculate the values before you output them
billcooke51 68:61bde79fc489 75 // the wave can be much faster
billcooke51 68:61bde79fc489 76 for (int k = 0; k < 32; k++){
billcooke51 68:61bde79fc489 77 temp[k] = (1+sin(3.14159*k/16.))/2.;
billcooke51 68:61bde79fc489 78 }
billcooke51 68:61bde79fc489 79 while(1){
billcooke51 68:61bde79fc489 80 for (int k = 0; k < 32; k++){
billcooke51 68:61bde79fc489 81 aout = temp[k];
billcooke51 68:61bde79fc489 82 }
billcooke51 68:61bde79fc489 83 }
billcooke51 68:61bde79fc489 84 }
billcooke51 68:61bde79fc489 85 */