PDA

View Full Version : using a pot to control a time range



glkosec
- 17th June 2008, 18:33
I am relativley new to programming and am trying to make a simple timing circuit with a PIC12F675. I have written some code that uses a potentiometer and the AD converter to scroll through the time range based on the conversion.

The only problem is i cannot control the range that the time is for(at least i couldn't figure the math out to do so).

Is there anyone that thinks they might be able to help. I will post the code below. I appreciate any and all suggestions.

MAIN:
A1 = %00000000
GOSUB READ_A_D_AN0
LOAD_OUT = 1 ' ON FIRST TRIGGERABLE
PAUSE 100
BUTTON MAIN_TRIGGER, 1, 255, 0, a1, 0, RUN
GOTO MAIN



TIME:
FOR DELAY = 0 TO COUNTER STEP 1
PAUSE 10
GOSUB READ_A_D_AN0
NEXT DELAY
RETURN



RUN:
PAUSE 10
LOAD_OUT = 0
GOSUB TIME
PAUSE 10
GOTO MAIN






READ_A_D_AN0: 'used for Time Delay 1
PAUSEUS 50 ' wait 50 microsec
ADCIN 0, AD_AN0_VALUE ' read channel 0 to AD_AN0_VALUE
COUNTER = (AD_AN0_VALUE */ 1000) >> 2

RETURN

skimask
- 17th June 2008, 18:50
Is that the whole program that you have coded?
'cause I don't see ANY of the ADC DEFINEs as shown in the PBP manual there, not that they're really needed, but could be helpful.
Also, are you sure the program is even running? (i.e. blinky LED?)

peterdeco1
- 17th June 2008, 19:12
Try this. I've used something similar to it on a 12F675 and had no problems. You should be able to turn on the LED for up to 255 seconds (the maximum byte value).

CMCON = 7 'comparators off
trisio = %11111111 'ALL INPUTS - HIGH & LOW COMMANDS CONVERT TO OUTPUTS
DEFINE OSCCAL_1K 1 'Set OSCCAL for 1K device
POTVALUE VAR BYTE 'POTENTIOMETER VALUE VARIABLE
LEDTIMER VAR BYTE 'LED TIMER VARIABLE

START:
LET LEDTIMER = 0 'RESET TIMER TO 0
Low GPIO.4 'TURN OFF LED ON GPIO.4
PAUSE 5000 'STAY OFF FOR AT LEAST 5 SECONDS
ADCIN 1,POTVALUE 'READ VALUE OF POT
IF POTVALUE = 0 THEN START 'STAY HERE OFF UNTIL POT IS TURNED

LEDON:
High GPIO.4 'TURN ON LED FOR 1 SECOND INCREMENTS
PAUSE 1000
LET LEDTIMER = (LEDTIMER + 1) 'COUNT SECONDS
ADCIN POTVALUE 'READ POT VALUE
IF POTVALUE > LEDTIMER THEN LEDON 'STAY HERE ON UNTIL LEDTIMER = POTVALUE
GOTO START 'LEDTIMER = POTVALUE GOTO START & RESET

glkosec
- 17th June 2008, 19:45
no this is not the entire programs i have my defines set as follows

'Definitions'
DEFINE OSC 4
DEFINE ADC_BITS 10 ' set number of bits in result
DEFINE ADC_CLOCK 3 ' set clock source
DEFINE ADC_SAMPLEUS 50 ' set sampling time for microseconds
'Definitions'

'Register Initializations'
ADCON0 = %10000011
ANSEL = %00000011 ; A to D on AN0 and AN1
'ANSELH = %00001100
CMCON = %00000111
TRISIO = %00011111 ; define Inputs and Outputs
GPIO = %00000000 'set all pin states to off
'OPTION_REG = %11100000
WPU = %00000000
'Register Initializations'


'Variables'
LOAD_OUT VAR GPIO.5
DELAY VAR WORD
COUNTER VAR WORD
AD_AN0_VALUE VAR WORD
MAIN_TRIGGER VAR GPIO.2
A1 var Byte
i var word


im trying to achieve multiple time ranges. for example 0.5 sec to 5 sec, and i want to use the pot to adjust in that range.

But when i do the AD conversion it is giving me a number between 0 and 1024. But inorder to get my specific range i need a value of 50 to 500.

I am basically trying to figure out a way to spread the 50 to 500 range out over the full 0 to 1024 range so i get the same resolution and get my two endpoints of 50 and 500.

I have also changed thge code as follows.

MAIN:
A1 = %00000000
GOSUB READ_A_D_AN0
LOAD_OUT = 1 ' ON FIRST TRIGGERABLE
PAUSE 100
BUTTON MAIN_TRIGGER, 1, 255, 0, a1, 0, RUN
PAUSE 100
GOTO MAIN


TIME:

for i = 1 to counter step 1
FOR DELAY = 1 TO 10 STEP 1
PAUSE 1
GOSUB READ_A_D_AN0
NEXT DELAY
next i
RETURN

RUN:
PAUSE 10
LOAD_OUT = 0
GOSUB TIME
PAUSE 10
GOTO MAIN

I appreciate the help, thank you very much.

Acetronics2
- 17th June 2008, 19:59
Hi,

The simplest way :

Time = ( ADC result >> 1 + 50 ) MIN 500 ...

or better

Time = ( ADC Result >> 1 + 50 ) * 8 / 9

Applause ... Thanks !

Alain