PDA

View Full Version : pic program crashing



comwarrior
- 8th July 2009, 12:45
hi everyone...
I've writen a PB program to controll a pulse motor...
It compiles without any errors or warnings...
It runs in the oshon soft pic simulation IDE with out a problem

As soon as i try to run it in a PIC random ports become high but don't change...
I have a test program that runs fine and that uses the same serout2 command and it sends data to my PC without a problem...

I'm guessing i missed something that i should have setup... can someone take a look and tell me what i missed?
I've got no idea on where to begin to diagnose this type of problem...



include "bs1defs.bas"
Define CONF_WORD = 0x3F3A
Define OSC 20 ' Set clock speed

Define ADC_BITS 10 ' Set number of bits in result
Define ADC_SAMPLEUS 10 ' Set sampling time in uS

ADCON0.7 = 0 ' ADC clock set to Fosc/2
ADCON0.6 = 0 ' ADC clock set to Fosc/2
ADCON1.6 = 0 ' ADC clock set to Fosc/2

INTCON = 0 ' Disable all interupts
'DEFINE SER2_BITS 8 ' Ser2 number of data bits
'RCSTA = %10010000 ' Enable serial port and continuous receive
'TXSTA = %00100100 ' Enable transmit and asynchronous mode with BRGH 1
'SPBRG = 20 ' Set baud rate to 57600 with 20mhz clock (BRGH = 1)

ADT1 var word ' Create ADT1 to store curent ad conversion for AD channel 0
ADT1MAX var word ' Create ADT1MAX to store curent ADC0 max val
ADT1MAX = 0
ADT1VAL var word
ADT1VAL = 0
ADV1 var word
VOLT1H var word
VOLT1L var word
C1N var bit
C1N = 0
C1S var bit
C1S = 0
PCOUNTER var byte
PCOUNTER = 0
PTICK var bit
PTICK = 0
RPM var word
RPM = 0
TMR1_VAL var word
TMR1_VAL = 0

TRISE = 255 ' Set PortE to all inputs
TRISD = 0 ' Set PortD to all outputs
TRISC = 0 ' Set PortC to all outputs
TRISB = 0 ' Set PortB to all outputs
TRISA = 255 ' Set PORTA to all input
ADCON1 = 0 ' PORTA is analog

PIE1.0 = 0 ' Disable TMR1 interupt (double check)
T1CON.5 = 1 ' Setup TMR1 timings
T1CON.4 = 1
T1CON.3 = 1
T1CON.1 = 0
T1CON.0 = 1

main:
gosub getadc
gosub control
gosub backemfreuse
gosub rpmcounter
gosub adcvonvert
gosub telemetry
goto main

getadc:
ADCIN 0, ADT1
return

adcvonvert:
ADT1 = ADT1 / 64
VOLT1H = ((48 * ADT1) / 10000)
VOLT1L = ((48 * ADT1) - (((48 * ADT1) / 10000) * 10000)) / 100
return

rpmcounter:
if PCOUNTER = 4 then
T1CON.0 = 0 ' Pause TMR1
TMR1_VAL.lowbyte = TMR1L ' Load TMR1 Low end byte
TMR1_VAL.Highbyte = TMR1H ' Load TMR1 High end byte
RPM = 60000 / ((TMR1_VAL * 16)/10000)
PCOUNTER = 0
TMR1L = 0
TMR1H = 0
T1CON.0 = 1
endif
return

telemetry:
'HSEROUT ["V1:",DEC5 VOLT1H,".",DEC5 VOLT1L," RPM:", DEC5 RPM,10]

SEROUT2 PORTD.0,16468,["V1:",DEC VOLT1H,".",DEC VOLT1L," RPM:",DEC RPM,10]
RETURN

control:
'IF ADT1 > 200 and ADT2 > 200 then
' LOW PORTB.0
' LOW PORTB.1
' C1N = 0
' C1S = 0
' return
' ENDIF
IF ADT1 > 200 then
high PORTB.0
LOW PORTD.7
C1N = 1
IF PTICK = 0 then
PCOUNTER = PCOUNTER + 1
PTICK = 1
endif
endif
IF ADT1 < 200 then
LOW PORTB.0
C1N = 0
IF PTICK = 1 then
PTICK = 0
endif
endif
return

backemfreuse:
if C1N = 0 then 'and C1S = 0
High PORTD.7
Else
LOW PORTD.7
endif
return

end


Thank you

