main.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. /**
  24. * A
  25. * F B
  26. * G
  27. * E C
  28. * D H
  29. */
  30. #define INDCTR_SEGMENT_PORT PORTB
  31. #define INDCTR_SEGMENT_DDR DDRB
  32. #define INDCTR_SEGMENT_A PB0
  33. #define INDCTR_SEGMENT_B PB2
  34. #define INDCTR_SEGMENT_C PB4
  35. #define INDCTR_SEGMENT_D PB6
  36. #define INDCTR_SEGMENT_E PB7
  37. #define INDCTR_SEGMENT_F PB1
  38. #define INDCTR_SEGMENT_G PB3
  39. #define INDCTR_SEGMENT_H PB5
  40. #define INDCTR_COMMON_PORT PORTD
  41. #define INDCTR_COMMON_DDR DDRD
  42. #define INDCTR_COMMON_1 (1<<PD0)
  43. #define INDCTR_COMMON_2 (1<<PD1)
  44. #define INDCTR_COMMON_3 (1<<PD2)
  45. #define INDCTR_COMMON_4 (1<<PD3)
  46. #define INDCTR_COMMON_ALL (INDCTR_COMMON_1 | INDCTR_COMMON_2 | INDCTR_COMMON_3 | INDCTR_COMMON_4)
  47. #define INDCTR_COMMON_DOT 1
  48. typedef enum {
  49. Sym_0 = 0xD7,
  50. Sym_1 = 0x14,
  51. Sym_2 = 0xCD,
  52. Sym_3 = 0x5D,
  53. Sym_4 = 0x1E,
  54. Sym_5 = 0x5B,
  55. Sym_6 = 0xDB,
  56. Sym_7 = 0x19,
  57. Sym_8 = 0xDF,
  58. Sym_9 = 0x5F,
  59. Sym_a = 0x9F,
  60. Sym_b = 0xDA,
  61. Sym_c = 0xC8,
  62. Sym_d = 0xDC,
  63. Sym_e = 0xCB,
  64. Sym_f = 0x8B,
  65. Sym_minus = 1<<INDCTR_SEGMENT_G,
  66. Sym_minus_t = 1<<INDCTR_SEGMENT_A,
  67. Sym_minus_b = 1<<INDCTR_SEGMENT_D,
  68. Sym_gradus = 0x0F,
  69. Sym_t = 0xCA,
  70. Sym_dot = 1<<INDCTR_SEGMENT_H,
  71. Sym_blank = 0x00
  72. } indctr_sym_t;
  73. /* Convert 0x0-0xF digit to 7-seg indicators symbol */
  74. const indctr_sym_t IndctrNums[16] = {
  75. Sym_0, Sym_1, Sym_2, Sym_3,
  76. Sym_4, Sym_5, Sym_6, Sym_7,
  77. Sym_8, Sym_9, Sym_a, Sym_b,
  78. Sym_c, Sym_d, Sym_e, Sym_f
  79. };
  80. /* DS18B20 termometer */
  81. #define DS18B20_CMD_CONVERTTEMP 0x44
  82. #define DS18B20_CMD_RSCRATCHPAD 0xbe
  83. #define DS18B20_CMD_WSCRATCHPAD 0x4e
  84. #define DS18B20_CMD_CPYSCRATCHPAD 0x48
  85. #define DS18B20_CMD_RECEEPROM 0xb8
  86. #define DS18B20_CMD_RPWRSUPPLY 0xb4
  87. #define DS18B20_CMD_SEARCHROM 0xf0
  88. #define DS18B20_CMD_READROM 0x33
  89. #define DS18B20_CMD_MATCHROM 0x55
  90. #define DS18B20_CMD_SKIPROM 0xcc
  91. #define DS18B20_CMD_ALARMSEARCH 0xec
  92. #define DS18B20_PORT PORTD
  93. #define DS18B20_DDR DDRD
  94. #define DS18B20_PIN PIND
  95. #define DS18B20_PAD PD7
  96. #define DS18B20_INPUT_MODE DS18B20_DDR&=~(1<<DS18B20_PAD)
  97. #define DS18B20_OUTPUT_MODE DS18B20_DDR|=(1<<DS18B20_PAD)
  98. #define DS18B20_LOW DS18B20_PORT&=~(1<<DS18B20_PAD)
  99. #define DS18B20_HIGH DS18B20_PORT|=(1<<DS18B20_PAD)
  100. #define DS18B20_VALUE (DS18B20_PIN & (1<<DS18B20_PAD))
  101. static int8_t Temperature;
  102. static uint8_t ds18b20_Reset(void);
  103. static void ds18b20_WriteBit(uint8_t bit);
  104. static uint8_t ds18b20_ReadBit(void);
  105. static uint8_t ds18b20_ReadByte(void);
  106. static void ds18b20_WriteByte(uint8_t byte);
  107. static void ds18b20_StartMeasure(void);
  108. static void ds18b20_ReadTemperature(void);
  109. #define TIM1_PRESCALER (1<<CS11)
  110. /* BTN */
  111. #define BTN_LVL_NO 0xED
  112. #define BTN_LVL_7 0xD8
  113. #define BTN_LVL_6 0xD0
  114. #define BTN_LVL_5 0xC5
  115. #define BTN_LVL_4 0xB4
  116. #define BTN_LVL_3 0x94
  117. #define BTN_LVL_2 0x3F
  118. typedef enum {
  119. btn_no = 0,
  120. btn_1,
  121. btn_2,
  122. btn_3,
  123. btn_4,
  124. btn_5,
  125. btn_6,
  126. btn_7
  127. } btn_t;
  128. /* DS1307 */
  129. #define DS1307_ADR 0x68
  130. #define DS1307_ADR_RD 0xD0
  131. #define DS1307_ADR_WR 0xD1
  132. typedef struct {
  133. uint8_t seconds;
  134. uint8_t minutes;
  135. uint8_t hours;
  136. } time_t;
  137. /* Global varaibles */
  138. volatile struct {
  139. uint8_t needDot : 1;
  140. uint8_t newTime : 1;
  141. uint8_t newTemp : 1;
  142. uint8_t newBTN : 1;
  143. uint8_t T1OC : 1;
  144. uint8_t : 3; // дополнение до 8 бит (резерв)
  145. } Flag;
  146. volatile static indctr_sym_t Display[4];
  147. volatile static uint8_t resultADC;
  148. static uint8_t twi_buf[8];
  149. static time_t Clock;
  150. static void setTime(void);
  151. /* Functions prototypes */
  152. static void startADC(void);
  153. static btn_t getBTN(void);
  154. static void getTime(void);
  155. #endif // __MAIN_H