PDA

View Full Version : A/D problem on 18F2550 ?



Tissy
- 23rd December 2005, 12:15
Before i get flamed, i have read the data sheet many times, but its perhaps that i am not understanding it !

If i change the Swicth input to say PORTC.1, all works fine, on PORTA.1 it does not.

I am missing something in this code. I have been trying for a day and it is driving me mad !!



Clear
ADCON1 = %00001111
DEFINE OSC 20
DEFINE LOADER_USED 1

' ---------- [ I/O Definition ] ----------
TRISA = %00000011 ' Set PORTC (0-2 input) rest Outputs
TRISB = %10000001 ' Set PORTB (0)INPUT (1-5)OUTPUTS
TRISC = %01000111 ' Set PORTC (0-2 input) rest Outputs
PORTB = 0

Switch VAR PORTA.1 ' use a pin with Schmitt Trigger input
RedLED VAR PORTB.1
X VAR word
Presses VAR BYTE
LOOP VAR BYTE
confirm_flag var byte

Main:
if Switch = 1 THEN
WHILE Switch = 1 : WEND
COUNT Switch, 1500, Presses
BRANCHL Presses,[routine1, routine2, routine3]
GOTO MAIN
endif
GOTO Main

'--------------
routine0: ' button was not pressed again
' or pressed more than 3 times
GOTO Main

'--------------
routine1: ' pressed once
HIGH REDled
PAUSE 500
LOW REDled
GOSUB Confirmation
if confirm_flag = 1 then gosub flash
Goto main

'--------------
routine2: ' pressed twice
FOR X = 1 TO 2
HIGH REDled
PAUSE 500
LOW REDled
PAUSE 500
NEXT X
GOSUB Confirmation
if confirm_flag = 1 then gosub flash

GOTO Main

'--------------
routine3: ' pressed thrice
FOR X = 1 TO 3
HIGH REDled
PAUSE 500
LOW REDled
PAUSE 500
NEXT X
GOSUB Confirmation
if confirm_flag = 1 then gosub flash

GOTO Main

Flash:
For loop = 1 to 10
Redled = 1
Pause 50
Redled = 0
Pause 50
Next Loop
Return

Confirmation:
confirm_flag=0
x=0

WHILE (x<300) and (confirm_flag=0)

Pause 10 'check for button every 10ms
if Switch = 1 then confirm_flag = 1 ' if button pushed then set the flag
x=x+1 ' increment the pause counter
WEND

Return

Many thanks,

Steve

mister_e
- 24th December 2005, 09:17
Well the only pointer i can say

look for ADCON, ADCON0, ADCON1 and/or CMCON register. PORTA can be set as analog i/o by default. So you nbeed to set it to digital.

Tissy
- 24th December 2005, 11:28
I thought i have, the register that i thought was applicable was the ADCON1 which has been set to ADCON1 = %00001111.

Are there any others that i am missing.

I thought this was the only one, as if i set the code to:



Main:
if Switch = 1 THEN
gosub flash
endif
GOTO Main

Flash:
For loop = 1 to 10
Redled = 1
Pause 50
Redled = 0
Pause 50
Next Loop
Return

When you push the button, it does jump to the Flash routine. So i am guessing it is in digital mode.

I have a pull-down resistor on the input pin, which when the switch is pressed the input line goes to 5v.

I'll keep trying for a solution. Of course it could be my code up the spout !!

Steve

Darrel Taylor
- 24th December 2005, 21:40
Tissy,

Well, my original comment is still there, but I guess the thought behind it didn't make it through.

' use a pin with Schmitt Trigger input
<br>