PDA

View Full Version : Timing scheme



glkosec
- 21st October 2008, 17:49
i AM TRYING TO CREATE A TIMER USING A 12f675 and am having trouble doing so.

The timer works by using two pots to control the ON time and the CYCLE time.

In other words i use one pot to set my cycle time to lets say 1 min. and then i use the other pot to set an ON time of 5 sec. The problem that i am having is that i need it to work so that if i make my ON time larger it makes the OFF time smaller while keeping the CYCLE time the same.

I cant figure out how to basically do two things at once.

The final problem is that i need the ON time and the CYCLE time to be adjustable is specific time ranges.

Any help at all would be greatly appreciated.

I have created a timer before but i simply controlled the ON and OFF time not the CYCLE time as with this timer.

I have thought about using the TMR0 function of the chip to run in the background but have never used it before and the datasheet is not much help.

Thank you for any help at all.

skimask
- 21st October 2008, 18:04
I think what you are talking about is 'VERY VERY Very slow speed PWM'
Is the code in your other post about the same subject still the same?

glkosec
- 21st October 2008, 19:45
I had figured out how to do the other post by using the code below...

ON_TIME:
PAUSE 525 ' low end pause time
for i = 1 to ON_counter step 1 ' high end pause loop time
GOSUB READ_A_D_AN0
PAUSEus 4750
nEXT I
RETURN

OFF_TIME:
PAUSE 525 ' low end pause time
for i = 1 to OFF_counter step 1 ' high end pause loop time
GOSUB READ_A_D_AN1
PAUSEus 4750
nEXT I
RETURN



I used this with AD on the two pots and then just called on time of off time when it was neccessary. But now i have to coordinate these two time ranges together as explained above. I appreciate your help.

mackrackit
- 21st October 2008, 22:48
The problem that i am having is that i need it to work so that if i make my ON time larger it makes the OFF time smaller while keeping the CYCLE time the same.
8 bit ADC has 265 steps. Subtract the ON reading from 256, if the ON is reading 64 then OFF will be 192. ON is 192 then OFF will be 64...


I cant figure out how to basically do two things at once.
Not going to happen with software routines.


The final problem is that i need the ON time and the CYCLE time to be adjustable is specific time ranges.
Maybe if
POT_TIME is > 0 and < 50 then T1
POT_TIME is >50 and < 100 then T2

glkosec
- 5th November 2008, 15:53
I have written the piece of code below and it works at the endpoints of 0.25 to 25 sec for the on time and 30 to 60 sec cycle time. The problem is that it does not work for numbers in between. I think my functions are not working correctly but i dont know. The other problem i see is that if You do the math to figure out the loop times they come out about 4 seconds longer than what is being seen on the hardware. This seems impossible to me, if anything they should be longer not shorter because i was not taking into account the adc reads? Anybody know of an explanation of whats going on here?


MAIN:
GOSUB ON_RUN
pause 10
GOTO MAIN


ON_TIME:
PAUSE 250 ' low end pause time (0.25 sec)
for i = 0 to ON_counter step 1 ' high end pause loop time (25 sec)
GOSUB READ_A_D_AN2
GOSUB READ_A_D_AN3
PAUSE 27
nEXT I
RETURN


OFF_TIME_RUN:
' PAUSE 5000
for K = 0 to OFF_TIME step 1 'total pause loop time
GOSUB READ_A_D_AN3
GOSUB READ_A_D_AN2
GOSUB OFF_TIME_CALC
PAUSE 33
nEXT K
RETURN

ON_RUN:
LOAD_OUT = 1
GOSUB ON_TIME
GOTO OFF_RUN

OFF_RUN:
LOAD_OUT = 0
GOSUB OFF_TIME_CALC
GOSUB OFF_TIME_RUN
GOTO INITIALIZE


OFF_TIME_CALC:
OFF_TIME = (CYCLE_COUNTER - ON_COUNTER) '+ 100
IF OFF_TIME < 120 THEN OFF_TIME = OFF_TIME + 150
if OFF_TIME = 1024 THEN OFF_TIME = OFF_TIME + 180
' if OFF_TIME > 2000 THEN OFF_TIME = OFF_TIME - 50
RETURN



READ_A_D_AN2: 'used for ON Time Delay
PAUSEUS 50 ' wait 50 microsec
ADCIN 2, AD_AN2_VALUE ' read channel 2 to AD_AN0_VALUE
ON_COUNTER = AD_AN2_VALUE ' set counter to AD value
RETURN

READ_A_D_AN3: 'used for Cycle Time Delay
PAUSEUS 50 ' wait 50 microsec
ADCIN 3, AD_AN3_VALUE ' read channel 3 to AD_AN3_VALUE
CYCLE_COUNTER = AD_AN3_VALUE + 1024 ' set counter to AD value + 1024
RETURN


I appreciate any help that anyone may give. Thank you again.

mackrackit
- 5th November 2008, 16:21
Can you post your whole code? I am making a guess the problem is in the variable size some where. Or in the ADC setup. And the routine "INITIALIZE" seems to be missing.

When you post code. the use of code tags helps.

glkosec
- 6th November 2008, 13:29
'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 = %10001011 ' set as right justified and turn on AD
ANSEL = %01001100 ' A to D on AN2 and AN3
CMCON = %00000111 ' set all pins to digital
TRISIO = %00011111 ' define Inputs and Outputs
GPIO = %00000000 ' set all pin states to off
WPU = %00000000 ' shut off weak pull ups
'Register Initializations'


'Variables'
LOAD_OUT VAR GPIO.5
OFF_COUNTER VAR WORD
ON_COUNTER VAR WORD
AD_AN2_VALUE VAR WORD
AD_AN3_VALUE VAR WORD
i var word
K VAR WORD
'TEST_TRIGGER VAR GPIO.3
'TEST_COUNTER VAR WORD
CYCLE_COUNTER VAR WORD
OFF_TIME VAR WORD
'Variables'

'************************************************* ***************************
'************************************************* ***************************

INITIALIZE:
GOSUB READ_A_D_AN2
GOSUB READ_A_D_AN3

MAIN:
GOSUB ON_RUN
pause 10
GOTO MAIN


ON_TIME:
PAUSE 250 ' low end pause time (0.25 sec)
for i = 0 to ON_counter step 1 ' high end pause loop time (25 sec)
GOSUB READ_A_D_AN2
GOSUB READ_A_D_AN3
PAUSE 27
nEXT I
RETURN


OFF_TIME_RUN:
' PAUSE 5000
for K = 0 to OFF_TIME step 1 'total pause loop time
GOSUB READ_A_D_AN3
GOSUB READ_A_D_AN2
GOSUB OFF_TIME_CALC
PAUSE 33
nEXT K
RETURN

ON_RUN:
LOAD_OUT = 1
GOSUB ON_TIME
GOTO OFF_RUN

OFF_RUN:
LOAD_OUT = 0
GOSUB OFF_TIME_CALC
GOSUB OFF_TIME_RUN
GOTO INITIALIZE


OFF_TIME_CALC:
OFF_TIME = (CYCLE_COUNTER - ON_COUNTER) '+ 100
IF OFF_TIME < 120 THEN OFF_TIME = OFF_TIME + 150
if OFF_TIME = 1024 THEN OFF_TIME = OFF_TIME + 180
' if OFF_TIME > 2000 THEN OFF_TIME = OFF_TIME - 50
RETURN



READ_A_D_AN2: 'used for ON Time Delay
PAUSEUS 50 ' wait 50 microsec
ADCIN 2, AD_AN2_VALUE ' read channel 2 to AD_AN0_VALUE
ON_COUNTER = AD_AN2_VALUE ' set counter to AD value
RETURN

READ_A_D_AN3: 'used for Cycle Time Delay
PAUSEUS 50 ' wait 50 microsec
ADCIN 3, AD_AN3_VALUE ' read channel 3 to AD_AN3_VALUE
CYCLE_COUNTER = AD_AN3_VALUE + 1024 ' set counter to AD value + 1024
RETURN

END

Acetronics2
- 6th November 2008, 13:54
Hi, Glqosec

One good thing to know is what granularity ( resolution ) you need for ONtime and Cycle duration ...

Alain

aratti
- 6th November 2008, 15:01
I can suggest the following approch:

Read cycle and ontime word variables
Compare the the two variables and adjust ontime to cycle if ontime is greater then cycle

IF ONTIME>CYCLE THEN ONTIME=CYCLE

If you don't want offtime=0 then you can add the proper offset

IF ONTIME>=CYCLE THEN ONTIME=(CYCLE-OFFSET)

Set high output pin
loop for for the cycle delay and check ontime
when loop reach ontime value set low output pin
continue the loop till you reach the cycle time.

Ontime will be adjustable and always within cycle time

Al.