home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 27 / IOPROG_27.ISO / COMMON / LISTC2.ZIP / pwm.c < prev   
Encoding:
C/C++ Source or Header  |  1998-02-11  |  2.4 KB  |  88 lines

  1. /* PWM per luminosita' led */
  2. #include <pic1684.h>
  3.  
  4. void aspetta(unsigned int nvol);
  5. void inizializza();
  6. void getkey();
  7.  
  8. void inizializza()
  9. {
  10.   INTCON=0;             /* disabilita gli IRQ */
  11.   PORTB=0;
  12.   TRISB=0xff;           /* porta B: tutti i pin come input */
  13.   OPTION=0;             /* abilita resistenze di pull-up sulla porta B */
  14.   TRISA=0;        /* porta A: tutti i pin come output */
  15.   PORTA=0;
  16. }
  17.  
  18. void aspetta(unsigned int nvol) /* Con una frequenza di 6.7Khz */
  19. {                               /* aspetta circa nvol x 4 ms */
  20.   unsigned int i;               
  21.   unsigned int ib;
  22.   for(i=0;i!=nvol;i++) {
  23.     for (ib=0;ib!=1;ib++);
  24.   }
  25. }
  26.  
  27. unsigned int n1,n2;             /* valori di attesa per PWM */
  28. unsigned int key;               /* stato della porta B */
  29. unsigned int key1,key2,kp1,kp2; /* variabili per filtro anti-rimbalzo */
  30.  
  31. void main() {
  32.   unsigned int dummy;           
  33.   n1=8;                         /* PWM a 4 bit */
  34.   n2=15-n1;
  35.   key1=0;
  36.   key2=0;
  37.   kp1=0;
  38.   kp2=0;
  39.   inizializza();
  40.   do {
  41.     PORTA=0x0f;                 /* ciclo PWM */
  42.     aspetta(n1);                /* con f.lavoro=6.7 Khz si raggiunge */
  43.     dummy=PORTB&key;            /* una frequenza di PWM di 15Hz circa */
  44.     if (dummy!=0xff)            
  45.       getkey();                 
  46.     PORTA=0;
  47.     aspetta(n2);
  48.   } while(1);
  49. }
  50.  
  51. void getkey() {
  52.   key=PORTB;
  53.   if ((key&1)==0)  {            /* se sembra premuto il tasto su PB0 */
  54.     if (kp1)    {               /* e se sembrava giα premuto 66 ms fa */
  55.       key1++;                   /* incrementa il contatore del filtro */
  56.       if (key1==15) {           /* se sono 66x16 ms che il tasto sembra premuto */
  57.         key1=0;                 /* allora vuol dire che Φ effettivamente premuto */
  58.         if (n1!=0)              /* e diminuisce la luce */
  59.           n1--;
  60.         else n1=15;
  61.         n2=15-n1;
  62.       }
  63.     }
  64.     else kp1=1;                 /* anche se non sembrava premuto 66 ms fa ora lo sembra! */
  65.   }
  66.   else {                        /* se non sembra premuto */
  67.     kp1=0;                      /* non e' premuto (non gestisce il rilascio) */
  68.     key1=0;                     /* resetta il contatore del filtro */
  69.   }
  70.   if ((key&2)==0) {             
  71.     if (kp2) {
  72.       key2++;
  73.       if (key2==15) {
  74.         key2=0;
  75.         n1++;
  76.         n1=n1&0x0f;
  77.         n2=15-n1;
  78.       }
  79.     }
  80.     else kp2=1;
  81.   }
  82.   else {
  83.     kp2=0;
  84.     key2=0;
  85.   }
  86. }
  87.   
  88.