![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
-
Dependencies: mbed DS3231 Thermistor
main.cpp@0:60316223b876, 2019-08-12 (annotated)
- Committer:
- attila0970
- Date:
- Mon Aug 12 11:11:12 2019 +0000
- Revision:
- 0:60316223b876
asd;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
attila0970 | 0:60316223b876 | 1 | #include "mbed.h" |
attila0970 | 0:60316223b876 | 2 | #include "DS3231.h" |
attila0970 | 0:60316223b876 | 3 | #include "Thermistor.h" |
attila0970 | 0:60316223b876 | 4 | |
attila0970 | 0:60316223b876 | 5 | |
attila0970 | 0:60316223b876 | 6 | #define read_addr 0xa0 |
attila0970 | 0:60316223b876 | 7 | #define write_addr 0xa1 |
attila0970 | 0:60316223b876 | 8 | |
attila0970 | 0:60316223b876 | 9 | |
attila0970 | 0:60316223b876 | 10 | |
attila0970 | 0:60316223b876 | 11 | I2C flash(PB_3, PB_10); |
attila0970 | 0:60316223b876 | 12 | DS3231 rtc(PB_9, PB_8); |
attila0970 | 0:60316223b876 | 13 | |
attila0970 | 0:60316223b876 | 14 | AnalogIn bed(PB_0); |
attila0970 | 0:60316223b876 | 15 | |
attila0970 | 0:60316223b876 | 16 | InterruptIn butt(PB_5); |
attila0970 | 0:60316223b876 | 17 | |
attila0970 | 0:60316223b876 | 18 | //DigitalOut led(PA_4); |
attila0970 | 0:60316223b876 | 19 | DigitalOut green(PA_0); |
attila0970 | 0:60316223b876 | 20 | DigitalOut red(PA_1); |
attila0970 | 0:60316223b876 | 21 | |
attila0970 | 0:60316223b876 | 22 | Serial pc(USBTX, USBRX); |
attila0970 | 0:60316223b876 | 23 | |
attila0970 | 0:60316223b876 | 24 | void page_write(char addr1, char addr2, char data); |
attila0970 | 0:60316223b876 | 25 | void page_read(char addr1, char addr2); |
attila0970 | 0:60316223b876 | 26 | void write_byte(char addr1, char addr2, char data); |
attila0970 | 0:60316223b876 | 27 | char read_byte(char addr1, char addr2, int range); |
attila0970 | 0:60316223b876 | 28 | |
attila0970 | 0:60316223b876 | 29 | void callback(); |
attila0970 | 0:60316223b876 | 30 | |
attila0970 | 0:60316223b876 | 31 | int conv_hex_to_int(char data); |
attila0970 | 0:60316223b876 | 32 | |
attila0970 | 0:60316223b876 | 33 | char buff[258]; |
attila0970 | 0:60316223b876 | 34 | char data[256]; |
attila0970 | 0:60316223b876 | 35 | |
attila0970 | 0:60316223b876 | 36 | int hour; |
attila0970 | 0:60316223b876 | 37 | int minute; |
attila0970 | 0:60316223b876 | 38 | int second; |
attila0970 | 0:60316223b876 | 39 | int butt_flag = 0; |
attila0970 | 0:60316223b876 | 40 | |
attila0970 | 0:60316223b876 | 41 | double temp_buff; |
attila0970 | 0:60316223b876 | 42 | float temp; |
attila0970 | 0:60316223b876 | 43 | double r; |
attila0970 | 0:60316223b876 | 44 | double temp_tizedes; |
attila0970 | 0:60316223b876 | 45 | |
attila0970 | 0:60316223b876 | 46 | int location = 2; |
attila0970 | 0:60316223b876 | 47 | int eddmegafaszom = 0; |
attila0970 | 0:60316223b876 | 48 | |
attila0970 | 0:60316223b876 | 49 | int main() { |
attila0970 | 0:60316223b876 | 50 | flash.frequency(100000); |
attila0970 | 0:60316223b876 | 51 | rtc.setI2Cfrequency(400000); |
attila0970 | 0:60316223b876 | 52 | pc.baud(115200); |
attila0970 | 0:60316223b876 | 53 | green = 1; |
attila0970 | 0:60316223b876 | 54 | red = 0; |
attila0970 | 0:60316223b876 | 55 | |
attila0970 | 0:60316223b876 | 56 | butt.fall(&callback); |
attila0970 | 0:60316223b876 | 57 | |
attila0970 | 0:60316223b876 | 58 | //pc.printf("\nI am ready! %02X %02X\n", 0x1ff >> 8, 256 >> 8); |
attila0970 | 0:60316223b876 | 59 | |
attila0970 | 0:60316223b876 | 60 | location = 2; //eeprom aktuális memória címnek a helye |
attila0970 | 0:60316223b876 | 61 | while(1) { |
attila0970 | 0:60316223b876 | 62 | //gomb lenyomás után |
attila0970 | 0:60316223b876 | 63 | if(butt_flag != 0){ //gomb megnyomásához rendelt esemény |
attila0970 | 0:60316223b876 | 64 | red = 1; |
attila0970 | 0:60316223b876 | 65 | green = 1; |
attila0970 | 0:60316223b876 | 66 | read_byte(0x00, 0x00, location); //"location" változó mennyiségében kiolvassa az adatokat az eepromból, és eltárolja majd a data tömbben. Ez a végén lévő függvényben valósul meg. |
attila0970 | 0:60316223b876 | 67 | if (location >= 750){ //soros port lefagyása elleni védekező mechanizmus elje, ez bontja fel több max 750 db-os részre a data tömb elemeit. |
attila0970 | 0:60316223b876 | 68 | |
attila0970 | 0:60316223b876 | 69 | for (int i = 0; i < location/750; i++){ |
attila0970 | 0:60316223b876 | 70 | for (int j = i*750; j < (i + 1) * 750; j += 4){ |
attila0970 | 0:60316223b876 | 71 | pc.printf("%d:%d;%d.%d \n", data[i], data[i+1], data[i+2], data[i+3]); |
attila0970 | 0:60316223b876 | 72 | wait_ms(10); |
attila0970 | 0:60316223b876 | 73 | } |
attila0970 | 0:60316223b876 | 74 | //wait_ms(50); |
attila0970 | 0:60316223b876 | 75 | } |
attila0970 | 0:60316223b876 | 76 | for (int i = location-(location%750); i <= location; i+=4){ |
attila0970 | 0:60316223b876 | 77 | |
attila0970 | 0:60316223b876 | 78 | pc.printf("%d:%d;%d.%d \n", data[i], data[i+1], data[i+2], data[i+3]); |
attila0970 | 0:60316223b876 | 79 | } |
attila0970 | 0:60316223b876 | 80 | } |
attila0970 | 0:60316223b876 | 81 | else{ //ez a ciklus csak simán kitolja soros portra az adatokat, akkor lép bele, ha nincs 750 mért adat. |
attila0970 | 0:60316223b876 | 82 | for (int i = 2; i < location; i += 4){ |
attila0970 | 0:60316223b876 | 83 | pc.printf("%d:%d;%d.%d \n", data[i], data[i+1], data[i+2], data[i+3]); |
attila0970 | 0:60316223b876 | 84 | wait_ms(10); |
attila0970 | 0:60316223b876 | 85 | } |
attila0970 | 0:60316223b876 | 86 | |
attila0970 | 0:60316223b876 | 87 | wait(0.5); |
attila0970 | 0:60316223b876 | 88 | location = 2; //eeprom kihasználtságát kezdi előről, az elejétől kezdi újra felülírni az eepromot. |
attila0970 | 0:60316223b876 | 89 | wait(0.1); |
attila0970 | 0:60316223b876 | 90 | butt_flag = 0; //gomb lenyomása utáni állapotot állítja vissza alap helyzetbe. |
attila0970 | 0:60316223b876 | 91 | red = 0; |
attila0970 | 0:60316223b876 | 92 | green = 1; |
attila0970 | 0:60316223b876 | 93 | rtc.setTime(0, 0, 0); //rtc nullázása |
attila0970 | 0:60316223b876 | 94 | } |
attila0970 | 0:60316223b876 | 95 | //fő ismétlődő ciklus eleje |
attila0970 | 0:60316223b876 | 96 | green = !green; //led bizgerálás |
attila0970 | 0:60316223b876 | 97 | //************************************************************************************* |
attila0970 | 0:60316223b876 | 98 | wait(20); //itt állítod be a mintavételi sebeggéget!!!!!! |
attila0970 | 0:60316223b876 | 99 | //************************************************************************************* |
attila0970 | 0:60316223b876 | 100 | rtc.readTime(&hour,&minute,&second); //kolvasod az időt az rtc-ből |
attila0970 | 0:60316223b876 | 101 | |
attila0970 | 0:60316223b876 | 102 | double val=bed; //* |
attila0970 | 0:60316223b876 | 103 | double r=((1-val)/val)*4700; //hőmérségkelt meghatározása |
attila0970 | 0:60316223b876 | 104 | temp = (1/( log(r/10000) /3950 + 1/(25+273.15))-273.15)-20.0f; //* |
attila0970 | 0:60316223b876 | 105 | |
attila0970 | 0:60316223b876 | 106 | //adatok beírésa az eepromba négyessével(óra, perc, hőfok 2 bíte-on) |
attila0970 | 0:60316223b876 | 107 | write_byte(location >> 8, location, (char)minute); |
attila0970 | 0:60316223b876 | 108 | location++; |
attila0970 | 0:60316223b876 | 109 | write_byte(location >> 8, location, (char)second); |
attila0970 | 0:60316223b876 | 110 | location++; |
attila0970 | 0:60316223b876 | 111 | write_byte(location >> 8, location, (char)floor(temp)); |
attila0970 | 0:60316223b876 | 112 | location++; |
attila0970 | 0:60316223b876 | 113 | temp_tizedes = (temp-floor(temp))*100; |
attila0970 | 0:60316223b876 | 114 | write_byte(location >> 8, location, (char)temp_tizedes); |
attila0970 | 0:60316223b876 | 115 | location++; |
attila0970 | 0:60316223b876 | 116 | |
attila0970 | 0:60316223b876 | 117 | if (location >= 32000 && butt_flag == 0){ //ez volt szar eddig, bocsika.... itt ezt a 32000-et tartsd meg aztán jó lesz minden :D |
attila0970 | 0:60316223b876 | 118 | red = !red; //lényegében csak belép egy végtelen ciklusba, hogy ha megtelik a memúria, és csak akkor lép ki, ha megnyomod a gombot, vagy úkraindítod a programot D: |
attila0970 | 0:60316223b876 | 119 | wait(0.2); |
attila0970 | 0:60316223b876 | 120 | } |
attila0970 | 0:60316223b876 | 121 | } |
attila0970 | 0:60316223b876 | 122 | } |
attila0970 | 0:60316223b876 | 123 | } |
attila0970 | 0:60316223b876 | 124 | |
attila0970 | 0:60316223b876 | 125 | void callback(){ //gomb interrupt függvénye |
attila0970 | 0:60316223b876 | 126 | |
attila0970 | 0:60316223b876 | 127 | butt_flag = 1; |
attila0970 | 0:60316223b876 | 128 | |
attila0970 | 0:60316223b876 | 129 | } |
attila0970 | 0:60316223b876 | 130 | |
attila0970 | 0:60316223b876 | 131 | |
attila0970 | 0:60316223b876 | 132 | void write_byte(char addr1, char addr2, char data){ |
attila0970 | 0:60316223b876 | 133 | buff[0] = addr1; |
attila0970 | 0:60316223b876 | 134 | buff[1] = addr2; |
attila0970 | 0:60316223b876 | 135 | buff[2] = data; |
attila0970 | 0:60316223b876 | 136 | flash.write(write_addr, buff, 3, false); |
attila0970 | 0:60316223b876 | 137 | wait_ms(5); |
attila0970 | 0:60316223b876 | 138 | } |
attila0970 | 0:60316223b876 | 139 | |
attila0970 | 0:60316223b876 | 140 | char read_byte(char addr1, char addr2, int range){ |
attila0970 | 0:60316223b876 | 141 | buff[0] = addr1; |
attila0970 | 0:60316223b876 | 142 | buff[1] = addr2; |
attila0970 | 0:60316223b876 | 143 | flash.write(write_addr, buff, 2, true); |
attila0970 | 0:60316223b876 | 144 | flash.read(read_addr, data, range, false); |
attila0970 | 0:60316223b876 | 145 | char faszom = data[0]; |
attila0970 | 0:60316223b876 | 146 | return faszom; |
attila0970 | 0:60316223b876 | 147 | } |
attila0970 | 0:60316223b876 | 148 | |
attila0970 | 0:60316223b876 | 149 |