#include #include #include unsigned short int pulse; // räknar inkommande pulser unsigned short int position; //vilket läge vi befinner oss i unsigned short int checkPulse; //håller koll på om det kommer en puls unsigned short int vector1[64]; //håller koll på pulser i cs1 unsigned short int vector2[64]; //håller koll på pulser i cs2 //hanterar knapparna void buttonHandler(){ unsigned short int buttons; // kontrollerar knapparna buttons = PINB&0x60; switch(buttons){ case 0x40 : clearDisp(); position = 1; _delay_ms (2000); //PINB = 0x60; //aktiverar pullup break; case 0x20 : clearDisp(); position = 0; //PINB = 0x60; //aktiverar pullup _delay_ms (2000); writeLine(); break; } } void pulseCounter(){ if(TCNT1 >= 9766){ pulse = TCNT0*6; TCNT0 = 0; TCNT1 = 0; checkPulse = 0; if(position == 1){ clearDisp(); } } } void startDisp(){ PORTA = 0x2C; PORTD = 0x3F; PORTA = 0x28; PORTA = 0x34; PORTD = 0x3F; PORTA = 0x30; } void closeDisp(){ PORTA = 0x2C; PORTD = 0x3E; PORTA = 0x28; PORTA = 0x34; PORTD = 0x3E; PORTA = 0x30; } void writeData(short int chipselect, short int type, short int x, short int y){ _delay_us (10); if( chipselect == 1){ PORTA = 0x2C; PORTD = 64+y; //sätter DB7 och DB6 till 0 1 PORTA = 0x28; //skickar y-koordinaten på port D _delay_us (10); PORTA = 0x2C; PORTD = 184+x; //sätter DB7 till DB3 till 1 0 1 1 1 PORTA = 0x28; //skickar x-koordinaten på port D _delay_us (10); PORTA = 0x2D; PORTD = type; //väljer värdet på datan PORTA = 0x29; //skickar datan på port D _delay_us (10); } if(chipselect == 2){ PORTA = 0x34; PORTD = 64+y; //sätter DB7 och DB6 till 0 1 PORTA = 0x30; //skickar y-koordinaten på port D _delay_us (10); PORTA = 0x34; PORTD = 184+x; //sätter DB7 till DB3 till 1 0 1 1 1 PORTA = 0x30; //skickar x-koordinaten på port D _delay_us (10); PORTA = 0x35; PORTD = type; //väljer värdet på datan PORTA = 0x31; //skickar datan på port D _delay_us (10); } } void clearDisp(){ for(int i =0; i<64; i++){ for(int j =0; j<8; j++){ writeData(1,0,j,i); writeData(2,0,j,i); } } } void fillDisp(){ for(int i =0; i<64; i++){ for(int j =0; j<8; j++){ writeData(1,255,j,i); writeData(2,255,j,i); } } } void writeLine(){ //ritar linje for(int i =0; i<64; i++){ writeData(1,0x80,3,i); writeData(1,1,4,i); writeData(2,0x80,3,i); writeData(2,1,4,i); } } void writePulse(short int chipselect, short int y){ for(int j =1; j<7; j++){ writeData(chipselect,255,j,y); } } void removePulse(short int chipselect, short int y){ for(int j =1; j<3; j++){ writeData(chipselect,0,j,y); } for(int j =5; j<7; j++){ writeData(chipselect,0,j,y); } writeData(chipselect,0x80,3,y); writeData(chipselect,0x01,4,y); } void one(short int chipselect, short int y){ for(int j =3; j<5; j++){ writeData(chipselect,255,j,y); } } void two(short int chipselect, short int y){ for(int i =1; i<7; i++){ writeData(chipselect,1,3,y+i); writeData(chipselect,129,4,y+i); } writeData(chipselect,255,3,y+7); writeData(chipselect,255,4,y); writeData(chipselect,1,3,y); writeData(chipselect,129,4,y+7); } void three(short int chipselect, short int y){ for(int i =1; i<7; i++){ writeData(chipselect,1,3,y+i); writeData(chipselect,129,4,y+i); } writeData(chipselect,255,3,y+7); writeData(chipselect,255,4,y+7); writeData(chipselect,1,3,y); writeData(chipselect,129,4,y); } void four(short int chipselect, short int y){ for(int i =1; i<7; i++){ writeData(chipselect,1,4,y+i); } writeData(chipselect,255,3,y); writeData(chipselect,255,4,y+7); writeData(chipselect,255,3,y+7); writeData(chipselect,1,4,y); } void five(short int chipselect, short int y){ for(int i =1; i<7; i++){ writeData(chipselect,1,3,y+i); writeData(chipselect,129,4,y+i); } writeData(chipselect,255,3,y); writeData(chipselect,255,4,y+7); writeData(chipselect,1,3,y+7); writeData(chipselect,129,4,y); } void six(short int chipselect, short int y){ for(int i =1; i<7; i++){ writeData(chipselect,1,3,y+i); writeData(chipselect,129,4,y+i); } writeData(chipselect,255,3,y); writeData(chipselect,255,4,y+7); writeData(chipselect,1,3,y+7); writeData(chipselect,255,4,y); } void seven(short int chipselect, short int y){ for(int i =1; i<7; i++){ writeData(chipselect,1,3,y+i); } writeData(chipselect,255,3,y+7); writeData(chipselect,255,4,y+7); writeData(chipselect,1,3,y); } void eight(short int chipselect, short int y){ for(int i =1; i<7; i++){ writeData(chipselect,1,3,y+i); writeData(chipselect,129,4,y+i); } writeData(chipselect,255,3,y); writeData(chipselect,255,4,y+7); writeData(chipselect,255,3,y+7); writeData(chipselect,255,4,y); } void nine(short int chipselect, short int y){ for(int i =1; i<7; i++){ writeData(chipselect,1,3,y+i); writeData(chipselect,1,4,y+i); } writeData(chipselect,255,3,y); writeData(chipselect,255,4,y+7); writeData(chipselect,255,3,y+7); writeData(chipselect,1,4,y); } void zero(short int chipselect, short int y){ for(int i =1; i<7; i++){ writeData(chipselect,1,3,y+i); writeData(chipselect,128,4,y+i); } writeData(chipselect,255,3,y); writeData(chipselect,255,4,y+7); writeData(chipselect,255,3,y+7); writeData(chipselect,255,4,y); } void numberHandler(short int number, short int pos){ if(number == 0){ zero(1, pos); } if(number == 1){ one(1, pos); } if(number == 2){ two(1, pos); } if(number == 3){ three(1, pos); } if(number == 4){ four(1, pos); } if(number == 5){ five(1, pos); } if(number == 6){ six(1, pos); } if(number == 7){ seven(1, pos); } if(number == 8){ eight(1, pos); } if(number == 9){ nine(1, pos); } } void displayPulse(){ unsigned short int temp; temp = pulse; if(temp >= 100){ if(temp/100 ==1){ one(1, 0); } if(temp/100 ==2){ two(1,0); } temp=temp%100; numberHandler(temp/10, 10); temp=temp%10; numberHandler(temp, 20); } else if(temp < 100 && temp >= 10){ numberHandler(temp/10, 10); temp=temp%10; numberHandler(temp, 20); } else{ numberHandler(temp, 20); } } void displayGraph(){ for(int i =63; i>=0 ; i--){ if(TCNT0 > checkPulse){ // writePulse(2, 0); checkPulse =TCNT0; vector1[0] =1; //i=0; } if(vector1[i] == 1 && i!=63){ removePulse(2,i); writePulse(2, i+1); vector1[i]=0; vector1[i+1]=1; } if(vector2[i] == 1 && i!=63){ removePulse(1,i); writePulse(1, i+1); vector2[i]=0; vector2[i+1]=1; } if(vector1[i] == 1 && i==63){ removePulse(2,i); writePulse(1, 0); vector1[i]=0; vector2[0]=1; } if(vector2[i] == 1 && i==63){ removePulse(1,i); vector2[i]=0; } _delay_ms (1); } } void main(){ DDRB = 0x00; //sätter alla portar som ingående DDRD = 0xFF; //sätter alla portar som utgående DDRA = 0xFF; //sätter alla som utgående TCCR1B = 0x05; //startar intern klocka med frekvens 1mhz/1024 TCCR0 = 0x07; //startar extern klocka startDisp(); //fillDisp(); clearDisp(); position = 1; while(1){ PINB = 0x60; //aktiverar pull-up/down buttonHandler(); pulseCounter(); if(position == 1){ displayPulse(); } if(position == 0){ displayGraph(); } } }