main.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #ifndef __MAIN_H
  2. #define __MAIN_H
  3. #include <ioavr.h>
  4. #include <intrinsics.h>
  5. #include <stdint.h>
  6. #include "rtos.h"
  7. #include "twim.h"
  8. /* Timers */
  9. // T0 period 4 ms == 0x100 - (8000000 / 256 * 0.004)
  10. #define TIM0_PRESCALER 1<<CS02
  11. #define TIM0_CNT 0x83
  12. #define TIM0_CHANNEL 0x00
  13. /* LEDs */
  14. #define LED_PORT PORTC
  15. #define LED_DDR DDRC
  16. #define LED_1 (1<<PC2)
  17. #define LED_2 (1<<PC3)
  18. #define LED_ON(led) LED_PORT |= led
  19. #define LED_OFF(led) LED_PORT &= ~led
  20. #define LED_TOGGLE(led) LED_PORT ^= led
  21. /* 7-segment LED indikators */
  22. #define INDCTR_NUMS 4
  23. #define INDCTR_SEGMENT_PORT PORTB
  24. #define INDCTR_SEGMENT_DDR DDRB
  25. #define INDCTR_SEGMENT_A PB0
  26. #define INDCTR_SEGMENT_B PB2
  27. #define INDCTR_SEGMENT_C PB4
  28. #define INDCTR_SEGMENT_D PB6
  29. #define INDCTR_SEGMENT_E PB7
  30. #define INDCTR_SEGMENT_F PB1
  31. #define INDCTR_SEGMENT_G PB3
  32. #define INDCTR_SEGMENT_H PB5
  33. #define INDCTR_COMMON_PORT PORTD
  34. #define INDCTR_COMMON_DDR DDRD
  35. #define INDCTR_COMMON_1 (1<<PD0)
  36. #define INDCTR_COMMON_2 (1<<PD1)
  37. #define INDCTR_COMMON_3 (1<<PD2)
  38. #define INDCTR_COMMON_4 (1<<PD3)
  39. #define INDCTR_COMMON_ALL (INDCTR_COMMON_1 | INDCTR_COMMON_2 | INDCTR_COMMON_3 | INDCTR_COMMON_4)
  40. #define INDCTR_COMMON_DOT 1
  41. typedef enum {
  42. Sym_0 = ~0x28,
  43. Sym_1 = ~0xEB,
  44. Sym_2 = ~0x32,
  45. Sym_3 = ~0xA2,
  46. Sym_4 = ~0xE1,
  47. Sym_5 = ~0xA4,
  48. Sym_6 = ~0x24,
  49. Sym_7 = ~0xEA,
  50. Sym_8 = ~0x20,
  51. Sym_9 = ~0xA0,
  52. Sym_a = ~0x60,
  53. Sym_b = ~0x25,
  54. Sym_c = ~0x37,
  55. Sym_d = ~0x23,
  56. Sym_e = ~0x34,
  57. Sym_f = ~0x74,
  58. Sym_minus = ~0xF7,
  59. Sym_gradus = ~0xF0,
  60. Sym_t = ~0x35,
  61. Sym_dot = 1<<INDCTR_SEGMENT_H
  62. } indctr_sym_t;
  63. /* Convert 0x0-0xF digit to 7-seg indicators symbol */
  64. const indctr_sym_t IndctrNums[16] = {
  65. Sym_0, Sym_1, Sym_2, Sym_3,
  66. Sym_4, Sym_5, Sym_6, Sym_7,
  67. Sym_8, Sym_9, Sym_a, Sym_b,
  68. Sym_c, Sym_d, Sym_e, Sym_f
  69. };
  70. /* DS18B20 termometer */
  71. #define DS18B20_PORT PORTD
  72. #define DS18B20_DDR DDRD
  73. #define DS18B20_PIN PIND
  74. #define DS18B20_PAD PD7
  75. /* BTN */
  76. #define BTN_LVL_NO 0xED
  77. #define BTN_LVL_7 0xD8
  78. #define BTN_LVL_6 0xD0
  79. #define BTN_LVL_5 0xC5
  80. #define BTN_LVL_4 0xB4
  81. #define BTN_LVL_3 0x94
  82. #define BTN_LVL_2 0x3F
  83. typedef enum {
  84. btn_no = 0,
  85. btn_1,
  86. btn_2,
  87. btn_3,
  88. btn_4,
  89. btn_5,
  90. btn_6,
  91. btn_7
  92. } btn_t;
  93. /* DS1307 */
  94. #define DS1307_ADR 0x68
  95. #define DS1307_ADR_RD 0xD0
  96. #define DS1307_ADR_WR 0xD1
  97. typedef struct {
  98. uint8_t seconds;
  99. uint8_t minutes;
  100. uint8_t hours;
  101. } time_t;
  102. /* Global varaibles */
  103. volatile struct {
  104. uint8_t needDot : 1;
  105. uint8_t newTime : 1;
  106. uint8_t newBTN : 1;
  107. uint8_t : 5; // дополнение до 8 бит (резерв)
  108. } Flag;
  109. volatile static indctr_sym_t Display[4];
  110. volatile static uint8_t resultADC;
  111. static uint8_t twi_buf[8];
  112. static time_t Clock;
  113. static void setTime(void);
  114. /* Functions prototypes */
  115. static void startADC(void);
  116. static btn_t getBTN(void);
  117. static void getTime(void);
  118. #endif // __MAIN_H