PDA

View Full Version : Need help with 16F676 button press



dw_picbasic
- 1st January 2021, 21:23
Hello all,
I've been playing with a clock program.
The program will display 24hour on line 1 of an 8x2 LCD
And display 12 hour time on line 2

This is working fine.
I now want to add a button which will stall the increment of seconds.
But for some reason the chip is not responding to the button press.
Pin 13 RA0 is tied to ground via 10k resistor and pulled to 5+ by the button.
But the chip is not detecting the button.

There is an IF statement that looks for 5+ at PORTA.0 pin 13 which calls a subroutine
I've verified this works by temporarily changing it to look for low state rather than high state.

And I've verified with a DVM the pin change from 0 to 5+
And I tempo

What am I missing?

Thanks

------------------------------------------------------------
' 16F676 8x2 Clock Program
' Clock Displayed on Handtronix LCD
' Oscillator = Internal
' LCD 4-bit mode.

#CONFIG
__config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF & _CP_OFF
#ENDCONFIG

' Defines necessary for PBP3's built-in LCD functions
DEFINE LCD_DREG PORTC ' Set LCD Data port
DEFINE LCD_DBIT 0 ' Set starting Data bit (0 or 4) if 4-bit bus
DEFINE LCD_RSREG PORTC ' Set LCD Register Select port
DEFINE LCD_RSBIT 4 ' Set LCD Register Select bit
DEFINE LCD_EREG PORTA ' Set LCD Enable port
DEFINE LCD_EBIT 5 ' Set LCD Enable bit - RA5 pin 2
DEFINE LCD_BITS 4 ' Set LCD 4-bit mode
DEFINE LCD_LINES 2 ' Set LCD lines
DEFINE LCD_COMMANDUS 1500 ' Set command delay time in us
DEFINE LCD_DATAUS 44 ' Set data delay time in us

ANSEL = 0 ' Disable Analog
TRISC = 0 ' PortC all output
TRISA = %00000111 ' RA0,RA1,RA2 (pins 13,12,11) inputs for buttons
' RA3,RA4,RA5 (pins 4,3,2) output

mhr var byte 'Military hour (24hr)
lhr var byte 'Local hour (12hr)
mn var byte 'minute
sc var byte 'second

mhr = 18
lhr = 0
mn = 16
sc = 0

clock_pulse var word
clock_pulse = 1005

Pause 1000 ' Wait for LCD to startup
Lcdout $fe, 1 ' Clear LCD screen
pause 10
lcdout $fe, $0C ' Turn cursor off

mainloop:
lcdout $FE, $80 'Cursor at First Line
lcdout " " 'clear line 1
lcdout $FE, $80 'Cursor at First Line

' FIRST LINE OF CLOCK
if mhr < 10 then lcdout "0" 'To insure hour is double-digit number
lcdout dec mhr,":"

if mn < 10 then lcdout "0"
lcdout dec mn, ":"

if sc < 10 then lcdout "0"

lcdout dec sc
pause clock_pulse

'SECOND LINE OF CLOCK
lcdout $FE, $C0 'Cursor at 2nd line
Lcdout " " ' Clear 2nd line
lcdout $FE, $C0 'Cursor at 2nd line

if mhr > 12 then
lhr = mhr - 12
elseif mhr = 0 then
lhr = 12
else
lhr = mhr
endif

if lhr < 10 then lcdout "0"
lcdout dec lhr, ":"
if mn < 10 then lcdout "0"
lcdout dec mn, " "
if mhr < 13 then
lcdout "AM"
else
lcdout "PM"
endif


'LOOK FOR BUTTON PRESS TO STALL INCREMENT TIME
if PORTA.0 = 1 Then second_pause 'Look for button press and stall clock


'INCREMENT TIME
sc = sc + 1
if sc > 59 then
sc = 0
mn = mn + 1
endif
if mn > 59 then
mn = 0
mhr = mhr + 1
endif
if mhr > 23 then
mhr = 0
endif

Goto mainloop ' Update Display

second_pause:
While PORTA.0 = 1
wend

Goto mainloop 'Update Display

End ' END OF PROGRAM

dw_picbasic
- 1st January 2021, 23:11
I found it!
I changed

if PORTA.0 = 1 Then second_pause

to

if PORTA.0 == 1 Then second_pause

Its now working correctly

HenrikOlsson
- 1st January 2021, 23:59
I have a hard time believing that was the actual problem. In PBP (unlike, for example, C) = and == are the same thing. And you basically have the proof in your own code sine the WHILE loop works and you have only one = in that comparison.

second_pause:
While PORTA.0 = 1 ' <--- See, you don't need ==
wend

I would try changing it back and see if it really returns to a non working state.

/Henrik.

dw_picbasic
- 2nd January 2021, 00:34
I have a hard time believing that was the actual problem. In PBP (unlike, for example, C) = and == are the same thing. And you basically have the proof in your own code sine the WHILE loop works and you have only one = in that comparison.

second_pause:
While PORTA.0 = 1 ' <--- See, you don't need ==
wend

I would try changing it back and see if it really returns to a non working state.

/Henrik.


Hi Hentik,
I added the second = to the While loop when I updated the IF statement.
I can definitely change the IF statement back to a single = and see if it goes back to not working - just for a validation test.

Prior to making the change in the IF statement - I also removed the bias resistors and set PortA.0, 1, & 2 with internal pull-ups - and setup the buttons so that they pull to ground.

I'll change the IF statement and let you know

dw_picbasic
- 2nd January 2021, 00:48
Well - you were right.
I changed it back to a single = and it still works.
So I can only guess.
The only other change I made was to add the internal pull-ups and switch the buttons to press pins to ground.
And I did that before changing the IF statement and it still didn't work until after I changed the IF statement.
I added two more buttons - one to manually increment hour and one to manually increment minute.
But in any case - its working - which is a good thing! :-]

flotulopex
- 2nd January 2021, 15:55
Hi dw_picbasic,

I'm an electrician and when I need a lamp to light on, I turn on (a kind of "1" level) the button that will provide energy to the lamp.

In electronic, it is different. As I learned over time, a good practice is to use "inverted-logic" for digital inputs.

I see, you pull-down your input pin so you wait for a high level (or "1" or "active high") to trigger an event. I might suggest you to "invert" your way of thinking and trigger your event(s) when the button goes to a "low" level (or "0" or "active low").

Waiting for a "low level" means that you need to keep the input always "high" like in this shema.

8997

Your PIC has alread built-in "weak pull-ups" (find this in your datasheet) like R1 so you don't need to add anything to your button. Use them in place of your external resistor(s) :wink:

This way of doing has some advantages and one of them is a good protection against noise.

8994