Last active
March 27, 2017 21:12
-
-
Save RcrdBrt/57fe73048b0289ae42f58b766e50596c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <c8051f020.h> | |
int c=0; | |
int d=0; | |
bit event=0;//1= è avvenuto un interrupt; | |
bit a=1; | |
bit toverflow=0; | |
bit secondevent= 0; // seconda pressione | |
sbit Button = P3^7; | |
sbit Led = P1^6; | |
//slide 9 | |
void init(){ | |
EA=1;//1 ABILITA INTERRUPT, 0 DISABILITA | |
WDTCN=0xde; //sequenza di azzeramento del watchdog | |
WDTCN=0xad; //(due operazioni per evitare reset accidentale) | |
OSCICN &= 0x014; | |
XBR0=0x0; | |
XBR1=0X0; | |
XBR2=0X040; | |
P1MDOUT |= 0X040; | |
//STEP1 | |
CKCON &=0xF0;//bit t0m, il timer 0 usa il system clock / 12, (invariati i primi 4 bit, a zero i successivi) | |
//STEP 2 (16 bit o 8?) | |
TMOD &= 0xFB;//spengo il terzo bit (se acceso si appoggia a un clock esterno); | |
TMOD |= 0x02;//isolo la parte di registro del timer 0 da quella a 1 e metto a 1 il secondo bit (autoreload) | |
TL0 = 0x00; //timer 0 low = 0; | |
TH0 = 0x00; //timer 0 high = 0; | |
TR0 = 1; //attivo il timer 0; | |
//INTERRUPT | |
ET0 = 1;//ABILITO INTERRUPT TIMER | |
} | |
void timer0 (void) interrupt 1 using 2 { | |
if (event) | |
++c; | |
++d; | |
} | |
void button (void) interrupt 19 using 0{ | |
if(event) | |
secondevent=1; | |
event= 1; | |
} | |
void polling() | |
{ | |
if(TF0){ | |
TF0=0; | |
++c; | |
} | |
if(c>=501){ | |
c=0; | |
Led= !Led; | |
} | |
} | |
void main(void){ | |
//int c=0; | |
init(); | |
while(1){ | |
if (c >= 1 && c <= 500) { | |
if (secondevent) { | |
a = !a; | |
secondevent = 0; | |
event = 0; | |
} | |
} | |
else { | |
event = 0; | |
secondevent = 0; | |
c = 0; | |
} | |
if (a) { | |
if (d >= 1000) { | |
Led = !Led; | |
d = 0; | |
} | |
} | |
else { | |
d = 0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment