Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. #include <c8051f020.h>
  2.  
  3. int c=0;
  4. int d=0;
  5. bit event=0;//1= รจ avvenuto un interrupt;
  6. bit a=1;
  7. bit toverflow=0;
  8. bit secondevent= 0; // seconda pressione
  9. sbit Button = P3^7;
  10. sbit Led = P1^6;
  11. //slide 9
  12. void init(){
  13. EA=1;//1 ABILITA INTERRUPT, 0 DISABILITA
  14. WDTCN=0xde; //sequenza di azzeramento del watchdog
  15. WDTCN=0xad; //(due operazioni per evitare reset accidentale)
  16.  
  17. OSCICN &= 0x014;
  18. XBR0=0x0;
  19. XBR1=0X0;
  20. XBR2=0X040;
  21. P1MDOUT |= 0X040;
  22.  
  23. //STEP1
  24. CKCON &=0xF0;//bit t0m, il timer 0 usa il system clock / 12, (invariati i primi 4 bit, a zero i successivi)
  25. //STEP 2 (16 bit o 8?)
  26. TMOD &= 0xFB;//spengo il terzo bit (se acceso si appoggia a un clock esterno);
  27. TMOD |= 0x02;//isolo la parte di registro del timer 0 da quella a 1 e metto a 1 il secondo bit (autoreload)
  28. TL0 = 0x00; //timer 0 low = 0;
  29. TH0 = 0x00; //timer 0 high = 0;
  30. TR0 = 1; //attivo il timer 0;
  31.  
  32. //INTERRUPT
  33. ET0 = 1;//ABILITO INTERRUPT TIMER
  34.  
  35. }
  36.  
  37. void timer0 (void) interrupt 1 using 2 {
  38. if (event)
  39. ++c;
  40. ++d;
  41. }
  42.  
  43.  
  44. void button (void) interrupt 19 using 0{
  45. if(event)
  46. secondevent=1;
  47. event= 1;
  48. }
  49.  
  50. void polling()
  51. {
  52. if(TF0){
  53. TF0=0;
  54. ++c;
  55. }
  56. if(c>=501){
  57. c=0;
  58. Led= !Led;
  59. }
  60. }
  61.  
  62. void main(void){
  63. //int c=0;
  64.  
  65. init();
  66. while(1){
  67. if (c >= 1 && c <= 500) {
  68. if (secondevent) {
  69. a = !a;
  70. secondevent = 0;
  71. event = 0;
  72. }
  73. }
  74. else {
  75. event = 0;
  76. secondevent = 0;
  77. c = 0;
  78. }
  79. if (a) {
  80. if (d >= 1000) {
  81. Led = !Led;
  82. d = 0;
  83. }
  84. }
  85. else {
  86. d = 0;
  87. }
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement