
Finished all tasks of the workshop, just need to do the final task where the main decided which functions to run
main.cpp@0:6d566e6d9aa7, 2020-11-17 (annotated)
- Committer:
- kwstasfane1
- Date:
- Tue Nov 17 15:26:43 2020 +0000
- Revision:
- 0:6d566e6d9aa7
Finished all tasks, all i really need to do is to put them together 6.Task4)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kwstasfane1 | 0:6d566e6d9aa7 | 1 | #include "mbed.h" |
kwstasfane1 | 0:6d566e6d9aa7 | 2 | |
kwstasfane1 | 0:6d566e6d9aa7 | 3 | #include "C12832.h" //LCD display library |
kwstasfane1 | 0:6d566e6d9aa7 | 4 | |
kwstasfane1 | 0:6d566e6d9aa7 | 5 | Serial pc(USBTX,USBRX); //serial port to communicate with pc |
kwstasfane1 | 0:6d566e6d9aa7 | 6 | //PwmOut PWM1(p21); //used for the pwm setup |
kwstasfane1 | 0:6d566e6d9aa7 | 7 | //PwmOut led (LED1); |
kwstasfane1 | 0:6d566e6d9aa7 | 8 | |
kwstasfane1 | 0:6d566e6d9aa7 | 9 | //task5 testing and orages song |
kwstasfane1 | 0:6d566e6d9aa7 | 10 | DigitalIn fire(p14); |
kwstasfane1 | 0:6d566e6d9aa7 | 11 | PwmOut spkr(p26); //pwm directed to pin26 speaker |
kwstasfane1 | 0:6d566e6d9aa7 | 12 | |
kwstasfane1 | 0:6d566e6d9aa7 | 13 | |
kwstasfane1 | 0:6d566e6d9aa7 | 14 | //extra song, Star WARS |
kwstasfane1 | 0:6d566e6d9aa7 | 15 | float frequency [] = {175,175,175,233,349,311,294,523,932,698,622,587,523,932,698,622,587,622,523}; |
kwstasfane1 | 0:6d566e6d9aa7 | 16 | float beat [] = {21,21,21,128,64,21,21,21,128,64,21,21,21,128,64,21,21,21,128}; |
kwstasfane1 | 0:6d566e6d9aa7 | 17 | int length = sizeof(beat)/sizeof(beat[0]); //to get the length of the array //not use it in a main() |
kwstasfane1 | 0:6d566e6d9aa7 | 18 | |
kwstasfane1 | 0:6d566e6d9aa7 | 19 | //oranges and lemons song |
kwstasfane1 | 0:6d566e6d9aa7 | 20 | //float frequency [] = {659, 554, 659, 554, 550, 494, 554, 587, 494, 659, 554, 440}; //frequency array |
kwstasfane1 | 0:6d566e6d9aa7 | 21 | //float beat[] = {1, 1, 1, 1, 1, 0.5, 0.5, 1, 1, 1, 1, 2}; //beat array |
kwstasfane1 | 0:6d566e6d9aa7 | 22 | |
kwstasfane1 | 0:6d566e6d9aa7 | 23 | C12832 lcd(p5, p7, p6, p8, p11); //define lcd pins |
kwstasfane1 | 0:6d566e6d9aa7 | 24 | |
kwstasfane1 | 0:6d566e6d9aa7 | 25 | char getChar(); |
kwstasfane1 | 0:6d566e6d9aa7 | 26 | float getFloat(); |
kwstasfane1 | 0:6d566e6d9aa7 | 27 | //void basicPWM(); |
kwstasfane1 | 0:6d566e6d9aa7 | 28 | //void ledPwm(float); |
kwstasfane1 | 0:6d566e6d9aa7 | 29 | |
kwstasfane1 | 0:6d566e6d9aa7 | 30 | |
kwstasfane1 | 0:6d566e6d9aa7 | 31 | int main() |
kwstasfane1 | 0:6d566e6d9aa7 | 32 | { |
kwstasfane1 | 0:6d566e6d9aa7 | 33 | //Task2.1 |
kwstasfane1 | 0:6d566e6d9aa7 | 34 | //pc.printf("Workshop 6\n\r"); //the \n\r gives new line and cariage return carriage return |
kwstasfane1 | 0:6d566e6d9aa7 | 35 | |
kwstasfane1 | 0:6d566e6d9aa7 | 36 | //Task2.2 |
kwstasfane1 | 0:6d566e6d9aa7 | 37 | /*char c; //this holds the data pc sends |
kwstasfane1 | 0:6d566e6d9aa7 | 38 | while(1) |
kwstasfane1 | 0:6d566e6d9aa7 | 39 | { |
kwstasfane1 | 0:6d566e6d9aa7 | 40 | c = pc.getc(); //gets character from input stream |
kwstasfane1 | 0:6d566e6d9aa7 | 41 | lcd.cls(); |
kwstasfane1 | 0:6d566e6d9aa7 | 42 | lcd.locate(10,10); |
kwstasfane1 | 0:6d566e6d9aa7 | 43 | lcd.printf("%c", c); //display the character |
kwstasfane1 | 0:6d566e6d9aa7 | 44 | }*/ |
kwstasfane1 | 0:6d566e6d9aa7 | 45 | |
kwstasfane1 | 0:6d566e6d9aa7 | 46 | //Task2.3 |
kwstasfane1 | 0:6d566e6d9aa7 | 47 | /*while(1) |
kwstasfane1 | 0:6d566e6d9aa7 | 48 | { |
kwstasfane1 | 0:6d566e6d9aa7 | 49 | float num = getFloat(); //read the formated data as printf prints it |
kwstasfane1 | 0:6d566e6d9aa7 | 50 | lcd.cls(); |
kwstasfane1 | 0:6d566e6d9aa7 | 51 | lcd.locate(10,10); |
kwstasfane1 | 0:6d566e6d9aa7 | 52 | lcd.printf("%4.3f",num); //4.3 decimal accuracy |
kwstasfane1 | 0:6d566e6d9aa7 | 53 | }*/ |
kwstasfane1 | 0:6d566e6d9aa7 | 54 | //basicPWM(); |
kwstasfane1 | 0:6d566e6d9aa7 | 55 | |
kwstasfane1 | 0:6d566e6d9aa7 | 56 | //Task4 |
kwstasfane1 | 0:6d566e6d9aa7 | 57 | /*float brightness = 0.0; |
kwstasfane1 | 0:6d566e6d9aa7 | 58 | char c; |
kwstasfane1 | 0:6d566e6d9aa7 | 59 | |
kwstasfane1 | 0:6d566e6d9aa7 | 60 | while(1) |
kwstasfane1 | 0:6d566e6d9aa7 | 61 | { |
kwstasfane1 | 0:6d566e6d9aa7 | 62 | pc.printf("Control of LED dimmer by host terminal \n\r"); |
kwstasfane1 | 0:6d566e6d9aa7 | 63 | pc.printf("Press 'u'= brighter, 'd'=dimmer\n\r"); |
kwstasfane1 | 0:6d566e6d9aa7 | 64 | pc.printf("%c %1.3f \n \r", c, brightness); |
kwstasfane1 | 0:6d566e6d9aa7 | 65 | |
kwstasfane1 | 0:6d566e6d9aa7 | 66 | c = pc.getc(); |
kwstasfane1 | 0:6d566e6d9aa7 | 67 | |
kwstasfane1 | 0:6d566e6d9aa7 | 68 | if(brightness > 0.2) //set 0.2 upper limit |
kwstasfane1 | 0:6d566e6d9aa7 | 69 | { |
kwstasfane1 | 0:6d566e6d9aa7 | 70 | brightness = 0.2; |
kwstasfane1 | 0:6d566e6d9aa7 | 71 | } |
kwstasfane1 | 0:6d566e6d9aa7 | 72 | else if(brightness < 0.0) //set 0.0 lower limit |
kwstasfane1 | 0:6d566e6d9aa7 | 73 | { |
kwstasfane1 | 0:6d566e6d9aa7 | 74 | brightness = 0.0; |
kwstasfane1 | 0:6d566e6d9aa7 | 75 | } |
kwstasfane1 | 0:6d566e6d9aa7 | 76 | else |
kwstasfane1 | 0:6d566e6d9aa7 | 77 | { |
kwstasfane1 | 0:6d566e6d9aa7 | 78 | if(c == 'u') |
kwstasfane1 | 0:6d566e6d9aa7 | 79 | { |
kwstasfane1 | 0:6d566e6d9aa7 | 80 | brightness = brightness + 0.0001; //brightness increase |
kwstasfane1 | 0:6d566e6d9aa7 | 81 | } |
kwstasfane1 | 0:6d566e6d9aa7 | 82 | if(c == 'd') |
kwstasfane1 | 0:6d566e6d9aa7 | 83 | { |
kwstasfane1 | 0:6d566e6d9aa7 | 84 | brightness = brightness - 0.0001; //brightness decrease |
kwstasfane1 | 0:6d566e6d9aa7 | 85 | } |
kwstasfane1 | 0:6d566e6d9aa7 | 86 | } |
kwstasfane1 | 0:6d566e6d9aa7 | 87 | |
kwstasfane1 | 0:6d566e6d9aa7 | 88 | ledPwm(brightness); |
kwstasfane1 | 0:6d566e6d9aa7 | 89 | |
kwstasfane1 | 0:6d566e6d9aa7 | 90 | |
kwstasfane1 | 0:6d566e6d9aa7 | 91 | }*/ |
kwstasfane1 | 0:6d566e6d9aa7 | 92 | |
kwstasfane1 | 0:6d566e6d9aa7 | 93 | //Task 5, speaker test |
kwstasfane1 | 0:6d566e6d9aa7 | 94 | /*while(1) |
kwstasfane1 | 0:6d566e6d9aa7 | 95 | { |
kwstasfane1 | 0:6d566e6d9aa7 | 96 | for(float i= 2000.0; i<10000.0; i+=100) |
kwstasfane1 | 0:6d566e6d9aa7 | 97 | { |
kwstasfane1 | 0:6d566e6d9aa7 | 98 | spkr.period(1.0/i); //frequency sweep from 2Khz to 10KHz, period=1/f |
kwstasfane1 | 0:6d566e6d9aa7 | 99 | spkr = 0.5; //50% duty cycle |
kwstasfane1 | 0:6d566e6d9aa7 | 100 | wait(0.1); |
kwstasfane1 | 0:6d566e6d9aa7 | 101 | } |
kwstasfane1 | 0:6d566e6d9aa7 | 102 | |
kwstasfane1 | 0:6d566e6d9aa7 | 103 | spkr = 0.0; //nute speaker |
kwstasfane1 | 0:6d566e6d9aa7 | 104 | while(!fire){} |
kwstasfane1 | 0:6d566e6d9aa7 | 105 | }*/ |
kwstasfane1 | 0:6d566e6d9aa7 | 106 | |
kwstasfane1 | 0:6d566e6d9aa7 | 107 | //task5, speaker orange song |
kwstasfane1 | 0:6d566e6d9aa7 | 108 | /*while(1) |
kwstasfane1 | 0:6d566e6d9aa7 | 109 | { |
kwstasfane1 | 0:6d566e6d9aa7 | 110 | for(int i=0; i<= 11; i++) |
kwstasfane1 | 0:6d566e6d9aa7 | 111 | { |
kwstasfane1 | 0:6d566e6d9aa7 | 112 | spkr.period(1.0/frequency[i]); //variable frequency dewpending on song notes |
kwstasfane1 | 0:6d566e6d9aa7 | 113 | spkr = 0.5; //50% duty cycle |
kwstasfane1 | 0:6d566e6d9aa7 | 114 | wait(0.5*beat[i]); //hold for beat period |
kwstasfane1 | 0:6d566e6d9aa7 | 115 | } |
kwstasfane1 | 0:6d566e6d9aa7 | 116 | |
kwstasfane1 | 0:6d566e6d9aa7 | 117 | spkr = 0.0; //mute speaker |
kwstasfane1 | 0:6d566e6d9aa7 | 118 | while(!fire){} |
kwstasfane1 | 0:6d566e6d9aa7 | 119 | }*/ |
kwstasfane1 | 0:6d566e6d9aa7 | 120 | |
kwstasfane1 | 0:6d566e6d9aa7 | 121 | |
kwstasfane1 | 0:6d566e6d9aa7 | 122 | while(1) |
kwstasfane1 | 0:6d566e6d9aa7 | 123 | { |
kwstasfane1 | 0:6d566e6d9aa7 | 124 | for(int i=0; i<length; i++) |
kwstasfane1 | 0:6d566e6d9aa7 | 125 | { |
kwstasfane1 | 0:6d566e6d9aa7 | 126 | spkr.period(1.0/frequency[i]); //convert frequency to time and use it as period |
kwstasfane1 | 0:6d566e6d9aa7 | 127 | spkr = 0.5; //50% duty cycle |
kwstasfane1 | 0:6d566e6d9aa7 | 128 | wait(0.5*beat[i]*0.02); //0.02 is used to adjust the speed of the music |
kwstasfane1 | 0:6d566e6d9aa7 | 129 | } |
kwstasfane1 | 0:6d566e6d9aa7 | 130 | |
kwstasfane1 | 0:6d566e6d9aa7 | 131 | spkr = 0.0; //mute the speaker |
kwstasfane1 | 0:6d566e6d9aa7 | 132 | while(!fire){} |
kwstasfane1 | 0:6d566e6d9aa7 | 133 | } |
kwstasfane1 | 0:6d566e6d9aa7 | 134 | |
kwstasfane1 | 0:6d566e6d9aa7 | 135 | //return 0; |
kwstasfane1 | 0:6d566e6d9aa7 | 136 | |
kwstasfane1 | 0:6d566e6d9aa7 | 137 | |
kwstasfane1 | 0:6d566e6d9aa7 | 138 | } |
kwstasfane1 | 0:6d566e6d9aa7 | 139 | |
kwstasfane1 | 0:6d566e6d9aa7 | 140 | /*void ledPwm(float brightness) //for the led online adjusting |
kwstasfane1 | 0:6d566e6d9aa7 | 141 | { |
kwstasfane1 | 0:6d566e6d9aa7 | 142 | led.period(0.001); //100Hz=T |
kwstasfane1 | 0:6d566e6d9aa7 | 143 | led = brightness; |
kwstasfane1 | 0:6d566e6d9aa7 | 144 | }*/ |
kwstasfane1 | 0:6d566e6d9aa7 | 145 | |
kwstasfane1 | 0:6d566e6d9aa7 | 146 | /*void basicPWM() //for the PWM online adjusting |
kwstasfane1 | 0:6d566e6d9aa7 | 147 | { |
kwstasfane1 | 0:6d566e6d9aa7 | 148 | //create a PWM signal to see on the osciloscope (Pulse = 100Hz and DT= 50% |
kwstasfane1 | 0:6d566e6d9aa7 | 149 | PWM1.period(getFloat()); //set the PWM period to 10ms, used to configure period remotel as well |
kwstasfane1 | 0:6d566e6d9aa7 | 150 | PWM1 = getFloat(); //set the duty cycle to 50% (from 0.0 to 1.0 0% to 100%) |
kwstasfane1 | 0:6d566e6d9aa7 | 151 | }*/ |
kwstasfane1 | 0:6d566e6d9aa7 | 152 | |
kwstasfane1 | 0:6d566e6d9aa7 | 153 | float getFloat() |
kwstasfane1 | 0:6d566e6d9aa7 | 154 | { |
kwstasfane1 | 0:6d566e6d9aa7 | 155 | float num; |
kwstasfane1 | 0:6d566e6d9aa7 | 156 | pc.scanf("%f", &num); |
kwstasfane1 | 0:6d566e6d9aa7 | 157 | return (num); |
kwstasfane1 | 0:6d566e6d9aa7 | 158 | } |
kwstasfane1 | 0:6d566e6d9aa7 | 159 | |
kwstasfane1 | 0:6d566e6d9aa7 | 160 | char getChar() |
kwstasfane1 | 0:6d566e6d9aa7 | 161 | { |
kwstasfane1 | 0:6d566e6d9aa7 | 162 | char c; |
kwstasfane1 | 0:6d566e6d9aa7 | 163 | pc.getc(); |
kwstasfane1 | 0:6d566e6d9aa7 | 164 | return c; |
kwstasfane1 | 0:6d566e6d9aa7 | 165 | } |