15-02-2012, 03:43 PM
Kood:
#include <inttypes.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/signal.h>
#include <avr/wdt.h>
#define set_bit sbi
#define clear_bit cbi
#define is_set(sfr, bit) (sfr & _BV(bit))
volatile uint16_t speed = 1 , safe = 0;
volatile uint32_t cnt = 0 , t = 0 , Wait_Turbo = 0;
static const uint8_t hex [] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
volatile uint8_t numbers[11] = {
//HGFEDCBA
0b01000100,//0 --B--
0b11110110,//1 | |
0b10001100,//2 H A
0b10010100,//3 | |
0b00110110,//4 --G--
0b00010101,//5 | |
0b00000101,//6 E D
0b11110100,//7 | |
0b00000100,//8 --F-- C
0b00010100,//9
0b11111111
};
void delay( uint16_t len ){
len = len - 2;
for ( ; len > 0 ; len--){
asm( "nop" );
asm( "nop" );
}
}
void delayms( uint8_t len ){
len--;
for ( ; len > 0 ; len-- ){
delay( 999 );
asm( "nop" );
asm( "nop" );
asm( "nop" );
asm( "nop" );
asm( "nop" );
asm( "nop" );
asm( "nop" );
}
delay( 998 );
}
void doSPI( uint8_t data ){
sbi( DDRB , 5 );
sbi( DDRB , 3 );
SPCR = (1 << SPE) | (1 << MSTR) | 0b0011;
sbi( SPSR , 2 );
SPDR = data;
while( !(SPSR & (1 << SPIF)) );
}
void WriteNum( uint32_t num , uint8_t digits ){
uint8_t i;
cbi( PORTB , 2 );
for ( i = 0 ; i < digits ; i++ ) {
if ( num || !i )
doSPI( numbers[num%10] );
else
doSPI( numbers[10] );
num/=10;
}
sbi( PORTB , 2 );
}
// Programmers Notepad - pomm.c
SIGNAL ( SIG_OVERFLOW0 ) { //Siia j�utakse ca 30 korda sekundis
if ( safe > 8 )
return;
if ( t > speed )
t -= speed;
else
t = 0;
if ( Wait_Turbo )
Wait_Turbo--;
}
uint8_t turbo = 0;
void Turbo( uint8_t mode ){
if ( !(turbo & (1 << mode)) ) {
turbo |= 1 << mode;
speed *= 2;
}
}
int main( void ){
CLKPR = 128;
CLKPR = 0;
wdt_reset();
wdt_enable( WDTO_500MS );
cbi( PRR , 2 );
DDRB = 0x45;
DDRC = 0x20;
PORTC = 0x0F;
PORTD = 0xFF;
sei ();
delayms( 200 );
TCCR0B = 0b101;
TIMSK0 = 1;
WriteNum( 900 , 3 );
while ( is_set (PIND , 0 ) ) {
PORTB ^= 1;
delay( 100 );
wdt_reset();
}
t=27466;
Wait_Turbo = 20;
for (;;){
if ( t > 0 )
PORTB ^= 1; //Kontrollsignaal pommile
delay( 100 );
if ( (t / 20) % 2) set_bit( PORTB , 6 ); else clear_bit( PORTB , 6 );
if ( (t / 27) % 2) set_bit( PORTC , 5 ); else clear_bit( PORTC , 5 );
if ( safe < 100 ) safe++;
if ( is_set( PIND , 7 ) || !is_set(PIND , 2 ) ) safe=0;
if ( is_set( PIND , 1 ) ) Turbo( 1 );
if ( is_set( PIND , 3 ) ) Turbo( 2 );
if ( !is_set( PIND , 4 ) ) Turbo( 3 );
if ( is_set( PIND , 5 ) ) Turbo( 4 );
if ( is_set( PINC , 0 ) ) Turbo( 5 );
if ( is_set( PINC , 2 ) ) Turbo( 6 );
if ( is_set( PINC , 3 ) ) Turbo( 7 );
if ( !is_set( PIND , 0 ) ) {
if ( !Wait_Turbo )
speed *= 2; //Tempo magnificooooo!!!!
Wait_Turbo = 10;
}
WriteNum(t * 512 / 15625,3);
wdt_reset();
}
}