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 support@ccsinfo.com

Button noise suppression && software uart (if needed

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



Joined: 16 Sep 2009
Posts: 21

View user's profile Send private message

Button noise suppression && software uart (if needed
PostPosted: Sun Sep 27, 2009 10:44 am     Reply with quote

Hi again!

I've some questions to ask...

1. I'm using some push-buttons with 16F877a...But i need to suppress the noise of the button to get a clear input signal... There are some ways to do this (correct me if i'm wrong):

* You can send a delay(xxx) command to suppress it (easiest way, but whole program halts within the whole delay period)
ex : delay_ms(1000)

*If you want the program not to halt, you can use the delay function in a for - next loop which allows you to use interrupts, partially enabling the pic --- like
for i= 0 to 1000
delay_ms(1)
next i

*You can use a dummy pic, which serially sends the button inputs via a byte to the main pic and main pic use the byte as the main input. I generally prefer to use this..At least i used to do this...It was easy to send & receive data with BASIC compiler...I send the inputs from the dummy pic to main pic and run the main pic program according to the input byte...

Questions

*First of all, is there a way to suppress the button noise without completely stopping the program and without the need of the interrupts?

*If not, and/or i have to use interrupts, how can i do these in the safest and easiest way :

I need to get input from 1 analog (2 if -needed-) sensor/s and send commands to
*1 SPI-communicating chip (AD5206 digital potentiometer)
*1 RS232 communicating device
*1 LCD screen to notify the user what is going on with the system

with the 5 input pushbuttons.

*And if i dont want to use interrupts at all and use 1 port software UART (i want to use the HW UART to communicate with the rs232 device) to make communication between the main pic and the dummy pic which all the pushbuttons are connected, is there any safe & easy way to make a good communication which half of the data isn't crappy/ which you can receive the whole data without losing a bit? i've tried using KBHIT but i KNOW i should call it somewhat different... (in the help, it is written that i should call the KBHIT 10 times the bit rate...I'm using a 20Mhz crystal osc and using a 19200 baud serial UART which is i'm communicating nearly 100 times the bit rate...)

Here is the code :

Code:

#include <16f628A.h>

#Fuses HS,NOWDT,NOPROTECT,NOBROWNOUT,NOLVP,NOPUT,NOCPD
#use delay(clock=20000000)
#use rs232(baud=19200, xmit=pin_A1, rcv=pin_B0, parity=N, stop=1,FORCE_SW) 

#include <Flex_LCD420.c>
#include <stdlib.h>

#BYTE databyte = 0x20
#BIT start = databyte.0
#BIT menuup = databyte.1
#BIT menudown = databyte.2
#BIT confirm = databyte.3

int8 res;

void rxntx()

      if(kbhit()){
      res = getc();
         If (res==0xFF){         //When a 0xFF is received, send 0xFF with the input data (2 bytes)
             databyte=0;
             putc(0xFF);
             delay_us(1);
             start=input(pin_b4);
             menuup=input(pin_b1);
             menudown=input(pin_b2);
             confirm=input(pin_b3);
             putc(databyte);
        }else{
             putc('h');
             delay_us(1);
             putc('u');
             delay_us(1);
             putc('h');
             delay_us(1);
             putc('?');
             delay_us(1);            // When a 0xFF is not received as input, send "huh?"
             break;
         }
      }
}



void main()

{
   port_b_pullups(TRUE);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   enable_interrupts(global);

   while(1)
   {
      rxntx();
   }

}



I've tested this code with SIOW : It does send & receive data but it sometimes sends corrupted data or no data at all...


If you have another alternative for the button noise suppression while communicating with the devices i'm using with, please dont hesitate to tell...

Thanks in advance!!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Sep 27, 2009 10:55 am     Reply with quote

Here is the "Button Command" code that I wrote for the Code Library:
http://www.ccsinfo.com/forum/viewtopic.php?t=23837

Here's a version of it that polls the buttons during a timer interrupt:
http://www.ccsinfo.com/forum/viewtopic.php?t=31849

Here's a version that uses interrupts and has a button buffer:
http://www.ccsinfo.com/forum/viewtopic.php?t=39585&start=1
salgulsen



Joined: 16 Sep 2009
Posts: 21

View user's profile Send private message

PostPosted: Mon Sep 28, 2009 5:00 am     Reply with quote

Thanks for the help, PCM Programmer...I've tried the code, it works quite well...except i have a question about the auto-repeat function...I'm having a weird problem...

It is pretty ok when i use :

if(button(PIN_A1, 0, 50, 1, A1, 1)){
printf(lcd_putc,"A1");
}

This prints "A1" and waits for 500 ms and repeats writing A1 on LCD each 10 ms (the main delay is 10 ms)

But when i write :

if(button(PIN_A1, 0, 50, 1, A1, 1)){
printf(lcd_putc,"\nA1");
}

After it fills the LCD's first 2 characters of each line with A1, this somehow waits for 3000 ms's to auto-repeat the function... Here is my whole code :

Code:

#include <16f628A.h>

#Fuses HS,NOWDT,NOPROTECT,NOBROWNOUT,NOLVP,NOPUT,NOCPD
#use delay(clock=20000000)

#include <Flex_LCD420.c>
#include <stdlib.h>
#include <Button.c>

int8 A0 = 0;   // For the button on pin A0
int8 A1 = 0;   // For the button on pin A1



void main()

{
   port_b_pullups(TRUE);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   set_tris_a(0x0F);
   set_tris_b(0x80);
   
   lcd_init();
   delay_ms(100);
   
   while(1)
   {
      if(button(PIN_A0, 0, 50, 1, A0, 1)){   
         printf(lcd_putc,"A0");
      }
      if(button(PIN_A1, 0, 50, 1, A1, 1)){   
         printf(lcd_putc,"A1");
      }

   output_toggle(pin_b7);
   delay_ms(10);
   }

}


I assume it is something with the flex_lcd420 code but i've checked it, it has no delay in the lcd_gotoxy command...What can be the problem?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Sep 28, 2009 1:00 pm     Reply with quote

Quote:
if(button(PIN_A0, 0, 50, 1, A0, 1)){
printf(lcd_putc,"A0");
}
if(button(PIN_A1, 0, 50, 1, A1, 1)){
printf(lcd_putc,"A1");

First, change your auto-repeat value to a more reasonable number.
With a setting of '1', as above, your repeat rate is 100 times a second.
That's way too fast. Trying setting it to 20. This will give an auto-
repeat rate of 5 per second. That's small enough so you won't quickly
over-run the LCD line.
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