Why doesn't this work ?

Discussion about the ZBasic language including the System Library. If you're not sure where to post your message, do it here. However, do not make test posts here; that's the purpose of the Sandbox.
Post Reply
FFMan
Posts: 502
Joined: 09 January 2010, 12:52 PM

Why doesn't this work ?

Post by FFMan »

See code below. If i assign register.portb to another variable my code doesn't do anything. Does the value of register.portb change at runtime ?

Code: Select all

Option Explicit
option com1speed 115200

	Public 	const uiDelay	as unsignedinteger	=600
	
Sub Main()

	Dim		byMask			as byte = &b00001111
	Dim		byRegister		as byte	

	dim 	inPos			as integer
	dim 	byLoop			as integer
	dim 	byDDR			as byte
	
	byRegister	= Register.Portb
	
	'Set the pins to outputs
	byDDR=Register.ddrb	
	Call SetBits(Register.DDRB,&b00001111,&b00001111)
	
	'This works
	for byLoop=1 to 2500
		call StepperStep(Register.Portb,byMask,inPos,true)
		debug.print inPos
	next 
	
	'this doesn't work
	for byLoop=1 to 2500
		call StepperStep(byRegister,byMask,inPos,true)
		debug.print inPos
	next 

	
End Sub

dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Re: Why doesn't this work ?

Post by dkinzer »

FFMan wrote:Does the value of register.portb change at runtime ?
It can. I can't tell what might be happening here because the routine StepperStep() isn't shown. Generally speaking, the value of Register.PORTx will be the value last written to Register.PORTx

You might alter your code to output the value of Register.PortB on each iteration of the loop to see if it is, in fact, changing.
- Don Kinzer
FFMan
Posts: 502
Joined: 09 January 2010, 12:52 PM

Post by FFMan »

here's the missing piece - still being refined to get the best from stepper driver.

Would it be better to pass the register byVal ?

Code: Select all

Sub StepperStep(byRef byRegister as byte,byVal byMask as byte,byRef inStepperPos as integer,byVal blClockwise as boolean)

	dim byCode(0 to 4) 	as byte
	dim byLoop 			as byte

	if blClockWise=true then
		'byCode(0)=&b00000001
		'byCode(1)=&b00000011
		'byCode(2)=&b00000010
		'byCode(3)=&b00000110
		'byCode(4)=&b00000100
		'byCode(5)=&b00001100
		'byCode(6)=&b00001000
		'byCode(7)=&b00001001
		'byCode(8)=&b00000000

		byCode(0)=&b00000001
		byCode(1)=&b00000010
		byCode(2)=&b00000100
		byCode(3)=&b00001000
		byCode(4)=&b00000000
		inStepperPos=inStepperPos+1
	else
		'byCode(0)=&b00001000
		'byCode(1)=&b00001100
		'byCode(2)=&b00000100
		'byCode(3)=&b00000110
		'byCode(4)=&b00000010
		'byCode(5)=&b00000011
		'byCode(6)=&b00000001
		'byCode(7)=&b00001001
		'byCode(8)=&b00000000
		
		byCode(0)=&b00001000
		byCode(1)=&b00000100
		byCode(2)=&b00000010
		byCode(3)=&b00000001
		byCode(4)=&b00000000
		inStepperPos=inStepperPos-1
	end if

	for byLoop=0 to 4
		Call SetBits(byRegister,byMask,byCode(byLoop))
		Call DelayMicroSeconds(uiDelay)
	next byLoop
	
End Sub

FFMan
Posts: 502
Joined: 09 January 2010, 12:52 PM

Post by FFMan »

in fact passing the register byVal doesn't help - i'll play some more later
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

FFMan wrote:Would it be better to pass the register byVal ?
No. However, I think it will work as you want if you change the definition of byRegister like this:

Code: Select all

   Dim byRegister as byte Based Register.PortB.DataAddress
And delete this line:

Code: Select all

  byRegister = Register.Portb
This way in the second For loop the address of PORTB will be passed to StepperStep().
- Don Kinzer
Post Reply