main.h 6.0 KB

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