CODICE SORGENTE C XC8 PER CONTAPEZZI MANUALE.
Il Codice è scritto in C, il compilatore usato è l'XC8, quarzo 4MHz. Pigiando il pulsante , il contatore avanza fino a 9999 dopo di che si azzera. Il circuito per la simulazione:
/*
* File: main.c
* Author: PRIMOK_V
*
* Created on 7 luglio 2019, 21.22
*/
#define _XTAL_FREQ 4000000
#include <xc.h>
#include "Config.h"
#include "contapezzi.h"
unsigned char DD0=0, DD1=0, DD2=0, DD3=0;
volatile unsigned int Count=0;
volatile unsigned char n_digit=0;
unsigned int rilascio=1;
unsigned char StatoKeys; //Variabile di stato per gestione tasti.
volatile unsigned char TimerDebounce;//Timer_debounce tasto.
volatile FLAGS_UNION FlagInt; //Definisce FlagInt come variabile di tipo bit singolo
#define EndDebounce FlagInt.t.bit0 //Se=1 => Terminato debounce tasto/ritardo per auto-ripetizione tas
#define FlagImpulso FlagInt.t.bit1 //Se=1 => Tasto premuto
#define IMPULSO PORTAbits.RA4
//******************************************************************************
// COSTANTI PER TASTI
//******************************************************************************
#define ST_DEB_ON 0x1 //Stato di gestione debounce in chiusura
#define ST_KEY_ON 0x2 //Stato di gestione tasto premuto
#define ST_DEB_OFF 0x3 //Stato di gestione debounce in apertura
#define ST_KEY_OFF 0x4 //Stato di gestione tasto a riposo
//---------------------------------------------------------------------------
// ROUTINE INTERRUPT
//---------------------------------------------------------------------------
void interrupt isr(void){
if(T0IF){//Ogni 2 msec
if(++n_digit>4){
n_digit=1;
}
PORTA = 0b11111111;//Spegne tutti i digit
PORTB = 0b11111111;//Spegne tutti i digit
switch (n_digit) {
case 1:
PORTB = DD0;//Carica cifra meno significativa
PORTA = 0b00000111; //Accende transistor del 1° digit
break;
case 2:
PORTB = DD1;//Carica cifra meno significativa
PORTA = 0b00001110; //Accende transistor del 2° digit
break;
case 3:
PORTB = DD2;//Carica cifra meno significativa
PORTA = 0b00001101; //Accende transistor del 3° digit
break;
case 4:
PORTB = DD3;//Carica cifra meno significativa
PORTA = 0b00001011; //Accende transistor del 4° digit
break;
}
if(TimerDebounce>0){
if (--TimerDebounce == 0){ //Se avvenuto time-out per debounce tasto
EndDebounce=1;
}//end if (--TimerDebounce ==0) {
}//Fine if(TimerDebounce>0){
TMR0=130;
T0IF=0;
}
}
//------ Maschera per il display 7 segmenti anodo comune
unsigned char mask(unsigned char num) {
unsigned char ret;
switch (num) {
case 0 :
ret=0xC0;
break;
case 1 :
ret= 0xF9;
break;
case 2 :
ret= 0xA4;
break;
case 3 :
ret= 0xB0;
break;
case 4 :
ret= 0x99;
break;
case 5 :
ret= 0x92;
break;
case 6 :
ret= 0x82;
break;
case 7 :
ret= 0xF8;
break;
case 8 :
ret= 0x80;
break;
case 9 :
ret= 0x90;
break;
} //case end
return ret;
}
void conversione(){
DD0 = mask(Count%10);
DD1 = mask((Count/10)%10);
DD2 = mask((Count/100)%10);
DD3 = mask((Count/1000));
}
void main(void) {
CMCON = 0x07;// Disabilita Comparatori
TRISB = 0x00; // PORTB tutta out
PORTB = 0xFF; // Spegni PORTB
TRISA = 0b00010000; // RA4 ingresso
Count =0; // Valore iniziale
PORTA = 0b00001111;
VRCON=0b00000000;
GIE=1;
T0IE=1;
INTCONbits.T0IE=1;
INTCONbits.PEIE=1;
INTCONbits.TMR0IF=0;
OPTION_REG=0b00000011;
INTEDG=1;
TMR0=130;
conversione();
FlagInt.v=0;
StatoKeys=ST_KEY_OFF;
while(1){
if(FlagImpulso){
FlagImpulso=0;
Count = Count + 1;
conversione();
if (Count > 9999){
Count = 0;
conversione();
}
}
switch (StatoKeys){//Gestione degli stati per i tasti
//-----------------------------------------------------------------------------
case ST_KEY_OFF://Stato di tasto a riposo
if ((IMPULSO) ){//Se premuto pulsante di incremento
EndDebounce=0;//resetta flag di time-out
TimerDebounce=TimeDebounce;//Carico variabile per debounce
StatoKeys=ST_DEB_ON;//setta stato debounce in chiusura
}
break;
//-----------------------------------------------------------------------------
case ST_DEB_ON://Stato di gestione debounce in chiusura
if (EndDebounce){//Se terminato debounce, gestisce aggiornamento
// buffer tasti
//------------------------------------------------------------------------------
if(IMPULSO){
FlagImpulso=1;//setta flag di programmazione
}
StatoKeys=ST_KEY_ON;//setta stato tasto premuto
}//Fine if (EndDebounce){
break;
//-----------------------------------------------------------------------------
case ST_KEY_ON://Stato di uno o piu' tasti premuti
if (!IMPULSO){//Se nessun tasto premuto
EndDebounce=0; //resetta flag di time-out
TimerDebounce=TimeDebounce;
StatoKeys=ST_DEB_OFF;//setta stato debounce in apertura
}
break;
//-----------------------------------------------------------------------------
case ST_DEB_OFF://Stato di gestione debounce in apertura
if (EndDebounce){
EndDebounce=0;
//Se terminato debounce gestisce aggiornamento buffer tasti:
StatoKeys=ST_KEY_OFF;//setta stato tasti a riposo
}
break;
}//Fine StatoKeys
}//While
}//Main
Video