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

Push buttons are not detected. Please help

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



Joined: 25 Jan 2012
Posts: 13

View user's profile Send private message

Push buttons are not detected. Please help
PostPosted: Wed Jan 25, 2012 10:52 am     Reply with quote

I have to design a clock using ds1302 for a project. My code is posted below. The problem is that I used 3 buttons from portb and the button push event is not detected. The code compiles with no errors.

This is my code:
Code:

#include <main.h>

#define alarm_morning_on_hr    10
#define alarm_morning_on_min   20
#define alarm_morning_off_hr   30
#define alarm_morning_off_min  40

#define alarm_evening_on_hr    50
#define alarm_evening_on_min   60
#define alarm_evening_off_hr   70
#define alarm_evening_off_min  80

#define LCD_ENABLE_PIN    PIN_A2
#define LCD_RS_PIN        PIN_A0
#define LCD_RW_PIN        PIN_A1
#define LCD_DATA4         PIN_D4
#define LCD_DATA5         PIN_D5
#define LCD_DATA6         PIN_D6
#define LCD_DATA7         PIN_D7
#include <lcd.c>


#define RTC_SCLK PIN_C3
#define RTC_IO   PIN_C5
#define RTC_RST  PIN_C2
#include <DS1302.C>

unsigned int key=0,state=0,loop=0,backlit;
unsigned int  hr,min,sec;
unsigned int day,mth,year,dow,hour,houra,mina;

#int_EXT
void  EXT_isr(void)
{

}

#int_TIMER1             //131 ms overflow
void  TIMER1_isr(void)
{

}


#int_TIMER2             //3.3 ms overflow, 50.1 ms interrupt
void  TIMER2_isr(unsigned int key)
{
output_toggle(PIN_D0);
if(input_state(PIN_B1)==0)key = 1;
if(input_state(PIN_B2)==0)key = 2;
if(input_state(PIN_B3)==0)key = 3;
}

void main()
{
PORT_B_PULLUPS(TRUE);
setup_adc_ports(NO_ANALOGS);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_2);      //131 ms overflow
setup_timer_2(T2_DIV_BY_16,208,15);         //3.3 ms overflow, 50.1 ms interrupt
enable_interrupts(INT_EXT);
enable_interrupts(INT_TIMER1);
enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);
rtc_init();
lcd_init();

while(TRUE){

   if(loop==0)state=0;
      
   if(state==0){
               rtc_get_time(hr,min,sec);
            if(key==1||key==2)state=1;
               printf(lcd_putc,"\fTIME\n%02d:%02d\n"hr,min);
             delay_ms(10);
   }
               
   if(state==1){
               delay_ms(1);
               rtc_get_time(hr,min,sec);
             printf(lcd_putc,"\fSET TIME\n%02d:%02d\n"hr,min);
             delay_ms(5);
             
               if(key==1){
            hr++;
            if(hr==24)hr=0;
            hour=hr;
             rtc_set_datetime(day,mth,year,dow,hour,min);
            key=0;loop=0;
            }

             if(key==2){
             min++;
            if(min==60)min=0;
             rtc_set_datetime(day,mth,year,dow,hour,min);
             key=0;loop=0;
             }

            loop++;
            if(loop==500)loop=0;   
   }

}

}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jan 25, 2012 12:23 pm     Reply with quote

You didn't tell us your PIC.
lasal



Joined: 25 Jan 2012
Posts: 13

View user's profile Send private message

PostPosted: Wed Jan 25, 2012 1:18 pm     Reply with quote

i used pic16f877
temtronic



Joined: 01 Jul 2010
Posts: 9243
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Jan 25, 2012 1:22 pm     Reply with quote

I'd uses the PORTB Interrupt capability of the PIC if I had the choice of pins.
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Wed Jan 25, 2012 3:35 pm     Reply with quote

Using the timer is a perfectly reasonable way of scanning the keys, but you'd probably be better to scan a bit faster, and have some debounce (so the key has to be low for two successive interrupts before being accepted).
The code is awfully badly indented, making the actual logic hard to follow.

There are quite a few issues though. If B3 ever goes low, it'll override whatever is on the other inputs (since it is the last input checked).
The scan as it stands has 'zero key rollover', with the latter keys always having priority over the earlier ones checked.
Then, what will happen if in the delays between triggering state=1, and entering the next part of the code, another key is detected?.
Then there is the loop speed. If a key is pressed, and loop is triggered, the register will be incremented at 50* per second (the interrupt rate). Much faster than useable...
Then there is a syntax error than may be causing problems. _Interrupt handlers do not, and cannot receive a variable in their definition_. You have the timer_2 handler defined as receiving a variable called 'key' It won't receive this, and having this definition will _override_ the global definition of key. Your code will then write to the non existent local definition, potentially overwriting memory, and losing data.

Best Wishes
lasal



Joined: 25 Jan 2012
Posts: 13

View user's profile Send private message

PostPosted: Wed Jan 25, 2012 5:20 pm     Reply with quote

thank you for your explanation
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jan 26, 2012 4:10 pm     Reply with quote

If you need better push-button code, here is the button.c file (and example
program) in the Code Library:
http://www.ccsinfo.com/forum/viewtopic.php?t=23837

Here is an improved method that does the button polling in a interrupt
routine every 10 ms, and also it puts the button presses into a circular
buffer:
http://www.ccsinfo.com/forum/viewtopic.php?t=39585&start=1
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