123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319 |
- #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 <<SPIF)));
- SPDR = data;
- while(!(SPSR & (1 <<SPIF)));
- PORTB |= (1 << SS_BIT);
- }
- btw32_t TK_ReadData(void)
- {
- uint8_t i = 0;
- btw32_t data;
-
- PORTB = (PORTB & 0xF8) | 0x04;
- PORTB &= ~(1 << SS_BIT);
- for(i = 0; i < 4; i++)
- {
- SPDR = 0xFF;
- while(!(SPSR & (1 <<SPIF)));
- data.byte[3 - i] = SPDR;
- }
- PORTB |= (1 << SS_BIT);
- return(data);
- }
- static int lcd_putchar(char c, FILE *stream)
- {
- LCD_SendData(c);
- return 0;
- }
- static int uart_putchar(char c, FILE *stream)
- {
- cli();
- if(UCSR0A & (1 << UDRIE0))
- {
- UDR0 = c;
- }
- else
- {
- if(!FIFO_IS_FULL(fifo0))
- {
- FIFO_PUSH(fifo0, c);
- }
- }
- sei();
- return 0;
- }
- /*
- void transmit(packet_str *packet)
- {
- uint8_t i = 0;
- if(txcomplete)
- {
- txcomplete = 0;
- packet->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;
- }*/
- }
- }
|