-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcycloscope_photo_trigger.c_da_pulire
152 lines (114 loc) · 3.96 KB
/
cycloscope_photo_trigger.c_da_pulire
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// ------- Preamble -------- //
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include "pinDefines.h"
#include <avr/sleep.h>
#include <avr/power.h>
#define USART_BAUDRATE 9600
#define BAUD_PRESCALE (((F_CPU/(USART_BAUDRATE*16UL)))-1)
#ifndef UCSRB
# define UCSRB UCSR0B
# define RXEN RXEN0
# define TXEN TXEN0
# define UCSRC UCSR0C
#define UBRRH UBRR0H
#define UBRRL UBRR0L
#define UCSZ0 UCSZ00
#define UCSZ1 UCSZ01
#endif
#define DELAY 3
// ------- Global Variables ---------- //
volatile int x=0;
volatile uint8_t tot_overflow;
volatile int f_wdt=1;
void initInterrupt0(void) { //Imposta i registri per abilitare Interrupt sul pin INT0
EIMSK |= (1 << INT0);
EICRA |= (1 << ISC00);
sei();
}
ISR(INT0_vect) { //Imposta la routine da eseguire quando si riceve una variazione solo pin INT0
if (bit_is_set(BUTTON_PIN, BUTTON)) {
LED_PORT |= (1 << LED1);
x=x+1;
} else {
LED_PORT &= ~(1 << LED1);
}
if (x>=10) { //Impostare a 238 per i test su strada//
LED_PORT |= (1 << LED0);
// _delay_ms(100);
// LED_PORT &= ~(1 << LED0);
x=0;
}
}
/*
ISR(USART_RX_vect) {
char ReceivedByte ;
ReceivedByte = UDR0 ; // Fetch the received byte value into the variable " ByteReceived "
UDR0 = ReceivedByte ; // Echo back the received byte back to the computer
}
ISR (TIMER1_OVF_vect) {
UDR0 ='a';
}
*/
void enterSleep(void)
{
set_sleep_mode(SLEEP_MODE_PWR_SAVE); /* EDIT: could also use SLEEP_MODE_PWR_DOWN for lowest power consumption. */
sleep_enable();
/* Now enter sleep mode. */
sleep_mode();
/* The program will continue from here after the WDT timeout*/
sleep_disable(); /* First thing to do is disable sleep. */
/* Re-enable the peripherals. */
power_all_enable();
}
int main(void) {
LED_DDR = 0xff; /* all LEDs active */
BUTTON_PORT |= (1 << BUTTON); /* pullup */
initInterrupt0();
// initTimer0();
/* Clear the reset flag. */
MCUSR &= ~(1<<WDRF);
/* In order to change WDE or the prescaler, we need to
* set WDCE (This will allow updates for 4 clock cycles).
*/
WDTCSR |= (1<<WDCE) | (1<<WDE);
/* set new watchdog timeout prescaler value */
WDTCSR = 1<<WDP0 | 1<<WDP3; /* 8.0 seconds */
/* Enable the WD interrupt (note no reset). */
WDTCSR |= _BV(WDIE);
// //UCSRB = (1 << RXEN ) | (1 << TXEN ) ; // Turn on the transmission and reception circuitry
// //UCSRC = (1 << URSEL ) | (1 << UCSZ0 ) | (1 << UCSZ1 ) ; // Use 8 - bit character sizes
// UCSR0B = _BV(RXEN0) | _BV(TXEN0); /* Enable RX and TX */
// UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); /* 8-bit data */
// UBRRH = ( BAUD_PRESCALE >> 8) ; // Load upper 8 - bits of the baud rate value into the high byte of the UBRR register
// UBRRL = BAUD_PRESCALE ; // Load lower 8 - bits of the baud rate value into the low byte of the UBRR register
// UCSRB |= (1 << RXCIE0 ) ; // Enable the USART Recieve Complete interrupt ( USART_RXC )
//// sei () ; // Enable the Global Interrupt Enable flag so that interrupts can be processed
// UDR0 ='a';
// TIMSK1 |= (1 << TOIE1); // enable timer interrupt
//// TCCR1B |= (1 << CS12) | (1 << CS11) | (1 << CS10); // Turn on the counter
// TCCR1B |= (1 << CS12) | (1 << CS10); // Turn on the counter
// sei () ; // Enable the Global Interrupt Enable flag so that interrupts can be processed
// set_sleep_mode (SLEEP_MODE_PWR_DOWN); /* Queste 3 istruzioni abilitano il modo in basso consumo della CPU */
// sleep_enable();
// while (1) {
// sleep_cpu ();
// }
while (1) {
if(f_wdt == 1)
{
/* Toggle the LED */
LED_PORT ^= (1 << LED0);
/* Don't forget to clear the flag. */
f_wdt = 0;
/* Re-enter sleep mode. */
enterSleep();
}
else
{
/* Do nothing. */
}
}
return (0); /* This line is never reached */
}