PDA

View Full Version : bad expression error, linking addresses incorrectly.



robertpeach
- 24th August 2009, 11:08
hi sorry again but i can't seem to work out what im doing wrong, its gonna be a very simple answer but i cant see it.

im using a 18f2431, im trying to link the addresses of my temporarily saved data in the buffers, to be used in a calculation, and then that "answer" to be linked to be put as the duty in the hpwm output.

sorry im a rookie but im tryin to learn :)

this is my code:


DEFINE OSC 20
DEFINE DEBUG_REG PORTC
DEFINE DEBUG_BIT 6
DEFINE DEBUG_BAUD 9600
DEFINE DEBUG_MODE 0 ' 1 = inverted, 0 = true

T1 VAR WORD
T2 var word
T3 var word

Capture1 VAR PIR3.1
Capture2 var pir3.2
Capture3 var pir3.3
False CON 0
True CON 1

' auto time base reset, frequency mode, capture on every rising edge
period1 CON %01000101
period2 CON %01000101
period3 CON %01000101

ANSEL0=0 ' All digital
TRISA.2 = 1 ' this is your (CAP1) input pin measuring the frequency
TRISA.3 = 1
TRISA.4 = 1
INTCON = 0 ' Interrupts off
TMR5H = 0 ' Clear high byte of TMR5 counter
TMR5L = 0 ' Clear low byte
T5CON = %00000001 ' prescale=1:1, int clock, TMR5=on
CAP1CON = period1 ' we're measuring a period
CAP2CON = period2
CAP3CON = period3

Main:
Capture = False ' Reset capture flag
GOSUB pulsewidth ' get pulsewidth


' At 20MHz, to figure a PWM frequency of 19.455kHz
'
' TPWM = time period of PWM frequency
' PTPER = 12-bit period register PTPERL and PTPERH
' PTMRPS = PWM time base prescaler
'
' (PTPER+1)*PTMRPS 257
' TPWM = ---------------- = ------------ = 0.0000514
' Fosc/4 5000000
'
' Frequency = 1/TPWM = 1/0.0000514 = 19.455kHz
'
' PWM resolution (bits resolution for duty cycle)
'
' log(20MHz/19.455kHz) 3.01
' Resolution = ------------------ = ----------- = 10 bits
' .301 .301

' so we'll need a word sized var for Duty
Duty Var Word

PORTB = 0 ' clear port latch
TRISB = %11000000 ' PWM0,1,2,3,4,5 outputs

TRISC = 2 ' RC1 = FLTA input (ground RC1 to halt PWM)
' RC1 should be pulled high for normal PWM operation
' when fault A is enabled.
' PCPWM init
PTCON0 = %00000000 ' 1:1 postscale, Fosc/4 1:1 prescale, free running mode
' PTCON0 = %00000100 would give 19.45kHz/4
PTPERL = 0 '
PTPERH = 1 ' PTPER = $0100 or 256d for ~19.45kHz

' PWM4,5 independent
PWMCON0 = %01010100 ' PWM[5:0] outputs enabled
PWMCON1 = 1 ' updates enabled, overrides sync w/timebase
PTCON1 = %10000000 ' PWM time base is ON, counts up
FLTCONFIG = %00000011 ' enable fault A, cycle-by-cycle mode

Duty = dutycycle ' ~50% however this will be changed in respect to algorithm
PDC2L = Duty.LowByte ' maintain a duty cycle on PWM4,5
PDC2H = Duty.HighByte ' independent PWM outputs.
PAUSE 5
GOTO Main

pulsewidth:
' Frequency measurement mode, capture Timer5 count every rising edge
' with auto reset of Timer5 on each event
WHILE Capture = False
WEND
T1.HighByte = CAP1BUFH
T1.LowByte = CAP1BUFL
T2.HighByte = CAP2BUFH
T2.LowByte = CAP2BUFL
T3.HighByte = CAP3BUFH
T3.LowByte = CAP3BUFL

F69H VAR BYTE 'Value of period at CAP1 ready for calculation, f69h being its address from data sheet
F67H VAR BYTE 'Value of period at CAP2 ready for calculation
F65H VAR BYTE 'Value of period at CAP3 ready for calculation

''''''''''''''''''''''''''''''''''''Algorithm for finding power''''''''''''''''''''''''

averageperiod var byte 'saving average period to ram
averageperiod = (F69H+F67H+F65H)/3 'average period found using 3 periods

'for now im using arbitary random scaling factors, until i have the actual graphs to get the real rpm against power

rpmvalue var byte 'saving rpm value to ram
rpmvalue=60/(8*averageperiod) 'finding rpm value using current period measurement

power var byte 'saving power to ram

IF rpmvalue<=300 Then
power=rpmvalue/15
else
power=rpmvalue/10
endif


' now i need to link power to the arbitary pwm output
'this is a standard quadratic function (quadratic in example case anyway)

dutycycle var byte 'byte being the one that is answer of algorithm when thats created
dutycycle = (power^2)+(5*power)+3 'finding dutycycle using rpmvalue

RETURN

END

sorry it was alot neater than that in microcode studio...

robertpeach
- 24th August 2009, 15:54
also when i try and look at some of the threads that rorchach, or whatever his name is has posted in my antivirus keeps stopping me... i keep getting attacked by a trojan horse in those threads... not sure if anyone else has got this? but it keeps happening to me only on those recent threads.

Melanie
- 24th August 2009, 16:19
Those threads and the Spammer have been killed.

robertpeach
- 25th August 2009, 14:42
ok doesnt look like anyone could help me before.. but i have a new question regarding the 2431.

ive been trying to get a pwm output using the following code:


Duty Var Word

PORTB = 0 ' clear port latch
TRISB = %11000000 ' PWM0,1,2,3,4,5 outputs

TRISC = 2 ' RC1 = FLTA input (ground RC1 to halt PWM)
' RC1 should be pulled high for normal PWM operation
' when fault A is enabled.
' PCPWM init
DTCON = %00000101 ' ~500nS dead-time (for complementary outputs only)
PTCON0 = %00000000 ' 1:1 postscale, Fosc/4 1:1 prescale, free running mode
' PTCON0 = %00000100 would give 19.45kHz/4
PTPERL = 0 '
PTPERH = 1 ' PTPER = $0100 or 256d for ~19.45kHz

' PWM4,5 independent, PWM0,1,2,3 complementary
PWMCON0 = %01010100 ' PWM[5:0] outputs enabled
PWMCON1 = 1 ' updates enabled, overrides sync w/timebase
PTCON1 = %10000000 ' PWM time base is ON, counts up
FLTCONFIG = %00000011 ' enable fault A, cycle-by-cycle mode

Duty = 800 ' ~50%
PDC2L = Duty.LowByte ' maintain a fixed 50% duty cycle on PWM4,5
PDC2H = Duty.HighByte ' independent PWM outputs.

last night this code worked for me, and i was getting a good pwm output. however today it wont work, ive checked my wiring is correct, by testing using multimeter and with other programs. however when i run the current program i get a pulse width output for about 10 seconds before it dies, i also havee an led attached to one of the pwm outputs which also fades out at the same time to check. ive fiddled alot with the fault byte to check it wasnt that. but i cant think of what else could be causing this problem? also for the 10 seconds it does appear... the 0 points on my pwm wave are actually slanted upwards slightly...?

additionally since i havnt purchased a crystal yet, i thought id use the internal oscilattor INTIO1. this runs at a maximum of 8mhz. maybe this is causing the problem? ive tried to adjust my prescalers inrespect to this, however i still get the problems.

robertpeach
- 25th August 2009, 16:26
oh and sometimes (moreso now) i dont get a pwm at all... it just makes the led (thats linked to a pwm output) turn on for about 10 seconds and then fade out and nothing more.

robertpeach
- 26th August 2009, 13:04
ok ignore that one... took me a while (two days almost) to figure out where i went wrong on that one...

but i still am having trouble with using values that are from a specific buffer, and using them in calculations etc... do i need to use there addresses? or just the buffer name? etc