heater3 (Конфликтующая копия с компьютера L362Com 2019-06-26).c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. #include "heater3.h"
  2. volatile uint8_t btn_pressed = 0;
  3. volatile uint8_t rxbyte = 0;
  4. volatile uint8_t rxcounter = 0;
  5. volatile uint8_t rxbuffer[8];
  6. volatile uint8_t tick = 0;
  7. FIFO(64) fifo0;
  8. //ISR(PCINT3_vect)
  9. //{
  10. //PCICR &= ~(1 << PCIE3);
  11. //btn_pressed = ~((PIND >> 4) | 0xF0);
  12. //}
  13. ISR(USART0_RX_vect)
  14. {
  15. rxbyte = UDR0;
  16. if(rxcounter || (rxbyte == 0x55))
  17. {
  18. rxbuffer[rxcounter] = rxbyte;
  19. rxcounter++;
  20. }
  21. }
  22. ISR(USART0_TX_vect)
  23. {
  24. if(!FIFO_IS_EMPTY(fifo0))
  25. {
  26. UDR0 = FIFO_FRONT(fifo0);
  27. FIFO_POP(fifo0);
  28. }
  29. }
  30. ISR(TIMER1_COMPA_vect)
  31. {
  32. tick = 1;
  33. }
  34. void init()
  35. {
  36. /* Watchdog clear*/
  37. MCUSR &= ~(1 << WDRF);
  38. WDTCSR |= (1 << WDCE) | (1 << WDE);
  39. WDTCSR = 0x00;
  40. /* GPIO Initialization */
  41. PORTC |= (1 << LED1) | (1 << LED2) | (1 << GATE1) | (1 << GATE2);
  42. DDRC |= (1 << LED1) | (1 << LED2) | (1 << GATE1) | (1 << GATE2);
  43. /* UART Initialization */
  44. TXD_PORT |= (1 << TXD_BIT);
  45. TXD_DDR |= (1 << TXD_BIT);
  46. UBRR0H = 0;
  47. UBRR0L = 38;
  48. UCSR0A = (1 << U2X0);
  49. UCSR0B = (1 << RXCIE0) | (1 << TXCIE0) | (1 << RXEN0) | (1 << TXEN0);
  50. UCSR0C = (1 << UCSZ00) | (1 << UCSZ01);
  51. /* SPI Initialization */
  52. DDRB |= (1 << MOSI_BIT) | (1 << SCK_BIT) | (1 << SS_BIT) | (7 << PORTB0);
  53. PORTB |= (1 << MOSI_BIT) | (1 << SCK_BIT) | (1 << SS_BIT);
  54. SPCR = (1 << SPE) | (1 << MSTR);
  55. /* TWI Initialization */
  56. TWBR = (uint8_t)(((F_CPU / TWI_FREQ) - 16) / 2);
  57. TWCR = (1 << TWEN);
  58. /* Timer1 Initialization */
  59. TCCR1B = (1 << CS12) | (1 << CS10) | (1 << WGM12);
  60. OCR1A = 0x1000;
  61. TIMSK1 = (1 << OCIE1A);
  62. /* ADC Initialization */
  63. ADMUX |= (1 << REFS0) | (1 << REFS1);
  64. ADCSRA = (1 << ADEN) | (1 << ADPS2);
  65. DIDR0 = 0x0F;
  66. //PCICR = (1 << PCIE3);
  67. //PCMSK3 = 0xF0;
  68. sei();
  69. }
  70. void POT_SendData(uint8_t data, potnum_t potnum)
  71. {
  72. PORTB = (PORTB & 0xF8) | (potnum & 0x07);
  73. PORTB &= ~(1 << SS_BIT);
  74. SPDR = 0x11;
  75. while(!(SPSR & (1 <<SPIF)));
  76. SPDR = data;
  77. while(!(SPSR & (1 <<SPIF)));
  78. PORTB |= (1 << SS_BIT);
  79. }
  80. btw32_t TK_ReadData(void)
  81. {
  82. uint8_t i = 0;
  83. btw32_t data;
  84. PORTB = (PORTB & 0xF8) | 0x04;
  85. PORTB &= ~(1 << SS_BIT);
  86. for(i = 0; i < 4; i++)
  87. {
  88. SPDR = 0xFF;
  89. while(!(SPSR & (1 <<SPIF)));
  90. data.byte[3 - i] = SPDR;
  91. }
  92. PORTB |= (1 << SS_BIT);
  93. return(data);
  94. }
  95. static int lcd_putchar(char c, FILE *stream)
  96. {
  97. LCD_SendData(c);
  98. return 0;
  99. }
  100. static int uart_putchar(char c, FILE *stream)
  101. {
  102. cli();
  103. if(UCSR0A & (1 << UDRIE0))
  104. {
  105. UDR0 = c;
  106. }
  107. else
  108. {
  109. if(!FIFO_IS_FULL(fifo0))
  110. {
  111. FIFO_PUSH(fifo0, c);
  112. }
  113. }
  114. sei();
  115. return 0;
  116. }
  117. /*
  118. void transmit(packet_str *packet)
  119. {
  120. uint8_t i = 0;
  121. if(txcomplete)
  122. {
  123. txcomplete = 0;
  124. packet->checksum = 0;
  125. for(i = 1; i < (TX_LENGTH - 1); i++)
  126. {
  127. packet->checksum += ((uint8_t*)packet)[i];
  128. }
  129. txbuffer = (uint8_t*)packet;
  130. txcounter = TX_LENGTH - 1;
  131. UDR0 = txbuffer[0];
  132. }
  133. }
  134. */
  135. void exec_cmd(rxdata_str *rxdata)
  136. {
  137. /*
  138. switch(rxdata->command)
  139. {
  140. case CMD_RESET:
  141. break;
  142. case CMD_CHONOFF:
  143. break;
  144. case CMD_SETTEMP:
  145. break;
  146. }*/
  147. }
  148. int main()
  149. {
  150. uint16_t i = 0;
  151. uint8_t adc_ch = 0;
  152. uint16_t adc_data = 0;
  153. uint8_t pot_data[4] = { 0x00, 0x55, 0xAA, 0xFF };
  154. //btw32_t tk_data;
  155. heater_str heater_ch[2];
  156. //rxdata_str rxdata;
  157. for(i = 0; i < 2; i++)
  158. {
  159. heater_ch[i].start = 0;
  160. heater_ch[i].status.rsv = 0;
  161. heater_ch[i].status.channel = i;
  162. heater_ch[i].status.onoff = 0;
  163. heater_ch[i].status.ocp = 0;
  164. heater_ch[i].preset = 0;
  165. heater_ch[i].tcouple = 0;
  166. heater_ch[i].tmeas = 0;
  167. heater_ch[i].tmeas_raw = 0;
  168. heater_ch[i].tset = 0;
  169. heater_ch[i].tset_raw = 0;
  170. heater_ch[i].current = 0;
  171. heater_ch[i].checksum = 0;
  172. }
  173. //static FILE lcd_stdout = FDEV_SETUP_STREAM(lcd_putchar, NULL, _FDEV_SETUP_WRITE);
  174. static FILE uart_stdout = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE);
  175. stdout = &uart_stdout;
  176. init();
  177. //printf("HELLO NIGGA\n");
  178. //LCD_Init();
  179. /*
  180. POT_SendData(pot_data[0], 0);
  181. POT_SendData(pot_data[1], 1);
  182. POT_SendData(pot_data[2], 2);
  183. POT_SendData(pot_data[3], 3);
  184. */
  185. //transmit(&packet_ch[0]);
  186. i = 0;
  187. ADCSRA |= (1 << ADSC);
  188. TCNT1 = 0x00;
  189. PORTC &= ~(1 << GATE1);
  190. while(1)
  191. {
  192. if((rxcounter == 2) && (rxbuffer[1] == 'R'))
  193. {
  194. cli();
  195. WDTCSR |= (1 << WDCE) | (1 << WDE);
  196. while(1);
  197. }
  198. if(rxcounter == 8)
  199. {
  200. rxcounter = 0;
  201. uint8_t temp = 0;
  202. for(i = 1; i < 7; i++)
  203. {
  204. temp += rxbuffer[i];
  205. }
  206. if(temp == rxbuffer[7])
  207. {
  208. exec_cmd((rxdata_str*)rxbuffer);
  209. }
  210. }
  211. if(tick)
  212. {
  213. tick = 0;
  214. adc_data = ADCL;
  215. adc_data |= ((uint16_t)ADCH) << 8;
  216. switch(adc_ch)
  217. {
  218. case ADC_MEAS1:
  219. heater_ch[0].tmeas_raw = adc_data;
  220. printf("T0: %.4u\n", heater_ch[0].tmeas_raw);
  221. break;
  222. case ADC_MEAS2:
  223. heater_ch[1].tmeas_raw = adc_data;
  224. printf("T1: %.4u\n", heater_ch[1].tmeas_raw);
  225. break;
  226. case ADC_CUR1:
  227. heater_ch[0].current = (uint16_t)((float)adc_data * 2.28);
  228. printf("C0: %.4u\n", heater_ch[0].current);
  229. break;
  230. case ADC_CUR2:
  231. heater_ch[1].current = (uint16_t)((float)adc_data * 2.28);;
  232. printf("C1: %.4u\n", heater_ch[1].current);
  233. break;
  234. }
  235. //printf("T0: %.4u, T1: %.4u \n", heater_ch[0].tmeas_raw, heater_ch[1].tmeas_raw);
  236. //printf("C0: %.4u, C1: %.4u \n", heater_ch[0].current, heater_ch[1].current);
  237. if(++adc_ch > 0x03)
  238. adc_ch = 0;
  239. ADMUX = (ADMUX & 0xFC) | adc_ch;
  240. ADCSRA |= (1 << ADSC);
  241. }
  242. //fprintf(&lcd_stdout, "CH0: %.3d / %.3d ", packet_ch[0].tmeas, packet_ch[0].tset);
  243. //if(i & (0x01))
  244. //fprintf(&lcd_stdout, "\xD9");
  245. //else
  246. //fprintf(&lcd_stdout, " ");
  247. //transmit(&packet_ch[0]);
  248. /*
  249. LCD_SetPos(0, 1);
  250. fprintf(&lcd_stdout, "CH1: %.3d / %.3d ", packet_ch[1].tmeas, packet_ch[1].tset);
  251. if(i & 0x01)
  252. fprintf(&lcd_stdout, "\xD9");
  253. else
  254. fprintf(&lcd_stdout, " ");
  255. //transmit(&packet_ch[1]);*/
  256. /*
  257. switch(btn_pressed)
  258. {
  259. case 0x01:
  260. POT_SendData(++pot_data[0], 0);
  261. btn_pressed = 0;
  262. break;
  263. case 0x02:
  264. POT_SendData(++pot_data[1], 1);
  265. btn_pressed = 0;
  266. break;
  267. case 0x04:
  268. POT_SendData(++pot_data[2], 2);
  269. btn_pressed = 0;
  270. break;
  271. case 0x08:
  272. POT_SendData(++pot_data[3], 3);
  273. btn_pressed = 0;
  274. break;
  275. }*/
  276. }
  277. }