Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
karudu
/
Scope
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Pipelines
Members
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
ac21eb86
authored
Oct 18, 2022
by
karudu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Esimene commit
parents
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
101 additions
and
0 deletions
scope.ino
scope.ino
0 → 100644
View file @
ac21eb86
#define PIN_BUTTON_1 5
#define PIN_BUTTON_2 4
#define PIN_BUTTON_3 3
#define PIN_BUTTON_4 2
#define PIN_ADC 0 // A0
#define BUTTON_1 0
#define BUTTON_2 1
#define BUTTON_3 2
#define BUTTON_4 3
// https://www.gammon.com.au/adc
unsigned
long
G_LastDebounceTime
[
4
];
int
G_ButtonState
[
4
];
int
G_PrevButtonState
[
4
];
#define DEBOUNCE_DELAY_MS 25
byte
ReadButton
(
int
Button
)
{
const
int
ButtonPins
[
4
]
=
{
PIN_BUTTON_1
,
PIN_BUTTON_2
,
PIN_BUTTON_3
,
PIN_BUTTON_4
};
int
State
=
digitalRead
(
ButtonPins
[
Button
]);
if
(
State
!=
G_PrevButtonState
[
Button
])
G_LastDebounceTime
[
Button
]
=
millis
();
if
((
millis
()
-
G_LastDebounceTime
[
Button
])
>
DEBOUNCE_DELAY_MS
)
{
if
(
State
!=
G_ButtonState
[
Button
])
{
G_ButtonState
[
Button
]
=
State
;
if
(
G_ButtonState
[
Button
]
==
LOW
)
return
1
;
}
}
G_PrevButtonState
[
Button
]
=
State
;
return
0
;
}
// Set timer 1 to a specified measuring interval
void
SetTimerPeriod
()
{
cli
();
TCCR1A
=
0
;
TCCR1B
=
0
;
// Reset TCCR
TCNT0
=
0
;
// Reset the counter
TCCR1B
=
bit
(
CS11
)
|
bit
(
WGM12
);
// CTC mode, prescaler 8
OCR1A
=
19999
;
// 100 Hz ((16 MHz / 8 * 100 Hz) - 1)
TIMSK1
|=
bit
(
OCIE1B
);
// Enable CompareB interrupt
sei
();
}
volatile
int
results
[
100
];
volatile
int
resultNumber
=
0
;
// ADC complete ISR
ISR
(
ADC_vect
)
{
if
(
resultNumber
<=
99
)
results
[
resultNumber
++
]
=
ADC
;
}
EMPTY_INTERRUPT
(
TIMER1_COMPB_vect
);
char
G_PrintFBuf
[
32
];
void
setup
()
{
pinMode
(
PIN_BUTTON_1
,
INPUT_PULLUP
);
pinMode
(
PIN_BUTTON_2
,
INPUT_PULLUP
);
pinMode
(
PIN_BUTTON_3
,
INPUT_PULLUP
);
pinMode
(
PIN_BUTTON_4
,
INPUT_PULLUP
);
Serial
.
begin
(
2000000
);
SetTimerPeriod
();
// Set timer to 100 Hz
ADCSRA
=
bit
(
ADEN
)
|
bit
(
ADIE
)
|
bit
(
ADIF
);
// Enable the ADC and the read complete interrupt
ADCSRA
|=
bit
(
ADPS2
);
// Prescaler 16
ADMUX
=
bit
(
REFS0
)
|
PIN_ADC
;
ADCSRB
=
bit
(
ADTS0
)
|
bit
(
ADTS2
);
// Timer 1 CompareB
ADCSRA
|=
bit
(
ADATE
);
// Enable auto trigger
for
(
int
i
=
0
;
i
<
4
;
i
++
)
G_LastDebounceTime
[
i
]
=
0
;
for
(
int
i
=
0
;
i
<
4
;
i
++
)
G_PrevButtonState
[
i
]
=
HIGH
;
}
void
loop
()
{
for
(
int
i
=
0
;
i
<
4
;
i
++
)
{
byte
State
=
ReadButton
(
i
);
if
(
State
==
1
)
{
sprintf
(
G_PrintFBuf
,
"Button %d is %d
\n
"
,
i
,
State
);
Serial
.
println
(
G_PrintFBuf
);
}
}
if
(
resultNumber
==
100
)
{
for
(
int
i
=
0
;
i
<
100
;
i
++
)
Serial
.
println
(
results
[
i
]);
resultNumber
=
0
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment