PDA

View Full Version : Code check -- button not working



docwisdom
- 13th February 2006, 02:34
I have a number of buttons in my program, all are working except for the select button (SD). I have tried hooking the button to a different port with the same problem, I have swapped the button with others and the button itself works. So I am assuming its in the code somewhere.

Im sure there are inefficiencies in the code, let me know if there are really bad ones.

thanks

p.s. I know I havent coded in the 2nd and 3rd hpwm lines yet, I only have 1 color LED hooked up to the board so far.




TRISB = %00000000 'portb output
ADCON1 = %11111111 'all ports digital

define OSC 4 '4mhz oscillator


DEFINE LCD_DREG PORTB ' Set LCD Data port
DEFINE LCD_DBIT 0 ' Set starting Data bit (0)


DEFINE LCD_RSREG PORTB ' Set LCD Register Select port
DEFINE LCD_RSBIT 4 ' Set LCD Register Select bit


DEFINE LCD_EREG PORTB ' Set LCD Enable port
DEFINE LCD_EBIT 6 ' Set LCD Enable bit


DEFINE LCD_BITS 4 ' Set LCD bus size (4 or 8 bits)
DEFINE LCD_LINES 2 ' Set number of lines on LCD
DEFINE LCD_COMMANDUS 2000 ' Set command delay time in us
DEFINE LCD_DATAUS 50 ' Set data delay time in us

DEFINE CCP1_REG PORTC 'Hpwm 1 pin port
DEFINE CCP1_BIT 2 'Hpwm 1 pin bit
DEFINE CCP2_REG PORTC 'Hpwm 1 pin port
DEFINE CCP2_BIT 1 'Hpwm 1 pin bit

B0 Var Byte 'up button
B1 var byte 'down button
B2 var byte 'select button
B3 var byte 'daylight button
B4 Var byte 'tungsten
B9 var byte 'lock button
B10 var byte 'power button

rval var byte 'red
gval var byte 'green
bval var byte 'blue

rval = 0 'red at 0
gval = 0 'green at 0
bval = 0 'blue at 0
B0 = 0 'zero button up
B1 = 0 'zero button down
B2 = 0 'zero select button
B3 = 0 'zero daylight button
B4 = 0 'zero tungsten button
B9 = 0 'zero lock button
B10 = 0 'zero power button

while portc.6 = 0 'power button
wend

main:
high portc.4 'power indicator
pause 500 '.5 second delay
LCDOUT $FE, 1, "LEADS ", $FE, $C0, "by MSL" 'tag line
pause 1000 '1 second delay
home:
lcdout $fe, 1, " R", DEC3 rval, " G", $FE, $C0, DEC3 gval, " B", DEC3 bval
hpwm 1, rval, 5000 'set dim level

sd:
button portb.7,1,255,0,B2,0,bru 'detect select button
pause 20
goto adjr
bru:
BUTTON porta.0,1,100,1,B0,0,brd 'detect + button
pause 20
goto brup
brd:
button porta.1,1,100,1,B1,0,ld 'detect - button
pause 20
goto brdown
ld:
button portc.7,1,255,0,B9,0,pd 'detect lock button
pause 20
goto lock
pd:
button portc.6,1,255,0,B10,0,dd 'detect power button
pause 20
goto power
dd:
button portc.5,1,255,0,B3,0,td 'detect daylight button
pause 20
goto daylight
td:
button portc.4,1,255,0,B4,0,bru 'detect tungsten button
pause 20
goto tungsten

brup: 'brightness up
rval = rval + 1
gval = gval + 1
bval = bval + 1
hpwm 1, rval, 5000
pause 100
goto home
brdown: 'brightness down
rval = rval - 1
gval = gval - 1
bval = bval - 1
hpwm 1, rval, 5000
pause 100
goto home

adjr: 'adjust red level
lcdout $fe, 1, "Red", $FE, $C0, DEC3 rval 'display red level
ru: 'check up button
BUTTON porta.0,1,100,1,B0,0,rd ' Check button
pause 20
goto rup
rd: 'check up button
button porta.1,1,100,1,B1,0,sd2
pause 20
goto rdown
sd2: 'check for select button
button portb.7,1,100,1,B2,0,ru
pause 20
goto adjg 'goto adjust green
rup: 'red up
rval = rval + 1
hpwm 1, rval, 5000
pause 100
goto adjr
rdown: 'red down
rval = rval - 1
hpwm 1, rval, 5000
pause 100
goto adjr

adjg: 'adjust green
lcdout $fe, 1, "Green", $FE, $C0, DEC3 gval 'display green level
gu: 'check up button
BUTTON porta.0,1,100,1,B0,0,gd ' Check button
pause 20
goto gup
gd: 'check down button
button porta.1,1,100,1,B1,0,sd3
pause 20
goto gdown
sd3: 'check select button
button portb.7,1,100,1,B2,0,gu
pause 20
goto adjb 'goto adjust blue
gup: 'green up
gval = gval + 1
hpwm 2, gval, 5000
pause 100
goto adjg
gdown: 'green down
gval = gval - 1
hpwm 2, gval, 5000
pause 100
goto adjg

adjb: 'adjust blue
lcdout $fe, 1, "Blue", $FE, $C0, DEC3 bval 'display green level
bu: 'check up button
BUTTON porta.0,1,100,1,B0,0,bd ' Check button
pause 20
goto bup
bd: 'check down button
button porta.1,1,100,1,B1,0,sd4
pause 20
goto bdown
sd4: 'check select button
button portb.7,1,100,1,B2,0,bu
pause 20
goto home 'goto home
bup: 'blue up
bval = bval + 1
hpwm 1, bval, 5000
pause 100
goto adjb
bdown: 'blue down
bval = bval - 1
hpwm 1, bval, 5000
pause 100
goto adjb

lock: 'lock the keys
high portc.3 'lock indicator
pause 1000
while portc.7 = 0 'wait for unlock
wend
low portc.3 'lock indicator
goto home

power: 'power down display and LEDs
low portc.4 'take down power indicator
hpwm 1, 0, 5000 'take down red channel
lcdout $fe, 1 'clear display
pause 1000
while portc.6 = 0 'wait for power on
wend
high portc.4 'put up power indicator
goto home

daylight: 'set color levels to approx 5600k
rval = 255
gval = 255
bval = 252
goto home


tungsten: 'set color levesl to approx 3200k
rval = 255
gval = 241
bval = 224
goto home

Darrel Taylor
- 14th February 2006, 05:07
What's Up DOC?

The SD button is on portb.7, but at the very top TRISB is set to all outputs.

The button command never sees the pin.

HTH,
  Darrel

RRacer
- 14th February 2006, 15:12
Isn't the Button command supposed to take care of TRIS settings?

The manual says "Pin is automatically made an input."


http://www.melabs.com/resources/pbpmanual/5_4-5_6.htm#56

docwisdom
- 14th February 2006, 17:12
Ill give it a shot. It sounds logical. It may be overriding the button commands ability to make it an input.

Darrel Taylor
- 15th February 2006, 02:07
Yes, someone keeping me in check. Good.

You're right RRacer, it does say that.

And now I'm back to gee, I don't know. Unless ...

Since there isn't a LOADER_USED define, can I assume you are using ICSP?   And, is the programmer always connected?   Is the programmer conflicting with the RB7 input? Just another thought.
<br>
Darrel

P.S. What chip are you using? 16F737? The ADCON1 seems odd. That won't affect PORTB.7, I'm just trying to figure out what chip it is.

docwisdom
- 16th February 2006, 16:06
Yes, I am using ICSP. I only have a pin header connected, I disconnect the programmer after each burn.
Like I said, I tried the same button on a pin on the porta side and still the same issue. The button registers voltage when pressed, so its not a hardware fault.

check out the photos of the LED up and in action! Damn, Lamina makes some really bright stuff.

http://share.shutterfly.com/action/welcome?sid=2CcN3DhozYuOg

docwisdom
- 1st March 2006, 03:07
I am back, trying to get this problem solved again. Anyone have any new ideas on why this single button wont work?

sougata
- 1st March 2006, 07:24
Hi,

I would like to see the schematic and know which PIC you are using.

Regards

Sougata

docwisdom
- 1st March 2006, 19:39
I am using the 16F767. The select button (the one not working) is wired just as all the other buttons are, and they all work. So I am pretty confident the hardware is correct. As I stated before, I have tried a different pin, so I dont think that is the problem. I have also taken a volt meter to the output of the button and I get a 4.97 volt reading, so the button itself works.


-brian

sougata
- 1st March 2006, 23:13
Hi docwisdom,

A quick glance at your code looked like everthing is okay by the algo. However the algo can be cut down pretty much. So my intensions were to duplicate your circuit since simulation works.

The more peculiar a problem is the more I get interested.

Hope somebody else solves the problem.

Regards

Sougata

docwisdom
- 2nd March 2006, 00:17
sorry sougata, no full diagram has been made yet. My buttons are wired just as shown in the manual. Vin to button, button out to pin of PIC, and also pulled down to ground with a resistor.

when the button is pressed i get voltage, just dont know why the pins not reading high.

Bruce
- 2nd March 2006, 00:46
I wonder is this has anything to do with RB6 & RB7 being dedicated to
in-circuit DEBUG is this bit is clear in config word?

docwisdom
- 2nd March 2006, 22:43
I have an interesting solution. I retyped it.
Maybe there was a glitch in MCS or something, but I retyped it, just as before and it works great now. Beats the heck out of me.