Need help with cogs and stack assignment
eagletalontim
Posts: 1,399
I guess people are scared to post in my other topic, but I need to get something working as soon as possible so I can test it out before it gets dark if at all possible. What I am attempting to do is start a new cog to update my LCD display while also running the main loop.
Here is a basic version of what I am doing :
[PHP]
VAR
LONG LCDstack[15]
BYTE gear
LONG rpm
OBJ
lcd : "Serial_Lcd"
getrpm : "jm_freqin"
num : "simple_numbers"
PUB Main
rpm := 0
gear := 1
getrpm.init(15)
lcd.init(8, 9600, 2)
lcd.displayOn
lcd.backLight(false)
lcd.gotoxy(0,0)
lcd.str(string("Testing Version "))
waitcnt(200_000_000 + cnt)
lcd.cls
lcd.str(string("Gear: "))
lcd.gotoxy(0,1)
lcd.str(string("RPM : "))
gear := 1
cognew(LCDupdate, @LCDstack)
shiftgear(gear)
repeat
rpm := getrpm.freq * 3
if ina[upbutton] == 1
gear++
gear := shiftgear(gear)
if ina[downbutton] == 1
gear--
gear := shiftgear(gear)
repeat while ina[downbutton] == 1 or ina[upbutton] == 1
waitcnt(3_000_000 + cnt)
waitcnt(10_000_000 + cnt)
PUB LCDupdate
repeat
lcd.gotoxy(5,0)
lcd.str(num.dec(gear))
lcd.str(string("um"))
lcd.gotoxy(5,1)
lcd.str(num.dec(rpm))
waitcnt(clkfreq / 1000 + cnt)
PUB shiftgear(tmp)
if tmp > 4
tmp := 4
if tmp < 1
tmp := 1
dira[solenoida]~~
dira[solenoidb]~~
outa[16..23]~
dira[16..23]~~
if tmp == 1
outa[solenoida] := 1
outa[solenoidb] := 1
if tmp == 2
outa[solenoida] := 0
outa[solenoidb] := 1
if tmp == 3
outa[solenoida] := 0
outa[solenoidb] := 0
if tmp == 4
outa[solenoida] := 1
outa[solenoidb] := 0
outa[16..23] := numbers[tmp]
return tmp[/PHP]
For some reason if I start the program without nulling out the cognew command, it starts in first gear, but as soon as I hit a button, it goes to 4th gear according to the 7 segment LED display. If I null it out, it works fine. Also, the LCD does not get updated as needed. Can someone help me get this fixed? The LCD only shows Gear: and RPM: Nothing else shows on the screen. It is like the LCDupdate is not working at all.
Here is a basic version of what I am doing :
[PHP]
VAR
LONG LCDstack[15]
BYTE gear
LONG rpm
OBJ
lcd : "Serial_Lcd"
getrpm : "jm_freqin"
num : "simple_numbers"
PUB Main
rpm := 0
gear := 1
getrpm.init(15)
lcd.init(8, 9600, 2)
lcd.displayOn
lcd.backLight(false)
lcd.gotoxy(0,0)
lcd.str(string("Testing Version "))
waitcnt(200_000_000 + cnt)
lcd.cls
lcd.str(string("Gear: "))
lcd.gotoxy(0,1)
lcd.str(string("RPM : "))
gear := 1
cognew(LCDupdate, @LCDstack)
shiftgear(gear)
repeat
rpm := getrpm.freq * 3
if ina[upbutton] == 1
gear++
gear := shiftgear(gear)
if ina[downbutton] == 1
gear--
gear := shiftgear(gear)
repeat while ina[downbutton] == 1 or ina[upbutton] == 1
waitcnt(3_000_000 + cnt)
waitcnt(10_000_000 + cnt)
PUB LCDupdate
repeat
lcd.gotoxy(5,0)
lcd.str(num.dec(gear))
lcd.str(string("um"))
lcd.gotoxy(5,1)
lcd.str(num.dec(rpm))
waitcnt(clkfreq / 1000 + cnt)
PUB shiftgear(tmp)
if tmp > 4
tmp := 4
if tmp < 1
tmp := 1
dira[solenoida]~~
dira[solenoidb]~~
outa[16..23]~
dira[16..23]~~
if tmp == 1
outa[solenoida] := 1
outa[solenoidb] := 1
if tmp == 2
outa[solenoida] := 0
outa[solenoidb] := 1
if tmp == 3
outa[solenoida] := 0
outa[solenoidb] := 0
if tmp == 4
outa[solenoida] := 1
outa[solenoidb] := 0
outa[16..23] := numbers[tmp]
return tmp[/PHP]
For some reason if I start the program without nulling out the cognew command, it starts in first gear, but as soon as I hit a button, it goes to 4th gear according to the 7 segment LED display. If I null it out, it works fine. Also, the LCD does not get updated as needed. Can someone help me get this fixed? The LCD only shows Gear: and RPM: Nothing else shows on the screen. It is like the LCDupdate is not working at all.
Comments
In your case you called init in COG0 and used it for some output.
Then you call cognew and want to use the display from COG1. But here you did not initialize the LCD which leaves the I/O pins in input-mode. No transfer possible.
So, I'd change the code to do the whole LCD-stuff in your LCDupdate-function.
Yes, it looks like the main loop updates rpm (a global variable) so the LCD should be displaying the updated value with each loop in the display cog.