//Select: PD5
//all commands end in \n
-// cc -- channel address in hex
+// cc -- channel address in hex
//Scc(0|1|/) -- switch channel off/on/toggle
//Scc? -- read channel status - 2nd char: 'e' in case of overcurrent, 3rd char: enable 1/0
//Ccc? -- read current
usb_serial_write(txbuf,8);
usb_serial_flush_output();
}
-
-
+
+
ISR(TIMER0_OVF_vect) {
time++;
asm volatile("wdr");
- }
+ }
void getdata(uint8_t buf) {
if (rxcnt != 0 || (buf == 'S' || buf == 'A' || buf == 'L' || buf == 'C' || buf == 'I'
if (rxbuf[0] == 'A') {
// do something
}
-
- }
+ }
}
- if (rxcnt >= 7 || buf == '\n' || buf == '\r') { rxcnt = 0; }
- }
+ if (rxcnt >= 7 || buf == '\n' || buf == '\r') { rxcnt = 0; }
+ }
void original_value(uint8_t channel, uint8_t get_value_1, uint8_t get_value_2) {
uint16_t result = (get_value_1 << 8) | get_value_2;
- send_answer_hex(channel,result);
-}
-
+ send_answer_hex(channel,result);
+ }
+
void update_eeprom(void) {
//etc...
// eeprom_update_byte((uint8_t*)0x28,adc_enable);
}
void do_channel(uint8_t channel) {
- uint8_t get_value_1;
- uint8_t get_value_2;
- if(channel<4)
- ADC1_select();
- else
- ADC2_select();
- SPDR = 0b11000011 + ((channel & 0b00000011) << 4); // MSB=Byte1
- while ((SPSR & (1<<SPIF)) == 0);
- get_value_1 = SPDR;
- SPDR = 0b11101011; // LSB=Byte2
- while ((SPSR & (1<<SPIF)) == 0);
- get_value_2 = SPDR;
- if(channel<4)
- ADC1_unselect();
- else
- ADC2_unselect();
- }
+ uint8_t get_value_1;
+ uint8_t get_value_2;
+ if(channel<4)
+ ADC1_select();
+ else
+ ADC2_select();
+ SPDR = 0b11000011 + ((channel & 0b00000011) << 4); // MSB=Byte1
+ while ((SPSR & (1<<SPIF)) == 0) {};
+ get_value_1 = SPDR;
+ SPDR = 0b11101011; // LSB=Byte2
+ while ((SPSR & (1<<SPIF)) == 0) {};
+ get_value_2 = SPDR;
+ if(channel<4)
+ ADC1_unselect();
+ else
+ ADC2_unselect();
+ }
void read_channel(uint8_t channel) {
- uint8_t get_value_1;
- uint8_t get_value_2;
- if(channel<4)
- ADC1_select();
- else
- ADC2_select();
- SPDR = 0b00000000; // MSB=Byte1
- while ((SPSR & (1<<SPIF)) == 0);
- get_value_1 = SPDR;
- SPDR = 0b00000000; // LSB=Byte2
- while ((SPSR & (1<<SPIF)) == 0);
- get_value_2 = SPDR;
- if(channel<4)
- ADC1_unselect();
- else
- ADC2_unselect();
-
- original_value(channel, get_value_1, get_value_2);
-}
+ uint8_t get_value_1;
+ uint8_t get_value_2;
+ if(channel<4)
+ ADC1_select();
+ else
+ ADC2_select();
+ SPDR = 0b00000000; // MSB=Byte1
+ while ((SPSR & (1<<SPIF)) == 0) {};
+ get_value_1 = SPDR;
+ SPDR = 0b00000000; // LSB=Byte2
+ while ((SPSR & (1<<SPIF)) == 0) {};
+ get_value_2 = SPDR;
+ if(channel<4)
+ ADC1_unselect();
+ else
+ ADC2_unselect();
+ original_value(channel, get_value_1, get_value_2);
+ }
__attribute__((naked)) int main(void) {
-
+
CLKPR = (1 << CLKPCE); // prescaler 2 = 8 MHz
CLKPR = (1 << CLKPS0); // prescaler 2
- usb_init();
+ usb_init();
// Configure ports
//UART: RX PD2, TX PD3
//SPI: out: PB2, PB1 ; in: PB3
//CS: PB7, PB0
-
+
MCUCR |= (1<<JTD);
MCUCR |= (1<<JTD); //yes, twice
-
+
PORTB = 0b11011001;
DDRB = 0b10000111;
PORTF = 0b11010011;
DDRF = 0b01100000;
- PORTC = 0b11000000;
+ PORTC = 0b11000000;
DDRC = 0b00000000;
PORTE = 0b01000000;
DDRE = 0b00000000;
-
- //limit[3] |= eeprom_read_byte((uint8_t*)0x26);
-
+
+ //limit[3] |= eeprom_read_byte((uint8_t*)0x26);
+
//Timer0 at ~488 Hz overflow for ADC control and buttons
TCCR0B = 4 << CS00; //(1 << CS01) | (1 << CS00);
TIMSK0 = (1 << TOIE0); //Overflow interrupt`
-
+
//SPI 2 MHz, Master, mode 1, no interrupts
SPCR = (1 << SPE) | (1 << MSTR) | (0 << CPOL) | (1 << CPHA) | ( 0 << SPR0);
//Watchdog at .5 seconds
WDTCSR = (1<<WDCE);
- WDTCSR = (1<<WDE) | (5<<WDP0);
- sei();
-
+ WDTCSR = (1<<WDE) | (5<<WDP0);
+ sei();
+
uint16_t lasttime = 0;
uint16_t step = 0;
getdata(n);
n = usb_serial_getchar();
}
-
+
if(time != lasttime) {
+ /*
+ LED2_ON();
+ _delay_us(500);
+ LED2_OFF();
+ _delay_us(500);
+ */
+
switch(step++) {
case 10: do_channel(0); break;
case 20: read_channel(0); break;
step = 0;
}
}
-
+
// update_eeprom();
lasttime = time;
}