PDA

View Full Version : Problem getting button to turn on LED using 12F675



Jeff
- 2nd June 2011, 04:43
5623

I have a noob question. i have been playing with this all night. Im using Microcode 3005 and PBP 2.46. Chip is 12F675. I have checked the usual...blinked the LED's using blink program as a test, and tested the button. All i get is: LED 1 stays on. Schematic is similar to the one above 'cept RC osc, different chip of course.


CMCON = $1F
CMCON = %00000111

GPIO = %00000001 ' Set GPIO 1 high to turn on LED1
TRISIO = %00000010 ' set GPIO 4-1 to INputs, RA0 OUTput

Main:
'*** Test the switch state ***
if GPIO.0 = 0 then led2 'If switch is pressed then jump to LED2 routine

GPIO.1 = 1 ' Turn LED1 on
GPIO.2 = 0 ' Turn LED2 off
goto Main ' Jump to the top of the main loop

LED2:
'*** Turn LED2 on ***
GPIO.2 = 1 ' LED2 on
GPIO.1 = 0 ' LED1 off
goto Main ' Jump to the top of the main loopAny hints on how to troubleshoot this? I don't really want it re-written for me because i am trying to learn but i may change my mind later. thanks.

Jeff
- 2nd June 2011, 05:23
I found a mistake, cmcon was the wrong address

ADCON0 = $1F
ADCON0 = %00000000 'TURN OFF CONVERTER

CMCON = $19 'MAKE PINS DIGITAL
CMCON = %00000111

GPIO = %00000001 ' Set GPIO 1 high to turn on LED1
TRISIO = %00000110 ' set GPIO 1 & 2 to INputs, GPIO 0 OUTput

Main:
'*** Test the switch state ***
if GPIO.0 = 0 then led2 'If switch is pressed then jump to LED2 routine

GPIO.1 = 1 ' Turn LED1 on
GPIO.2 = 0 ' Turn LED2 off
goto Main ' Jump to the top of the main loop

LED2:
'*** Turn LED2 on ***
GPIO.2 = 1 ' LED2 on
GPIO.1 = 0 ' LED1 off
goto Main ' Jump to the top of the main loop

Jeff
- 2nd June 2011, 06:22
Still doesnt work though. BTW, how do i edit my post??

cncmachineguy
- 2nd June 2011, 09:21
Check the pins to make sure you have them set up as digital and not analog inputs.

mackrackit
- 2nd June 2011, 12:16
ANSEL turns the ADC on or off on the 12F675.

Use this to disable all ADC
ANSEL=%00000000
CMCON=7

Jeff
- 2nd June 2011, 20:03
Thanks guys, you were both right; rookie mistaek on my part. It works now - onward and upward. Now the button turns on LED2 and off LED1. SWEET!


ANSEL = $9F 'FIRST FOUR BITS MAKE IT DIGITAL INPUT
ANSEL = %00000000

ADCON0 = $1F
ADCON0 = %00000000 'TURN OFF CONVERTER

CMCON = $19 'MAKE PINS DIGITAL
CMCON = %00000111


GPIO = %00000010 ' Set GPIO 1 high to turn on LED1
TRISIO = %00000001 ' set GPIO 1 & 2 to INputs, GPIO 0 OUTput

Main:
'*** Test the switch state ***
if GPIO.0 = 0 then led2 'If switch is pressed then jump to LED2 routine

GPIO.1 = 1 ' Turn LED1 on
GPIO.2 = 0 ' Turn LED2 off
goto Main ' Jump to the top of the main loop

LED2:
'*** Turn LED2 on ***
GPIO.2 = 1 ' LED2 on
GPIO.1 = 0 ' LED1 off
goto Main ' Jump to the top of the main loop

Archangel
- 3rd June 2011, 02:51
Thanks guys, you were both right; rookie mistake on my part. It works now - onward and upward. Now the button turns on LED2 and off LED1. SWEET!
Welcome back Jeff,
3 years almost. I think what trips up people is this statement,
CMCON = $?? 'MAKE PINS DIGITAL
Which seems to reappear on a regular basis, perhaps copy paste, it should more factually report,
CMCON = $?? 'Disable input Comparators
The ANSEL is the 'MAKE PINS DIGITAL part of the equation, as you found out, My post here is for the future newbies who read this post.

Jeff
- 3rd June 2011, 06:05
Welcome back Jeff,
3 years almost. I think what trips up people is this statement,
CMCON = $?? 'MAKE PINS DIGITAL
Which seems to reappear on a regular basis, perhaps copy paste, it should more factually report,
CMCON = $?? 'Disable input Comparators
The ANSEL is the 'MAKE PINS DIGITAL part of the equation, as you found out, My post here is for the future newbies who read this post.

3 years...yes, i probably gave up for 3 years because of this. Also, i was using picbasic, not pro. Pro is SOOOOO much easier. it is worth the lute. I was struggling with the stupid button command for a couple weeks and gave up. Then i read Chuck Hellebuyck's book; "Programming PI uC's with picbasic" and he mentioned a workaround by directly addressing memory. I am finally starting to 'get it'. Thanks.

Archangel
- 4th June 2011, 08:14
Glad to see you back, You are right, PBP IS worth the extra money.
I think the Button command is overly complicated vs 2 ifs, but maybe it saves codespace? Anyway keep on coding, the forum is here when you need it.