The Godfather talking
Share your stuff or I will make you regret it.
Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
April 18, 2024, 09:22:39 09:22


Login with username, password and session length


Pages: [1]
Print
Author Topic: (help) push button not working  (Read 5723 times)
0 Members and 1 Guest are viewing this topic.
yugal
Junior Member
**
 Warned
Online Online

Posts: 60

Thank You
-Given: 919
-Receive: 73


« on: April 21, 2013, 07:20:03 19:20 »

hi
am new to micro controller programming
this is my first code but pbup pbdn not working . please help to correct this

'* Notes : Ic pic16F676 COMPILIER PBP2.60
'************************************************* **************
@ DEVICE pic16F676
@ DEVICE pic16F676, WDT_OFF
@ DEVICE pic16F676, PWRT_ON
@ DEVICE pic16F676, PROTECT_ON
@ DEVICE pic16F676, MCLR_OFF
@ DEVICE pic16F676, INTRC_OSC_NOCLKOUT
'***********************************************
TRISA = %011000
TRISC = %000000
SEQ VAR BYTE
'***********************************************
LED1 VAR PORTC.0
LED2 VAR PORTC.1
LED3 VAR PORTC.2
LED4 VAR PORTC.3
LED5 VAR PORTC.4
LED6 VAR PORTC.5
LED7 VAR PORTA.0
LED8 VAR PORTA.1
LED9 VAR PORTA.2
LED10 VAR PORTA.5

PBUP VAR PORTA.3
PBDN VAR PORTA.4

SEQ = 4
PORTC = 0
PORTA = 0
CMCON = 7
ANSEL = 0
'***************************************
LOOP:
IF PBUP = 0 THEN UPP
IF PBDN = 0 THEN DNN
PAUSE 50
GOSUB ACTIONS:
GOTO LOOP
'**************************
UPP:
IF SEQ = 1 THEN RRT
SEQ = SEQ + 1
PAUSE 50
GOTO RRT
DNN:
IF SEQ = 7 THEN RRT
SEQ = SEQ - 1
PAUSE 50
RRT : RETURN

'*******************************************
ACTIONS:
IF SEQ = 1 THEN
LED1 = 1 :LED2 = 0 :LED3 = 0 :LED4 = 0 :LED5 = 0 :LED6 = 0 :
LED7 = 0 :LED8 = 0 :LED9 = 1 :LED10 = 1 :
ENDIF
IF SEQ = 2 THEN
LED1 = 0 :LED2 = 1 :LED3 = 0 :LED4 = 0 :LED5 = 0 :LED6 = 0 :
LED7 = 0 :LED8 = 1 :LED9 = 0 :LED10 = 1 :
ENDIF
IF SEQ = 3 THEN
LED1 = 0 :LED2 = 0 :LED3 = 1 :LED4 = 0 :LED5 = 0 :LED6 = 0 :
LED7 = 0 :LED8 = 1 :LED9 = 1 :LED10 = 0 :
ENDIF
IF SEQ = 4 THEN
LED1 = 0 :LED2 = 0 :LED3 = 0 :LED4 = 1 :LED5 = 0 :LED6 = 0 :
LED7 = 0 :LED8 = 0 :LED9 = 1 :LED10 = 1 :
ENDIF
IF SEQ = 5 THEN
LED1 = 0 :LED2 = 0 :LED3 = 0 :LED4 = 0 :LED5 = 1 :LED6 = 0 :
LED7 = 0 :LED8 = 0 :LED9 = 1 :LED10 = 1 :
ENDIF
IF SEQ = 6 THEN
LED1 = 0: LED2 = 0:LED3 = 0 :LED4 = 0 :LED5 = 0 :LED6 = 1 :
LED7 = 0 :LED8 = 1 :LED9 = 1 :LED10 = 0 :
ENDIF
IF SEQ = 7 THEN
LED1 = 0 :LED2 = 0 :LED3 = 0 :LED4 = 0 :LED5 = 0 :LED6 = 0 :
LED7 = 1 :LED8 = 0 :LED9 = 1 :LED10 = 1 :
ENDIF
PAUSE 50
RETURN
'************************************************* **************
 please correct
                  yugal
Logged
Catcatcat
Senior Member
****
Offline Offline

Posts: 417

Thank You
-Given: 275
-Receive: 1531



WWW
« Reply #1 on: April 21, 2013, 07:45:08 19:45 »

Here are some examples of work with buttons connected directly to the port. http://catcatcat.d-lan.dp.ua/en/skachat/primeryi-postroeniya-koda-programm-dlya-pic-kontrollerov/tsifrovoy-vvod-dannyih/ in an attachment to the project site.
Logged
solutions
Hero Member
*****
Offline Offline

Posts: 1823

Thank You
-Given: 655
-Receive: 900



« Reply #2 on: April 21, 2013, 08:24:14 20:24 »

I'm not a code jockey, but it looks to me like you are reading a different port A bit for up vs down....

PBUP VAR PORTA.3
PBDN VAR PORTA.4

