Skip to content

Instantly share code, notes, and snippets.

@ilsubyeega
Created April 17, 2024 09:27
Show Gist options
  • Save ilsubyeega/96705a71c185680dbb920fa530df6fa3 to your computer and use it in GitHub Desktop.
Save ilsubyeega/96705a71c185680dbb920fa530df6fa3 to your computer and use it in GitHub Desktop.
/*
* main.c
*
* Created: 4/17/2024 5:52:27 PM
* Author: USER
*/
#include <avr/io.h>
#include <util/delay.h>
#include <xc.h>
int direction = 0;
int next_count = 0;
int masks[] = { 0b00001000, 0b000000100, 0b00000010, 0b00000001 };
int main(void)
{
DDRD = 0x0f;
PORTD = 0xff;
while(1)
{
// wait 10ms each, total 100ms to detect the button
for (int i = 0; i < 10; i++) {
delay(10);
check_button();
}
// if iterations are done, reset.
if (next_count > 3) {
next_count = 0;
}
if (next_count < 0) {
next_count = 3;
}
PORTD = PORTD & 0xf0 // <--- don't change input register.
| masks[next_count]; // <--- mask based on interation.
if (direction) next_count++; else next_count--;
}
}
void check_button() {
if (~PIND & 0b00010000) { // if button 1 has pressed
direction = 0;
}
if (~PIND & 0b00100000) { // if button 2 has pressed
direction = 1;
}
}
void delay(int ms) {
for (int i = 0; i < ms; i++) _delay_ms(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment