Problem write to code space pic 18f452
Hi forum
This is my first post so be patient.
I am migrating a program from a 16f887 to 18f452. My windows application loads position map data to code space via serial comms to position a stepper motor. The 16f887 works fine. The 18f452 loaded half the data in each memory row and the data is incorrect.
I have a test program mostly the same as a sample from this web site see below and also see the result in the pic memory.(should be decimal 0 1 2 3 4 5 6 7)
regards Paul
DEFINE HSER_RCSTA 90h
' Set transmit register to transmitter enabled
DEFINE HSER_TXSTA 24h
' Set baud rate
DEFINE HSER_BAUD 9600
DEFINE HSER_EVEN 1
DEFINE ADC_BITS 8 ' Set number of bits in result
DEFINE ADC_CLOCK 3 ' Set clock source (rc = 3)
DEFINE ADC_SAMPLEUS 50 ' Set sampling time in microseconds
TRISA = 255 ' Set PORTA to all input
ADCON1 = 2 ' PORTA is analog
DEFINE OSC 20
OSCCON.0 = 0
TRISD = 000011
TRISB = 111000
TRISB = 110000
PORTB = 000000
'####################
I Var word ' Loop count
D Var word ' Data
A Var word ' Address
D=0 'set start value for data
For I = 0 To 7 ' Loop 8 times, once for each address $1800 to $1807
A = $1800 + I ' Increment Address
D = D+1 ' Change Data 0,1,2,3,4,5,6,7
Writecode A,D ' Send value in D to code space location A
Next I
mainloop:
For I = 0 To 7 ' Loop 8 times, once for each address $1800 to $1807
A = $1800 + I ' Increment Address
' Change Data
readcode A,D ' Send value in D to code space location A
HSEROUT [dec D,13]
Next I
High 0 ' Turn on LED connected to PORTB.0
if OSCCON.3 = 1 then PORTB.1 = 1
if OSCCON.3 = 0 then PORTB.1 = 0
if OSCCON.0 = 0 then PORTB.2 = 1 'test for osc start
HSEROUT [BIN OSCCON,13]
Pause 50 ' Delay for .5 seconds
Low 0 ' Turn off LED connected to PORTB.0
Pause 50 ' Delay for .5 seconds
Goto mainloop ' Go back to loop and blink LED forever
End
memory result 1800- 00513 01027 01541 00007 65535 65535 65535 65535
Re: Problem write to code space pic 18f452
With 18F, you must jump by 2 instead of 1... Not a bad Idea to use WORD instead of byte either... however here's a quick fix
Code:
For I = 0 To 14 step 2 ' Loop 8 times, once for each address $1800 to $1807
A = $1800 + I ' Increment Address
D =i/2' Change Data 0,1,2,3,4,5,6,7
Writecode A,D ' Send value in D to code space location A
Next I
mainloop:
For I = 0 To 14 step 2 ' Loop 8 times, once for each address $1800 to $1807
A = $1800 + I ' Increment Address
readcode A,D ' Send value in D to code space location A
HSEROUT [dec A, "=",dec D,13,10]
Next I
Re: Problem write to code space pic 18f452
And another variant
Code:
A = $1800
hserout ["Writing...",13,10]
For I = 0 To 7 step 2
D.highbyte =i
d.lowbyte=d.highbyte+1
Writecode A,D
HSEROUT [dec A, "=",dec D.highbyte, " ", dec d.lowbyte,13,10]
a=a+2
Next I
mainloop:
hserout ["reading...",13,10]
For a = $1800 To $1807 step 2
readcode A,D
HSEROUT [dec A, "=",dec D.highbyte, " ", dec d.lowbyte,13,10]
Next
Re: Problem write to code space pic 18f452
Thanks Steve
Stepping by two fixes my issue and your test code worked fine. In my real application I use 8bit decode ADC which give values 0 to 255 so i can fetch 0 to 255 position values from the map block of memory I load from my pc program. ie adcin value plus start memory register number = motor destination. I have multiple maps for position and also current control, speed , acceleration. Stepping by two means a day rewrite.
I am changing pic for speed as my top step speed for my motor control loops is limited by processor speed. Why does 18f index memory by two's and is there a fast pic that can write code space memory indexed by 1.
regards Paul
ps if the ME programmer displayed memory address in decimal when decimal display is selected I may have picked up the 16 value difference for each row. Perhaps a enhancement.
Re: Problem write to code space pic 18f452
How fast you want it?
Why the code space btw?!?
Re: Problem write to code space pic 18f452
The 16f887 at 20mhx runs the motor control loop at about 1/3 the speed I need. The code space for map storage was chosen for speed of retrieval.
see the forward loop below. The map data comprises 12 maps each with 256 words. The code below is the minimum logic requirement per motor step and needs to execute 180 times for 1 motor revolution. (I am using a stepper driver that needs only single pulse to advance the motor 1/2 step with 90 step motor.). The acdin for position is continually changing and needs to be read on the fly every motor step to update destination as well as speed and current changes.
(serial commands below are comment out used for debug)
Code:
'--------------------------------------------------------------------------------------------------------
fowarddir: 'forward loop
ADCIN 0, freq ' Read channel 0 to freq 0 to 255
Readcode mappos + mapindexcs , cmpos
if (pos > cmpos) and (mappos > 5) then mappos = mappos - 4
'HSEROUT ["d ",dec mappos ,13]
if computercontrol=0 then
Readcode freq + mapindex , des 'read position data from progspace store des
Readcode mappos + spdaddress, speed 'get speed value from codespace
Readcode mappos + curraddress, current 'get current value from codespace
ELSE
SPEED = manspeed
current = mancurrent
endif
HPWM 1,current,200 'current control out
PORTD = 110000 'd4 enable d5 step d6 fwd rev d7 = reset
readcode accaddress , accvalue 'get acc value from code space
PORTD = 010000
pos=pos+1
if speed > ACCVALUE THEN
pauseus SPEED 'step delay to control motor speed
ELSE
pauseus accvalue
ENDIF
'HSEROUT ["sp ",dec SPEED ," ac ", dec ACCVALUE," cur ",dec current,13]
'HSEROUT ["Fwd Accvalue",dec accvalue," Accaddress ",dec accaddress-6928," Des ",dec des-5000," Pos ",dec pos-5000,13]
if pos + accaddress-mapadrs[7] < des and accaddress < mapadrs[8] then accaddress = accaddress + 1
if pos + accaddress-mapadrs[7] > des and accaddress > mapadrs[7] then accaddress = accaddress - 1
if accaddress = mapadrs[7] then goto stoppedstate 'main loop
goto fowarddir
'---------------------------------------------------------------------------------------------------------