Skip to content

Instantly share code, notes, and snippets.

@nuttt
Created July 4, 2013 07:57
Show Gist options
  • Save nuttt/5925745 to your computer and use it in GitHub Desktop.
Save nuttt/5925745 to your computer and use it in GitHub Desktop.
AVR Lab 3-1
#include <avr/io.h>
#define F_CPU 1600000UL
#include <util/delay.h>
#define PORT_LED PORTB
#define DIR_LED DDRB
#define LED1 1
#define LED2 2
#define BTN 0
void delay_ms(unsigned int time){
while(time-- > 0){
_delay_ms(100.0);
}
}
int main(void){
DIR_LED |= (1<<LED1);
DIR_LED |= (1<<LED2);
// Internal Pull up
PORTB |= (1<<BTN);
int state = 0;
int l1, l2;
int sw;
while(1){
sw = ( PINB & (1<<BTN) );
sw ^= (1<<BTN);
if(sw){
if(state == 0)
state = 1;
else if(state == 1)
state = 3;
else if(state == 3)
state = 2;
else if(state == 2)
state = 0;
l1 = state & 1;
l2 = state & 2;
if(l1)
PORTB |= (1 << LED1);
else
PORTB &= ~(1 << LED1);
if(l2)
PORTB |= (1 << LED2);
else
PORTB &= ~(1 << LED2);
}
delay_ms(10);
/*PORT_LED &= ~(1<<LED);
delay_ms(200);
PORT_LED |= (1<<LED);
delay_ms(200);*/
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment