main.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #ifndef __MAIN_H
  2. #define __MAIN_H
  3. #include <ioavr.h>
  4. #include <intrinsics.h>
  5. #include <stdint.h>
  6. #include "twim.h"
  7. #ifndef F_CPU
  8. #define F_CPU 8000000UL
  9. #endif
  10. /* Timers */
  11. // T0 period 4 ms == 0x100 - (8000000 / 256 * 0.004)
  12. #define TIM0_PRESCALER 1<<CS02
  13. #define TIM0_CNT 0x83
  14. // T2 period 1 ms == 0x100 - (8000000 / 64 * 0.001)
  15. #define TIM2_PRESCALER 1<<CS22
  16. #define TIM2_CNT 0x83
  17. /* LEDs */
  18. #define LED_PORT PORTC
  19. #define LED_DDR DDRC
  20. #define LED_1 (1<<PC2)
  21. #define LED_2 (1<<PC3)
  22. #define LED_ON(led) LED_PORT |= led
  23. #define LED_OFF(led) LED_PORT &= ~led
  24. #define LED_TOGGLE(led) LED_PORT ^= led
  25. /* 7-segment LED indikators */
  26. #define INDCTR_NUMS 4
  27. /**
  28. * A
  29. * F B
  30. * G
  31. * E C
  32. * D H
  33. */
  34. #define INDCTR_SEGMENT_PORT PORTB
  35. #define INDCTR_SEGMENT_DDR DDRB
  36. #define INDCTR_SEGMENT_A (1<<PB0)
  37. #define INDCTR_SEGMENT_B (1<<PB2)
  38. #define INDCTR_SEGMENT_C (1<<PB4)
  39. #define INDCTR_SEGMENT_D (1<<PB6)
  40. #define INDCTR_SEGMENT_E (1<<PB7)
  41. #define INDCTR_SEGMENT_F (1<<PB1)
  42. #define INDCTR_SEGMENT_G (1<<PB3)
  43. #define INDCTR_SEGMENT_H (1<<PB5)
  44. #define INDCTR_COMMON_PORT PORTD
  45. #define INDCTR_COMMON_DDR DDRD
  46. #define INDCTR_COMMON_1 (1<<PD0)
  47. #define INDCTR_COMMON_2 (1<<PD1)
  48. #define INDCTR_COMMON_3 (1<<PD2)
  49. #define INDCTR_COMMON_4 (1<<PD3)
  50. #define INDCTR_COMMON_ALL (INDCTR_COMMON_1 | INDCTR_COMMON_2 | INDCTR_COMMON_3 | INDCTR_COMMON_4)
  51. #define INDCTR_COMMON_DOT 1
  52. typedef enum {
  53. Sym_0 = 0xD7,
  54. Sym_1 = 0x14,
  55. Sym_2 = 0xCD,
  56. Sym_3 = 0x5D,
  57. Sym_4 = 0x1E,
  58. Sym_5 = 0x5B,
  59. Sym_6 = 0xDB,
  60. Sym_7 = 0x15,
  61. Sym_8 = 0xDF,
  62. Sym_9 = 0x5F,
  63. Sym_a = 0x9F,
  64. Sym_b = 0xDA,
  65. Sym_c = 0xC8,
  66. Sym_d = 0xDC,
  67. Sym_e = 0xCB,
  68. Sym_f = 0x8B,
  69. Sym_minus = INDCTR_SEGMENT_G,
  70. Sym_minus_t = INDCTR_SEGMENT_A,
  71. Sym_minus_b = INDCTR_SEGMENT_D,
  72. Sym_gradus = 0x0F,
  73. Sym_t = 0xCA,
  74. Sym_dot = INDCTR_SEGMENT_H,
  75. Sym_blank = 0x00
  76. } indctr_sym_t;
  77. /* Convert 0x0-0xF digit to 7-seg indicators symbol */
  78. const indctr_sym_t IndctrNums[16] = {
  79. Sym_0, Sym_1, Sym_2, Sym_3,
  80. Sym_4, Sym_5, Sym_6, Sym_7,
  81. Sym_8, Sym_9, Sym_a, Sym_b,
  82. Sym_c, Sym_d, Sym_e, Sym_f
  83. };
  84. /* DS18B20 termometer */
  85. #define DS18B20_CMD_CONVERTTEMP 0x44
  86. #define DS18B20_CMD_RSCRATCHPAD 0xbe
  87. #define DS18B20_CMD_WSCRATCHPAD 0x4e
  88. #define DS18B20_CMD_CPYSCRATCHPAD 0x48
  89. #define DS18B20_CMD_RECEEPROM 0xb8
  90. #define DS18B20_CMD_RPWRSUPPLY 0xb4
  91. #define DS18B20_CMD_SEARCHROM 0xf0
  92. #define DS18B20_CMD_READROM 0x33
  93. #define DS18B20_CMD_MATCHROM 0x55
  94. #define DS18B20_CMD_SKIPROM 0xcc
  95. #define DS18B20_CMD_ALARMSEARCH 0xec
  96. #define DS18B20_PORT PORTD
  97. #define DS18B20_DDR DDRD
  98. #define DS18B20_PIN PIND
  99. #define DS18B20_PAD PD7
  100. #define DS18B20_INPUT_MODE DS18B20_DDR&=~(1<<DS18B20_PAD)
  101. #define DS18B20_OUTPUT_MODE DS18B20_DDR|=(1<<DS18B20_PAD)
  102. #define DS18B20_LOW DS18B20_PORT&=~(1<<DS18B20_PAD)
  103. #define DS18B20_HIGH DS18B20_PORT|=(1<<DS18B20_PAD)
  104. #define DS18B20_VALUE (DS18B20_PIN & (1<<DS18B20_PAD))
  105. __no_init int8_t Temperature @7;
  106. static uint8_t ds18b20_Reset(void);
  107. static void ds18b20_WriteBit(uint8_t bit);
  108. static uint8_t ds18b20_ReadBit(void);
  109. static uint8_t ds18b20_ReadByte(void);
  110. static void ds18b20_WriteByte(uint8_t byte);
  111. static void ds18b20_StartMeasure(void);
  112. static void ds18b20_ReadTemperature(void);
  113. #define TIM1_PRESCALER (1<<CS11)
  114. /* BTN */
  115. #define BTN_LVL_NO 0xED
  116. #define BTN_LVL_7 0xD8
  117. #define BTN_LVL_6 0xD0
  118. #define BTN_LVL_5 0xC5
  119. #define BTN_LVL_4 0xB4
  120. #define BTN_LVL_3 0x94
  121. #define BTN_LVL_2 0x3F
  122. typedef enum {
  123. btn_no = 0,
  124. btn_1,
  125. btn_2,
  126. btn_3,
  127. btn_4,
  128. btn_5,
  129. btn_6,
  130. btn_7
  131. } btn_t;
  132. /* DS1307 */
  133. #define DS1307_ADR 0x68
  134. #define DS1307_ADR_RD 0xD0
  135. #define DS1307_ADR_WR 0xD1
  136. typedef struct {
  137. uint8_t seconds;
  138. uint8_t minutes;
  139. uint8_t hours;
  140. } time_t;
  141. /* Global varaibles */
  142. __no_init indctr_sym_t Indicator1 @15;
  143. __no_init indctr_sym_t Indicator2 @14;
  144. __no_init indctr_sym_t Indicator3 @13;
  145. __no_init indctr_sym_t Indicator4 @12;
  146. __no_init uint8_t resultADC @11;
  147. __no_init struct {
  148. uint8_t newTime : 1;
  149. uint8_t newTemp : 1;
  150. uint8_t newBTN : 1;
  151. uint8_t getTime : 1;
  152. uint8_t startDS18B20 : 1;
  153. uint8_t readDS18B20 : 1;
  154. uint8_t : 2;
  155. } Flag @10;
  156. __no_init struct {
  157. uint8_t needDot : 1;
  158. uint8_t blinkIndktr : 1;
  159. uint8_t waitIndktr : 1;
  160. uint8_t : 5;
  161. } Flag2 @9;
  162. typedef enum {
  163. show_None = 0,
  164. show_HHMM,
  165. show_MMSS,
  166. show_TEMP,
  167. set_TIME
  168. } state_t;
  169. __no_init state_t State @8;
  170. static uint8_t twi_buf[5];
  171. static time_t Clock;
  172. #define PERIOD_ADC 100
  173. #define PERIOD_TWI 500
  174. #define PERIOD_SHT 5000
  175. #define PERIOD_DS18B20s 10000
  176. #define PERIOD_DS18B20r 750
  177. volatile static uint8_t counterADC;
  178. volatile static uint16_t counterTWI;
  179. volatile static uint16_t counterShowTemp;
  180. volatile static uint16_t counterDS18B20s;
  181. volatile static uint16_t counterDS18B20r;
  182. /* Functions prototypes */
  183. static void showHHMM(void);
  184. static void showMMSS(void);
  185. static void showTEMP(void);
  186. static btn_t getBTN(void);
  187. static void setTime(void);
  188. static void getTime(void);
  189. static uint8_t bcd2bin(uint8_t bcd);
  190. static uint8_t bin2bcd(uint8_t bin);
  191. #endif // __MAIN_H