Vai al contenuto
PLC Forum


modifica programma


Messaggi consigliati

Inserito:

ho trovato questo programma per eseguire un incremento in automatico e tutto funziona , come posso fare per fargli eseguire un incremento premendo un pulsante su RB0 .

Il programma è questo:

unsigned short mask(unsigned short num) {

switch (num) {

case 0 : return 0x3F;

case 1 : return 0x06;

case 2 : return 0x5B;

case 3 : return 0x4F;

case 4 : return 0x66;

case 5 : return 0x6D;

case 6 : return 0x7D;

case 7 : return 0x07;

case 8 : return 0x7F;

case 9 : return 0x6F;

} //case end

}

unsigned short i, j, v, por1, por2;

void interrupt() {

if (v==0) {

PORTA = 0;

PORTD = por2; // prepare mask for digit

PORTA = 2; // turn on 1st, turn off 2nd 7seg.

v = 1;

} else {

PORTA = 0;

PORTD = por1; // prepare mask for digit

PORTA = 1; // turn on 2nd, turn off 1st 7seg.

v = 0;

}

TMR0 = 0;

INTCON = 0x20;

}//~

void main() {

OPTION_REG = 0x80;

por2 = 0x3F;

j = 0;

TMR0 = 0;

INTCON = 0xA0; // Disable PEIE,INTE,RBIE,T0IE

PORTA = 0;

TRISA = 0;

PORTD = 0;

TRISD = 0;

do {

for (i = 0; i<=10; i++) { // count from 0 to 99

j = i % 10u;

por1 = mask(j); // prepare appropriate digit

j = (char)(i / 10u) % 10u;

por2 = mask(j); // prepare appropriate digit

Delay_ms(1000);

}

} while (1); // endless loop

}//~!

Se c'è qulcuno che sa mettermi sulla strada giusta .


Inserita:

RB0 può anche essere usato come interupt; se non ti interessa l'interrupt devi programmase RB0 come input nel registro TRISB.

Poi usando la funzione bit_test

if( (bit_test(PORTB,0) == 1) & (flg_b0_mem == 1))
{
    cont++;
        
}
else  {
  
      if( (bit_test(PORTB,0) == 0) & (flg_b0_mem == 1))
         {
               flg_b0_mem = 0;
         }
      if( (bit_test(PORTB,0) == 1) & (flg_b0_mem == 0))
         {
               flg_b0_mem = 1;
         }   
}

Se richiami questa funzione a tempo fisso, 10ms p.e., fai anche il debouncing sull'ingresso RB0

Inserita:

Se vuoi incrementare un valore ad ogni pressione di un pulsante collegato a RB0 quello è il codice

Inserita:

Ciao e grazie per la risposta ho provato a scrivere il tuo codice con il compilatore della mikroC ma mi da errore "bit_test"

Inserita:

Probabilmente non ha questa funzione di libreria (io uso CCS).

Se non esiste una funzione simile con il tuo compilatore, devi effetture il test usando AND (per gli 1) e OR (per gli zeri).

if( (bit_test(PORTB,0) == 1) & (flg_b0_mem == 1))
{
   cont++;
       
}
diventa
if ((PORTB && 01) & (flg_b0_mem == 1))

{
   cont++;
       
}

Ovviamente flg_b0_mem deve essere dichiarato come boolean

Crea un account o accedi per commentare

Devi essere un utente per poter lasciare un commento

Crea un account

Registrati per un nuovo account nella nostra comunità. è facile!

Registra un nuovo account

Accedi

Hai già un account? Accedi qui.

Accedi ora
×
×
  • Crea nuovo/a...