-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSirenCode.c
205 lines (168 loc) · 6.07 KB
/
SirenCode.c
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/************************************************************************/
/* EGB24 - Assessment 1 Part II */
/* */
/* */
/* Harrison Leach */
/* June 2022 */
/* */
/************************************************************************/
/************************************************************************/
/* INCLUDED LIBRARIES/HEADER FILES */
/************************************************************************/
#include <avr/io.h>
#include <avr/interrupt.h>
/************************************************************************/
/* DEFINES */
/************************************************************************/
#define S1 0x10
#define S2 0x20
#define S3 0x40
#define S4 0x80
#define LED1 0x10
#define LED2 0x20
#define LED3 0x40
#define ticks_one_sec 16000000
#define ticks_half_sec 8000000
#define F_INC 250
#define F_MAX_INIT 4900
#define F_MIN_INIT 2600
#define LEDS_OFF 0x0F
/************************************************************************/
/* ENUM DEFINITIONS */
/************************************************************************/
enum {
STATE1,
STATE2,
STATE3
};
/************************************************************************/
/* GLOBAL VARIABLES */
/************************************************************************/
volatile uint8_t pushbuttons_db;
volatile uint8_t reg1, reg2, reg3;
volatile uint16_t f_max, f_min, f_span, curr_f, top_next, f_dev;
volatile uint32_t num_ticks = 0;
/************************************************************************/
/* Initialisation */
/************************************************************************/
void init()
{
cli(); //Disable interrupts
//Set sysclk to 16 MHz
CLKPR = 0x80; // Prescaler change enable
CLKPR = 0x00; // Set prescaler to zero
DDRF &= 0x0F; // Set PORTF bits 7-4 as inputs (PBs)
DDRD |= 0xF0; // Set PORTD bits 7-4 as outputs (LEDs)
PORTD &= LEDS_OFF; // initalise LEDS to off
DDRB |= 0x40; // set PORTB bit 6 output (Jout)
//Init Timer 0
TCCR0A = 0x02;
TCCR0B = 0x04;
OCR0A = 0x7D; // 2ms Period
TIMSK0 = 0x02; // interrupt on Output Compare Match A
//Init Timer 1
// Using OC1b, connected to B6
TCCR1A = 0x33; // Fast PWM clear on CMP, TOP in OCR1A
TCCR1B = 0x18; // Fast PWM TOP in OCR1A, prescalar 1
TIMSK1 = 0X01; // Enable overflow interrupt
f_max = F_MAX_INIT;
f_min = F_MIN_INIT;
f_span = 2 * (f_max - f_min);
curr_f = f_min;
top_next = ticks_one_sec/curr_f;
OCR1A = top_next;
OCR1B = top_next/2;
TCCR1B |=0x01; // start clock
sei(); //Enable interrupts
}/*init*/
/************************************************************************/
/* MAIN LOOP */
/************************************************************************/
int main(void)
{
uint8_t pb_now=0, pb_prev=0, pb_pressed;
uint8_t state = STATE1;
init();
// main program loop
while(1)
{
// determine if any pushbutton pressed
pb_now = pushbuttons_db;
pb_pressed = pb_now & (pb_now ^ pb_prev);
pb_prev = pb_now;
if (pb_pressed & S4){
state = STATE1;
f_max = F_MAX_INIT;
f_min = F_MIN_INIT;
}
switch (state)
{
case STATE1: PORTD &= LEDS_OFF;
PORTD |= LED1; // turn LED1 on
TIMSK1 = 0X01;
if (pb_pressed & S1)
state = STATE2;
break;
case STATE2: PORTD &= LEDS_OFF;
PORTD |= LED2; // turn LED2 on
curr_f = f_max;
top_next = ticks_one_sec/curr_f;
TIMSK1 = 0X00;
OCR1A = top_next;
OCR1B = top_next/2;
if (pb_pressed & S1)
state = STATE3;
if (pb_pressed & S2){
if (f_max < (F_MAX_INIT + 500)) f_max += F_INC;
}
if (pb_pressed & S3){
if (f_max > (F_MAX_INIT - 500)) f_max -= F_INC;
}
break;
case STATE3: PORTD &= LEDS_OFF;
PORTD |= LED3; // turn LED3 on
curr_f = f_min;
top_next = ticks_one_sec/curr_f;
OCR1A = top_next;
OCR1B = top_next/2;
if (pb_pressed & S1)
state = STATE1;
if (pb_pressed & S2){
if (f_min < (F_MIN_INIT + 500)) f_min += F_INC;
}
if (pb_pressed & S3){
if (f_min > (F_MIN_INIT - 500)) f_min -= F_INC;
}
break;
default: state = STATE1;
}//switch
} /*while*/
}/*end main*/
// Interrupt Service Routines
ISR(TIMER0_COMPA_vect){
uint8_t pb0;
uint8_t delta;
pb0 = ~PINF;
delta = pb0 ^ pushbuttons_db;
pushbuttons_db ^= (reg3 & delta);
reg3 = (reg2 & delta);
reg2 = (reg1 & delta);
reg1 = delta;
}
ISR(TIMER1_OVF_vect){
OCR1A = top_next;
OCR1B = top_next/2;
num_ticks += top_next;
if (num_ticks > ticks_one_sec) {
num_ticks = 0;
}
f_span = 2 * (f_max - f_min);
f_dev = (f_span * (uint64_t) num_ticks)/ticks_one_sec;
if (num_ticks > ticks_half_sec){
f_dev = f_dev - (f_max - f_min);
curr_f = f_max - f_dev;
} else {
curr_f = f_dev + f_min;
}
top_next = ticks_one_sec/curr_f;
}