main.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  1. /**
  2. * My Nixie Clock IN-4x6
  3. * Vladimir N. Shilov <shilow@ukr.net>
  4. * 2020.01.06
  5. */
  6. /* Compiler libs */
  7. #include <stdbool.h>
  8. #include <stdint.h>
  9. #include <stdlib.h>
  10. #include <avr/io.h>
  11. #include <avr/interrupt.h>
  12. #include <avr/sleep.h>
  13. #include <avr/eeprom.h>
  14. #include <avr/pgmspace.h>
  15. /* Project libs */
  16. #include "i2c.h"
  17. #include "ds3231.h"
  18. #include "rtos.h"
  19. #include "event-system.h"
  20. #include "main.h"
  21. #include "common.h"
  22. /* Defines */
  23. /* Timer2 settings
  24. * Prescaler (CS22/CS21/CS20 - value):
  25. * 0/0/0 - stop
  26. * 0/0/0 - 1 (no prescale)
  27. * 0/1/0 - 8
  28. * 0/1/1 - 32
  29. * 1/0/0 - 64
  30. * 1/0/1 - 128
  31. * 1/1/0 - 256
  32. * 1/1/1 - 1024
  33. */
  34. #define TIMER2_HZ 360
  35. #if F_CPU == 16000000
  36. #define TIMER2_PRESCALER 256
  37. #define TIMER2_CS (1<<CS22 | 1<<CS21 | 0<<CS20)
  38. #elif F_CPU == 8000000
  39. #define TIMER2_PRESCALER 64
  40. #define TIMER2_CS (0<<CS22 | 1<<CS21 | 1<<CS20)
  41. #endif // F_CPU
  42. #define TIMER2_CNT (0x100 - (F_CPU / TIMER2_PRESCALER / TIMER2_HZ))
  43. /* Display timeout, sec */
  44. #define DISP_WDT_TIME 10
  45. #ifdef USE_UART
  46. /* USART */
  47. #define BAUD 19200UL
  48. #define BAUD_PRESCALE (uint8_t)((F_CPU / BAUD / 16UL) - 1)
  49. #define CHAR_NEWLINE '\n'
  50. #define CHAR_RETURN '\r'
  51. #define RETURN_NEWLINE "\r\n"
  52. #endif // USE_UART
  53. #ifdef USE_BRIGHT_CONTROL
  54. //static void startADC(void);
  55. //volatile uint8_t resultADC = 0;
  56. /* Lamp brightness */
  57. #define BRIGHT_IDX_MAX 4
  58. #define LIGHT_LEVEL 100
  59. #define FULL_BRIGHT_ON 0x06
  60. #define FULL_BRIGHT_OFF 0x22
  61. static const uint8_t PROGMEM brightConv[BRIGHT_IDX_MAX+1] = {
  62. #if F_CPU == 16000000
  63. /* 155, 164, 181, 211, 255 */
  64. /* for 83: */
  65. 117, 151, 185, 219, 255
  66. #elif F_CPU == 8000000
  67. 54, 71, 106, 165, 255
  68. #endif // F_CPU
  69. };
  70. #endif // USE_BRIGHT_CONTROL
  71. /* Variables */
  72. static volatile uint8_t Digit[LAMP_NUM] = {1, 2, 3, 4, 5, 6};
  73. static rtc_t RTC, setRTC;
  74. static volatile struct {
  75. uint8_t RTC_Int: 1;
  76. uint8_t blinkC: 1; // флаг задающий ритм мигания
  77. uint8_t blink0: 1; // мигать разрядами 1-2
  78. uint8_t blink1: 1; // мигать разрядами 3-4
  79. uint8_t blink2: 1; // мигать разрядами 5-6
  80. uint8_t saveEEP: 1;
  81. uint8_t rezerv: 2;
  82. } Flag;
  83. static btn_t Button[BTN_NUM] = {
  84. {0, evBTN1Pressed, evBTN1Holded, BUTTON1_PIN},
  85. {0, evBTN2Pressed, evBTN2Pressed, BUTTON2_PIN},
  86. {0, evBTN3Pressed, evBTN3Pressed, BUTTON3_PIN}
  87. };
  88. static volatile uint8_t DISP_WDT = 0;
  89. static EEMEM uint8_t EEP_BrightIdx;
  90. static uint8_t brightIdx;
  91. static EEMEM uint8_t EEP_SummerTime;
  92. /* Function prototypes */
  93. static void Board_Init(void);
  94. static void btnProcess(void);
  95. static void valIncrease(uint8_t * val, uint8_t max);
  96. static void valDecrease(uint8_t * val, uint8_t max);
  97. static void blink(void);
  98. static void setSummerWinterTime(void);
  99. #ifdef LAMP_TEST
  100. static void lampTest(void);
  101. #endif // LAMP_TEST
  102. #ifdef USE_UART
  103. void usart_putc (char send);
  104. void usart_puts (const char *send);
  105. #endif // USE_UART
  106. void main(void) {
  107. /**
  108. * Локальные переменные
  109. */
  110. uint8_t event = 0;
  111. Flag.RTC_Int = 0;
  112. Flag.blink0 = 0;
  113. Flag.blink1 = 0;
  114. Flag.blink2 = 0;
  115. Flag.blinkC = 0;
  116. Flag.saveEEP = 0;
  117. #ifdef USE_BRIGHT_CONTROL
  118. brightIdx = eeprom_read_byte(&EEP_BrightIdx);
  119. if (brightIdx > BRIGHT_IDX_MAX) {
  120. brightIdx = BRIGHT_IDX_MAX;
  121. }
  122. #endif // USE_BRIGHT_CONTROL
  123. /**
  124. * Инициализация, настройка...
  125. */
  126. Board_Init();
  127. /* Initialize Scheduler */
  128. RTOS_Init();
  129. tdelay_ms(2000);
  130. #ifdef LAMP_TEST
  131. lampTest();
  132. tdelay_ms(5000);
  133. #endif // LAMP_TEST
  134. /* Initialize I2C Bus and RTC */
  135. I2C_Init();
  136. RTC_Init();
  137. RTC_ReadAll(&RTC);
  138. /* Initialize Event State Machine */
  139. ES_Init(stShowTime);
  140. RTOS_SetTask(btnProcess, 3, BTN_SCAN_PERIOD);
  141. #ifdef USE_BRIGHT_CONTROL
  142. // RTOS_SetTask(startADC, 10, 500);
  143. #endif // USE_BRIGHT_CONTROL
  144. showTime();
  145. /** main loop */
  146. do {
  147. /* new second interrupt from RTC */
  148. if (Flag.RTC_Int != 0) {
  149. Flag.RTC_Int = 0;
  150. ES_PlaceEvent(evNewSecond);
  151. RTC_ReadTime(&RTC);
  152. if (RTC.Sec == 0 && RTC.Min == 0) {
  153. // begin of new hour
  154. if (RTC.Hr == 0) {
  155. // begin of new day
  156. RTC_ReadCalendar(&RTC);
  157. ES_PlaceEvent(evRefreshCal);
  158. }
  159. #ifdef USE_BRIGHT_CONTROL
  160. if (RTC.Hr >= FULL_BRIGHT_ON && RTC.Hr < FULL_BRIGHT_OFF) {
  161. // if (resultADC < LIGHT_LEVEL) {
  162. OCR2 = pgm_read_byte(&brightConv[BRIGHT_IDX_MAX]);
  163. } else {
  164. OCR2 = pgm_read_byte(&brightConv[brightIdx]);
  165. }
  166. #endif // USE_BRIGHT_CONTROL
  167. setSummerWinterTime();
  168. } // begin new hour
  169. if (DISP_WDT != 0) {
  170. DISP_WDT --;
  171. if (DISP_WDT == 0) {
  172. ES_PlaceEvent(evDisplayWDT);
  173. if (Flag.saveEEP != 0) {
  174. Flag.saveEEP = 0;
  175. eeprom_update_byte(&EEP_BrightIdx, brightIdx);
  176. }
  177. }
  178. }
  179. } // End of New Second
  180. event = ES_GetEvent();
  181. if (event) {
  182. ES_Dispatch(event);
  183. }
  184. // крутим диспетчер
  185. RTOS_DispatchTask();
  186. // делать нечего -- спим, ждём прерывание
  187. set_sleep_mode(SLEEP_MODE_IDLE);
  188. sleep_mode();
  189. } while(1);
  190. }
  191. /**
  192. * П о д п р о г р а м м ы
  193. */
  194. /**
  195. * @brief Initializy perephireal
  196. */
  197. static void Board_Init(void) {
  198. /* power off Analog Comparator */
  199. ACSR = ACD;
  200. /* GPIO */
  201. DDRB = ANODB_PINS; // as output
  202. PORTB = BUTTON_PINS; // enable pull-up
  203. DDRC = DIGIT_PINS; // as output
  204. DDRD = (DOT_PIN | ANODD_PINS); // as output
  205. /* Timer2 - refresh Nixie values */
  206. TCCR2 = TIMER2_CS;
  207. TCNT2 = TIMER2_CNT;
  208. TIMSK = _BV(TOIE2);
  209. #ifdef USE_BRIGHT_CONTROL
  210. /* ADC init */
  211. // ADMUX = (1<<REFS0 | 1<<ADLAR | 1<<MUX2 | 1<<MUX1 | 1<<MUX0); // Vref = AVcc, channel ADC7, Left adjusted result
  212. // ADCSRA = (1<<ADEN | 1<<ADSC | 1<<ADIE | 1<<ADPS2 | 1<<ADPS1 | 1<<ADPS0); // enable ADC, prescaler = 128
  213. OCR2 = pgm_read_byte(&brightConv[BRIGHT_IDX_MAX]);
  214. TIMSK |= _BV(OCIE2);
  215. #endif // USE_BRIGHT_CONTROL
  216. /* Interrupt from RTC */
  217. MCUCR = _BV(ISC11); // falling edge
  218. GICR = _BV(INT1);
  219. #ifdef USE_UART
  220. /* USART */
  221. // Turn on USART hardware (no RX, TX)
  222. UCSRB |= (0 << RXEN) | (1 << TXEN);
  223. // 8 bit char sizes
  224. UCSRC |= (1 << UCSZ0) | (1 << UCSZ1);
  225. // Set baud rate
  226. UBRRH = (BAUD_PRESCALE >> 8);
  227. UBRRL = BAUD_PRESCALE;
  228. #endif // USE_UART
  229. /* Enable Interrupts */
  230. sei();
  231. }
  232. /**
  233. * @brief Correct current time for Sun or Winter
  234. */
  235. static void setSummerWinterTime(void) {
  236. uint8_t sunTime = eeprom_read_byte(&EEP_SummerTime);
  237. /* Переход на летнее время */
  238. if ((RTC.Mon == 3) && (RTC.WD == 7) && (RTC.Hr == 3) && (sunTime != 0)) {
  239. if ((RTC.Day + 7) > 31) {
  240. RTC.Hr = 4;
  241. RTC_WriteHH(&RTC);
  242. sunTime = 0;
  243. eeprom_update_byte(&EEP_SummerTime, sunTime);
  244. }
  245. }
  246. /* Переход на зимнее время */
  247. if ((RTC.Mon == 10) && (RTC.WD == 7) && (RTC.Hr == 4) && (sunTime == 0)) {
  248. if ((RTC.Day + 7) > 31) {
  249. RTC.Hr = 3;
  250. RTC_WriteHH(&RTC);
  251. sunTime = 1;
  252. eeprom_update_byte(&EEP_SummerTime, sunTime);
  253. }
  254. }
  255. }
  256. void dotOn(void) {
  257. PORTD |= DOT_PIN;
  258. }
  259. void dotOff(void) {
  260. PORTD &= ~(DOT_PIN);
  261. }
  262. void dotOnPersistent(void) {
  263. RTOS_DeleteTask(dotOff);
  264. dotOn();
  265. }
  266. /**
  267. * @brief Обработка кнопок.
  268. * @param : None
  269. * @retval : None
  270. */
  271. static void btnProcess(void) {
  272. uint8_t i;
  273. for (i=0; i<BTN_NUM; i++) {
  274. if (Button[i].pin != 0) {
  275. // button pressed
  276. if (BUTTON_STATE(Button[i].pin) == 0) {
  277. Button[i].time ++;
  278. if (Button[i].time >= BTN_TIME_HOLDED) {
  279. Button[i].time -= BTN_TIME_REPEATED;
  280. if (Button[i].holded == Button[i].pressed) {
  281. // if pressed and holded - same function, then button pressed auto repeat
  282. ES_PlaceEvent(Button[i].pressed);
  283. }
  284. }
  285. } else {
  286. // button released
  287. if (Button[i].time >= (BTN_TIME_HOLDED - BTN_TIME_REPEATED)) {
  288. ES_PlaceEvent(Button[i].holded); // process long press
  289. } else if (Button[i].time >= BTN_TIME_PRESSED) {
  290. ES_PlaceEvent(Button[i].pressed); // process short press
  291. }
  292. Button[i].time = 0;
  293. RTOS_SetTask(btnProcess, BTN_TIME_PAUSE, BTN_SCAN_PERIOD);
  294. }
  295. } /* end (pin == 0) */
  296. } /* end FOR */
  297. }
  298. void showTime(void) {
  299. /*
  300. Digit[0] = DIGIT_BLANK;
  301. Digit[1] = DIGIT_BLANK;
  302. Digit[2] = DIGIT_BLANK;
  303. Digit[3] = resultADC / 100;
  304. Digit[4] = (resultADC % 100) / 10;
  305. Digit[5] = resultADC % 10;
  306. */
  307. dotOn();
  308. RTOS_SetTask(dotOff, 500, 0);
  309. if (RTC.Hr > 0x09) {
  310. Digit[0] = RTC.Hr >> 4;
  311. } else {
  312. Digit[0] = DIGIT_BLANK;
  313. }
  314. Digit[1] = RTC.Hr & 0x0F;
  315. Digit[2] = RTC.Min >> 4;
  316. Digit[3] = RTC.Min & 0x0F;
  317. Digit[4] = RTC.Sec >> 4;
  318. Digit[5] = RTC.Sec & 0x0F;
  319. }
  320. void showWDM(void) {
  321. DISP_WDT = DISP_WDT_TIME;
  322. Digit[0] = RTC.WD & 0x0F;
  323. Digit[1] = DIGIT_BLANK;
  324. Digit[2] = RTC.Day >> 4;
  325. Digit[3] = RTC.Day & 0x0F;
  326. Digit[4] = RTC.Mon >> 4;
  327. Digit[5] = RTC.Mon & 0x0F;
  328. }
  329. void showYear(void) {
  330. DISP_WDT = DISP_WDT_TIME;
  331. Digit[0] = DIGIT_BLANK;
  332. Digit[1] = DIGIT_BLANK;
  333. Digit[2] = 0x02;
  334. Digit[3] = 0x00;
  335. Digit[4] = RTC.Year >> 4;
  336. Digit[5] = RTC.Year & 0x0F;
  337. }
  338. #ifdef USE_BRIGHT_CONTROL
  339. void showBright(void) {
  340. DISP_WDT = DISP_WDT_TIME;
  341. Digit[0] = DIGIT_BLANK;
  342. Digit[1] = DIGIT_BLANK;
  343. Digit[2] = DIGIT_BLANK;
  344. Digit[3] = brightIdx;
  345. Digit[4] = DIGIT_BLANK;
  346. Digit[5] = DIGIT_BLANK;
  347. }
  348. void incBright(void) {
  349. if (brightIdx < BRIGHT_IDX_MAX) {
  350. brightIdx ++;
  351. OCR2 = pgm_read_byte(&brightConv[brightIdx]);
  352. Flag.saveEEP = 1;
  353. }
  354. }
  355. void decBright(void) {
  356. if (brightIdx > 0 ) {
  357. brightIdx --;
  358. OCR2 = pgm_read_byte(&brightConv[brightIdx]);
  359. Flag.saveEEP = 1;
  360. }
  361. }
  362. /*
  363. static void startADC(void) {
  364. // enable interrupt and start conversion
  365. ADCSRA |= ((1<<ADSC) | (1<<ADIE));
  366. }*/
  367. #endif // USE_BRIGHT_CONTROL
  368. static void blink(void) {
  369. static uint8_t s = 0;
  370. switch (s) {
  371. case 0:
  372. Flag.blinkC = 0;
  373. RTOS_SetTask(blink, 750, 0);
  374. s = 1;
  375. break;
  376. case 1:
  377. Flag.blinkC = 1;
  378. RTOS_SetTask(blink, 250, 0);
  379. s = 0;
  380. break;
  381. default:
  382. s = 0;
  383. }
  384. }
  385. void setTimeShow(void) {
  386. DISP_WDT = DISP_WDT_TIME;
  387. dotOnPersistent();
  388. RTOS_SetTask(dotOff, 500, 0);
  389. Digit[0] = setRTC.Hr >> 4;
  390. Digit[1] = setRTC.Hr & 0x0F;
  391. Digit[2] = setRTC.Min >> 4;
  392. Digit[3] = setRTC.Min & 0x0F;
  393. Digit[4] = 0x0;
  394. Digit[5] = 0x0;
  395. }
  396. void setTimeBegin(void) {
  397. RTC_ReadTime(&setRTC);
  398. RTOS_SetTask(btnProcess, 500, BTN_SCAN_PERIOD);
  399. }
  400. void setHHBegin(void) {
  401. Flag.blink0 = 1;
  402. Flag.blink1 = 0;
  403. Flag.blink2 = 0;
  404. RTOS_SetTask(blink, 0, 0);
  405. setTimeShow();
  406. }
  407. void setHHInc(void) {
  408. valIncrease(&setRTC.Hr, 23);
  409. }
  410. void setHHDec(void) {
  411. valDecrease(&setRTC.Hr, 23);
  412. }
  413. void setMMBegin(void) {
  414. Flag.blink0 = 0;
  415. Flag.blink1 = 1;
  416. Flag.blink2 = 0;
  417. RTOS_SetTask(blink, 0, 0);
  418. setTimeShow();
  419. }
  420. void setMMInc(void) {
  421. valIncrease(&setRTC.Min, 59);
  422. }
  423. void setMMDec(void) {
  424. valDecrease(&setRTC.Min, 59);
  425. }
  426. void setTimeEnd(void) {
  427. RTOS_SetTask(btnProcess, 500, BTN_SCAN_PERIOD);
  428. setRTC.Sec = 0;
  429. RTC_WriteTime(&setRTC);
  430. RTOS_DeleteTask(blink);
  431. Flag.blink0 = 0;
  432. Flag.blink1 = 0;
  433. Flag.blink2 = 0;
  434. Flag.blinkC = 0;
  435. RTC_ReadTime(&RTC);
  436. }
  437. /**
  438. * Setup Calendar functions
  439. */
  440. void setDateBegin(void) {
  441. RTC_ReadCalendar(&setRTC);
  442. RTOS_SetTask(btnProcess, 500, BTN_SCAN_PERIOD);
  443. }
  444. void setDateEnd(void) {
  445. RTOS_SetTask(btnProcess, 500, BTN_SCAN_PERIOD);
  446. RTC_WriteCalendar(&setRTC);
  447. Flag.blink0 = 0;
  448. Flag.blink1 = 0;
  449. Flag.blink2 = 0;
  450. Flag.blinkC = 0;
  451. RTC_ReadCalendar(&RTC);
  452. dotOff();
  453. }
  454. void setWDMShow(void) {
  455. DISP_WDT = DISP_WDT_TIME;
  456. dotOnPersistent();
  457. Digit[0] = setRTC.WD & 0x0F;
  458. Digit[1] = DIGIT_BLANK;
  459. Digit[2] = setRTC.Day >> 4;
  460. Digit[3] = setRTC.Day & 0x0F;
  461. Digit[4] = setRTC.Mon >> 4;
  462. Digit[5] = setRTC.Mon & 0x0F;
  463. }
  464. void setYearShow(void) {
  465. DISP_WDT = DISP_WDT_TIME;
  466. dotOff();
  467. Digit[0] = DIGIT_BLANK;
  468. Digit[1] = DIGIT_BLANK;
  469. Digit[2] = 0x02;
  470. Digit[3] = 0x00;
  471. Digit[4] = setRTC.Year >> 4;
  472. Digit[5] = setRTC.Year & 0x0F;
  473. }
  474. void setWDayBegin(void) {
  475. Flag.blink0 = 1;
  476. Flag.blink1 = 0;
  477. Flag.blink2 = 0;
  478. RTOS_SetTask(blink, 0, 0);
  479. setWDMShow();
  480. }
  481. void setMDayBegin(void) {
  482. Flag.blink0 = 0;
  483. Flag.blink1 = 1;
  484. Flag.blink2 = 0;
  485. RTOS_SetTask(blink, 0, 0);
  486. setWDMShow();
  487. }
  488. void setMonthBegin(void) {
  489. Flag.blink0 = 0;
  490. Flag.blink1 = 0;
  491. Flag.blink2 = 1;
  492. RTOS_SetTask(blink, 0, 0);
  493. setWDMShow();
  494. }
  495. void setYearBegin(void) {
  496. Flag.blink0 = 0;
  497. Flag.blink1 = 0;
  498. Flag.blink2 = 1;
  499. RTOS_SetTask(blink, 0, 0);
  500. setYearShow();
  501. }
  502. void setIncWDay(void) {
  503. if (setRTC.WD < 7) {
  504. setRTC.WD ++;
  505. } else {
  506. setRTC.WD = 1;
  507. }
  508. }
  509. void setDecWDay(void) {
  510. if (setRTC.WD > 1) {
  511. setRTC.WD --;
  512. } else {
  513. setRTC.WD = 7;
  514. }
  515. }
  516. void setIncMDay(void) {
  517. valIncrease(&setRTC.Day, 31);
  518. if (setRTC.Day == 0) {
  519. setRTC.Day = 1;
  520. }
  521. }
  522. void setDecMDay(void) {
  523. valDecrease(&setRTC.Day, 31);
  524. if (setRTC.Day == 0) {
  525. setRTC.Day = 0x31;
  526. }
  527. }
  528. void setIncMonth(void) {
  529. valIncrease(&setRTC.Mon, 12);
  530. if (setRTC.Mon == 0) {
  531. setRTC.Mon = 1;
  532. }
  533. }
  534. void setDecMonth(void) {
  535. valDecrease(&setRTC.Mon, 12);
  536. if (setRTC.Mon == 0) {
  537. setRTC.Mon = 0x12;
  538. }
  539. }
  540. void setIncYear(void) {
  541. valIncrease(&setRTC.Year, 99);
  542. }
  543. void setDecYear(void) {
  544. valDecrease(&setRTC.Year, 99);
  545. }
  546. /**
  547. * @brief Increase BCD value.
  548. * @param : val, max
  549. * @retval : None
  550. */
  551. static void valIncrease(uint8_t * val, uint8_t max) {
  552. uint8_t bin = 10 * (*val >> 4) + (*val & 0x0f);
  553. if (bin < max) {
  554. bin ++;
  555. } else {
  556. bin = 0;
  557. }
  558. *val = ((bin / 10 ) << 4) | (bin % 10);
  559. }
  560. /**
  561. * @brief Decrease BCD value.
  562. * @param : value, max
  563. * @retval : None
  564. */
  565. static void valDecrease(uint8_t * val, uint8_t max) {
  566. uint8_t bin = 10 * (*val >> 4) + (*val & 0x0f);
  567. if (bin > 0) {
  568. bin --;
  569. } else {
  570. bin = max;
  571. }
  572. *val = ((bin / 10 ) << 4) | (bin % 10);
  573. }
  574. #ifdef USE_UART
  575. void usart_putc (char send) {
  576. // Do nothing for a bit if there is already
  577. // data waiting in the hardware to be sent
  578. while ((UCSRA & (1 << UDRE)) == 0) {};
  579. UDR = send;
  580. }
  581. void usart_puts (const char *send) {
  582. // Cycle through each character individually
  583. while (*send) {
  584. usart_putc(*send++);
  585. }
  586. }
  587. #endif // USE_UART
  588. #ifdef LAMP_TEST
  589. /**
  590. * Lamp Test
  591. */
  592. static void lampValInc(uint8_t n)
  593. {
  594. if (Digit[n] != DIGIT_BLANK) {
  595. Digit[n] ++;
  596. } else {
  597. Digit[n] = 1;
  598. }
  599. if (Digit[n] > 9) {
  600. Digit[n] = 0;
  601. if (n > 0) {
  602. lampValInc(n-1);
  603. }
  604. }
  605. }
  606. static void lampTest(void)
  607. {
  608. uint8_t i=0;
  609. uint8_t k, x;
  610. dotOn();
  611. for (k = 0; k<LAMP_NUM; k++) {
  612. Digit[k] = DIGIT_BLANK;
  613. }
  614. while (true) {
  615. Digit[LAMP_NUM-1] = i;
  616. i ++;
  617. if (i > 9) {
  618. dotOn();
  619. i = 0;
  620. lampValInc(LAMP_NUM-2);
  621. }
  622. tdelay_ms(10);
  623. if (i == 5) {
  624. dotOff();
  625. }
  626. x = 1;
  627. for (k = 0; k<LAMP_NUM; k++) {
  628. if (Digit[k] == 9) {
  629. x ++;
  630. }
  631. }
  632. if (x == LAMP_NUM) {
  633. tdelay_ms(2000);
  634. break;
  635. }
  636. }
  637. }
  638. #endif // LAMP_TEST
  639. /**
  640. * П р е р ы в а н и я
  641. */
  642. /**
  643. * @brief RTC one seconds interrupt
  644. */
  645. ISR (INT1_vect) {
  646. Flag.RTC_Int = 1;
  647. }
  648. /**
  649. * @brief Refresh Nixie output
  650. * @note Digit[] must be in range 0x00 - 0x0F
  651. */
  652. #pragma GCC optimize ("O3")
  653. ISR(TIMER2_OVF_vect) {
  654. static uint8_t idx = 0;
  655. // reload timer
  656. TCNT2 = TIMER2_CNT;
  657. // read current register value and clean bits
  658. uint8_t pb = PORTB & ~ANODB_PINS;
  659. uint8_t pd = PORTD & ~ANODD_PINS;
  660. uint8_t pc = PORTC & ~DIGIT_PINS;
  661. #ifndef USE_BRIGHT_CONTROL
  662. // power off lamps
  663. PORTB = pb;
  664. PORTD = pd;
  665. PORTC = pc;
  666. #endif
  667. switch (idx) {
  668. case 0:
  669. // output lamp value
  670. PORTC = pc | Digit[0];
  671. // power on lamp
  672. if (Digit[0] != DIGIT_BLANK) {
  673. if (Flag.blink0 == 0 || Flag.blinkC == 0) {
  674. PORTD = pd | ANOD1;
  675. }
  676. }
  677. idx = 1;
  678. break;
  679. case 1:
  680. PORTC = pc | Digit[1];
  681. if (Digit[1] != DIGIT_BLANK) {
  682. if (Flag.blink0 == 0 || Flag.blinkC == 0) {
  683. PORTD = pd | ANOD2;
  684. }
  685. }
  686. idx = 2;
  687. break;
  688. case 2:
  689. PORTC = pc | Digit[2];
  690. if (Digit[2] != DIGIT_BLANK) {
  691. if (Flag.blink1 == 0 || Flag.blinkC == 0) {
  692. PORTD = pd | ANOD3;
  693. }
  694. }
  695. idx = 3;
  696. break;
  697. case 3:
  698. PORTC = pc | Digit[3];
  699. if (Digit[3] != DIGIT_BLANK) {
  700. if (Flag.blink1 == 0 || Flag.blinkC == 0) {
  701. PORTB = pb | ANOD4;
  702. }
  703. }
  704. idx = 4;
  705. break;
  706. case 4:
  707. PORTC = pc | Digit[4];
  708. if (Digit[4] != DIGIT_BLANK) {
  709. if (Flag.blink2 == 0 || Flag.blinkC == 0) {
  710. PORTB = pb | ANOD5;
  711. }
  712. }
  713. idx = 5;
  714. break;
  715. case 5:
  716. PORTC = pc | Digit[5];
  717. if (Digit[5] != DIGIT_BLANK) {
  718. if (Flag.blink2 == 0 || Flag.blinkC == 0) {
  719. PORTB = pb | ANOD6;
  720. }
  721. }
  722. idx = 0;
  723. break;
  724. default:
  725. idx = 0;
  726. break;
  727. }
  728. }
  729. #ifdef USE_BRIGHT_CONTROL
  730. /**
  731. * @brief Power Off Nixie output
  732. * @note For Brightnes dimming
  733. */
  734. #pragma GCC optimize ("O3")
  735. ISR(TIMER2_COMP_vect) {
  736. // power off lamps
  737. PORTB &= ~ANODB_PINS;
  738. PORTD &= ~ANODD_PINS;
  739. PORTC &= ~DIGIT_PINS;
  740. }
  741. /*
  742. ISR(ADC_vect) {
  743. resultADC = ADCH;
  744. ADCSRA &= ~(1<<ADIE); // disable interrupt
  745. }*/
  746. #endif // USE_BRIGHT_CONTROL
  747. /**
  748. * @brief заглушка для неиспользуемых прерываний
  749. */
  750. ISR(__vector_default,ISR_NAKED) {
  751. reti();
  752. }