|
|
View previous topic :: View next topic |
Author |
Message |
xinyh
Joined: 23 Mar 2004 Posts: 11
|
Problem of button programming |
Posted: Sun Apr 25, 2004 4:33 am |
|
|
Hi Dear All,
I try a button programming of PIC16F877, now the problem is when I switch on, A1 is in high already, what ever which button is pressed, it never change. My programming is below, anyone can help me? thanks very much!
// button.c
// To develop a matrix keyboard by using RB1, RB2 & RB4, RB5
// B4,B5 are in column, B1,B2 are in line
// Keyboard works in polling style, set B1=L, B2=H, then test B4, B5
// If B5=L,B4=H, or B5=H, B4=L,then the pressed button can be detected
// so B1, B2 are output, B4,B5 are input
#include "C:\pic16\button.h"
#define OUTB1 PIN_B1
#define OUTB2 PIN_B2
#define INPB4 PIN_B4
#define INPB5 PIN_B5
int j=0x00;
int KEYSERVE()
{
output_low(OUTB1);
if(!INPB4) j=0x01;
if(!INPB5) j=0x03;
output_low(OUTB2);
if(!INPB4) j=0x02;
if(!INPB5) j=0x04;
while(1)
{
if((INPB4)&&(INPB5))
break; // Wait key to be released
}
return(j);
}
int KEYSCAN()
{
while(1)
{
output_low(OUTB1);
if((!INPB4)||(!INPB5))
break;
} // Wait for button is pressed
delay_ms(10);
if((!INPB4)||(!INPB5))
KEYSERVE(); // If still there are button is pressed, call keyserve()
else j=0x00;
output_high(OUTB1);
return(j);
}
void main() {
while(1)
{
KEYSCAN();
if(j!=0x00)
{
switch (j){
case(0x01): output_high(PIN_A0);
break;
case(0x02): output_high(PIN_A1);
break;
case(0x03): output_high(PIN_A2);
break;
case(0x04): output_high(PIN_A3);
break;
default: break;
}
}
}
} |
|
|
xinyh
Joined: 23 Mar 2004 Posts: 11
|
|
Posted: Mon Apr 26, 2004 9:42 am |
|
|
Hi All,
Got it. Just use !input(PIN_B1) instead of (!PIN_B1). |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Mon Apr 26, 2004 10:20 am |
|
|
Hi xinyh,
If you enable the WARNING ERRORS in the compiler options, the compiler would generate an error file complaining:
Code: |
>>> Warning 203 "C:\PICC\key01.C" Line 81(1,1): Condition always TRUE
>>> Warning 204 "C:\PICC\key01.C" Line 82(1,1): Condition always FALSE
>>> Warning 203 "C:\PICC\key01.C" Line 85(1,1): Condition always TRUE
>>> Warning 204 "C:\PICC\key01.C" Line 86(1,1): Condition always FALSE
>>> Warning 203 "C:\PICC\key01.C" Line 89(1,1): Condition always TRUE
>>> Warning 203 "C:\PICC\key01.C" Line 91(1,1): Condition always TRUE
>>> Warning 203 "C:\PICC\key01.C" Line 102(1,1): Condition always TRUE
>>> Warning 203 "C:\PICC\key01.C" Line 105(1,1): Condition always TRUE
>>> Warning 203 "C:\PICC\key01.C" Line 110(1,1): Condition always TRUE
>>> Warning 203 "C:\PICC\key01.C" Line 122(1,1): Condition always TRUE
Memory usage: ROM=3% RAM=7% - 7%
0 Errors, 10 Warnings.
|
I just put some expresion in the rigth form:
Code: |
static int8 key_pressed;
#define OUTB1 PIN_B1
#define OUTB2 PIN_B2
#define INPB4 PIN_B4
#define INPB5 PIN_B5
int j=0x00;
int KEYSERVE()
{
output_low(OUTB1);
if(!input(INPB4)) j=0x01;
if(!input(INPB5)) j=0x03;
output_low(OUTB2);
if(!input(INPB4)) j=0x02;
if(!input(INPB5)) j=0x04;
key_pressed = FALSE;
do
{
if((input(INPB4))&&(input(INPB5)))
key_pressed = TRUE; // Wait key to be released
}while(!key_pressed);
return(j);
}
int KEYSCAN()
{
key_pressed = FALSE;
do
{
output_low(OUTB1);
if((!input(INPB4))||(!input(INPB5)))
{
key_pressed = TRUE; // Wait for button is pressed
delay_ms(10);
}
}while(!key_pressed);
if((!input(INPB4))||(!input(INPB5)))
KEYSERVE(); // If still there are button is pressed, call keyserve()
else j=0x00;
output_high(OUTB1);
return(j);
}
void main()
{
while(1)
{
KEYSCAN();
if(j!=0x00)
{
switch (j)
{
case(0x01): output_high(PIN_A0);
break;
case(0x02): output_high(PIN_A1);
break;
case(0x03): output_high(PIN_A2);
break;
case(0x04): output_high(PIN_A3);
break;
default: break;
}
}
}
}
|
And the actual error file:
Code: |
>>> Warning 203 "C:\PICC\key02.C" Line 114(1,1): Condition always TRUE
Memory usage: ROM=4% RAM=5% - 5%
0 Errors, 1 Warnings.
|
1 Warnings because the expresion: while(1)
Regards,
Humberto |
|
|
Guest
|
|
Posted: Wed Apr 28, 2004 12:04 am |
|
|
Hi Humberto,
Now it works very well. Thanks very much!
Btw, I can't find the ENABLE WARNING ERRORS in the C compiler, maybe the version is different!
Best Regards,
Xinyh |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Wed Apr 28, 2004 9:54 am |
|
|
Hi xinyh,
Open the CCS Help, in Overview, open Invoking the Compiler line compiler Options:
+EW = SHOW WARNING MESSAGES
Humberto |
|
|
Guest
|
|
Posted: Wed Apr 28, 2004 9:34 pm |
|
|
Hi Humberto,
It's interesting, but how to edit it? Appreciate to show me the detail, pls.
Best Regards,
Xinyh |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Thu Apr 29, 2004 9:22 am |
|
|
Quote: |
It's interesting, but how to edit it? Appreciate to show me the detail, pls.
|
Double click in CCSC.CHM
Itīs an HTML file.
Humberto
PD: Game is over |
|
|
|
|
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
|