er
Dependencies: mbed oled_ssd1306_1-3inch
Fork of Nucleo_i2c_OLED1234 by
main.cpp@2:45b51e456076, 2017-11-02 (annotated)
- Committer:
- mijimy
- Date:
- Thu Nov 02 08:16:07 2017 +0000
- Revision:
- 2:45b51e456076
- Parent:
- 1:d349d4271837
RSM
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mijimy | 2:45b51e456076 | 1 | |
ittraining | 0:94c1a94c5a47 | 2 | #include "mbed.h" |
ittraining | 0:94c1a94c5a47 | 3 | #include "I2C_SSD1306Z.h" |
mijimy | 1:d349d4271837 | 4 | #include "logo.h" |
ittraining | 0:94c1a94c5a47 | 5 | |
ittraining | 0:94c1a94c5a47 | 6 | I2C i2c(I2C_SDA, I2C_SCL); |
mijimy | 1:d349d4271837 | 7 | |
ittraining | 0:94c1a94c5a47 | 8 | |
mijimy | 2:45b51e456076 | 9 | InterruptIn speedsig(D2); //define and name the interrupt input |
mijimy | 2:45b51e456076 | 10 | InterruptIn rpmsig(D3); //define and name the interrupt input |
mijimy | 2:45b51e456076 | 11 | DigitalOut pulse(D13); |
mijimy | 2:45b51e456076 | 12 | DigitalOut pulse1(D12); |
mijimy | 1:d349d4271837 | 13 | float t_period = 0; // This is the period between interrupts in microseconds |
mijimy | 1:d349d4271837 | 14 | Timer t,tr; |
mijimy | 1:d349d4271837 | 15 | long timing,timingr,oldvalue,oldtiming,speed=0; |
mijimy | 1:d349d4271837 | 16 | int count1=0,count2=0; |
mijimy | 1:d349d4271837 | 17 | //P//wmOut output(led); |
mijimy | 1:d349d4271837 | 18 | void ISR1() { //this is the response to interrupt, i.e. the ISR |
mijimy | 1:d349d4271837 | 19 | if(speed<175) |
mijimy | 1:d349d4271837 | 20 | { pulse=1; |
mijimy | 1:d349d4271837 | 21 | pulse1=1; |
mijimy | 1:d349d4271837 | 22 | } |
mijimy | 1:d349d4271837 | 23 | else |
mijimy | 1:d349d4271837 | 24 | { count1++; |
mijimy | 1:d349d4271837 | 25 | if(count1>1) |
mijimy | 1:d349d4271837 | 26 | { pulse=1; |
mijimy | 1:d349d4271837 | 27 | pulse1=1; |
mijimy | 1:d349d4271837 | 28 | count1=0; |
mijimy | 1:d349d4271837 | 29 | } |
mijimy | 1:d349d4271837 | 30 | } |
mijimy | 1:d349d4271837 | 31 | timing=t.read_us(); |
mijimy | 1:d349d4271837 | 32 | } |
mijimy | 1:d349d4271837 | 33 | void ISR2() { //this is the response to interrupt, i.e. the ISR |
mijimy | 1:d349d4271837 | 34 | if(speed<175) |
mijimy | 1:d349d4271837 | 35 | { pulse=0; |
mijimy | 1:d349d4271837 | 36 | pulse1=0; |
mijimy | 1:d349d4271837 | 37 | } |
mijimy | 1:d349d4271837 | 38 | else |
mijimy | 1:d349d4271837 | 39 | { count2++; |
mijimy | 1:d349d4271837 | 40 | if(count2>1) |
mijimy | 1:d349d4271837 | 41 | { pulse=0; |
mijimy | 1:d349d4271837 | 42 | pulse1=0; |
mijimy | 1:d349d4271837 | 43 | count2=0; |
mijimy | 1:d349d4271837 | 44 | } |
mijimy | 1:d349d4271837 | 45 | } |
mijimy | 1:d349d4271837 | 46 | t.reset(); |
mijimy | 1:d349d4271837 | 47 | } |
mijimy | 1:d349d4271837 | 48 | |
mijimy | 1:d349d4271837 | 49 | void ISR3() { //this is the response to interrupt, i.e. the ISR |
mijimy | 1:d349d4271837 | 50 | timingr=tr.read_us(); |
mijimy | 1:d349d4271837 | 51 | tr.reset(); |
mijimy | 1:d349d4271837 | 52 | } |
ittraining | 0:94c1a94c5a47 | 53 | |
ittraining | 0:94c1a94c5a47 | 54 | int main() |
ittraining | 0:94c1a94c5a47 | 55 | { |
mijimy | 1:d349d4271837 | 56 | char buffer [20]; |
mijimy | 2:45b51e456076 | 57 | speedsig.fall(&ISR1); // attach the address of the ISR function to the |
mijimy | 1:d349d4271837 | 58 | speedsig.rise(&ISR2); // attach the address of the ISR function to the |
mijimy | 1:d349d4271837 | 59 | rpmsig.rise(&ISR3); // attach the address of the ISR function to the |
mijimy | 1:d349d4271837 | 60 | wait(0.5); |
mijimy | 1:d349d4271837 | 61 | t.start(); //start timer |
mijimy | 1:d349d4271837 | 62 | tr.start(); |
mijimy | 1:d349d4271837 | 63 | Init_LCD(); |
mijimy | 1:d349d4271837 | 64 | draw_LCD(NISSAN); |
mijimy | 1:d349d4271837 | 65 | wait(2.0); |
mijimy | 1:d349d4271837 | 66 | clear_LCD(); |
mijimy | 1:d349d4271837 | 67 | print_Line(0, " Speed and Rev"); |
mijimy | 1:d349d4271837 | 68 | print_Line(1, " Meter "); |
mijimy | 1:d349d4271837 | 69 | print_Line(2, " by: Miji"); |
mijimy | 1:d349d4271837 | 70 | print_Line(3, " ************* "); |
mijimy | 1:d349d4271837 | 71 | wait(2.0); |
mijimy | 1:d349d4271837 | 72 | clear_LCD(); |
mijimy | 1:d349d4271837 | 73 | draw_LCD(B13); |
mijimy | 1:d349d4271837 | 74 | wait(2.0); |
mijimy | 1:d349d4271837 | 75 | clear_LCD(); |
mijimy | 1:d349d4271837 | 76 | while(1) { |
mijimy | 1:d349d4271837 | 77 | int rpm=30000000/timingr; |
mijimy | 1:d349d4271837 | 78 | long freq_m=100000000/(timing*4); |
mijimy | 1:d349d4271837 | 79 | speed=(freq_m*63)/1067; |
mijimy | 1:d349d4271837 | 80 | if(tr.read()>0.3f) // timeout |
mijimy | 1:d349d4271837 | 81 | rpm=0; |
mijimy | 1:d349d4271837 | 82 | if(t.read()>0.8f) // timeout |
mijimy | 1:d349d4271837 | 83 | speed=0; |
mijimy | 1:d349d4271837 | 84 | if(speed<2 || speed>300) |
mijimy | 1:d349d4271837 | 85 | speed=0; |
mijimy | 1:d349d4271837 | 86 | sprintf(buffer,"Speed: %3d kmh " ,speed); |
mijimy | 1:d349d4271837 | 87 | print_Line(0, buffer); |
mijimy | 1:d349d4271837 | 88 | if(rpm<300 || rpm >10000) |
mijimy | 1:d349d4271837 | 89 | rpm=0; |
mijimy | 1:d349d4271837 | 90 | sprintf(buffer,"Rev: %5d rpm" ,rpm); |
mijimy | 1:d349d4271837 | 91 | print_Line(2, buffer); |
mijimy | 1:d349d4271837 | 92 | wait(0.3); |
mijimy | 1:d349d4271837 | 93 | |
mijimy | 1:d349d4271837 | 94 | } |
mijimy | 1:d349d4271837 | 95 | } |
ittraining | 0:94c1a94c5a47 | 96 | |
mijimy | 1:d349d4271837 | 97 | /* |
mijimy | 1:d349d4271837 | 98 | int main() |
mijimy | 1:d349d4271837 | 99 | { |
mijimy | 1:d349d4271837 | 100 | confSysClock(); //Configure system clock (72MHz HSE clock, 48MHz USB clock) |
ittraining | 0:94c1a94c5a47 | 101 | |
ittraining | 0:94c1a94c5a47 | 102 | printf("init oled\n"); |
ittraining | 0:94c1a94c5a47 | 103 | |
ittraining | 0:94c1a94c5a47 | 104 | Init_LCD(); |
ittraining | 0:94c1a94c5a47 | 105 | clear_LCD(); |
ittraining | 0:94c1a94c5a47 | 106 | |
ittraining | 0:94c1a94c5a47 | 107 | while (1) { |
ittraining | 0:94c1a94c5a47 | 108 | |
ittraining | 0:94c1a94c5a47 | 109 | |
mijimy | 1:d349d4271837 | 110 | draw_LCD(B13); |
ittraining | 0:94c1a94c5a47 | 111 | wait(2.0); |
ittraining | 0:94c1a94c5a47 | 112 | clear_LCD(); |
ittraining | 0:94c1a94c5a47 | 113 | print_Line(0, "IT Maker Space"); |
ittraining | 0:94c1a94c5a47 | 114 | print_Line(1, "ARM mbed RTOS"); |
ittraining | 0:94c1a94c5a47 | 115 | print_Line(2, "ST Micro"); |
ittraining | 0:94c1a94c5a47 | 116 | print_Line(3, "ittraining.com"); |
ittraining | 0:94c1a94c5a47 | 117 | wait(2.0); |
ittraining | 0:94c1a94c5a47 | 118 | clear_LCD(); |
ittraining | 0:94c1a94c5a47 | 119 | |
ittraining | 0:94c1a94c5a47 | 120 | } |
ittraining | 0:94c1a94c5a47 | 121 | |
mijimy | 1:d349d4271837 | 122 | }*/ |
ittraining | 0:94c1a94c5a47 | 123 | |
ittraining | 0:94c1a94c5a47 | 124 |