#include "heater3.h" volatile uint8_t btn_pressed = 0; volatile uint8_t rxbyte = 0; volatile uint8_t rxcounter = 0; volatile uint8_t rxbuffer[8]; volatile uint8_t tick = 0; FIFO(64) fifo0; //ISR(PCINT3_vect) //{ //PCICR &= ~(1 << PCIE3); //btn_pressed = ~((PIND >> 4) | 0xF0); //} ISR(USART0_RX_vect) { rxbyte = UDR0; if(rxcounter || (rxbyte == 0x55)) { rxbuffer[rxcounter] = rxbyte; rxcounter++; } } ISR(USART0_TX_vect) { if(!FIFO_IS_EMPTY(fifo0)) { UDR0 = FIFO_FRONT(fifo0); FIFO_POP(fifo0); } } ISR(TIMER1_COMPA_vect) { tick = 1; } void init() { /* Watchdog clear*/ MCUSR &= ~(1 << WDRF); WDTCSR |= (1 << WDCE) | (1 << WDE); WDTCSR = 0x00; /* GPIO Initialization */ PORTC |= (1 << LED1) | (1 << LED2) | (1 << GATE1) | (1 << GATE2); DDRC |= (1 << LED1) | (1 << LED2) | (1 << GATE1) | (1 << GATE2); /* UART Initialization */ TXD_PORT |= (1 << TXD_BIT); TXD_DDR |= (1 << TXD_BIT); UBRR0H = 0; UBRR0L = 38; UCSR0A = (1 << U2X0); UCSR0B = (1 << RXCIE0) | (1 << TXCIE0) | (1 << RXEN0) | (1 << TXEN0); UCSR0C = (1 << UCSZ00) | (1 << UCSZ01); /* SPI Initialization */ DDRB |= (1 << MOSI_BIT) | (1 << SCK_BIT) | (1 << SS_BIT) | (7 << PORTB0); PORTB |= (1 << MOSI_BIT) | (1 << SCK_BIT) | (1 << SS_BIT); SPCR = (1 << SPE) | (1 << MSTR); /* TWI Initialization */ TWBR = (uint8_t)(((F_CPU / TWI_FREQ) - 16) / 2); TWCR = (1 << TWEN); /* Timer1 Initialization */ TCCR1B = (1 << CS12) | (1 << CS10) | (1 << WGM12); OCR1A = 0x1000; TIMSK1 = (1 << OCIE1A); /* ADC Initialization */ ADMUX |= (1 << REFS0) | (1 << REFS1); ADCSRA = (1 << ADEN) | (1 << ADPS2); DIDR0 = 0x0F; //PCICR = (1 << PCIE3); //PCMSK3 = 0xF0; sei(); } void POT_SendData(uint8_t data, potnum_t potnum) { PORTB = (PORTB & 0xF8) | (potnum & 0x07); PORTB &= ~(1 << SS_BIT); SPDR = 0x11; while(!(SPSR & (1 <checksum = 0; for(i = 1; i < (TX_LENGTH - 1); i++) { packet->checksum += ((uint8_t*)packet)[i]; } txbuffer = (uint8_t*)packet; txcounter = TX_LENGTH - 1; UDR0 = txbuffer[0]; } } */ void exec_cmd(rxdata_str *rxdata) { /* switch(rxdata->command) { case CMD_RESET: break; case CMD_CHONOFF: break; case CMD_SETTEMP: break; }*/ } int main() { uint16_t i = 0; uint8_t adc_ch = 0; uint16_t adc_data = 0; uint8_t pot_data[4] = { 0x00, 0x55, 0xAA, 0xFF }; //btw32_t tk_data; heater_str heater_ch[2]; //rxdata_str rxdata; for(i = 0; i < 2; i++) { heater_ch[i].start = 0; heater_ch[i].status.rsv = 0; heater_ch[i].status.channel = i; heater_ch[i].status.onoff = 0; heater_ch[i].status.ocp = 0; heater_ch[i].preset = 0; heater_ch[i].tcouple = 0; heater_ch[i].tmeas = 0; heater_ch[i].tmeas_raw = 0; heater_ch[i].tset = 0; heater_ch[i].tset_raw = 0; heater_ch[i].current = 0; heater_ch[i].checksum = 0; } //static FILE lcd_stdout = FDEV_SETUP_STREAM(lcd_putchar, NULL, _FDEV_SETUP_WRITE); static FILE uart_stdout = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE); stdout = &uart_stdout; init(); //printf("HELLO NIGGA\n"); //LCD_Init(); /* POT_SendData(pot_data[0], 0); POT_SendData(pot_data[1], 1); POT_SendData(pot_data[2], 2); POT_SendData(pot_data[3], 3); */ //transmit(&packet_ch[0]); i = 0; ADCSRA |= (1 << ADSC); TCNT1 = 0x00; PORTC &= ~(1 << GATE1); while(1) { if((rxcounter == 2) && (rxbuffer[1] == 'R')) { cli(); WDTCSR |= (1 << WDCE) | (1 << WDE); while(1); } if(rxcounter == 8) { rxcounter = 0; uint8_t temp = 0; for(i = 1; i < 7; i++) { temp += rxbuffer[i]; } if(temp == rxbuffer[7]) { exec_cmd((rxdata_str*)rxbuffer); } } if(tick) { tick = 0; adc_data = ADCL; adc_data |= ((uint16_t)ADCH) << 8; switch(adc_ch) { case ADC_MEAS1: heater_ch[0].tmeas_raw = adc_data; printf("T0: %.4u\n", heater_ch[0].tmeas_raw); break; case ADC_MEAS2: heater_ch[1].tmeas_raw = adc_data; printf("T1: %.4u\n", heater_ch[1].tmeas_raw); break; case ADC_CUR1: heater_ch[0].current = (uint16_t)((float)adc_data * 2.28); printf("C0: %.4u\n", heater_ch[0].current); break; case ADC_CUR2: heater_ch[1].current = (uint16_t)((float)adc_data * 2.28);; printf("C1: %.4u\n", heater_ch[1].current); break; } //printf("T0: %.4u, T1: %.4u \n", heater_ch[0].tmeas_raw, heater_ch[1].tmeas_raw); //printf("C0: %.4u, C1: %.4u \n", heater_ch[0].current, heater_ch[1].current); if(++adc_ch > 0x03) adc_ch = 0; ADMUX = (ADMUX & 0xFC) | adc_ch; ADCSRA |= (1 << ADSC); } //fprintf(&lcd_stdout, "CH0: %.3d / %.3d ", packet_ch[0].tmeas, packet_ch[0].tset); //if(i & (0x01)) //fprintf(&lcd_stdout, "\xD9"); //else //fprintf(&lcd_stdout, " "); //transmit(&packet_ch[0]); /* LCD_SetPos(0, 1); fprintf(&lcd_stdout, "CH1: %.3d / %.3d ", packet_ch[1].tmeas, packet_ch[1].tset); if(i & 0x01) fprintf(&lcd_stdout, "\xD9"); else fprintf(&lcd_stdout, " "); //transmit(&packet_ch[1]);*/ /* switch(btn_pressed) { case 0x01: POT_SendData(++pot_data[0], 0); btn_pressed = 0; break; case 0x02: POT_SendData(++pot_data[1], 1); btn_pressed = 0; break; case 0x04: POT_SendData(++pot_data[2], 2); btn_pressed = 0; break; case 0x08: POT_SendData(++pot_data[3], 3); btn_pressed = 0; break; }*/ } }