This is an old revision of the document!
Τσιπάκι:https://os.mbed.com/platforms/FRDM-K64F/
Για να είμαστε σε θέση να χρησιμοποιήσουμε τον μικροεπεξεργαστή ακολουθήσαμε τα εξής βήματα :
Μετά τα παραπάνω βήματα είμαστε έτοιμοι να ξεκινήσουμε.
Για το α ερώτημα της άσκησης 1
#include "mbed.h" DigitalOut myled(LED1); int main() { while(1) { myled = 1; wait(0.2); myled = 0; wait(0.2); } }
β)
#include "mbed.h" int main() { printf("Hello World!"); }
γ)
Για την υλοποίηση του ερωτήματος γ ακολουθήσαμε τα εξής βήματα:
#include "mbed.h" PwmOut r(D7); PwmOut g(D5); PwmOut b(D6); char c; bool isOn = false; int main(){ printf("Press l to turn ON/OFF the led or +,- for dimming control /n"); while(1) { c = getc(stdin); if ( c == 'l' ) { if (isOn){ r = 0; g = 0; b = 0; isOn = false; }else { r = 1; g = 1; b = 1; isOn = true; } }else if ( c == '-' & isOn) { g = g - 0.01; r = r - 0.01; b = b - 0.01; if (g or b or r < 0 ){ r,g,b = 0; } }else if ( c == '+' & isOn){ g = g + 0.01; r = r + 0.01; b = b + 0.01; if (g or b or r > 1 ){ r,g,b = 1; } } } }
δ) Για την υλοποίηση του ερωτήματος χρησιμοποιήσαμε την κλάση InterruptIn της βιβλιοθήκης mbed και συνδέσαμε ενα πλήκτρο. Κάθε φορά που το πλήκτρο σηκώνεται καλούμαι την συνάρτηση increaseAndDisplayCounter η οποία και αυξάνει και τυπώνει τον μετρητή.
#include "mbed.h" cnt = 0; InterruptIn interrupt(D8); void increaseCounter(){ cnt++; printf("Counter = : %d\n", cnt); } int main() { while(1) { interrupt.rise(&increaseAndDisplayCounter); wait(1); } }
ε) Για το ε δηλώσαμε το onboard πλήκτρο σαν InterrruptIn και χρησιμοποιήσαμε την συνάρτηση fall η οποία κάθε φορά που το πλήκτρο πιέζεται ανάβει ή σβήνει το λαμπάκι.
#include "mbed.h" DigitalOut led(LED_RED); InterruptIn onboardButton(SW2); void Pressed() { if (led){ led = 0; } else { led = 1; } } int main() { while (1) { onboardButton.fall(&Pressed); } }
στ) Για την υλοποίση του στ ερωτήματος χρειαστήκαμε την κλάση Ticker η οποία διακόπτει το πρόγραμμα μας με συχνότητα που ορίζουμε εμεις. Φτιάξαμε την συνάρτηση changeRandomColor() που επιλέγει τυχαία χρώμα. Σαν δεύτερο όρισμα στην συνάρτηση βάλαμε την τιμή 1 η οποία είναι η συχνότητα σε second με την οποία γίνεται το interrupt
#include "mbed.h" Ticker timer; PwmOut r(D7); PwmOut g(D5); PwmOut b(D6); float red_random, green_random, blue_random ; void changeRandomColor() { int randomValue = rand() % 3; r,g,b = 0; if (randomValue == 1){ r = 1; }else if (randomValue == 2){ g = 1; } else { b = 1; } red_led = red_random; green_led = green_random; blue_led = blue_random; } int main() { while(1) { timer.attach(&changeRandomColor, 1.0); } }
ζ)Για την χρήση του ποτενσιόμετρου έπρεπε να ακολουθήσουμε τα εξής βήματα:
AnalogIn potentiometer(A3); DigitalOut red(D7); char c; int main() { float potValue ; while(1) { potValue = potentiometer.read(); red = 1; wait(potValue*1000); red = 0; } }
α) Για το ερώτημα α έπρεπε να κάνουμε:
#include "mbed.h" #include "dhry.h" DigitalOut myled(LED1); InterruptIn button(SW2); Timer timer; Serial pc(USBTX, USBRX); //serial channel over HDK USB interface double benchtime, dps; unsigned long loops; void button_pressed() { double DMIPS; double sum_DMIPS = 0; double max_DMIPS = -1000; double avg; int number_of_runs=0; printf("Dhrystone Benchmark Program \n"); timer.start(); while(1) { loops = 0l; myled = 1; timer.reset(); do { Proc0(); loops += LOOPS; benchtime = timer.read(); } while (benchtime <= 10.000); myled = 0; number_of_runs++; dps = (double)loops / benchtime; DMIPS = dps/1757; if(max_DMIPS < DMIPS){ max_DMIPS = DMIPS; } sum_DMIPS += DMIPS; avg = sum_DMIPS/number_of_runs; printf("Dhrystone time for %ld passes = %.3f sek\r\n", loops, benchtime); printf("This machine benchmarks at %.0f DMPIS\r\n", DMIPS); printf("The best DMIPS is : %.0f\r\n", max_DMIPS); printf("The average DMIPS is : %.0f\r\n", avg); wait(1.0); } } int main(int, char**) { button.fall(&button_pressed); while(1){} }
β) Στο ερώτημα αυτό ήταν απαραίτητη η κάρτα SD για την αποθήκευση των txt αρχείων κάτι το οποίο δεν είχαμε στην κατοχή μας με σκοπό να μην μπορέσουμε να υλοποιήσουμε το παραπάνω κομμάτι το οποίο είναι κάτι πολύ απλο αν συμπεριλάβουμε ότι στο α ερώτημα έχουμε τον κώδικα ώστε να τρέξουμε το benchmark όσες φορές επιθυμούμε.
γ) Για το γ ερώτημα χρειαζόματε τα εξής βήματα:
#include "mbed.h" #include "dhry.h" #include "rtos.h" Thread benchThread; Thread potThread; DigitalOut myled(LED1); InterruptIn button(SW2); float potValue ; Timer timer; Serial pc(USBTX, USBRX); //serial channel over HDK USB interface double benchtime, dps; unsigned long loops; void dhrystoneBenchmark() { double DMIPS; double sum_DMIPS = 0; double max_DMIPS = -1000; double avg; int number_of_runs=0; printf("Dhrystone Benchmark Program \n"); timer.start(); for(int i=0; i<10; i++){ loops = 0l; myled = 1; timer.reset(); do { Proc0(); loops += LOOPS; benchtime = timer.read(); } while (benchtime <= 10.000); myled = 0; number_of_runs++; dps = (double)loops / benchtime; DMIPS = dps/1757; if(max_DMIPS < DMIPS){ max_DMIPS = DMIPS; } sum_DMIPS += DMIPS; avg = sum_DMIPS/number_of_runs; printf("Dhrystone time for %ld passes = %.3f sek\r\n", loops, benchtime); printf("This machine benchmarks at %.0f DMPIS\r\n", DMIPS); printf("The best DMIPS is : %.0f\r\n", max_DMIPS); printf("The average DMIPS is : %.0f\r\n", avg); wait(1.0); } } AnalogIn potentiometer(A3); DigitalOut red(D7); void runPotentiometer(){ while(1) { potValue = potentiometer.read(); red = 1; wait(potValue*1000); red = 0; } } int main(int, char**) { dhrystoneBenchmark(); button.fall(&runPotentiometer); while (1){} }
#if DEMO == DEMO_HTTP #include "mbed.h" #include "http_request.h" #include "mbed_mem_trace.h" #include <iostream> using namespace std; void dump_response(HttpResponse* res) { char buffer[3000]; string result; printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str()); printf("Headers:\n"); for (size_t ix = 0; ix < res->get_headers_length(); ix++) { printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str()); } } NetworkInterface *connect_to_default_network_interface() { printf("[Connecting to network...\n"); NetworkInterface* network = NetworkInterface::get_default_instance(); if (!network) { printf(" No network interface found, select an interface in 'network-helper.h'\n"); return NULL; } nsapi_error_t connect_status = network->connect(); if (connect_status != NSAPI_ERROR_OK) { printf(" Failed to connect to network (%d)\n", connect_status); return NULL; } printf("Connected to the network\n"); printf("IP address: %s\n", network->get_ip_address()); return network; } int main() { printf("Program Starting..\n"); // Connect to the network with the default networking interface NetworkInterface* network = connect_to_default_network_interface(); // Do a GET request { // By default the body is automatically parsed and stored in a buffer, this is memory heavy. // To receive chunked response, pass in a callback as last parameter to the constructor. //char request = "http://api.openweathermap.org/data/2.5/forecast?"+city+"&cnt=1&APPID=3ff29328f7c661abef83a312168bc559"; HttpRequest* get_req = new HttpRequest(network, HTTP_GET, "http://api.openweathermap.org/data/2.5/forecast?q=Ioannina&cnt=1&APPID=3ff29328f7c661abef83a312168bc559"); HttpResponse* get_res = get_req->send(); if (!get_res) { printf("HttpRequest failed (error code %d)\n", get_req->get_error()); return 1; } printf("\n----- HTTP GET response -----\n"); dump_response(get_res); delete get_req; } wait(osWaitForever); } #endif