Stap 4: Referentieprogramma
#include#define COL 1 #define ROW 2 int col = 0; //mark current column int row = 0; //mark current row int ledState[8][8]; //mark current led’s state /* haveUpdate used to eliminate jitter of input, it will update data when obtains extern interrupt program, and set haveUpdate to 1 stand for we have updated data already, then open timer and ignore extern interrupt signal or do nothing in interrupt program during this time, CPU set haveUpdate to 0 when time’s out in timer interrupt program. */ int haveUpdate = 0; void setup() { memset(ledState, 0, sizeof(ledState)); //clear state of all led initPort(); TCCR2A = _BV(COM2A0) | _BV(COM2B1) | _BV(WGM21) | _BV(WGM20); TCCR2B = _BV(WGM22) | _BV(CS20); //no divide OCR2A = 100; OCR2B = 99; //duty is 99% attachInterrupt(0, externInterrupt, FALLING ); cli(); //disable all interrupt TCCR1A=0; //we need no set TCCR1A because it is apply to pwm TCCR1B=(1< TCNT1=0xFE79; //25ms TIMSK1 = 0; //close overflow interrupt sei(); //enable interrupt } ISR(TIMER1_OVF_vect){ //timer interrupt program TIMSK1 = 0; // close overflow interrupt haveUpdate = 0; } void loop() { scan(); } void externInterrupt() { if(haveUpdate == 0){ ledState[row][col] = 1; haveUpdate = 1; TCNT1=0xFE79; TIMSK1 = (1< } } void initPort() { int i, startPin = 4; for(i=0; i<10; i++){ pinMode(startPin+i, OUTPUT); } setData(COL, 0x00); setData(ROW, 0x00); pinMode(3, OUTPUT); } void setData(int flag, int data) { int i, startPin; if(COL == flag){ startPin = 4; }else{ startPin = 7; } for(i=0; i<3; i++){ digitalWrite(startPin+i, (data & (1< } } void scan() { for(row=0; row<8; row++){ setData(ROW, row); for(col=0; col<8; col++){ setData(COL, col); if(ledState[row][col] > 0) OCR2B = 1; else OCR2B = 98; delayMicroseconds(300); OCR2B = 98; } } }