251209_main_test
This commit is contained in:
parent
edf2acc6cc
commit
63ca975f6f
@ -154,3 +154,124 @@ int uart_putchar(char c, FILE *stream) {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
uint16_t tim1_ic_val = 0;
|
||||||
|
char tim1_ic_flag = 0;
|
||||||
|
|
||||||
|
uint16_t read_chained_timer(void) {
|
||||||
|
uint8_t sreg = SREG;
|
||||||
|
cli();
|
||||||
|
|
||||||
|
uint8_t high_byte = TCNT0;
|
||||||
|
uint8_t low_byte = TCNT1;
|
||||||
|
|
||||||
|
SREG = sreg;
|
||||||
|
sei();
|
||||||
|
|
||||||
|
return ((uint16_t)high_byte << 8) | low_byte;
|
||||||
|
}
|
||||||
|
|
||||||
|
ISR(TIMER1_OVF_vect) {
|
||||||
|
TCNT0++;
|
||||||
|
}
|
||||||
|
|
||||||
|
ISR(PCINT0_vect) {
|
||||||
|
if(!(PINB & (1 << PULSE_IN_PIN))){ // pulsein pin is low
|
||||||
|
TCNT0 = 0x00; // reset tim0
|
||||||
|
TCNT1 = 0x00; // reset tim1
|
||||||
|
}else{ // pulsein pin is high
|
||||||
|
tim1_ic_val = read_chained_timer();
|
||||||
|
tim1_ic_flag = 1;
|
||||||
|
//GIFR |= (1 << PCIF); // Clear the PCINT Flag (PCIF) after processing the interrupt
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int d = 0;
|
||||||
|
int code = 0;
|
||||||
|
|
||||||
|
DDRB &= ~(1 << PULSE_IN_PIN); // set to input
|
||||||
|
// PORTB |= (1 << PULSE_IN_PIN); // enable pullup
|
||||||
|
PCMSK |= (1 << PCINT4); // enable pin change interrupt 4
|
||||||
|
GIMSK |= (1 << PCIE); // enable pin change interrupts
|
||||||
|
|
||||||
|
//TCCR1B |= (1 << CS10); // clk/1 prescaler
|
||||||
|
TCNT1 = 0x00; // reset tim1
|
||||||
|
TCCR1 &= ~((1 << CS13) | (1 << CS12) | (1 << CS11) | (1 << CS10)); // clear prescalers - stop tim1
|
||||||
|
TCCR1 |= (1 << CS12); // CS12 high for clk/8 prescaler
|
||||||
|
//TCCR1 |= (1 << CS11) | (1 << CS10);
|
||||||
|
TIMSK |= (1 << TOIE1); // enable tim1 overflow int
|
||||||
|
|
||||||
|
TCCR0B = 0x00; // clear prescalers to stop tim0
|
||||||
|
TCNT0 = 0x00;
|
||||||
|
|
||||||
|
sei(); // enable int
|
||||||
|
|
||||||
|
|
||||||
|
//if(SERIAL_OUT){
|
||||||
|
uart_init();
|
||||||
|
_delay_ms(500);
|
||||||
|
//}else{
|
||||||
|
// Initialize I/O: Set the NeoPixel pin as an output
|
||||||
|
DDRB |= (1 << NEOPIXEL_PIN);
|
||||||
|
//}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//if(SERIAL_OUT){
|
||||||
|
printf("starting..\n");
|
||||||
|
//}else{
|
||||||
|
//setPixelColor(0, 0, 255, 0);
|
||||||
|
ws2812_send();
|
||||||
|
//}
|
||||||
|
|
||||||
|
while(1){
|
||||||
|
if(tim1_ic_flag){
|
||||||
|
//if(SERIAL_OUT){
|
||||||
|
//printf("%d\n", (int)tim1_ic_val);
|
||||||
|
//}
|
||||||
|
|
||||||
|
if(tim1_ic_val >= 400){
|
||||||
|
if(tim1_ic_val > 2000){
|
||||||
|
//if(SERIAL_OUT){
|
||||||
|
printf(" %d\n", code);
|
||||||
|
//}
|
||||||
|
|
||||||
|
if(code == 7774){
|
||||||
|
//if(SERIAL_OUT){
|
||||||
|
printf("+\n");
|
||||||
|
//}else{
|
||||||
|
setPixelColor(1, 0, 255, 0);
|
||||||
|
ws2812_send();
|
||||||
|
//}
|
||||||
|
}else if(code == 15964){
|
||||||
|
//if(SERIAL_OUT){
|
||||||
|
printf("-\n");
|
||||||
|
//}else{
|
||||||
|
setPixelColor(1, 255, 0, 0);
|
||||||
|
ws2812_send();
|
||||||
|
//}
|
||||||
|
}
|
||||||
|
|
||||||
|
code = 0;
|
||||||
|
}else if(tim1_ic_val > 605){
|
||||||
|
//if(SERIAL_OUT){
|
||||||
|
printf("1");
|
||||||
|
//}
|
||||||
|
++code;
|
||||||
|
}else{
|
||||||
|
//if(SERIAL_OUT){
|
||||||
|
printf("0");
|
||||||
|
//}
|
||||||
|
}
|
||||||
|
code *=2;
|
||||||
|
}
|
||||||
|
|
||||||
|
tim1_ic_flag = 0;
|
||||||
|
}
|
||||||
|
_delay_us(10);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user