Commit ac21eb86 by karudu

Esimene commit

parents
Showing with 101 additions and 0 deletions
#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;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment