Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Peter_Game.c
- *
- * Created: 10/03/2012 13:57:46
- * Author: Peter
- */
- #include <avr/io.h>
- #include <stdlib.h>
- #define LIGHTDIODE 0
- #define WAITPRESS 1
- #define GETPRESS 2
- #define CHECKPRESS 3
- #define BLINK 4
- static unsigned char state;
- static unsigned char get_portB_input(void) {
- return ~PINB;
- }
- static void set_portA(unsigned char port_value) {
- PORTA = ~port_value;
- }
- static void led_on(unsigned char ledNo) {
- PORTA &= ~(1 << ledNo);
- }
- static void leds_on() {
- set_portA(0xff);
- }
- static void leds_off(void) {
- set_portA(0x00);
- }
- static void init_io(void) {
- DDRA = 0xff;
- DDRB = 0x00;
- leds_off();
- }
- static void delay(int time) {
- volatile int count1 = 0;
- while (count1 < time) {
- volatile int count2 = 0;
- while (count2 < time) {
- count2++;
- }
- count1++;
- }
- }
- static void blink(void)
- {
- int x = 10;
- while (x)
- {
- leds_on();
- delay(250);
- leds_off();
- delay(250);
- x--;
- }
- }
- int main(void) {
- int time = 0, wait_time = 0;
- int pressed = 0;
- int random_led = 0;
- int time_treshold = 0;
- int score = 0;
- init_io();
- state = BLINK;
- while (1)
- {
- switch (state)
- {
- case BLINK:
- {
- blink();
- state = LIGHTDIODE;
- delay(250);
- wait_time = 1000;
- score = 0;
- break;
- }
- case LIGHTDIODE:
- {
- random_led = 4 + (int)(rand() / (RAND_MAX / 4 + 1));
- led_on(random_led);
- delay(250);
- time = wait_time;
- state = WAITPRESS;
- time_treshold = wait_time / 4;
- break;
- }
- case WAITPRESS:
- {
- if(time > 0) {
- if(time == time_treshold){
- set_portA(15 | (1 << random_led));
- }
- else if(time == 2*time_treshold){
- set_portA(7 | (1 << random_led));
- }
- else if(time == 3*time_treshold){
- set_portA(3 | (1 << random_led));
- }
- else if(time == 4*time_treshold){
- set_portA(1 | (1 << random_led));
- }
- state = GETPRESS;
- break;
- }
- state = CHECKPRESS;
- break;
- }
- case GETPRESS:
- {
- pressed = get_portB_input();
- if(pressed == 0) {
- state = WAITPRESS;
- time--;
- } else {
- state = CHECKPRESS;
- }
- delay(70);
- break;
- }
- case CHECKPRESS:
- if(pressed == (1 << random_led)) {
- wait_time -= 0.1 * wait_time;
- leds_off();
- delay(500);
- state = LIGHTDIODE;
- score++;
- break;
- } else {
- leds_off();
- delay(500);
- state = BLINK;
- set_portA(score);
- delay(2000);
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement