You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
380 lines
8.2 KiB
380 lines
8.2 KiB
#include <avr/io.h>
|
|
#include <util/delay.h>
|
|
#include <avr/interrupt.h>
|
|
#include <stdlib.h>
|
|
|
|
#define DEBOUNCE_COUNT 100
|
|
#define BOMB_COUNTDOWN_TIME 65
|
|
|
|
unsigned char displaydata[8] = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, adress;
|
|
uint8_t debounce[8] = {0,0,0,0,0,0,0,0}; //input debounce
|
|
uint8_t debounced = 0;
|
|
|
|
|
|
|
|
void display_time(int time);
|
|
void display_clear(void);
|
|
|
|
void display_update(void);
|
|
void display_send_one(uint8_t address, unsigned char data);
|
|
void shift_send(unsigned char data);
|
|
|
|
void mp3_play(uint8_t mp3file);
|
|
void timer_start(void);
|
|
void timer_speed_up(void);
|
|
void timer_pause(void);
|
|
|
|
void motor_step_forward(void);
|
|
void motor_step_back(void);
|
|
|
|
uint8_t radio_signal(void);
|
|
|
|
enum{
|
|
BOMB_DISARMED,
|
|
BOMB_ARMED,
|
|
BOMB_DEFUSED,
|
|
BOMB_EXPLODED
|
|
} bombState;
|
|
|
|
//functions for updating the bomb in a given state
|
|
void bomb_logic_disarmed(void);
|
|
void bomb_logic_armed(void); // <-- this will be unique for each bomb
|
|
void bomb_logic_defused(void);
|
|
void bomb_logic_exploded(void);
|
|
|
|
//functions for ENTERING a state
|
|
void bomb_logic_disarm(void);
|
|
void bomb_logic_arm(void);
|
|
void bomb_logic_defuse(void);
|
|
void bomb_logic_explode(void);
|
|
|
|
//return_data_type function(input_data_type input_variable_local_name)
|
|
//void display_send_one(unsigned char data);
|
|
//PORTD |= 1<<0; //(set D.0) Shifts a one zero steps left
|
|
//PORTD &= ~(1<<0); //(reset D.0) Creates a byte consisting of ones, shifts a zero 0 steps left
|
|
|
|
void display_send_one(uint8_t address, unsigned char data) //Send the character 'data' to the display position defined in 'adress'
|
|
{
|
|
address = 7-address;
|
|
PORTD &= 0b10000000; //Clear all data pins
|
|
PORTA &= 0b11111100; //Clear address
|
|
PORTA |= (address & 0b00000011); //set address
|
|
PORTD |= (data & 0b01111111); //Set the 7 bit ASCII output for one character
|
|
_delay_us(5);
|
|
if (address & 0b0000100)
|
|
{
|
|
PORTA &= 0b11111011; //Clock the write pin
|
|
_delay_us(5);
|
|
PORTA |= 0b00000100;
|
|
}
|
|
else
|
|
{
|
|
PORTA &= 0b11110111; //Clock the write pin
|
|
_delay_us(5);
|
|
PORTA |= 0b00001000;
|
|
}
|
|
_delay_us(5);
|
|
}
|
|
|
|
void display_update(void) //Send the array displaydata to the display, setting the adress variable to the correct value for each char.
|
|
{
|
|
for (unsigned char i = 0; i < 8; i++)
|
|
{
|
|
display_send_one(i, displaydata[i]);
|
|
}
|
|
}
|
|
|
|
|
|
void mp3_play(uint8_t mp3file)
|
|
{
|
|
mp3file &= 0b00000111;
|
|
DDRB |= mp3file;
|
|
_delay_ms(40);
|
|
DDRB &=0b11111000;
|
|
}
|
|
|
|
|
|
void display_clear(void){
|
|
displaydata[0] = ' '; displaydata[1] = ' '; displaydata[2] = ' '; displaydata[3] = ' ';
|
|
displaydata[4] = ' '; displaydata[5] = ' '; displaydata[6] = ' '; displaydata[7] = ' ';
|
|
display_update();
|
|
}
|
|
|
|
void display_time(int time) //displays the 'time' given in seconds as MM:SS on the LCD
|
|
{
|
|
uint8_t current_digit = 0;
|
|
|
|
//static part of display
|
|
displaydata[0] = ' ';
|
|
displaydata[4] = ':';
|
|
displaydata[7] = ' ';
|
|
|
|
//limit to 60:00
|
|
if(time > 3600){
|
|
time = 3600;
|
|
}
|
|
|
|
//'-' sign for negative time?
|
|
if(time < 0){
|
|
time *= -1;
|
|
displaydata[1] = '-';
|
|
}else{
|
|
displaydata[1] = ' ';
|
|
}
|
|
|
|
//rest of the time
|
|
current_digit = 0;
|
|
while(time>=600){ //tens of minutes
|
|
current_digit += 1;
|
|
time -= 600;
|
|
}
|
|
displaydata[2] = current_digit + '0';
|
|
|
|
current_digit = 0;
|
|
while(time>=60){ //minutes
|
|
current_digit += 1;
|
|
time -= 60;
|
|
}
|
|
displaydata[3] = current_digit + '0';
|
|
|
|
current_digit = 0;
|
|
while(time>=10){ //tens of seconds
|
|
current_digit += 1;
|
|
time -= 10;
|
|
}
|
|
displaydata[5] = current_digit + '0';
|
|
|
|
|
|
displaydata[6] = time + '0';
|
|
|
|
display_update();
|
|
}
|
|
|
|
void timer_start(void){
|
|
//Set up Timer1
|
|
TCCR1B |= (1 << CS10) | (1 << CS12) | (1 << WGM12); //prescaler 1024
|
|
TCNT1 = 0; //zero out timer
|
|
//TIMSK1 |= (1 << OCIE1A); //enable output compare A
|
|
OCR1A = 4040; //output compare A will trigger at approx 1Hz. Callibrated at 5V
|
|
TIMSK1 |= (1 << OCIE1A);
|
|
}
|
|
|
|
void timer_speed_up(void){
|
|
TCNT1 = 1700; //set a timer a safe distance away from the trigger point.
|
|
OCR1A = 2020; //output compare A will triger at approx 2Hz. Callibrated at 5V
|
|
}
|
|
|
|
void timer_pause(void){
|
|
TCCR1B = 0; //stop timer
|
|
}
|
|
|
|
|
|
void motor_step_forward(void){
|
|
PORTD &= 0xF0; //PRE-enable zero
|
|
PORTB &= 0b01111111; //enable motor drive
|
|
PORTD &= 0xF0; _delay_us(50); PORTD |= 0b00000001; _delay_us(2000);
|
|
PORTD &= 0xF0; _delay_us(50); PORTD |= 0b00000100; _delay_us(2000);
|
|
PORTD &= 0xF0; _delay_us(50); PORTD |= 0b00000010; _delay_us(2000);
|
|
PORTD &= 0xF0; _delay_us(50); PORTD |= 0b00001000; _delay_us(2000);
|
|
PORTB |= 0b10000000; //disable motor drive
|
|
}
|
|
|
|
void motor_step_back(void){
|
|
PORTD &= 0xF0; //PRE-enable zero
|
|
PORTB &= 0b01111111; //enable motor drive
|
|
PORTD &= 0xF0; _delay_us(50); PORTD |= 0b00001000; _delay_us(2000);
|
|
PORTD &= 0xF0; _delay_us(50); PORTD |= 0b00000010; _delay_us(2000);
|
|
PORTD &= 0xF0; _delay_us(50); PORTD |= 0b00000100; _delay_us(2000);
|
|
PORTD &= 0xF0; _delay_us(50); PORTD |= 0b00000001; _delay_us(2000);
|
|
PORTB |= 0b10000000; //disable motor drive
|
|
}
|
|
|
|
|
|
uint8_t radio_signal(void){
|
|
return (PINB & 0b01000000);
|
|
}
|
|
|
|
//BOMB LOGIC
|
|
int16_t countdown = 65;
|
|
uint8_t armed = 0;
|
|
|
|
int main(void)
|
|
{
|
|
DDRA = 0b00001111;
|
|
DDRB = 0b10000000;
|
|
DDRC = 0b10111111;
|
|
DDRD = 0b11111111;
|
|
/*
|
|
D0-D6 data
|
|
D7 CU
|
|
A0-A1 adress
|
|
A2 /Write0
|
|
A3 /Write1
|
|
|
|
D0-D3 doubles as motor drive pins
|
|
|
|
C0-C5 inputs, C7 input/button
|
|
B0, B1, B2 MP3 playback triggers
|
|
B3 spare MP3 trigger
|
|
B4-B5 NMOS outputs
|
|
B6 Radio remote input
|
|
B7 enable motor drive
|
|
*/
|
|
PORTA |= 0b00000110;
|
|
PORTB = 0b10000000;
|
|
PORTC = 0b10111111;
|
|
PORTD = 0;
|
|
_delay_us(500);
|
|
PORTA &= 0b11111011; //Clock the write pin
|
|
_delay_us(500);
|
|
PORTA |= 0b00000100;
|
|
_delay_us(500);
|
|
PORTA &= 0b11110111; //Clock the write pin
|
|
_delay_us(500);
|
|
PORTA |= 0b00001000;
|
|
_delay_us(500);
|
|
PORTD |= 0b10000000;
|
|
|
|
timer_start();
|
|
display_clear();
|
|
|
|
sei(); //interrupts enable!
|
|
|
|
while(1)
|
|
{
|
|
if(radio_signal()){
|
|
if(bombState == BOMB_DISARMED){bomb_logic_arm();}
|
|
}else{
|
|
if(bombState != BOMB_DISARMED){bomb_logic_disarm();}
|
|
}
|
|
|
|
//input debouncing
|
|
for(int i=0; i<8; i++){
|
|
if(PINC & 1<<i){
|
|
if(debounce[i] < DEBOUNCE_COUNT ){
|
|
debounce[i]+=1;
|
|
}
|
|
}else{
|
|
if(debounce[i] > 0){
|
|
debounce[i]-=1;
|
|
}
|
|
}
|
|
if(debounce[i] == DEBOUNCE_COUNT){
|
|
debounced |= 1<<i;
|
|
}
|
|
if(debounce[i] == 0){
|
|
debounced &= ~(1<<i);
|
|
}
|
|
}
|
|
|
|
switch(bombState){
|
|
case BOMB_DISARMED:
|
|
bomb_logic_disarmed();
|
|
break;
|
|
|
|
case BOMB_ARMED:
|
|
bomb_logic_armed();
|
|
break;
|
|
|
|
case BOMB_DEFUSED:
|
|
bomb_logic_defused();
|
|
break;
|
|
|
|
case BOMB_EXPLODED:
|
|
bomb_logic_exploded();
|
|
break;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
void bomb_logic_disarm(void){
|
|
bombState = BOMB_DISARMED;
|
|
timer_pause();
|
|
}
|
|
|
|
void bomb_logic_disarmed(void){
|
|
displaydata[0] = 'D'; displaydata[1] = 'I'; displaydata[2] = 'S'; displaydata[3] = 'A';
|
|
displaydata[4] = 'R'; displaydata[5] = 'M'; displaydata[6] = 'E'; displaydata[7] = 'D';
|
|
//When disarmed, show the wire status by BIG/SMALL letters
|
|
for(int i=0; i<8; i++){
|
|
if(debounced >> i & 0x01){
|
|
displaydata[i] += 32;
|
|
}
|
|
}
|
|
display_update();
|
|
}
|
|
|
|
void bomb_logic_arm(void){
|
|
bombState = BOMB_ARMED;
|
|
countdown = BOMB_COUNTDOWN_TIME;
|
|
display_time(countdown);
|
|
mp3_play(1);
|
|
timer_start();
|
|
}
|
|
|
|
void bomb_logic_armed(void){
|
|
if(countdown == 0){
|
|
bomb_logic_explode();
|
|
}
|
|
|
|
if(debounced & 0b10000000){
|
|
bomb_logic_defuse();
|
|
}
|
|
|
|
if(debounced & 0b00100000){
|
|
bomb_logic_explode();
|
|
}
|
|
}
|
|
|
|
|
|
void bomb_logic_defuse(void){
|
|
bombState = BOMB_DEFUSED;
|
|
timer_pause();
|
|
displaydata[1] = '[';
|
|
displaydata[7] = ']';
|
|
display_update();
|
|
}
|
|
|
|
void bomb_logic_defused(void){
|
|
//not much to do here
|
|
}
|
|
|
|
void bomb_logic_explode(void){
|
|
bombState = BOMB_EXPLODED;
|
|
timer_pause();
|
|
mp3_play(2);
|
|
displaydata[0] = ' '; displaydata[1] = ' '; displaydata[2] = 'B'; displaydata[3] = 'O';
|
|
displaydata[4] = 'O'; displaydata[5] = 'M'; displaydata[6] = ' '; displaydata[7] = ' ';
|
|
display_update();
|
|
}
|
|
|
|
void bomb_logic_exploded(void){
|
|
//BOOM animation.
|
|
//Because of rand(), this takes a LOT of FLASH MEMORY
|
|
//Remove if in need of more PROGRAM space
|
|
for(int i=2; i<6; i++){
|
|
if(rand() > 0x3FFF){
|
|
displaydata[i] |= 0b00100000;
|
|
}else{
|
|
displaydata[i] &= 0b11011111;
|
|
}
|
|
}
|
|
display_update();
|
|
_delay_ms(100);
|
|
//not much to do here
|
|
//maybe blink an LED or something
|
|
}
|
|
|
|
uint8_t half_second = 0;
|
|
ISR(TIMER1_COMPA_vect){
|
|
half_second ^= 0x01; //will be true every other half-second
|
|
if(half_second){
|
|
if(countdown>0){countdown -= 1;}
|
|
display_time(countdown);
|
|
if(countdown < 10){mp3_play(1);}
|
|
}else{
|
|
display_send_one(4, ' ');
|
|
}
|
|
}
|