Skip to content

Instantly share code, notes, and snippets.

@SviMik

SviMik/abs_sum.c Secret

Created July 31, 2019 21:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SviMik/9b7d5af135ae5ea2b234d6699f818c67 to your computer and use it in GitHub Desktop.
Save SviMik/9b7d5af135ae5ea2b234d6699f818c67 to your computer and use it in GitHub Desktop.
/*
Copyright (C) 2019 SviMik
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*
Designed for Arduino Mini (ATmega328P)
Pin definitions:
Inputs: PC1, PC2
Output: PC3
LED1: PD7(-), PD6(+)
LED2: PD5(-), PD4(+)
No LED resistors are needed (built-in MCU pull-up resistors are used)
*/
#define F_CPU 16000000
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#define LED1(on) if(on){PORTD|=(1<<6);}else{PORTD&=~(1<<6);}
#define LED2(on) if(on){PORTD|=(1<<4);}else{PORTD&=~(1<<4);}
uint8_t ticks_in = 0;
uint8_t pin_prev = 0;
ISR(PCINT1_vect){
uint8_t pin_new = PINC;
uint8_t changedbits = pin_new ^ pin_prev;
pin_prev = pin_new;
if(changedbits & (1 << 1)){
ticks_in++;
}
if(changedbits & (1 << 2)){
ticks_in++;
}
}
int main(void){
// Init
PCICR|=(1<<PCIE1); // PCINT 1 enable
PCMSK1|=(1<<PCINT9)|(1<<PCINT10); // PC1, PC2 - in
DDRC |= (1<<3); // PC3 - out
DDRD |= (1<<7); // PD5 - Virtual GND for LED1 (always 0)
DDRD |= (1<<5); // PD5 - Virtual GND for LED2 (always 0)
sei();
// Main code
uint8_t ticks_sent = 0;
while(1){
if(ticks_sent != ticks_in){
PORTB ^= (1<<5); // Arduino led
PORTC ^= (1<<3); // out
_delay_us(250); // 250us is adequate for up to 2kHz output. Adjust if necessary
ticks_sent++;
}
LED1(PINC & (1<<1));
LED2(PINC & (1<<2));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment