PDA

View Full Version : Code running 19 times slower than intended



jellis00
- 9th April 2010, 03:10
I have taken the attached code from a development board (EASYPIC6) to a prototype board and it is running 38 times slower in the prototype board than the development board. The prototype uses the same code, the same MCU (18F4550) and the same XTAL (8 MHz) so don't understand why it would run 38 times slower. How do I know it is 38 times slower?...I have some blinking LEDs at the beginning of each routine for test purposes and a heartbeat LED in the main loop that are coded to be on for only 0.5 secs (which they do in the development board), but they are on for 19 secs in the prototype. Hence 19/0.5 = 38 times slower.

Can anyone look at this code and give me any ideas why this code would run 38 times slower in the prototype? I checked to make sure the 18F2550.inc file had the configs commented out, since they are set by this code.

Post Note: Adding the code made this posting exceed the 10,000 characer limit. I will try to post the code in another posting.

ScaleRobotics
- 9th April 2010, 03:16
I seem to be missing the attachment.

jellis00
- 9th April 2010, 03:16
I will try to post the code in another posting.



Pause 10 ' This statement is placed at beginning of code for ICD use

'Configuration Fuses
ASM ; 18F2550/4550, 8mhz crystal
__CONFIG _CONFIG1L, _PLLDIV_2_1L & _CPUDIV_OSC4_PLL6_1L & _USBDIV_2_1L
__CONFIG _CONFIG1H, _FOSC_HSPLL_HS_1H
__CONFIG _CONFIG2L, _PWRT_ON_2L & _BOR_OFF_2L & _VREGEN_ON_2L
__CONFIG _CONFIG2H, _WDT_OFF_2H & _WDTPS_512_2H
__CONFIG _CONFIG3H, _PBADEN_OFF_3H ; PortB resets as digital
__CONFIG _CONFIG4L, _LVP_OFF_4L & _XINST_OFF_4L
ENDASM

' Define Hardware / Set Registers
DEFINE OSC 16
DEFINE I2C_SLOW 1 ' Set i2c to the standard speed
DEFINE I2C_HOLD 1 ' Enable recieving i2c device to pause communication
Include "Modedefs.Bas"
INCLUDE "ALLDIGITAL.pbp"
INCLUDE "DT_INTS-18.bas"
INCLUDE "ReEnterPBP-18.bas"

TRISA = 0 ' PortA output connections used for LCD interface
TRISB = %00001100 ' RB2 & RB3 set as RTC Alarm1 & Alarm2 inputs
' PORTB.2 is an input from switch grounding
' PORTB.4 is used during test as a Green LED heartbeat
' and as a USB connected indicator during ops
TRISC.1 = 0 ' PORTC.1 used during test as a RED LED output
TRISC.2 = 0 ' PortC.2 is used for the LCD R/W connection

'Declare Variables, Aliases & Constants
day_cnt VAR BYTE ' Counter for day number during monthly epoch
ext_pwr VAR PORTC.0 ' Alias to control power to Ultrasonic sensors
i VAR Byte ' Index in FOR-NEXT loops
J VAR BYTE ' Index to EEPROM address for avg. range store
LED_RED VAr PortC.1 ' Red LED used to indicate battery is low
LED_GRN VAR PortB.4 ' Green LED used to indicate USB connected
rng0 VAR w0.Byte0 ' LSB of range measurement when right justified
rng1 VAR w0.byte1 ' MSB of range measurement when right justified
SCL VAR PORTB.1 ' I2C clock pin
SDA VAR PORTB.0 ' I2C data pin
Spare_1 VAR PORTB.7 ' PGD for ICSP & Spare I/O for normal ops
Spare_2 VAR PORTB.6 ' PGC for ICSP & Spare I/O for normal ops
w0 var word ' W0 is the word value of the range data

' Constants
RTCdevice CON $D0 ' Device write address = %11010000 for RTC
srfdevice CON $E0 ' Device address for SRF02 ultrasonic RangeFinder
Vthr CON 46 ' Threshold (3.3v) triggers battery low voltage