Jerson
- 8th July 2009, 13:30
Frankly, I've not looked through your code, but asking questions to see if you've eliminated those cases.

1. Disconnect the motor for tests. Use a LED instead
2. Check on the power supply decoupling. Not enough??
3. Back EMF protection. Since you seem to be using this, is the value within the supply rails?? Usually, a freewheeling diode is used to suppress back emf.

comwarrior
- 8th July 2009, 13:58
apologies, should have said...

it's curently sat on a plug board and the analog port used is set to 0 volts atm...
8 LED's on port B
power supply is the backup battery, single cell lithion ion battery curently at 3.8 volts...

I managed to find the problem... it hadn't actually crashed it's just port B was left in a 'random' state uppon startup...

I found this because at regular stages i got it to write the stage number into the onboard eprom and the read it back in the programmer and notices it had initialised and was running the main sequence...

So, the program is actually running...

But, i'm getting nothing up the serial test link to my PC... which works (same port, same command) in my test program...


SEROUT2 PORTD.0,16468,["V1:",DEC VOLT1H,".",DEC VOLT1L," RPM:",DEC RPM,10]

The test program uses...


SEROUT2 PORTD.0,16468,[dec abcd,10,13]

So their both the same, but i get data from the test program, but not the pulse motor program...

Any ideas why?

Thanks

comwarrior
- 8th July 2009, 14:37
i ented a couple of lines to try to debug the problem...

Here is the startup sequence...


include "bs1defs.bas"
Define CONF_WORD = 0x3F3A
Define OSC 24 ' Set clock speed
DEFINE SER2_BITS 8 ' Ser2 number of data bits
SEROUT2 PORTD.0,16468,["INITIALISING",10,13]
Define ADC_BITS 10 ' Set number of bits in result
Define ADC_SAMPLEUS 10 ' Set sampling time in uS
ADCON0.7 = 0 ' ADC clock set to Fosc/2
ADCON0.6 = 0 ' ADC clock set to Fosc/2
ADCON1.6 = 0 ' ADC clock set to Fosc/2

INTCON = 0 ' Disable all interupts
'RCSTA = %10010000 ' Enable serial port and continuous receive
'TXSTA = %00100100 ' Enable transmit and asynchronous mode with BRGH 1
'SPBRG = 20 ' Set baud rate to 57600 with 20mhz clock (BRGH = 1)
ADT1 var word ' Create ADT1 to store curent ad conversion for AD channel 0
ADT1MAX var word ' Create ADT1MAX to store curent ADC0 max val
ADT1MAX = 0
ADT1VAL var word
ADT1VAL = 0
ADV1 var word
VOLT1H var word
VOLT1L var word
C1N var bit
C1N = 0
C1S var bit
C1S = 0
PCOUNTER var byte
PCOUNTER = 0
PTICK var bit
PTICK = 0
RPM var word
RPM = 0
TMR1_VAL var word
TMR1_VAL = 0

TRISE = 255 ' Set PortE to all inputs
TRISD = 0 ' Set PortD to all outputs
TRISC = 0 ' Set PortC to all outputs
TRISB = 0 ' Set PortB to all outputs
TRISA = 255 ' Set PORTA to all input
ADCON1 = 0 ' PORTA is analog

PIE1.0 = 0 ' Disable TMR1 interupt (double check)
T1CON.5 = 1 ' Setup TMR1 timings
T1CON.4 = 1
T1CON.3 = 1
T1CON.1 = 0
T1CON.0 = 1

PORTB = 0
PORTC = 0
PORTD = 0
SEROUT2 PORTD.0,16468,["INITIALISED",10,13]

And on the PC I get "INITIALISING" and nothing else...
So somewhere between INITIALISING and INITIALISED things go wrong...

comwarrior
- 8th July 2009, 16:23
right, i've placed serout2's after every single initialisation line and i discoverd something REALLY confusing...

The serout2's stopped after this line...


TRISE = 255

Why on earth would a TRISE operation affect serout2 command on PORTD?

To prove it, i commented out the TRISE command and everything works perfectly...

so, why does the TRISE cause a problem with SEROUT2

comwarrior
- 8th July 2009, 16:33
and then, when i open the datasheet for the 16f877a and search for trise i find this....



SUMMARY OF REGISTERS ASSOCIATED WITH PORTD
89h TRISE IBF OBF IBOV PSPMODE — PORTE Data Direction Bits


Well, thats helpfull!

TRISE affects PORTD!

That is another for the FAQ's

Thanks for everyones help