Relay counter

Dependencies:   TM1638 mbed mbed-rtos AT45

Committer:
Oleg_B
Date:
Thu Jan 28 14:18:16 2021 +0000
Revision:
0:1b4c0e7c9316
Relay - counter

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Oleg_B 0:1b4c0e7c9316 1
Oleg_B 0:1b4c0e7c9316 2 /**************rele-counter v1 **********************************/
Oleg_B 0:1b4c0e7c9316 3 // при включении восстанавливаются значения текущие и уставка
Oleg_B 0:1b4c0e7c9316 4 // при подаче импульсов +24В на счетный вход значение увеличиваются +1
Oleg_B 0:1b4c0e7c9316 5 // при подаче +24В на вхд сброса, значения сбрасываются
Oleg_B 0:1b4c0e7c9316 6 // реле включается если значение равно или больше установленного
Oleg_B 0:1b4c0e7c9316 7 //
Oleg_B 0:1b4c0e7c9316 8
Oleg_B 0:1b4c0e7c9316 9
Oleg_B 0:1b4c0e7c9316 10
Oleg_B 0:1b4c0e7c9316 11 #include "mbed.h"
Oleg_B 0:1b4c0e7c9316 12 #include "rtos.h"
Oleg_B 0:1b4c0e7c9316 13 #include "TM1638.h"
Oleg_B 0:1b4c0e7c9316 14 #include "AT45.h"
Oleg_B 0:1b4c0e7c9316 15
Oleg_B 0:1b4c0e7c9316 16
Oleg_B 0:1b4c0e7c9316 17 // ------- input/output ----------------
Oleg_B 0:1b4c0e7c9316 18 DigitalOut led(PC_13);
Oleg_B 0:1b4c0e7c9316 19 DigitalOut rel(PA_15);
Oleg_B 0:1b4c0e7c9316 20 DigitalIn in1(PA_8);
Oleg_B 0:1b4c0e7c9316 21 DigitalIn in2(PA_9);
Oleg_B 0:1b4c0e7c9316 22 DigitalIn in3(PA_10);
Oleg_B 0:1b4c0e7c9316 23
Oleg_B 0:1b4c0e7c9316 24 // ------- displey --------------------
Oleg_B 0:1b4c0e7c9316 25 TM1638_LEDKEY8 LEDKEY8(PB_15,PB_14,PB_13, PB_12);
Oleg_B 0:1b4c0e7c9316 26 TM1638::KeyData_t keydata;
Oleg_B 0:1b4c0e7c9316 27
Oleg_B 0:1b4c0e7c9316 28
Oleg_B 0:1b4c0e7c9316 29 //----------- SPI FLASH ------------------------
Oleg_B 0:1b4c0e7c9316 30 SPI spi(PA_7, PA_6, PA_5); // mosi, miso, sclk
Oleg_B 0:1b4c0e7c9316 31 AT45 spif(&spi, PA_4); // NSS
Oleg_B 0:1b4c0e7c9316 32
Oleg_B 0:1b4c0e7c9316 33 char *spi_buffer = (char*) malloc(64);
Oleg_B 0:1b4c0e7c9316 34 char str[10];
Oleg_B 0:1b4c0e7c9316 35
Oleg_B 0:1b4c0e7c9316 36
Oleg_B 0:1b4c0e7c9316 37 // -------- глоб.переменные ---------------
Oleg_B 0:1b4c0e7c9316 38 int present_val = 0; // текущее заначение
Oleg_B 0:1b4c0e7c9316 39 int present_val_last = 0; // текущее значение (старое)
Oleg_B 0:1b4c0e7c9316 40 int max_value = 0; // значение вкл. реле
Oleg_B 0:1b4c0e7c9316 41
Oleg_B 0:1b4c0e7c9316 42 int led1=0; // индикация LED1
Oleg_B 0:1b4c0e7c9316 43 int led2=0; // индикация LED2
Oleg_B 0:1b4c0e7c9316 44 int led3=0; // индикация LED3
Oleg_B 0:1b4c0e7c9316 45
Oleg_B 0:1b4c0e7c9316 46 // -------- Прототипы функций: ----------
Oleg_B 0:1b4c0e7c9316 47 void count_value();
Oleg_B 0:1b4c0e7c9316 48
Oleg_B 0:1b4c0e7c9316 49
Oleg_B 0:1b4c0e7c9316 50 /********************************************************************/
Oleg_B 0:1b4c0e7c9316 51 int main() {
Oleg_B 0:1b4c0e7c9316 52
Oleg_B 0:1b4c0e7c9316 53 // предустановка значений
Oleg_B 0:1b4c0e7c9316 54 int update = 0;
Oleg_B 0:1b4c0e7c9316 55 int update2 = 0;
Oleg_B 0:1b4c0e7c9316 56 int delay_update_x10 = 2000; // задержка перед +/- x_inreasing_decreasing
Oleg_B 0:1b4c0e7c9316 57 int time_update_increase = 0; // отсчет времени до +10
Oleg_B 0:1b4c0e7c9316 58 int time_update_decrease = 0; // отсчет времени до -10
Oleg_B 0:1b4c0e7c9316 59 int delay_cycle = 25; // период вывода инф. на дисплей
Oleg_B 0:1b4c0e7c9316 60 int once_increase = 0;
Oleg_B 0:1b4c0e7c9316 61 int once_decrease = 0;
Oleg_B 0:1b4c0e7c9316 62 int x_inreasing_decreasing = 10; // +/- после удержания кнопки
Oleg_B 0:1b4c0e7c9316 63
Oleg_B 0:1b4c0e7c9316 64
Oleg_B 0:1b4c0e7c9316 65 int write_page_time = 5000; // время до записи max_value
Oleg_B 0:1b4c0e7c9316 66 int timer_to_write_page = 0; // отсчет времени до записи max_value
Oleg_B 0:1b4c0e7c9316 67 int write_page_start = 0; // старт процедуры записи
Oleg_B 0:1b4c0e7c9316 68
Oleg_B 0:1b4c0e7c9316 69
Oleg_B 0:1b4c0e7c9316 70 // Вычитываем сохраненное максимальное значение (11 стр для записи)
Oleg_B 0:1b4c0e7c9316 71 spif.read_page(spi_buffer, 11);
Oleg_B 0:1b4c0e7c9316 72 max_value = atoi(spi_buffer);
Oleg_B 0:1b4c0e7c9316 73
Oleg_B 0:1b4c0e7c9316 74 // Вычитываем сохраненное текущее значение (12 стр для записи)
Oleg_B 0:1b4c0e7c9316 75 spif.read_page(spi_buffer, 12);
Oleg_B 0:1b4c0e7c9316 76 present_val = atoi(spi_buffer);
Oleg_B 0:1b4c0e7c9316 77
Oleg_B 0:1b4c0e7c9316 78 // Делаем текущее значение != текущему прошлому значение
Oleg_B 0:1b4c0e7c9316 79 present_val_last = present_val + 10;
Oleg_B 0:1b4c0e7c9316 80
Oleg_B 0:1b4c0e7c9316 81 // Поток для работы с текущим значением
Oleg_B 0:1b4c0e7c9316 82 Thread count_thread;
Oleg_B 0:1b4c0e7c9316 83 count_thread.start(count_value);
Oleg_B 0:1b4c0e7c9316 84
Oleg_B 0:1b4c0e7c9316 85
Oleg_B 0:1b4c0e7c9316 86 while(1) {
Oleg_B 0:1b4c0e7c9316 87
Oleg_B 0:1b4c0e7c9316 88 /*** обработка нажатия кнопок ***/
Oleg_B 0:1b4c0e7c9316 89
Oleg_B 0:1b4c0e7c9316 90 LEDKEY8.getKeys(&keydata);
Oleg_B 0:1b4c0e7c9316 91
Oleg_B 0:1b4c0e7c9316 92 // Нажимаем кнопку X, текущее значение = 0
Oleg_B 0:1b4c0e7c9316 93 if(keydata[LEDKEY8_SW3_IDX] == LEDKEY8_SW3_BIT) {
Oleg_B 0:1b4c0e7c9316 94 present_val = 0;
Oleg_B 0:1b4c0e7c9316 95 }
Oleg_B 0:1b4c0e7c9316 96
Oleg_B 0:1b4c0e7c9316 97
Oleg_B 0:1b4c0e7c9316 98 // Нажимаем кнопку увеличить
Oleg_B 0:1b4c0e7c9316 99 if(keydata[LEDKEY8_SW1_IDX] == LEDKEY8_SW1_BIT) {
Oleg_B 0:1b4c0e7c9316 100 if(keydata[LEDKEY8_SW2_IDX] != LEDKEY8_SW2_BIT) {
Oleg_B 0:1b4c0e7c9316 101 time_update_increase = time_update_increase + delay_cycle;
Oleg_B 0:1b4c0e7c9316 102
Oleg_B 0:1b4c0e7c9316 103 // Один раз делаем +1
Oleg_B 0:1b4c0e7c9316 104 if(once_increase == 0) {
Oleg_B 0:1b4c0e7c9316 105 once_increase = 1;
Oleg_B 0:1b4c0e7c9316 106 if(max_value<999){max_value = max_value + 1;}
Oleg_B 0:1b4c0e7c9316 107 update = 0;
Oleg_B 0:1b4c0e7c9316 108 }
Oleg_B 0:1b4c0e7c9316 109
Oleg_B 0:1b4c0e7c9316 110 write_page_start = 1;
Oleg_B 0:1b4c0e7c9316 111 timer_to_write_page = 0;
Oleg_B 0:1b4c0e7c9316 112 }
Oleg_B 0:1b4c0e7c9316 113 }
Oleg_B 0:1b4c0e7c9316 114 else {
Oleg_B 0:1b4c0e7c9316 115 time_update_increase = 0;
Oleg_B 0:1b4c0e7c9316 116 once_increase = 0;
Oleg_B 0:1b4c0e7c9316 117 }
Oleg_B 0:1b4c0e7c9316 118
Oleg_B 0:1b4c0e7c9316 119
Oleg_B 0:1b4c0e7c9316 120
Oleg_B 0:1b4c0e7c9316 121 // Нажимаем кнопку уменьшить
Oleg_B 0:1b4c0e7c9316 122 if(keydata[LEDKEY8_SW2_IDX] == LEDKEY8_SW2_BIT) {
Oleg_B 0:1b4c0e7c9316 123 if(keydata[LEDKEY8_SW1_IDX] != LEDKEY8_SW1_BIT) {
Oleg_B 0:1b4c0e7c9316 124 time_update_decrease = time_update_decrease + delay_cycle;
Oleg_B 0:1b4c0e7c9316 125
Oleg_B 0:1b4c0e7c9316 126 // Один раз делаем -1
Oleg_B 0:1b4c0e7c9316 127 if(once_decrease == 0) {
Oleg_B 0:1b4c0e7c9316 128 once_decrease = 1;
Oleg_B 0:1b4c0e7c9316 129 if(max_value>0){max_value = max_value - 1;}
Oleg_B 0:1b4c0e7c9316 130 update = 0;
Oleg_B 0:1b4c0e7c9316 131 }
Oleg_B 0:1b4c0e7c9316 132
Oleg_B 0:1b4c0e7c9316 133 write_page_start = 1;
Oleg_B 0:1b4c0e7c9316 134 timer_to_write_page = 0;
Oleg_B 0:1b4c0e7c9316 135 }
Oleg_B 0:1b4c0e7c9316 136 }
Oleg_B 0:1b4c0e7c9316 137 else {
Oleg_B 0:1b4c0e7c9316 138 once_decrease = 0;
Oleg_B 0:1b4c0e7c9316 139 time_update_decrease = 0;
Oleg_B 0:1b4c0e7c9316 140 }
Oleg_B 0:1b4c0e7c9316 141
Oleg_B 0:1b4c0e7c9316 142
Oleg_B 0:1b4c0e7c9316 143
Oleg_B 0:1b4c0e7c9316 144
Oleg_B 0:1b4c0e7c9316 145 // При удержании кнопки увеличить на +10
Oleg_B 0:1b4c0e7c9316 146 if(keydata[LEDKEY8_SW1_IDX] == LEDKEY8_SW1_BIT) {
Oleg_B 0:1b4c0e7c9316 147 if(time_update_increase >= delay_update_x10) {
Oleg_B 0:1b4c0e7c9316 148 time_update_increase = delay_update_x10 - delay_cycle*10;
Oleg_B 0:1b4c0e7c9316 149 if(max_value<=990){max_value = max_value + x_inreasing_decreasing;}
Oleg_B 0:1b4c0e7c9316 150 update = 0;
Oleg_B 0:1b4c0e7c9316 151 }
Oleg_B 0:1b4c0e7c9316 152 }
Oleg_B 0:1b4c0e7c9316 153
Oleg_B 0:1b4c0e7c9316 154
Oleg_B 0:1b4c0e7c9316 155
Oleg_B 0:1b4c0e7c9316 156
Oleg_B 0:1b4c0e7c9316 157 // При удержании кнопки уменьшить на -10
Oleg_B 0:1b4c0e7c9316 158 if(keydata[LEDKEY8_SW2_IDX] == LEDKEY8_SW2_BIT) {
Oleg_B 0:1b4c0e7c9316 159 if(time_update_decrease >= delay_update_x10) {
Oleg_B 0:1b4c0e7c9316 160 time_update_decrease = delay_update_x10 - delay_cycle*10;
Oleg_B 0:1b4c0e7c9316 161 if(max_value>10){max_value = max_value - x_inreasing_decreasing;}
Oleg_B 0:1b4c0e7c9316 162 update = 0;
Oleg_B 0:1b4c0e7c9316 163 }
Oleg_B 0:1b4c0e7c9316 164 }
Oleg_B 0:1b4c0e7c9316 165
Oleg_B 0:1b4c0e7c9316 166
Oleg_B 0:1b4c0e7c9316 167
Oleg_B 0:1b4c0e7c9316 168 // Записываем в память максимальное значение
Oleg_B 0:1b4c0e7c9316 169 // через время write_page_time после нажатий кнопок
Oleg_B 0:1b4c0e7c9316 170 if(write_page_start == 1) {
Oleg_B 0:1b4c0e7c9316 171 timer_to_write_page = timer_to_write_page + delay_cycle;
Oleg_B 0:1b4c0e7c9316 172
Oleg_B 0:1b4c0e7c9316 173 if(timer_to_write_page >= write_page_time) {
Oleg_B 0:1b4c0e7c9316 174 write_page_start = 0;
Oleg_B 0:1b4c0e7c9316 175 timer_to_write_page = 0;
Oleg_B 0:1b4c0e7c9316 176 sprintf(spi_buffer, "%d", max_value);
Oleg_B 0:1b4c0e7c9316 177 spif.page_erase(11);
Oleg_B 0:1b4c0e7c9316 178 spif.write_page(spi_buffer, 11);
Oleg_B 0:1b4c0e7c9316 179
Oleg_B 0:1b4c0e7c9316 180 LEDKEY8.locate(0);
Oleg_B 0:1b4c0e7c9316 181 LEDKEY8.printf(" ");
Oleg_B 0:1b4c0e7c9316 182 Thread::wait(500);
Oleg_B 0:1b4c0e7c9316 183 LEDKEY8.locate(0);
Oleg_B 0:1b4c0e7c9316 184 LEDKEY8.printf("%d", max_value);
Oleg_B 0:1b4c0e7c9316 185 }
Oleg_B 0:1b4c0e7c9316 186 }
Oleg_B 0:1b4c0e7c9316 187
Oleg_B 0:1b4c0e7c9316 188
Oleg_B 0:1b4c0e7c9316 189
Oleg_B 0:1b4c0e7c9316 190 /*** вывод на дисплей ***/
Oleg_B 0:1b4c0e7c9316 191
Oleg_B 0:1b4c0e7c9316 192 led = !led ;
Oleg_B 0:1b4c0e7c9316 193
Oleg_B 0:1b4c0e7c9316 194 LEDKEY8.cls();
Oleg_B 0:1b4c0e7c9316 195 LEDKEY8.setBrightness(TM1638_BRT1);
Oleg_B 0:1b4c0e7c9316 196 LEDKEY8.locate(0);
Oleg_B 0:1b4c0e7c9316 197 LEDKEY8.printf("%d", max_value);
Oleg_B 0:1b4c0e7c9316 198 LEDKEY8.locate(3);
Oleg_B 0:1b4c0e7c9316 199 LEDKEY8.printf("%d", present_val);
Oleg_B 0:1b4c0e7c9316 200 if(led1==1){LEDKEY8.setIcon(TM1638_LEDKEY8::LD1);}else{LEDKEY8.clrIcon(TM1638_LEDKEY8::LD1);}
Oleg_B 0:1b4c0e7c9316 201 if(led2==1){LEDKEY8.setIcon(TM1638_LEDKEY8::LD2);}else{LEDKEY8.clrIcon(TM1638_LEDKEY8::LD2);}
Oleg_B 0:1b4c0e7c9316 202 if(led3==1){LEDKEY8.setIcon(TM1638_LEDKEY8::LD3);}else{LEDKEY8.clrIcon(TM1638_LEDKEY8::LD3);}
Oleg_B 0:1b4c0e7c9316 203
Oleg_B 0:1b4c0e7c9316 204
Oleg_B 0:1b4c0e7c9316 205 Thread::wait(delay_cycle);
Oleg_B 0:1b4c0e7c9316 206 }
Oleg_B 0:1b4c0e7c9316 207 }
Oleg_B 0:1b4c0e7c9316 208
Oleg_B 0:1b4c0e7c9316 209 /****************************************************************/
Oleg_B 0:1b4c0e7c9316 210
Oleg_B 0:1b4c0e7c9316 211 // Поток работы с текущим значением 10msec
Oleg_B 0:1b4c0e7c9316 212 void count_value() {
Oleg_B 0:1b4c0e7c9316 213
Oleg_B 0:1b4c0e7c9316 214 int c_inp1=0;
Oleg_B 0:1b4c0e7c9316 215 int c_inp2=0;
Oleg_B 0:1b4c0e7c9316 216 int c_inp3=0;
Oleg_B 0:1b4c0e7c9316 217 int m_inp1=0;
Oleg_B 0:1b4c0e7c9316 218 int m_inp2=0;
Oleg_B 0:1b4c0e7c9316 219 int m_inp3=0;
Oleg_B 0:1b4c0e7c9316 220
Oleg_B 0:1b4c0e7c9316 221 while(1) {
Oleg_B 0:1b4c0e7c9316 222
Oleg_B 0:1b4c0e7c9316 223 // фиксируем значения
Oleg_B 0:1b4c0e7c9316 224 if(in1==0){c_inp1=1;}else{c_inp1=0;}
Oleg_B 0:1b4c0e7c9316 225 if(in2==0){c_inp2=1;}else{c_inp2=0;}
Oleg_B 0:1b4c0e7c9316 226 if(in3==0){c_inp3=1;}else{c_inp3=0;}
Oleg_B 0:1b4c0e7c9316 227
Oleg_B 0:1b4c0e7c9316 228
Oleg_B 0:1b4c0e7c9316 229 // сохраняем тек.значение в 12 стр. при выкл. питания
Oleg_B 0:1b4c0e7c9316 230 if(c_inp1==0 && m_inp1==1){
Oleg_B 0:1b4c0e7c9316 231 sprintf(spi_buffer, "%d", present_val);
Oleg_B 0:1b4c0e7c9316 232 spif.page_erase(12);
Oleg_B 0:1b4c0e7c9316 233 spif.write_page(spi_buffer, 12);
Oleg_B 0:1b4c0e7c9316 234 }
Oleg_B 0:1b4c0e7c9316 235
Oleg_B 0:1b4c0e7c9316 236 // подсчет импульсов по входу 1
Oleg_B 0:1b4c0e7c9316 237 if(c_inp2==1 && m_inp2==0){
Oleg_B 0:1b4c0e7c9316 238 present_val++;
Oleg_B 0:1b4c0e7c9316 239 }
Oleg_B 0:1b4c0e7c9316 240
Oleg_B 0:1b4c0e7c9316 241 // сбрасываем текущее значение по входу 2
Oleg_B 0:1b4c0e7c9316 242 if(c_inp3==1 && m_inp3==0){
Oleg_B 0:1b4c0e7c9316 243 present_val=0;
Oleg_B 0:1b4c0e7c9316 244 }
Oleg_B 0:1b4c0e7c9316 245
Oleg_B 0:1b4c0e7c9316 246
Oleg_B 0:1b4c0e7c9316 247 // управление реле
Oleg_B 0:1b4c0e7c9316 248 if(present_val>=max_value){ rel=1; }else{rel=0; }
Oleg_B 0:1b4c0e7c9316 249
Oleg_B 0:1b4c0e7c9316 250
Oleg_B 0:1b4c0e7c9316 251 // управляем светодиодами
Oleg_B 0:1b4c0e7c9316 252 if(rel==1) {led1=1;}else{led1=0;}
Oleg_B 0:1b4c0e7c9316 253 if(c_inp2==1){led2=1;}else{led2=0;}
Oleg_B 0:1b4c0e7c9316 254 if(c_inp3==1){led3=1;}else{led3=0;}
Oleg_B 0:1b4c0e7c9316 255
Oleg_B 0:1b4c0e7c9316 256
Oleg_B 0:1b4c0e7c9316 257 // сохраняем значени
Oleg_B 0:1b4c0e7c9316 258 m_inp1=c_inp1;
Oleg_B 0:1b4c0e7c9316 259 m_inp2=c_inp2;
Oleg_B 0:1b4c0e7c9316 260 m_inp3=c_inp3;
Oleg_B 0:1b4c0e7c9316 261
Oleg_B 0:1b4c0e7c9316 262
Oleg_B 0:1b4c0e7c9316 263 Thread::wait(5);
Oleg_B 0:1b4c0e7c9316 264 }
Oleg_B 0:1b4c0e7c9316 265 }
Oleg_B 0:1b4c0e7c9316 266
Oleg_B 0:1b4c0e7c9316 267