'SETUP FOR USING DS1337 Real Time Clock

'Initialize LEDs to off
LOW LED_GRN
LOW LED_RED
GOSUB InitializeDisplay ' Initialize LCD before starting mainloop
GOSUB SetTimeAndDate ' Set RTC time/date before starting mainloop

' START SUBROUTINES
GOTO JumpPoint ' Jump over all subroutines

InitializeDisplay:
' Blink LED_GRN twice to indicate entered IntializeDisplay
For i = 0 to 1
HIGH LED_GRN
Pause 500
LOW LED_GRN
PAUSE 500
Next
' Saving characters by eliminating in post
Return

SetTimeAndDate: ' Subroutine to set current time, date and alarm

' Blink LED_GRN 3X times to indicate entered SetTimeAndDate
For i = 0 to 2
HIGH LED_GRN
Pause 500
LOW LED_GRN
PAUSE 500
Next
' Saving characters by eliminating in post
Return
' END OF SUBROUTINES

JumpPoint:

'Initialize epoch indexes on powerup/reset
day_cnt = 0
WRITE 9, day_cnt ' Store new epoch initialized day_count
Pause 20
J = 0 ' Initialize index at EEPROM start address
WRITE 10,J ' Store new epoch initialized J index
Pause 20

' SETUP FOR INTERRUPTS
ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
;INT_Handler USB_Handler
INT_Handler INT2_INT, _Range, PBP, yes
endm
INT_CREATE ; Creates the interrupt processor
ENDASM

' Per DT, DT_INTS already takes care of setting INTCON and RCON registers
' but this doesn't work unles INTCON2 set per below:
INTCON2 = %10000000 ' Set INT2 for falling edge (Bit4-low)
' on RTC's interrupt.
' Disable all PortB pull-ups (Bit7-high)
RTC_INT_FLG VAR INTCON3.1 'INT2 interrupt flag from RTC..DT_INTS handles

@ INT_ENABLE INT2_INT ; enable external (INT) interrupts
LOW ext_pwr ' Turn off power to SRF02 until I2C bus is needed again

' Begin Main Program Loop
mainloop:
PAUSE 100 ' Without this Pause, there's nothing to keep the WDT clear.
' HIGH, LOW & GOTO don't generate CLRWDT instuctions.
HIGH LED_GRN ' Blink LED_GRN during main loop

' Place code here to put MCU and Ultrasonic Ranger in SLEEP mode.
' while waiting for clock interrupt to wakeup from SLEEP mode.

' Awaiting interrupt from RTC

PAUSE 1000
LOW LED_GRN ' Turn off LED_GRN as Heartbeat during mainloop
Pause 900

Goto mainloop ' Do it forever

' Begin Interrupt Handler
Range:
' Saving characters by eliminating in post
' Blink LED_RED 3X to indicate entered Range ISR
For i = 0 to 2
HIGH LED_RED
Pause 500
LOW LED_RED
PAUSE 500
Next
@ INT_RETURN

'End of Interrupt Handler

mackrackit
- 9th April 2010, 05:01
If you are using the same code and the same PICŪ it has to be a hardware problem.

Check to OSC to make sure it is OK. Maybe wrong/bad capacitor??

jellis00
- 9th April 2010, 19:27
If you are using the same code and the same PICŪ it has to be a hardware problem.

Check to OSC to make sure it is OK. Maybe wrong/bad capacitor??

Thanks for this tip! I put an Oscilloscope on to Pin 9 (OSC1) of the 18F2550MCU and it appears that the crystal is not oscillating. Can't figure out why not.
I am using an 8 MHz crystal from Abracom (their P/n ABL-8-B2) with two 27 pf capacitors as C1 and C1. This crystal specs a CL of 18 pf. By my calculation I am close to that if you assume a PCB stray capacitance of Cs=4 pf (my terminatons to crystal and capacitors from the MCU are very short on the PCB), the calculation goes like this:
CL = Cs +(c1 X c2)/(c1+c2). When usining C1=C2=27pf in this calculation CL comes out to 17.5 pf which is very close to the spec'd 18 pf and I can't believe this difference would prevent the OSC from working.
I am operating the MCU with a regulated supply voltage of 3.1 volts, so the drive on the crystal should be sufficient to cause oscillation.
One thing I really don't understand is why the MCU is still operating (granted at 38 times slower than expected) when the OSC is not even oscillating.
Can anyone give me some hints or advice how to resolve this??

