2012-09-20 20:55:19 +02:00
|
|
|
#include <inttypes.h>
|
|
|
|
#include <avr/io.h>
|
|
|
|
#include <avr/interrupt.h>
|
2012-10-01 21:39:25 +02:00
|
|
|
#define __DELAY_BACKWARD_COMPATIBLE__
|
2012-09-20 20:55:19 +02:00
|
|
|
#include <util/delay.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
|
|
|
|
#define LED_L (1 << PORTC0)
|
|
|
|
#define LED_R (1 << PORTC2)
|
|
|
|
#define MOTOR_ON (0<<PORTB1)
|
|
|
|
#define MOTOR_OFF (1<<PORTB1)
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-09-24 19:45:51 +02:00
|
|
|
static void init_leds(void){
|
2012-09-20 20:55:19 +02:00
|
|
|
//enable LED channels as output
|
|
|
|
DDRC |= (1 << PORTC0) | (1 << PORTC1) | (1 << PORTC2) | (1 << PORTC3);
|
|
|
|
// both LEDs off
|
|
|
|
PORTC &= ~((1 << PORTC0) | (1 << PORTC1) | (1 << PORTC2) | (1 << PORTC3));
|
2012-09-20 21:20:51 +02:00
|
|
|
return;
|
2012-09-24 19:45:51 +02:00
|
|
|
}
|
2012-09-20 20:55:19 +02:00
|
|
|
|
2012-09-24 19:45:51 +02:00
|
|
|
static void inline led_on(int leds){
|
2012-09-20 20:55:19 +02:00
|
|
|
PORTC |= leds;
|
2012-09-24 19:45:51 +02:00
|
|
|
}
|
2012-09-20 20:55:19 +02:00
|
|
|
|
2012-09-24 19:45:51 +02:00
|
|
|
static void inline led_off(int leds){
|
2012-09-20 20:55:19 +02:00
|
|
|
PORTC &= ~leds;
|
2012-09-24 19:45:51 +02:00
|
|
|
return;
|
|
|
|
}
|
2012-09-20 20:55:19 +02:00
|
|
|
|
2012-09-25 07:23:14 +02:00
|
|
|
static void inline buzzr_off(void){
|
2012-09-24 19:45:51 +02:00
|
|
|
PORTC &= ~(1 << PORTC5);
|
|
|
|
PORTB &= ~(1 << PORTB2);
|
|
|
|
}
|
2012-09-20 20:55:19 +02:00
|
|
|
|
2012-09-24 19:45:51 +02:00
|
|
|
static void init_buzzr(void){
|
2012-09-20 20:55:19 +02:00
|
|
|
//its on B2 and C5, for reasons
|
|
|
|
DDRC |= (1 << PORTC5);
|
|
|
|
DDRB |= (1 << PORTB2);
|
|
|
|
//switch it off
|
|
|
|
buzzr_off();
|
|
|
|
return;
|
2012-09-24 19:45:51 +02:00
|
|
|
}
|
2012-09-20 20:55:19 +02:00
|
|
|
|
2012-09-24 19:45:51 +02:00
|
|
|
static void buzzr_up(void){
|
2012-09-20 20:55:19 +02:00
|
|
|
PORTC &= ~(1 << PORTC5);
|
|
|
|
PORTB |= (1 << PORTB2);
|
|
|
|
return;
|
2012-09-24 19:45:51 +02:00
|
|
|
}
|
2012-09-20 20:55:19 +02:00
|
|
|
|
2012-09-25 07:23:14 +02:00
|
|
|
static void buzzr_down(void){
|
2012-09-20 20:55:19 +02:00
|
|
|
PORTC |= (1 << PORTC5);
|
|
|
|
PORTB &= ~(1 << PORTB2);
|
2012-09-24 19:45:51 +02:00
|
|
|
}
|
2012-09-20 20:55:19 +02:00
|
|
|
|
2012-09-24 19:45:51 +02:00
|
|
|
|
|
|
|
|
2012-09-25 07:23:14 +02:00
|
|
|
static void buzzr_inv(void){
|
2012-09-20 20:55:19 +02:00
|
|
|
PORTC ^= (1 << PORTC5);
|
|
|
|
PORTB ^= (1 << PORTB2);
|
2012-09-24 19:45:51 +02:00
|
|
|
}
|
2012-09-20 20:55:19 +02:00
|
|
|
|
|
|
|
|
2012-09-24 19:45:51 +02:00
|
|
|
static void init_motor(void)
|
2012-09-20 20:55:19 +02:00
|
|
|
{
|
|
|
|
/* vibration motor on B1, initially off: */
|
|
|
|
DDRB |= (1 << PORTB1);
|
|
|
|
PORTB &= ~( 1<<PORTB1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-24 19:45:51 +02:00
|
|
|
static void set_motor(int val){
|
2012-09-20 20:55:19 +02:00
|
|
|
PORTB = ~(val);
|
|
|
|
PORTB = PORTB;
|
|
|
|
return;
|
2012-09-24 19:45:51 +02:00
|
|
|
}
|
2012-09-20 20:55:19 +02:00
|
|
|
|
|
|
|
|
2012-09-24 19:45:51 +02:00
|
|
|
static void init_switch(void){
|
2012-09-25 07:23:14 +02:00
|
|
|
DDRD &= ~( (1 << PORTD1) | (1<<PORTD0));
|
2012-09-27 18:45:02 +02:00
|
|
|
PORTD |= (1 << PORTD1) | (1<<PORTD0); //Pullups FTW
|
2012-09-20 21:20:51 +02:00
|
|
|
return;
|
2012-09-24 19:45:51 +02:00
|
|
|
}
|
2012-09-20 21:20:51 +02:00
|
|
|
|
2012-09-24 19:45:51 +02:00
|
|
|
static void blinkrattlebeep(void){
|
2012-09-20 21:20:51 +02:00
|
|
|
led_off(LED_L|LED_R);
|
|
|
|
_delay_ms(100);
|
|
|
|
led_on(LED_L);
|
|
|
|
_delay_ms(100);
|
|
|
|
led_off(LED_L);
|
|
|
|
led_on(LED_R);
|
|
|
|
_delay_ms(100);
|
|
|
|
led_off(LED_R);
|
|
|
|
set_motor(MOTOR_ON);
|
|
|
|
_delay_ms(200);
|
|
|
|
set_motor(MOTOR_OFF);
|
|
|
|
buzzr_up();
|
|
|
|
for (int i=0; i<100; i++){
|
|
|
|
_delay_ms(2);
|
|
|
|
buzzr_inv();
|
|
|
|
};
|
|
|
|
buzzr_off();
|
2012-09-24 19:45:51 +02:00
|
|
|
}
|
2012-09-20 21:20:51 +02:00
|
|
|
|
2012-09-24 19:45:51 +02:00
|
|
|
static void mode_motortest(void)
|
2012-09-24 19:11:00 +02:00
|
|
|
{
|
|
|
|
set_motor(MOTOR_ON);
|
|
|
|
_delay_ms(400);
|
|
|
|
set_motor(MOTOR_OFF);
|
|
|
|
_delay_ms(500);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-24 19:45:51 +02:00
|
|
|
static void mode_blinktest(void)
|
2012-09-24 19:11:00 +02:00
|
|
|
{
|
|
|
|
led_off(LED_L|LED_R);
|
|
|
|
_delay_ms(100);
|
|
|
|
led_on(LED_L);
|
|
|
|
_delay_ms(100);
|
|
|
|
led_off(LED_L);
|
|
|
|
led_on(LED_R);
|
|
|
|
_delay_ms(100);
|
|
|
|
led_off(LED_R);
|
2012-09-24 19:45:51 +02:00
|
|
|
return;
|
2012-09-24 19:11:00 +02:00
|
|
|
}
|
2012-09-20 21:20:51 +02:00
|
|
|
|
2012-09-24 19:45:51 +02:00
|
|
|
static void mode_beeptest(void)
|
|
|
|
{
|
|
|
|
buzzr_up();
|
|
|
|
for (int i=0; i<100; i++){
|
|
|
|
_delay_ms(2);
|
|
|
|
buzzr_inv();
|
|
|
|
};
|
|
|
|
buzzr_off();
|
|
|
|
return;
|
|
|
|
}
|
2012-09-20 20:55:19 +02:00
|
|
|
|
2012-09-25 20:19:14 +02:00
|
|
|
static void flashleds(int leds)
|
|
|
|
{
|
|
|
|
led_off(LED_L|LED_R);
|
|
|
|
for (int i=0; i<10; i++){
|
|
|
|
led_on(leds);
|
|
|
|
_delay_ms(10);
|
|
|
|
led_off(LED_L|LED_R);
|
|
|
|
_delay_ms(90);
|
|
|
|
};
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-09-25 07:23:14 +02:00
|
|
|
int main(void)
|
2012-09-20 20:55:19 +02:00
|
|
|
{
|
2012-09-20 21:20:51 +02:00
|
|
|
|
|
|
|
/* hardware initialisation: */
|
2012-09-20 20:55:19 +02:00
|
|
|
init_motor();
|
|
|
|
init_buzzr();
|
|
|
|
init_leds();
|
2012-09-24 19:45:51 +02:00
|
|
|
init_switch();
|
2012-09-20 21:20:51 +02:00
|
|
|
uint8_t mode = 0;
|
2012-09-20 20:55:19 +02:00
|
|
|
|
|
|
|
for(;;) /* ever */ {
|
|
|
|
//do something
|
2012-09-20 21:20:51 +02:00
|
|
|
|
2012-09-25 20:19:14 +02:00
|
|
|
|
2012-09-20 21:20:51 +02:00
|
|
|
|
|
|
|
switch(mode){
|
2012-09-24 19:45:51 +02:00
|
|
|
case 1:
|
|
|
|
mode_blinktest();
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
mode_motortest();
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
mode_beeptest();
|
2012-09-25 07:23:14 +02:00
|
|
|
break;
|
2012-09-25 20:19:14 +02:00
|
|
|
default: //mode=0;
|
2012-09-24 19:45:51 +02:00
|
|
|
blinkrattlebeep();
|
|
|
|
|
|
|
|
}
|
2012-09-27 18:45:02 +02:00
|
|
|
|
2012-09-25 20:19:14 +02:00
|
|
|
//check switches and change mode
|
|
|
|
switch( PIND & 0b00000011){
|
2012-09-27 18:45:02 +02:00
|
|
|
case 0b00000010: //left switch pressed
|
|
|
|
mode--;
|
|
|
|
if (mode > 3) mode =3;
|
2012-09-25 20:19:14 +02:00
|
|
|
flashleds(LED_L);
|
2012-09-24 19:45:51 +02:00
|
|
|
break;
|
2012-09-27 18:45:02 +02:00
|
|
|
case 0b00000001: //right switch pressed
|
|
|
|
mode++;
|
|
|
|
if (mode > 3) mode = 0;
|
2012-09-25 20:19:14 +02:00
|
|
|
flashleds(LED_R);
|
2012-09-24 19:45:51 +02:00
|
|
|
break;
|
2012-09-27 18:45:02 +02:00
|
|
|
case 0b00000000: //both switches pressed
|
|
|
|
flashleds(LED_R|LED_L);
|
|
|
|
break;
|
2012-09-25 20:19:14 +02:00
|
|
|
|
|
|
|
|
2012-09-24 19:45:51 +02:00
|
|
|
}
|
2012-09-20 21:20:51 +02:00
|
|
|
|
2012-09-20 20:55:19 +02:00
|
|
|
}
|
|
|
|
/* never return 0; */
|
2012-09-25 07:23:14 +02:00
|
|
|
return 0;
|
2012-09-20 20:55:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|