CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

ONE SHOT on button

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ceccargi



Joined: 31 Dec 2003
Posts: 1

View user's profile Send private message Send e-mail

ONE SHOT on button
PostPosted: Wed Dec 31, 2003 2:49 am     Reply with quote

this program must to count the button on RB4 and when the counter is 10 I must have anomalia_livello_minimo=1. The program it's OK but if I remove delay_us(500) or printf at the end of the program the code not working roperly.

#include <16F876.h>
#use delay(clock=20000000)
#fuses NOWDT,HS, NOPROTECT
#use rs232(baud=19200,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)

void main()
{
short selettivita;
short reset;

short os_selettivita;
short old_selettivita;

short anomalia_livello_minimo;

short ready_stazione;
int step_ctrl_livello;
int conteggio_step_livello;

// Inizializzazione variabili per controllo livello minimo
step_ctrl_livello=10;
conteggio_step_livello=0;

// Inizializzazione variabili per controllo ONE-SHOT
os_selettivita=0;
old_selettivita=1;

while (TRUE)
{



selettivita=input(PIN_B4); // Segnale sensore selettivit�

livello_minimo=input(PIN_B5); // Segnale fotocellula livello minimo


carter=1; // Segnale micro passaggio copia

micro_passaggio_copie=input(PIN_B6); // Segnale micro passaggio copia
reset=input(PIN_B7); // Segnale reset

// Fronte sensore selettivit�
os_selettivita=0;
if (selettivita && !old_selettivita)
{
os_selettivita=1;
}
old_selettivita=selettivita;

// Segnale per elettrovalvola selettivit�

// Conteggio passi per livello minimo
if (os_selettivita && !livello_minimo)
{
conteggio_step_livello=conteggio_step_livello + 1;
}

// Anomalia per livello minimo raggiunto
if (conteggio_step_livello > step_ctrl_livello)
{
anomalia_livello_minimo=1;
}

// Reset step per controllo livello minimo
if (livello_minimo)
{
conteggio_step_livello=0;
}



// Segnale di ready stazione
if (carter && !(anomalia_livello_minimo))
{
ready_stazione=1;
}
else
{
ready_stazione=0;
}

// Reset delle anomalie
if (reset)
{
anomalia_manca=0;
anomalia_doppio=0;
anomalia_scarico=0;
anomalia_micro_passaggio_copie=0;
anomalia_livello_minimo=0;
}

if (ready_stazione)
{
output_high(PIN_C3);
}
else
{
output_low(PIN_C3);
}

delay_us(500);
//printf("Numero step per livello minimo %D \n\r",conteggio_step_livello);

}
}
Ttelmah
Guest







Re: ONE SHOT on button
PostPosted: Wed Dec 31, 2003 5:18 am     Reply with quote

ceccargi wrote:
this program must to count the button on RB4 and when the counter is 10 I must have anomalia_livello_minimo=1. The program it's OK but if I remove delay_us(500) or printf at the end of the program the code not working roperly.


Not really reding the code (since the comments are unreadable to me), I can see one thing that might cause a problem. You have to remember just how fast a processor is. Without the delay (or the print, which takes quite a long time), the code will loop in only a few uSec. You are testing for the pin being 'high', so with even the shortest of button pushes, it will count up allmost immediately.
You need to detect 'key changes', rather than keys. Hence the detection should have something like:
Code:

//declare this somewhere in main
int1 lastkey;

//Then

if (input(PIN_B4)) {
   if (lastkey) {
       //here a +ve edge has been seen on the key
       lastkey=false;
       //Have the rest of your keyhandler

   }
}
else lastkey=true;

//Rest of code here


The 'key detected' routine, will then only get executed, when 'lastkey' is true, and B4 is high. lastkey will be set to true, when B4 goes low, so the button press will only be seen once.
Even with this, if there is key 'bounce', lasting for more than the loop time of the main code, multiple edges can be seen for one contact. You might want to add a 'retest', like:

Code:

if (input(PIN_B4)) {
   delay_us(10);
   if (input(PIN_B4) {
       if (lastkey) {
           //here a +ve edge has been seen on the key
           lastkey=false;
           //Have the rest of your keyhandler
       }
   }
}
else lastkey=true;

//Rest of code here


With this, the key has to still be pressed after 10uSec, reducing the risk of spurious triggering.

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group