mackrackit
- 9th April 2010, 20:45
I can not help much on the crystal as I do not use them. For good or bad I use those three pin resonators, that way I do not have to mess with capacitors.

I would guess the chip has defaulted to 31 kHz, that is what it is running at.

You could use the internal OSC. This chip has an 8 MHz built in. May or may not be good enough for serial...

jellis00
- 14th April 2010, 18:26
I finally found the problem thanks to rteo who advised that the 18F2550/4550 data sheet shows in one place that the external osc won't function with a supply voltage to the MCU of less than 4.2 volts. I had been running a supply voltage of 3.3 volts as a battery power conservation measure. I then experimented and discovered, rteo is partially correct.....my 18F2550 external osc circuit won't oscillate reliably until the supply voltage is at least 3.6 volts.
This discovery is going to make me change the design for this product to a 4.2 volt supply rail.

mackrackit
- 14th April 2010, 18:40
Maybe your crystal will not run at a voltage lower than 3.6, but the 4550 will operate with an external OSC of 4MHz at 3 volts. I typically run this chip at 3.3 with SD cards.

If you want to save power and run at a lower voltage get a three pin resonator at 4MHz.

falingtrea
- 14th April 2010, 19:12
If you want to be able to run at lower than 4.2 volts you should probably be using the LF version of the part. Looks like the LF version will work from 2V to 5V with slower clocks the lower the voltage goes. Some F parts may work lower than the 4.2V spec, but there may be some that flat out won't.

Bruce
- 14th April 2010, 22:40
Dave,

Are you sure you're not using the LF version running at 3V?

mackrackit
- 14th April 2010, 23:27
Yes, I just doubled checked, again.
http://www.picbasic.co.uk/forum/attachment.php?attachmentid=4193&stc=1&d=1271283398
Do not laugh at the messy board, everything on it runs at 3.3 volt. Another interesting thing is this chip can also be programmed at 3.3. I have the PicKit2 feeding a LM317. The PicKit is fooled into thinking it is running at 5 volts.
Along with the SD card there is a MAX6675 and a DS1337. Working on a PYRO LOGGER.
http://www.picbasic.co.uk/forum/attachment.php?attachmentid=4194&stc=1&d=1271283408
And the data sheet is a bit miss leading?
I take the extended range (LF) as going down to 2 volts.
I guess I could have a batch that was labeled incorrectly?
http://www.picbasic.co.uk/forum/attachment.php?attachmentid=4192&stc=1&d=1271283365

Bruce
- 14th April 2010, 23:48
Are you running it at 48MHz @ 3V?

mackrackit
- 14th April 2010, 23:50
Yup, with a 4MHz external purchased from you :)


DEFINE OSC 48
@ __CONFIG _CONFIG1L, _PLLDIV_1_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
@ __CONFIG _CONFIG1H, _FOSC_HSPLL_HS_1H
@ __CONFIG _CONFIG2H, _WDT_OFF_2H & _WDTPS_512_2H
@ __CONFIG _CONFIG3H, _PBADEN_OFF_3H & _MCLRE_OFF_3H
@ __CONFIG _CONFIG4L, _LVP_OFF_4L & _ICPRT_OFF_4L &_XINST_OFF_4L

Bruce
- 15th April 2010, 00:05
Well that's interesting. So much for those worthless voltage-to-frequency graphs in those darn data sheets ehh...;o)

If it works - it works - can't argue with that.

rsocor01
- 15th April 2010, 12:20
I'm also running my PIC18F4550 with a 4MHz external oscillator at 3.0V. I use the HSPLL setting for the oscillator. The define osc line is


DEFINE OSC 48

and it seems to be working fine. I'm feeding data to a color GLCD and it does it very quickly.

Robert