|
|
View previous topic :: View next topic |
Author |
Message |
lasal
Joined: 25 Jan 2012 Posts: 13
|
Push buttons are not detected. Please help |
Posted: Wed Jan 25, 2012 10:52 am |
|
|
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
|
|
Posted: Wed Jan 25, 2012 12:23 pm |
|
|
You didn't tell us your PIC. |
|
|
lasal
Joined: 25 Jan 2012 Posts: 13
|
|
Posted: Wed Jan 25, 2012 1:18 pm |
|
|
i used pic16f877 |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9221 Location: Greensville,Ontario
|
|
Posted: Wed Jan 25, 2012 1:22 pm |
|
|
I'd uses the PORTB Interrupt capability of the PIC if I had the choice of pins. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19498
|
|
Posted: Wed Jan 25, 2012 3:35 pm |
|
|
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
|
|
Posted: Wed Jan 25, 2012 5:20 pm |
|
|
thank you for your explanation |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
|
|
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
|