If you indeed have two buttons, then it's fine. But shouldn't these be changed from

IF PBUP = 0 THEN UPP
IF PBDN = 0 THEN DNN

to

IF PBUP = 0 THEN GOSUB UPP
IF PBDN = 0 THEN GOSUB DNN
Logged
Parmin
Hero Member
*****
Offline Offline

Posts: 582

Thank You
-Given: 494
-Receive: 133


Very Wise (and grouchy) Old Man


« Reply #3 on: April 22, 2013, 05:15:57 05:15 »

Did you put a couple of pullup resistors on the buttons?

I believe PORTA does not have internal pullups.
Logged

If I have said something that offends you, please let me know, so I can say it again later.
robotai
Junior Member
**
Offline Offline

Posts: 60

Thank You
-Given: 27
-Receive: 23


« Reply #4 on: April 22, 2013, 09:08:27 09:08 »

I supposed you have two buttons, one for up action and another for down action.

The code here seems not correct because it just "Jump", not calling routine.
Code:
LOOP:
IF PBUP = 0 THEN UPP
IF PBDN = 0 THEN DNN
PAUSE 50
GOSUB ACTIONS:
GOTO LOOP

I supposed what you want may be this.
Code:
LOOP:
IF PBUP = 0 THEN GOSUB UPP
IF PBDN = 0 THEN GOSUB DNN
PAUSE 50
GOSUB ACTIONS:
GOTO LOOP
'**************************
UPP:
IF SEQ >= 7 THEN RRT
SEQ = SEQ + 1
PAUSE 50
GOTO RRT
DNN:
IF SEQ <= 1 THEN RRT
SEQ = SEQ - 1
PAUSE 50
RRT : RETURN
1. Without the "GOSUB", your code won't return properly.
2. Seems you are doing actions based on SEQ from 1 to 7, the original "UPP" and "DNN" actions may be wrong.

Do you confirm your code writing into chip and running?
« Last Edit: April 22, 2013, 09:21:48 09:21 by robotai » Logged
gan_canny
Junior Member
**
Offline Offline

Posts: 89

Thank You
-Given: 101
-Receive: 26


« Reply #5 on: April 22, 2013, 12:32:08 12:32 »

Well nearly all buttons ( switches) are mechanical so they will bounce. Code must be crafted to allow for the bounce. The bounce in momentary but the CPU can read an input fast enough to experience bouncing. Often a delay is introduced after the input first sees the button pressed and a reread after the delay that spans the bounce to confirm it is truly pressed. Proteus simulations since they are no more than an artists sketch of the real world will often have ideal buttons and switches that don't oscillate ( bounce) before settling into the final state. Since you are utilizing both states of the button (up and down) a bounce during a down will be seen as an up etc.
« Last Edit: April 22, 2013, 12:34:40 12:34 by gan_canny » Logged
solutions
Hero Member
*****
Offline Offline

Posts: 1823

Thank You
-Given: 655
-Receive: 900



« Reply #6 on: April 22, 2013, 03:56:34 15:56 »

gan_canny: the delay is there, inside the switch state scanning loop, for debounce:

LOOP:
IF PBUP = 0 THEN UPP
IF PBDN = 0 THEN DNN
PAUSE 50
GOSUB ACTIONS:
GOTO LOOP

While he may need to tune the value of 50, the code is fine there.
Logged
yugal
Junior Member
**
 Warned
Online Online

Posts: 60

Thank You
-Given: 919
-Receive: 73


« Reply #7 on: April 22, 2013, 05:46:53 17:46 »

Hi, now  real hardware push button is active. this is my mistake , i am not using gosub  command.
 many thanks to solutions,Parmin,robotai,catcatcat and gen_canny for help.permin yes i am using pullup resistors on the buttons
                                                                      yugal
Logged
Checksum8
Active Member
***
Offline Offline

Posts: 129

Thank You
-Given: 123
-Receive: 100


« Reply #8 on: April 25, 2013, 04:11:28 16:11 »

Try using the "Button" command. It debounces the switch and allows for other options
See their web page for the online manual.
Code:

BUTTON Pin, Down, Delay, Rate, Var, Action, Label

Performs debounce and auto-repeat on Pin:

Pin  Pin number (0..7).
 
Down  State of pin when button is pressed (0..1).
 
Delay  Cycle count before auto-repeat starts (0..255). If 0, no debounce or auto-repeat is performed. If 255, debounce, but no auto-repeat, is performed.
 
Rate  Auto-repeat rate (0..255).
 
Var    Byte variable used for delay/repeat countdown. Should be initialized to 0 prior to use.
 
Action  State of button to perform GOTO (0 if not pressed, 1 if pressed).
 
Label  Execution resumes at this label if Action is true.


Logged
hondadave
Inactive

Offline Offline

Posts: 1

Thank You
-Given: 11
-Receive: 0


« Reply #9 on: May 25, 2013, 03:10:53 15:10 »

Just installed mine works perfect many thanks!
Logged
Pages: [1]
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright © 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC