COGS???
Travis M. Best
Posts: 9
I am trying to figure out how to split my program in two parts and be able to transfer my A/D Data between the two maybe by using a cog???
CON
_CLKMODE = XTAL1 + PLL16X
_XINFREQ = 5_000_000
OBJ
ADC : "CD_LTC1298"
PWM : "PWM_32_v2"
PUB MAIN | Temp,Temp1,Temp2,smpcnt,dimled,briled,midled,offled,offdim,dim,dimdled,leda,ledb,ledc,ledd,lede,ledf,ledg,ledh,ledchange,dimdim
adc.start(0)
PWM.Start
dim := 0
repeat
repeat until smpcnt > 59
Temp1 := adc.GetADC(0)
Temp2 := Temp2 + Temp1
smpcnt := smpcnt + 1
Temp := Temp2 / 60
offled := 0
offdim := 1
dimled := 30
midled := 40
briled := 100
smpcnt := 0
temp1 := 0
temp2 := 0
If Temp =< 81
If ledh < 100
ledchange := 100
dimdim := 30
ledh := 0
ledb := 0
ledc := 0
ledd := 0
lede := 0
ledf := 0
ledg := 0
PWM.Duty(4,briled,5000)
PWM.Duty(7,offled,5000)
PWM.Duty(9,dimled,5000)
PWM.Duty(13,dimled,5000)
PWM.Duty(14,dimled,5000)
PWM.Duty(22,dimled,5000)
PWM.Duty(23,offdim,5000)
PWM.Duty(25,offdim,5000)
PWM.Duty(26,offdim,5000)
ledh :=ledh + 1
If Temp => 82
If Temp =< 107
If leda < 100
ledchange := 100
dimdim := 30
ledb := 0
ledc := 0
ledd := 0
lede := 0
ledf := 0
ledg := 0
ledh := 0
PWM.Duty(4,offled,5000)
PWM.Duty(7,briled,5000)
PWM.Duty(9,briled,5000)
PWM.Duty(13,dimled,5000)
PWM.Duty(14,dimled,5000)
PWM.Duty(22,dimled,5000)
PWM.Duty(23,dimled,5000)
PWM.Duty(25,dimled,5000)
PWM.Duty(26,dimled,5000)
leda :=leda + 1
CON
_CLKMODE = XTAL1 + PLL16X
_XINFREQ = 5_000_000
OBJ
ADC : "CD_LTC1298"
PWM : "PWM_32_v2"
PUB MAIN | Temp,Temp1,Temp2,smpcnt,dimled,briled,midled,offled,offdim,dim,dimdled,leda,ledb,ledc,ledd,lede,ledf,ledg,ledh,ledchange,dimdim
adc.start(0)
PWM.Start
dim := 0
repeat
repeat until smpcnt > 59
Temp1 := adc.GetADC(0)
Temp2 := Temp2 + Temp1
smpcnt := smpcnt + 1
Temp := Temp2 / 60
offled := 0
offdim := 1
dimled := 30
midled := 40
briled := 100
smpcnt := 0
temp1 := 0
temp2 := 0
If Temp =< 81
If ledh < 100
ledchange := 100
dimdim := 30
ledh := 0
ledb := 0
ledc := 0
ledd := 0
lede := 0
ledf := 0
ledg := 0
PWM.Duty(4,briled,5000)
PWM.Duty(7,offled,5000)
PWM.Duty(9,dimled,5000)
PWM.Duty(13,dimled,5000)
PWM.Duty(14,dimled,5000)
PWM.Duty(22,dimled,5000)
PWM.Duty(23,offdim,5000)
PWM.Duty(25,offdim,5000)
PWM.Duty(26,offdim,5000)
ledh :=ledh + 1
If Temp => 82
If Temp =< 107
If leda < 100
ledchange := 100
dimdim := 30
ledb := 0
ledc := 0
ledd := 0
lede := 0
ledf := 0
ledg := 0
ledh := 0
PWM.Duty(4,offled,5000)
PWM.Duty(7,briled,5000)
PWM.Duty(9,briled,5000)
PWM.Duty(13,dimled,5000)
PWM.Duty(14,dimled,5000)
PWM.Duty(22,dimled,5000)
PWM.Duty(23,dimled,5000)
PWM.Duty(25,dimled,5000)
PWM.Duty(26,dimled,5000)
leda :=leda + 1
Comments
Do you want to pass variable values from, say, the Main to the called up method?
I think one option is that you can use global variables to pass values around.
What exactly are you trying to do?
Also, indenting is essential to your code. When you post code, use the little code box button under the smilies, one at the start to open the code and one at the end to close the code.
Otherwise it's hard for people to understand what your code is really doing.
Also, you could simply have a Main program call up 2 separate methods. One method continuously loops to read the A/D and inserts results into global variables. The second method does nothing but read the global variables and run IF statements and blink lights in a continuous loop.
You would always have some kind of time lag to consider, of course, so you'd have to evaluate what sort of timing your system requires.
hope that helps,
Mark
Post Edited (ElectricAye) : 12/15/2009 3:55:39 AM GMT
Depends on how many COGs you can efford and how responsive it needs to be.
I don't know your ADC object, but the start function makes me believe that it's already running in a COG. It just spends the time waiting for a 'measure now' command (adc.GetADC()) . Anyway, I'd have a look into this driver and change that in the way that it loops endlessly, doing the AD conversion and store the result in a variable for which you pass it's address in the start function.
In your main program you will always find the latest measurement in this variable.
Another way would be to put the if decision-making into the ADC COG as well. Because the value can only change from one measurement to the next and the ADC COG knows when it makes sense to check.
It all depends on the goals you have for this program and what the objects you use can do. How many measurements can be done per second? How many do you need? At what rate do you need to change the LED output? Do you need the ADC value for something else? (Logging for example - or calculations). It only makes sense to split up a program in several sub-tasks, when one COG can't do the job fast enough.