
Finished all tasks of the workshop, just need to do the final task where the main decided which functions to run
main.cpp
- Committer:
- kwstasfane1
- Date:
- 2020-11-17
- Revision:
- 0:6d566e6d9aa7
File content as of revision 0:6d566e6d9aa7:
#include "mbed.h" #include "C12832.h" //LCD display library Serial pc(USBTX,USBRX); //serial port to communicate with pc //PwmOut PWM1(p21); //used for the pwm setup //PwmOut led (LED1); //task5 testing and orages song DigitalIn fire(p14); PwmOut spkr(p26); //pwm directed to pin26 speaker //extra song, Star WARS float frequency [] = {175,175,175,233,349,311,294,523,932,698,622,587,523,932,698,622,587,622,523}; float beat [] = {21,21,21,128,64,21,21,21,128,64,21,21,21,128,64,21,21,21,128}; int length = sizeof(beat)/sizeof(beat[0]); //to get the length of the array //not use it in a main() //oranges and lemons song //float frequency [] = {659, 554, 659, 554, 550, 494, 554, 587, 494, 659, 554, 440}; //frequency array //float beat[] = {1, 1, 1, 1, 1, 0.5, 0.5, 1, 1, 1, 1, 2}; //beat array C12832 lcd(p5, p7, p6, p8, p11); //define lcd pins char getChar(); float getFloat(); //void basicPWM(); //void ledPwm(float); int main() { //Task2.1 //pc.printf("Workshop 6\n\r"); //the \n\r gives new line and cariage return carriage return //Task2.2 /*char c; //this holds the data pc sends while(1) { c = pc.getc(); //gets character from input stream lcd.cls(); lcd.locate(10,10); lcd.printf("%c", c); //display the character }*/ //Task2.3 /*while(1) { float num = getFloat(); //read the formated data as printf prints it lcd.cls(); lcd.locate(10,10); lcd.printf("%4.3f",num); //4.3 decimal accuracy }*/ //basicPWM(); //Task4 /*float brightness = 0.0; char c; while(1) { pc.printf("Control of LED dimmer by host terminal \n\r"); pc.printf("Press 'u'= brighter, 'd'=dimmer\n\r"); pc.printf("%c %1.3f \n \r", c, brightness); c = pc.getc(); if(brightness > 0.2) //set 0.2 upper limit { brightness = 0.2; } else if(brightness < 0.0) //set 0.0 lower limit { brightness = 0.0; } else { if(c == 'u') { brightness = brightness + 0.0001; //brightness increase } if(c == 'd') { brightness = brightness - 0.0001; //brightness decrease } } ledPwm(brightness); }*/ //Task 5, speaker test /*while(1) { for(float i= 2000.0; i<10000.0; i+=100) { spkr.period(1.0/i); //frequency sweep from 2Khz to 10KHz, period=1/f spkr = 0.5; //50% duty cycle wait(0.1); } spkr = 0.0; //nute speaker while(!fire){} }*/ //task5, speaker orange song /*while(1) { for(int i=0; i<= 11; i++) { spkr.period(1.0/frequency[i]); //variable frequency dewpending on song notes spkr = 0.5; //50% duty cycle wait(0.5*beat[i]); //hold for beat period } spkr = 0.0; //mute speaker while(!fire){} }*/ while(1) { for(int i=0; i<length; i++) { spkr.period(1.0/frequency[i]); //convert frequency to time and use it as period spkr = 0.5; //50% duty cycle wait(0.5*beat[i]*0.02); //0.02 is used to adjust the speed of the music } spkr = 0.0; //mute the speaker while(!fire){} } //return 0; } /*void ledPwm(float brightness) //for the led online adjusting { led.period(0.001); //100Hz=T led = brightness; }*/ /*void basicPWM() //for the PWM online adjusting { //create a PWM signal to see on the osciloscope (Pulse = 100Hz and DT= 50% PWM1.period(getFloat()); //set the PWM period to 10ms, used to configure period remotel as well PWM1 = getFloat(); //set the duty cycle to 50% (from 0.0 to 1.0 0% to 100%) }*/ float getFloat() { float num; pc.scanf("%f", &num); return (num); } char getChar() { char c; pc.getc(); return c; }