// Source: https:
#include <inttypes.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <stdlib.h>
#define INIT_TIMER_COMP 0xffff
#define LOWBYTE(val) ((uint8_t)((val) & 0xff))
#define HIGHBYTE(val) ((uint8_t)(((val) & 0xff00) >> 8))
#define B_DATA_PORT_MASK (_BV(DDB0))
int foo(void) {
return 2;
}
void toggle_b2(void)
{
PORTB ^= _BV(PORTB2);
}
void led_out (uint8_t val)
{
unsigned char tval;
tval = PORTB & _BV(PORTB2);
tval |= ~val & B_DATA_PORT_MASK;
PORTB = tval;
}
void stop_timer1 (void)
{
TCCR1B = TCCR1B & ~(_BV(CS10) | _BV(CS11) | _BV(CS12));
}
void start_timer1 (void)
{
TCCR1B |= _BV(WGM12);
TCCR1B |= _BV(CS12);
}
ISR (TIMER1_OVF_vect)
{
led_out(0x1);
}
void set_compare_timer1 (uint16_t val)
{
OCR1AH = HIGHBYTE(val);
OCR1AL = LOWBYTE(val);
}
typedef enum {
UP,
DOWN,
} SVAL;
#define INITSHIFT 0
int sit = (5<<INITSHIFT);
int shift = INITSHIFT;
ISR (TIMER1_COMPA_vect)
{
static unsigned int delay = 0;
static SVAL state = DOWN;
int in = PINB;
cli();
stop_timer1();
toggle_b2();
if (in & 0x2) {
state = DOWN;
led_out(0);
} else {
state = UP;
led_out(1);
}
delay = (rand() & 0xDEA8);
if (delay < 0x40) {
delay = 0x40;
}
if (sit-- <= 0) {
switch (shift) {
case 0:
if (state == UP) {
shift = 2;
sit = (5);
} else {
shift = 0;
sit = 1;
}
break;
case 1:
case 2:
if (state == UP) {
shift = 3;
sit = 1;
} else {
sit = 1;
shift = 0;
}
break;
case 3:
case 4:
if (state == UP) {
sit = 1;
shift = 3;
} else {
sit = (10);
shift = 1;
}
break;
}
}
if (shift == 0) {
set_compare_timer1(delay<<2);
} else {
set_compare_timer1(delay>>shift);
}
start_timer1();
led_out(shift);
sei();
}
void
ioinit (void)
{
DDRB = B_DATA_PORT_MASK | _BV(DDB2);
PORTB = 0;
TCCR1A = 0;
TCCR1B = 0;
set_compare_timer1(0x4000);
TCCR1A = 0;
TCCR1B |= _BV(WGM12);
TCCR1B |= _BV(CS11);
}
#define JVAL 100000;
int
main (void)
{
ioinit ();
srandom(0x12345678);
set_sleep_mode(SLEEP_MODE_IDLE);
TIMSK1 = _BV (OCIE1A);
cli();
sei ();
sleep_enable();
sei();
for (;;) {
sleep_cpu();
}
return (0);
}
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.