Commit 3bbfddbf authored by Isabella Skořepová's avatar Isabella Skořepová
Browse files

Created snake game for 8x8 led array

parents
project(8x8snake)
# Cross-compiling can only be done with v2.6 or later
cmake_minimum_required(VERSION 2.6)
# F_CPU
add_definitions(-DF_CPU=1000000UL)
# MCU
set(AVR_MCU atmega8)
# Flags for compiling C files
set(CMAKE_C_FLAGS "-g -Wall -Os")
# Flags for compiling C++ files
set(CMAKE_CXX_FLAGS "-g -Wall -Os")
# Should we verify after avrdude upload?
set(UPLOAD_VERIFY false)
# AVRDUDE programmer
set(PROGRAMMER_NAME usbasp)
# Port our programmer is on
set(PROGRAMMER_PORT usb)
# Extra AVRDUDE options
set(AVRDUDE_OPTIONS)
# Load all files from this directory
aux_source_directory(. SRC_LIST)
# Allow assembly files to be built
# ENABLE_LANGUAGE(ASM)
# Make the executable
add_avr_executable(main
${SRC_LIST}
)
/*
* 8x8matrix.cpp
*
* Created: 27.11.2013 14:13:29
* Author: skore13
*/
#include "wait.h"
#include <avr/io.h>
#include <stdint.h>
#include <avr/interrupt.h>
#define UP 0
#define LEFT 1
#define DOWN 2
#define RIGHT 3
void hideLine(){
PORTB &= ~(0b11);
PORTD &= ~(0b11111100);
}
void showLine(uint8_t i){
switch(i){
case 0:
PORTD |= 1<<6;
break;
case 1:
PORTD |= 1<<2;
break;
case 2:
PORTD |= 1<<7;
break;
case 3:
PORTD |= 1<<3;
break;
case 4:
PORTB |= 1<<0;
break;
case 5:
PORTD |= 1<<4;
break;
case 6:
PORTB |= 1<<1;
break;
case 7:
PORTD |= 1<<5;
break;
}
}
void setRow(uint8_t v){
PORTC |= 0b111111;
PORTD |= 0b11;
if(v&(1<<0))
clrb(PORTC,3);
if(v&(1<<1))
clrb(PORTD,1);
if(v&(1<<2))
clrb(PORTC,2);
if(v&(1<<3))
clrb(PORTD,0);
if(v&(1<<4))
clrb(PORTC,1);
if(v&(1<<5))
clrb(PORTC,5);
if(v&(1<<6))
clrb(PORTC,0);
if(v&(1<<7))
clrb(PORTC,4);
}
uint8_t image[8];
uint8_t image2[8];
uint8_t img;
uint8_t snake[65];
uint8_t l;
void renderSnake() {
for(uint8_t i = 0; i < l; i++){
if(img)
image2[snake[i]&0b111] |= 1 << (( snake[i] & (0b1110000) )>>4);
else
image[snake[i]&0b111] |= 1 << (( snake[i] & (0b1110000) )>>4);
}
}
void move() {
uint8_t val;
uint8_t val2 = 0;
for(uint8_t i = 0; i < l+1; i++){
val = snake[i];
snake[i] = val2;
val2 = val;
}
}
void clearImage(){
for(int i = 0; i<8; i++){
if(img)
image2[i] = 0;
else
image[i] = 0;
}
}
uint8_t dir = DOWN;
void addFront() {
snake[0] = 0;
snake[0] = snake[1];
if(dir == UP){
snake[0]&=0b01110111;
snake[0]++;
}else if(dir == RIGHT){
snake[0]&=0b01110111;
snake[0]+=0x10;
}else if(dir == DOWN){
snake[0]&=0b01110111;
if((snake[0]&0b111)==0)
snake[0]|=0b111;
else
snake[0]--;
}else if(dir == LEFT){
snake[0]&=0b01110111;
snake[0]-=0x10;
}
}
uint8_t counter;
uint16_t endCounter;
uint8_t random;
uint8_t target = 0x66;
uint8_t rndCounter;
void renderTarget() {
if(img)
image2[target&0b111] |= 1 << (( target & (0b1110000) )>>4);
else
image[target&0b111] |= 1 << (( target & (0b1110000) )>>4);
}
bool colision(){
for(int i = 1; i < l; i++){
if(snake[i] == snake[0])return true;
}
return false;
}
bool end = false;
void renderEnd(){
for(int i = 0; i < l; i++){
snake[i] = ((i&0b111000)<<1)|(i&0b111);
}
renderSnake();
}
uint8_t line;
ISR(TIMER0_OVF_vect){
hideLine();
if(img)
setRow(image[line]);
else
setRow(image2[line]);
showLine(line);
line ++;
if(line==8) line = 0;
TCNT0 = 150;
}
int main(void) {
sei();
TCCR0 = 2;
TIMSK |= 1;
PORTC = 0;
PORTD = 0;
PORTB = 0;
DDRB = 0b11000011;
DDRC = 0xff;
DDRD = 0xff;
// X Y
snake[0] = 0x33;
snake[1] = 0x34;
snake[2] = 0x35;
l=3;
/*
* PB4
* PB5 PB2
* PB3
*/
renderSnake();
while (true) {
if(!end)
counter++;
else
endCounter++;
if(counter > 120){
counter =0;
move();
addFront();
if(snake[0] == target) {
l++;
target = ((random&0b111000)<<1)|(random&0b111);
}
clearImage();
if(colision()){
end = true;
renderEnd();
}else{
renderSnake();
renderTarget();
}
img = !img;
}
if(!(PINB&1<<4))
dir = UP;
if(!(PINB&1<<5))
dir = LEFT;
if(!(PINB&1<<2))
dir = RIGHT;
if(!(PINB&1<<3))
dir = DOWN;
if(end&&(endCounter>1000)){
endCounter = 1020;
}
if((endCounter>1000)&&end&&(!(PINB&1<<4)||!(PINB&1<<5)||!(PINB&1<<2)||!(PINB&1<<3))){
snake[0] = 0x33;
snake[1] = 0x34;
snake[2] = 0x35;
target = 0x33;
l=3;
clearImage();
renderSnake();
renderTarget();
img = !img;
end = false;
endCounter = 0;
}
wait_ms(1);
rndCounter++;
if(rndCounter>0b111111)rndCounter = 0;
if(!(PINB&1<<4)||!(PINB&1<<5)||!(PINB&1<<2)||!(PINB&1<<3))
random = ((rndCounter&0b111000)<<1)|(rndCounter&0b111);
}
}
find_program(AVR_CC avr-gcc)
find_program(AVR_CXX avr-g++)
find_program(AVR_OBJCOPY avr-objcopy)
find_program(AVR_SIZE_TOOL avr-size)
find_program(AVRDUDE avrdude)
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_C_COMPILER ${AVR_CC})
set(CMAKE_CXX_COMPILER ${AVR_CXX})
add_definitions( -Wall )
IF(NOT CMAKE_BUILD_TYPE)
SET(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING
"Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel."
FORCE)
ENDIF(NOT CMAKE_BUILD_TYPE)
set(PROGRAMMER_NAME usbasp)
set(AVRDUDE_OPTIONS)
set(PROGRAMMER_PORT usb)
set(AVR_MCU atmega8)
set(UPLOAD_VERIFY true)
function(add_avr_executable EXECUTABLE_NAME)
set(elf_file ${EXECUTABLE_NAME}-${AVR_MCU}.elf)
set(hex_file ${EXECUTABLE_NAME}-${AVR_MCU}.hex)
set(map_file ${EXECUTABLE_NAME}-${AVR_MCU}.map)
add_executable(${elf_file} EXCLUDE_FROM_ALL ${ARGN})
set(common_opts "-mmcu=${AVR_MCU} -fshort-enums -fpack-struct")
set_target_properties(
${elf_file}
PROPERTIES
COMPILE_FLAGS "${common_opts}"
LINK_FLAGS "${common_opts} -Wl,-Map,${map_file}"
)
add_custom_command(
OUTPUT ${hex_file}
COMMAND
${AVR_OBJCOPY} -j .text -j .data -O ihex ${elf_file} ${hex_file}
COMMAND
${AVR_SIZE_TOOL} ${elf_file}
DEPENDS ${elf_file}
)
list(APPEND all_hex_files ${hex_file})
list(APPEND all_map_files ${map_file})
set(eeprom_image ${EXECUTABLE_NAME}-eeprom.hex)
add_custom_command(
OUTPUT ${eeprom_image}
COMMAND
${AVR_OBJCOPY} -j .eeprom --change-section-lma .eeprom=0
-O ihex ${elf_file} ${eeprom_image}
DEPENDS ${elf_file}
)
list(APPEND all_hex_files ${eeprom_image})
add_custom_target(
${EXECUTABLE_NAME}
ALL
DEPENDS ${all_hex_files}
)
get_directory_property(clean_files ADDITIONAL_MAKE_CLEAN_FILES)
list(APPEND clean_files ${all_map_files})
set_directory_properties(
PROPERTIES
ADDITIONAL_MAKE_CLEAN_FILES "${clean_files}"
)
string(TOUPPER ${EXECUTABLE_NAME} name_uc)
set(upload_file ${EXECUTABLE_NAME}-${AVR_MCU}.hex)
if(UPLOAD_VERIFY)
add_custom_target(
upload_${EXECUTABLE_NAME}
${AVRDUDE} -p ${AVR_MCU} -c ${PROGRAMMER_NAME} ${AVRDUDE_OPTIONS}
-U flash:w:${upload_file}
-P ${PROGRAMMER_PORT}
DEPENDS ${upload_file} ${eeprom_image}
COMMENT "Uploading ${upload_file} to ${AVR_MCU} using programmer ${PROGRAMMER_NAME} on ${PROGRAMMER_PORT}"
)
elseif(NOT UPLOAD_VERIFY)
add_custom_target(
upload_${EXECUTABLE_NAME}
${AVRDUDE} -V -p ${AVR_MCU} -c ${PROGRAMMER_NAME} ${AVRDUDE_OPTIONS}
-U flash:w:${upload_file}
-P ${PROGRAMMER_PORT}
DEPENDS ${upload_file} ${eeprom_image}
COMMENT "Uploading ${upload_file} to ${AVR_MCU} using programmer ${PROGRAMMER_NAME} on ${PROGRAMMER_PORT}"
)
endif(UPLOAD_VERIFY)
add_custom_target(
disassemble_${EXECUTABLE_NAME}
avr-objdump -h -S ${EXECUTABLE_NAME}-${AVR_MCU}.elf > ${EXECUTABLE_NAME}.lst
DEPENDS ${EXECUTABLE_NAME}-${AVR_MCU}.elf
)
endfunction(add_avr_executable)
function(add_avr_library LIBRARY_NAME)
_avr_get_mcu_list(${LIBRARY_NAME} target_mcus)
foreach(mcu ${target_mcus})
set(lib_file ${LIBRARY_NAME}-${mcu})
add_library(${lib_file} STATIC ${ARGN})
set_target_properties(
${lib_file}
PROPERTIES
COMPILE_FLAGS -mmcu=${mcu}
)
endforeach(mcu ${target_mcus})
endfunction(add_avr_library)
/*---------------------------------------------------------------------------
soubor: wait.c
verze: 1.0
datum: 9.1.2012
popis:
Cekaci rutiny a makra pro bitove operace
Cekaci rutiny jsou mnohem uspornejsi nez bezne "delay.h"
Tento soubor vynikl ze souboru "mojelib.h" jehoz autora bohuzel neznam.
---------------------------------------------------------------------------*/
#include "wait.h"
//cekej milisekund:
unsigned char reg21 = F_CPU / 60000; // F_CPU = frekv. oscilatoru [Hz],
// (vnitrni konstanta prekladace)
void wait_ms (unsigned int c)
{
asm("push r20");
asm("push r21");
asm("_Wms0:");
asm("ldi r20,0x14");
asm("_Wms1:");
asm("lds r21,reg21");
asm("_Wms2:");
asm("dec r21");
asm("brne _Wms2");
asm("dec r20");
asm("brne _Wms1");
asm("dec r24");
asm("brne _Wms0");
asm("dec r25");
asm("brpl _Wms0");
asm("pop r21");
asm("pop r20");
}
//cekej mikrosekund:
unsigned char reg20 = F_CPU / 6000000 + 1;
void wait_us(unsigned int c)
{
asm("push r20");
asm("_wus0:");
asm("lds r20,reg20"); // 1-6MHz: r20=1 6-12MHz: r20=2
asm("_wus1:");
asm("dec r20");
asm("brne _wus1");
asm("dec r24");
asm("brne _wus0");
asm("dec r25");
asm("brpl _wus0");
asm("pop r20");
}
//eof
//(c) OK1ZKV 2012
// Zmena bitu portu, IO registru nebo promenne:
#define setb(bajt,bit) bajt |= 1<<(bit) //nastav bit
#define clrb(bajt,bit) bajt &= ~(1<<(bit)) //nuluj bit
#define negb(bajt,bit) bajt ^= 1<<(bit) //neguj bit
// Casove smycky:
void wait_ms (unsigned int c); // cekej milisekund(max65553):
void wait_us (unsigned int c); // cekej mikrosekund(max65553):
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment