Skip to content

Instantly share code, notes, and snippets.

@willasaywhat
Created February 20, 2013 03:28
Show Gist options
  • Select an option

  • Save willasaywhat/344c79ea3c31b0273333 to your computer and use it in GitHub Desktop.

Select an option

Save willasaywhat/344c79ea3c31b0273333 to your computer and use it in GitHub Desktop.
Digital Out and Digital In with AVR :D (For Attiny4313)
/* Name: main.c
* Author: <insert your name here>
* Copyright: <insert your copyright message here>
* License: <insert your license reference here>
DDRB &= ~_BV(DDB2); //clear pin state
val = PINB & _BV(PB2); //get value of the pin
*/
#include <avr/io.h>
#include <util/delay.h>
void delay_ms( int ms );
void chaser();
int main(void)
{
int pins[] = {PD5, PD4, PD3, PD2, PD1, PB4, PB3, PB2, PB1, PB0};
DDRD &= ~_BV(PD6); //Clear PD6
int i;
int len = (sizeof(pins) / sizeof(int));
for(i = 0; i < len; i++){
if(i >= 5){
//DDRB |= (1 << pins[i]);
DDRB |= _BV(pins[i]);
}
else{
//DDRD |= (1 << pins[i]);
DDRD |= _BV(pins[i]);
}
}
while(1){
int val = PIND & _BV(PD6);
if(val == 0){
walker(pins, len);
}
else{
chaser(pins, len);
}
}
return 0; /* never reached */
}
void chaser(int pins[], int len){
int i;
for(i = 0; i < len; i++){
if(i >= 5){
//PORTB |= (1 << pins[i]);
PORTB |= _BV(pins[i]);
delay_ms(10);
//PORTB &= ~(1 << pins[i]);
PORTB &= ~_BV(pins[i]);
delay_ms(10);
}
else {
//PORTD |= (1 << pins[i]);
PORTD |= _BV(pins[i]);
delay_ms(10);
//PORTD &= ~(1 << pins[i]);
PORTD &= ~_BV(pins[i]);
delay_ms(10);
}
}
return;
}
void walker(int pins[], int len){
int i;
for(i = 0; i < (len/2)+1; i++){
//PORTB |= (1 << pins[i]);
PORTB |= _BV(pins[i+5]);
delay_ms(10);
//PORTB &= ~(1 << pins[i]);
PORTB &= ~_BV(pins[i+5]);
delay_ms(10);
//PORTD |= (1 << pins[i]);
PORTD |= _BV(pins[abs(i-5)]);
delay_ms(10);
//PORTD &= ~(1 << pins[i]);
PORTD &= ~_BV(pins[abs(i-5)]);
delay_ms(10);
}
return;
}
void delay_ms( int ms )
{
int i;
for (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