#!/usr/local/bin/php timers [GeWiki]
 
//arlotto 2006 : Démonstration de temporisation
// en utilisant le timer1 avec l'oscillateur 32.768kHz
// La led RB2 clignote chaque seconde pour montrer le passage dans l'IT
// La led RB1 s'allumme 5 secondes après l'appui sur le bouton RB0
// de la carte picdem2+
// Sans rechargement Toverflow = 2^16 . Tosc = 2^16 . (1/32768) = 2s
// En préchargeant le compteur à 32768 Toverflow = (2^16 - 32768)/32768 = 1s 
// Le même principe peut être utilisé avec les autres timers
// avec d'autres base de temps (osc du pic ,  osc externe, prescaler ,...) 
// ici les it sont utilisées dans le mode pic18 
// avec priorité (RCONbits.IPEN = 1 )
// L'autorisation se fait par les bits GIE et PEIE de INTCON.
 
#include <p18cxxx.h> 
#include <timers.h>   // pour fonctions timers
 
// configuration PICDEM2+ quartz
#pragma config OSC = HS
#pragma config WDT = OFF
#pragma config LVP = OFF
#pragma config PBADEN = OFF
 
#define Button0 PORTBbits.RB0
#define Led1 PORTBbits.RB1
#define Led2   PORTBbits.RB2
 
 
 
//prototypes des gestionnaires d'interruption
void InterruptHandlerHigh (void);
 
unsigned int cpt ;
 
void main(void){
unsigned char state=0 ;
Led1= 0 ;
Led2 = 0 ;
TRISBbits.TRISB1=0;
TRISBbits.TRISB2=0;
OpenTimer1(TIMER_INT_ON   &
           T1_16BIT_RW    &
           T1_SOURCE_EXT  &
           T1_PS_1_1      &
           T1_OSC1EN_ON   &  
           T1_SYNC_EXT_ON    );
WriteTimer1(32768); // Préchargement
IPR1bits.TMR1IP=1;           // T1 haute priorité
 
RCONbits.IPEN = 1;
INTCONbits.GIEH = 1 ; // autorisation high
 
for( ;; )
{
   switch (state) {
    case 0 : Led1 = 0 ;
             if ( Button0 == 0 ) {
             state = 1 ; }
             break ;
    case 1 : cpt = 5 ;
             state=2;
             break ; 
    case 2 : if ( cpt == 0 ) {  
             state = 3 ; }
             break ;
    case 3 :
            Led1 = 1 ;
            if(Button0 == 0 ) {
            state = 0 ;   
            }
            break ;                    
   }
 // la boucle n'est jamais bloquée !!
}
}
 
//Placement d'un saut vers le gestionnaire d'interruption
//----------------------------------------------------------------------------
// High priority interrupt vector
#pragma code InterruptVectorHigh = 0x08
void InterruptVectorHigh (void)
{
  _asm
    goto InterruptHandlerHigh //jump to interrupt routine
  _endasm
}
 
// Routine gestionnaire d'interruption
//----------------------------------------------------------------------------
// High priority interrupt routine
#pragma code
#pragma interrupt InterruptHandlerHigh
void InterruptHandlerHigh (void){
 
   if(PIR1bits.TMR1IF ){   // On passe ici chaque seconde
          PIR1bits.TMR1IF = 0 ;
          Led2 = !Led2 ;
          WriteTimer1(32768); // Préchargement
          if(cpt>=0) { cpt--; }
    }
 // Placer ici les autres parties
 // if(Autre bit F)
  // {
  //   Raz du bit F ;
  //   Traitement ;
  // }
}
 
timers.txt · Dernière modification: 2015/04/27 16:54 (édition externe)
 
Sauf mention contraire, le contenu de ce wiki est placé sous la licence suivante:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki