Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

problem with buttons

Status
Not open for further replies.

winsthon

New Member
Hello everyone I have a problem with buttons. Im new to PIC BASIC pro. You see I have this program which supposedly will take the analog input and convert into digital in binary form one at a time. Sample RA0 and RA1. I want to take the value of RA0 first by pressing the button attach on RC3 and when I want to take the value of RA1 using the button attach on RC1, I cant get them both, the buttons seems do not work. Soon I will use three buttons to add RA3 as input, can anyone help me with this.

heres my code:

INCLUDE "BS1DEFS.BAS "
Define OSC 4
Define ADC_CLOCK 3 'default value is 3
DEFINE ADC_BITS 8
Define ADC_SAMPLEUS 10 'default value is 20
Define LCD_BITS 8 'allowed values are 4 and 8 - the number of data interface lines
Define LCD_DREG PORTB
Define LCD_DBIT 0 '0 or 4 for 4-bit interface, ignored for 8-bit interface
Define LCD_RSREG PORTD 'RS port
Define LCD_RSBIT 1 'RS pin
Define LCD_EREG PORTD 'En port
Define LCD_EBIT 3 'En pin
Define LCD_RWREG PORTD 'R/W port
Define LCD_RWBIT 2 'R/W pin
DEFInE LCD_LINES 4 'LCD lines
DEFINE BUTTON_PAUSE 50 'button debounce delay is 50ms

an0 var word 'set variable an0 as word
an1 var word 'set variable an1 as word
btn1 var PORTC.3 'set RC3 to select RA0 as analog input
btn2 var PORTC.4 'set RC4 to select RA1 as analog input
ledrc0 var PORTC.0 'set ledrc1 as RC0 indicates RA0 as input
ledrc1 var PORTC.1 'set ledrc2 as RC1 indicates RA1 as input

TRISA = %11111111 ' Set PORTA to all input
ADCON1 = 4 ' Set RA0, RA1 & RA3 as analog input

goto loop 'skip subroutine

ra0: 'ra0 subroutine
high ledrc0 = 1
low ledrc1 = 0
Adcin 0, an0
Lcdout $fe, $01 ' Clears your LCD, sets cursor to Line 1 Column 1
Lcdout $fe, $c0 ' Cursor to Line 2 Column 1
Lcdout $fe, $14 ' move cursor 1 space
Lcdout $fe, $14 ' move cursor 1 space
Lcdout $fe, $14 ' move cursor 1 space
Lcdout $fe, $14, " ", "RA0", " ", "VALUE:" ' Formatted text for line 2
Lcdout $fe, $14 ' move cursor 1 space
Lcdout $fe, $94 ' cursor to line 3
Lcdout $fe, $14, BIN8 an0 ' Formatted text for line 3
pause 1000
return

ra1: 'ra1 subroutine
high ledrc1
low ledrc0
ADCIN 1, an1
Lcdout $fe, $01 ' Clears your LCD, sets cursor to Line 1 Column 1
Lcdout $fe, $c0 ' Cursor to Line 2 Column 1
Lcdout $fe, $14 ' move cursor 1 space
Lcdout $fe, $14 ' move cursor 1 space
Lcdout $fe, $14 ' move cursor 1 space
Lcdout $fe, $14, " ", "RA1", " ", "VALUE:" ' Formatted text for line 2
Lcdout $fe, $14 ' move cursor 1 space
Lcdout $fe, $94 ' cursor to line 3
Lcdout $fe, $14, BIN8 an1 ' Formatted text for line 3
pause 1000
return

loop: 'main

if btn1 = 1 then
Gosub ra0
endif
if btn2 = 1 then
gosub ra1
endif

Goto loop 'loop forever
END
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top