main.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /**
  2. * Dryer
  3. */
  4. #include "main.h"
  5. #include "i2c.h"
  6. #include "sensor.h"
  7. #include "rtos/rtos.h"
  8. #include "ssd1306xled/font6x8.h"
  9. #include "ssd1306xled/font8x16.h"
  10. #include "ssd1306xled/ssd1306xled.h"
  11. #include "ssd1306xled/ssd1306xledtx.h"
  12. #include "tinyavrlib/num2str.h"
  13. #include "string.h"
  14. /* Defines */
  15. /* Variables */
  16. static volatile struct {
  17. uint8_t newTempSet: 1;
  18. uint8_t AHT10state: 1;
  19. uint8_t rezerv: 6;
  20. } Flag;
  21. uint8_t TemperatureSetpoint;
  22. aht20_t Sensor;
  23. /* Function prototypes */
  24. static void board_Init(void);
  25. static void checkTemperatureSetpoint(void);
  26. static void sensorStart(void);
  27. static void sensorGetData(void);
  28. static void LedOn(void);
  29. static void LedOff(void);
  30. int main(void) {
  31. /* Init all */
  32. board_Init();
  33. RTOS_Init();
  34. I2C_Init();
  35. AHT20_Init();
  36. tdelay_ms(40);
  37. ssd1306_init();
  38. /* Clear flags */
  39. Flag.newTempSet = 1;
  40. Flag.AHT10state = 0;
  41. /* Set tasks */
  42. RTOS_SetTask(checkTemperatureSetpoint, 5, 50);
  43. RTOS_SetTask(sensorStart, 50, 1000);
  44. RTOS_SetTask(sensorGetData, 800, 1000);
  45. RTOS_SetTask(LedOn, 1, 1000);
  46. RTOS_SetTask(LedOff, 201, 1000);
  47. ssd1306_clear();
  48. // ssd1306tx_init(ssd1306xled_font6x8data, 0);
  49. ssd1306tx_stringxy((uint8_t const *)ssd1306xled_font8x16data, 0, 0, "Hello, World! :)");
  50. // ssd1306_setpos(0, 3);
  51. // ssd1306tx_string("SSD1306xLED Library");
  52. tdelay_ms(1000);
  53. ssd1306_clear();
  54. ssd1306tx_stringxy((uint8_t const *)ssd1306xled_font8x16data, 1, 0, "Set:");
  55. ssd1306tx_stringxy((uint8_t const *)ssd1306xled_font8x16data, 1, 3, " T:");
  56. ssd1306tx_stringxy((uint8_t const *)ssd1306xled_font8x16data, 1, 5, " H:");
  57. char buffer[9] = {0};
  58. uint8_t digits = 0;
  59. /* Infinity loop */
  60. do {
  61. if (Flag.newTempSet != 0) {
  62. Flag.newTempSet = 0;
  63. if (TemperatureSetpoint != 0) {
  64. digits = usint2decascii(TemperatureSetpoint, buffer);
  65. strncpy(buffer+5, " C \0", 4);
  66. ssd1306tx_stringxy((uint8_t const *)ssd1306xled_font8x16data, 40, 0, buffer+digits);
  67. } else {
  68. ssd1306tx_stringxy((uint8_t const *)ssd1306xled_font8x16data, 40, 0, " Off");
  69. }
  70. }
  71. if (Flag.AHT10state == 0) {
  72. digits = usint2decascii(Sensor.Temperature, buffer);
  73. strncpy(buffer+5, " C\0", 3);
  74. ssd1306tx_stringxy((uint8_t const *)ssd1306xled_font8x16data, 40, 3, buffer+digits);
  75. digits = usint2decascii(Sensor.Humidity, buffer);
  76. strncpy(buffer+5, " %\0", 3);
  77. ssd1306tx_stringxy((uint8_t const *)ssd1306xled_font8x16data, 40, 5, buffer+digits);
  78. } else {
  79. //ssd1306tx_string("AHT10 error");
  80. ssd1306tx_stringxy((uint8_t const *)ssd1306xled_font8x16data, 40, 3, "Error");
  81. ssd1306tx_stringxy((uint8_t const *)ssd1306xled_font8x16data, 40, 5, " ");
  82. }
  83. RTOS_DispatchTask();
  84. // nothing to do - sleep, wait for interrupt
  85. #if defined(__GNUC__)
  86. set_sleep_mode(SLEEP_MODE_IDLE);
  87. sleep_mode();
  88. #elif defined(__ICCAVR__)
  89. MCUCR = 1<<SE;
  90. __sleep();
  91. #endif
  92. } while (1);
  93. }
  94. /**
  95. * S u b r o u t i n e s
  96. */
  97. /**
  98. * @brief Initialize periphery
  99. */
  100. static void board_Init(void) {
  101. /* power off Analog Comparator */
  102. ACSR = ACD;
  103. /* GPIO */
  104. DDRB = 0x22; // PB1 - Triac control, PB5 - Green LED
  105. DDRC = 0x30; // I2C output
  106. PORTB = 0x03; // close Triac, enable pull-up for D8
  107. PORTC = 0x3e; // I2C = 1
  108. PORTD = 0xff; // enable pull-up for D0-D7 (Switch)
  109. /* Timer0 - RTOS & tdelay_ms() */
  110. TCCR0B = ((0<<CS02)|(1<<CS01)|(1<<CS00));
  111. TCNT0 = TIMER0_CNT;
  112. TIMSK0 |= (1<<TOIE0);
  113. /* Timer1 */
  114. /* Timer2 - refresh screen values */
  115. //TCCR2 = 0x00;
  116. //TCNT2 = 0x00;
  117. //TIMSK |= _BV(TOIE2);
  118. /* Enable Interrupts */
  119. ENABLE_INTERRUPT;
  120. }
  121. static void checkTemperatureSetpoint(void) {
  122. static uint8_t oldTempSet = 0;
  123. uint8_t pvalue;
  124. pvalue = ~(PIND);
  125. if (pvalue != 0) {
  126. switch (pvalue) {
  127. case 1<<PB0:
  128. TemperatureSetpoint = 55;
  129. break;
  130. case 1<<PB1:
  131. TemperatureSetpoint = 50;
  132. break;
  133. case 1<<PB2:
  134. TemperatureSetpoint = 60;
  135. break;
  136. case 1<<PB3:
  137. TemperatureSetpoint = 65;
  138. break;
  139. case 1<<PB4:
  140. TemperatureSetpoint = 70;
  141. break;
  142. case 1<<PB5:
  143. TemperatureSetpoint = 75;
  144. break;
  145. case 1<<PB6:
  146. TemperatureSetpoint = 80;
  147. break;
  148. case 1<<PB7:
  149. TemperatureSetpoint = 85;
  150. break;
  151. default:
  152. TemperatureSetpoint = 0;
  153. }
  154. } else {
  155. pvalue = (~(PINB)) & (1<<PB0);
  156. if (pvalue != 0) {
  157. TemperatureSetpoint = 90;
  158. } else {
  159. TemperatureSetpoint = 0;
  160. }
  161. }
  162. if (oldTempSet != TemperatureSetpoint) {
  163. Flag.newTempSet = 1;
  164. oldTempSet = TemperatureSetpoint;
  165. }
  166. }
  167. static void sensorStart(void) {
  168. aht20_st_t status;
  169. status = AHT20_StartMeasure();
  170. if (status != AHT_St_OK) {
  171. Flag.AHT10state = 1;
  172. } else {
  173. Flag.AHT10state = 0;
  174. }
  175. }
  176. static void sensorGetData(void) {
  177. aht20_st_t status;
  178. status = AHT20_GetData(&Sensor);
  179. if (status != AHT_St_OK) {
  180. Flag.AHT10state = 1;
  181. } else {
  182. Flag.AHT10state = 0;
  183. }
  184. }
  185. static void LedOn(void) {
  186. PORTB |= (1<<PB5);
  187. }
  188. static void LedOff(void) {
  189. PORTB &= ~(1<<PB5);
  190. }
  191. /**
  192. * I n t e r r u p t s
  193. */
  194. #if defined(__GNUC__)
  195. /**
  196. * @brief заглушка для неиспользуемых прерываний
  197. */
  198. ISR(__vector_default,ISR_NAKED) {
  199. reti();
  200. }
  201. #endif