contare gli impulsi in finestra di 1 secondo su pic

Devo conteggiare gli impulsi che mi giungono in un secondo ad una delle porte di un PIC16F876. Non ho idea di come fare la routine che tenga diciamo aperto il conteggio per esattamente 1 secondo, non di più ne di meno ed in modo che la possa richiamare quando e come mi pare a me.

Mi date qualche idea?? Possiiblmente in linguaggio Mikrobasic ??

Grazie

Reply to
Giovanni
Loading thread data ...

Il giorno Sat, 22 Sep 2007 18:09:40 +0200, "Giovanni" ha scritto:

In genere queste cose le faccio in assembler: configuro il pic in modo che TMR1 sia incrementato dall'instruction clock interno, poi definisco un loop, all'inizio del loop assegno un valore a TMR1 che corrisponda ad un tempo di ciclo ad es. di un decimo o un ventesimo del tempo totale (nel tuo caso 1 s/10 = 100 ms), alla fine del loop c'è un controllo che aspetta che si alzi il bit di overflow di TMR1, dopodichè il ciclo è finito e con un goto si ritorna all'inizio del loop. In tal modo ogni ciclo del loop dura esattamente 100 ms (nell'esempio di prima), quindi basta con una variabile ad 8 bit contare 10 cicli per avere un tempo di 1 s.

#define CYCLE 100 ; Sw cycle time (ms) #define TMR1_CLK 1 ; TIMER1 clock period (us) #define TMR1_PSC 2 ; TIMER1 prescale factor #define TMR1_CLKP (TMR1_PSC*TMR1_CLK) ; Timer1 clock period with prescaler (us) #define TMR1_CNT ((CYCLE*1000)/TMR1_CLKP) ; Timer1 counts for 1 cycle (prescaler) #define TMR1_CMP ....... ; TIMER1 counts compensation #define TMR1_SET (0x10000-TMR1_CNT+TMR1_CMP) ; Timer1 preset value

#define GATE_T (1000/CYCLE) ; Gate open period (cycles)

cblock ....... err ; Error code (0 = no errors) gateCnt ; Gate half period counter (frequency mode) endc

..................... bsf T1CON,TMR1ON ; Start Timer1

MainLoop bcf INTCON,GIE ; Disable interrupts clrf TMR1L ; Clear TMR1L, ensures no rollover into TMR1H movlw high TMR1_SET ; Set Timer1 movwf TMR1H movlw low TMR1_SET movwf TMR1L bcf PIR1,TMR1IF ; Clear Timer1 overflow flag bsf INTCON,GIE ; Enable interrupts .................. movlw GATE_T-1 ; Gate open period expired ? subwf gateCnt,W bnc ................... ; If gateCnt < GATE_T-1, no .................. incf gateCnt,F ; Update gate .................. clrf gateCnt ; Clear gate counter .................. btfsc PIR1,TMR1IF ; If Timer1 overflow then a cycle error occurred goto end_c_err ; If yes, set the error code goto end_c_ok ; Otherwise go on end_c_err movlw ERR_CYC ; Set the error code (cycle time error) movwf err goto end_c_end end_c_ok btfss PIR1,TMR1IF ; If Timer1 overflow then the cycle time expired goto end_c_ok ; If not, wait for the overflow end_c_end goto MainLoop

Reply to
Luigi C.

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.