View previous topic :: View next topic |
Author |
Message |
beaker404
Joined: 24 Jul 2012 Posts: 163
|
PIC16F877 to PIC18F4550 code migration |
Posted: Tue Aug 09, 2016 1:56 pm |
|
|
Any gotchas on this topic? I have code that runs fine on 16F877, moving to 18F4550 for more ram for improvements. I have stripped the code down to looping and sending a serial stream to hyperterminal. Scope shows waveform for sending, hyperterminal displays nothing. PCB works with '877 programmed with old code, so I think the hardware is ok. changed the crystal from 4MHZ to 20MHZ and 15pf caps for the '4550. I am set for 9600 8 N 1.
Code snippet of start of code:
main loop consists of a continuous while loop using fprintf. No interrupts are being enabled at this time, stripped all that out.
Code: |
/*
*************************************************************
* INCLUDES and CONTROLLER SETTINGS
*************************************************************
*/
#include <18f4550.h>
#DEVICE ADC=10
#include <stdlib.h>
//#fuses XT,NOWDT,NOPROTECT,NOLVP
#FUSES HS,NOWDT,NOPROTECT,PUT,NOBROWNOUT,NOLVP,NODEBUG
#use delay(clock=20000000)
#use rs232(baud=9600,xmit=PIN_C6, rcv=PIN_C7,stream=USB_SERIAL,ERRORS)
#use rs232(baud=4800,xmit=pin_b1,rcv=pin_b0,stream=CTRL) // serial comm w/ down hole power module.
/*
*************************************************************
* DEFINES
*************************************************************
*/
#DEFINE INTS_PER_SEC 15
#DEFINE TICKER_CLICKS 1
#DEFINE U_D_CNTRL PIN_C0
#DEFINE INC_CNTRL PIN_B6
#DEFINE CS_CNTRL PIN_B5
#DEFINE A_CNTRL PIN_B4
#DEFINE B_CNTRL PIN_B3
#DEFINE PWR_CNTRL PIN_B2
#DEFINE I_SENSE PIN_A0
#DEFINE POT_DELAY 1 // one ms delay
#DEFINE S_LENGTH 15
#DEFINE M_LENGTH 25
#DEFINE L_LENGTH 40
#DEFINE ALARM_ZERO PIN_B7 // alarm zero control
#USE FAST_IO(C) |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Tue Aug 09, 2016 2:12 pm |
|
|
Are you intending to use USB?. The fact your are calling the serial 'USB_SERIAL', suggests this. If so, the clock needs a lot more setting up:
Code: |
#FUSES HS, NOWDT, NOPROTECT, PUT, NOBROWNOUT, NOLVP, NODEBUG
#FUSES PLL5, CPUDIV1, NOPBADEN, VREGEN
|
This says to setup the USB to run from the oscillator/5 (it requires 4MHz), and to run the CPU directly from the crystal. It also says to wake with PORT B setup for digital.
Currently your CPU will probably be running at 5MHz (the default CPUDIV fuse is /4), so the serials will be running at 1/4 the clock rate they should be. Also PORT B, defaults set for analog unless you are changing this. Also 'be aware' that C3. 4 & 5 are all reserved for the USB.
The VREGEN fuse enables the USB Vreg. This requires the capacitor on the Vusb pin (the capacitor is required anyway, even if the regulator is not used). |
|
|
beaker404
Joined: 24 Jul 2012 Posts: 163
|
|
Posted: Tue Aug 09, 2016 2:17 pm |
|
|
Not using the USB ports on the chip. Poor naming on my part, there is an adapter that goes from TTL to USB on the board. This all works with '877 code. I suspect it is something to do with the 18F chip setup but I have been unable to find anything on the topic. I am using C6 and C7 for serial as the USE statement states. |
|
|
beaker404
Joined: 24 Jul 2012 Posts: 163
|
|
Posted: Tue Aug 09, 2016 2:19 pm |
|
|
One additional observation, my timing may be off from the crystal, a .5 sec delay seems to be allot longer when observed. Does the CPUDIV still need to be set up? does it influence the hardware UART on pins C6 and C7? |
|
|
beaker404
Joined: 24 Jul 2012 Posts: 163
|
|
Posted: Tue Aug 09, 2016 2:46 pm |
|
|
UPDATE, figured this one out, needed the CPUDIV1 callout in the #FUSE. That got my timing right and the serial started working again. You were right, default was CPUDIV4 causing my clock to be slow and serial to be way off. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9228 Location: Greensville,Ontario
|
|
Posted: Tue Aug 09, 2016 3:26 pm |
|
|
glad to see you got it 'up and running' ! I remember the 'fun' using it, ended up printing the 'possibe clock data paths' diagram and HIGHLIGHTING exactly what I needed. THAT helped me see what bits in which registers had to be set.
One of those 'picture worth 1000 words' moments !!
Jay |
|
|
gjs_rsdi
Joined: 06 Feb 2006 Posts: 468 Location: Bali
|
|
Posted: Tue Aug 09, 2016 10:18 pm |
|
|
Migrating from 16F877 to 18F4520 is strait forward, pin to pin replacement, done it in the past.
If you are not using USB, why the 18F4550?
Best wishes
Joe |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Wed Aug 10, 2016 12:35 am |
|
|
As I said the default for the CPUDIV is /4...
Have to agree wholeheartedly with gjs_rsdi's comment.
Unless you intend to switch to using the PIC USB in the future, using the 4550, costs you three I/O pins & extra power (the USB PLL permanently runs).
Pointless. There are much easier 'straight swap' chips, and some the offer
extra features (like an extra serial etc..).
The 4550, is a poor choice as an straight upgrade to the 877.
For a modern chip, the 18F45K22, has extra serials, more RAM, more ROM, etc. etc.. It is also half the money!...
The 4520, is also more money now than the more modern chip, though is otherwise a good alternative. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9228 Location: Greensville,Ontario
|
|
Posted: Wed Aug 10, 2016 4:55 am |
|
|
Like others, I did the 877 to 4550 thing, when I found out the USB drivers used up 1/3 code space, re thought the PIC choice. I'm now using the 18F46K22 for 99% of projects. Yes, overkill for 99% of them, but 2 HW UARTS, lots of mem and I/O pins! For USB I use a $2 TTL<>USB module.
When you consider PIC4550<>USB, you need to price out connector, caps, resistors, pcb space, etc. That will chew up a good part of $2 ! Then there's the driver space and PCb layout (D+, D- <> D-,D+ !) That $2 module frees up code space, has a couple LEDs, properly connected AND it works !
While there may be a 'better' PIC today, unless you're ordering 10,000 pieces, the additional cost per PIC isn't great compared to R&D costs ( seldom considered) or TIME wasted on 'internal USB issues'.
Jay |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Wed Aug 10, 2016 8:04 am |
|
|
i want to strongly second jay- 18F46K22 is your friend and so easily back compatible to 4620, 4525 etc etc.
my most recent design was K22 but built with 4525 in production. Just avoid the higher numbered timers for back compatibility.....
(only used the 4525 because we had hundreds unused in old stock .)
4550 is antique history IMHO
Last edited by asmboy on Wed Aug 10, 2016 3:34 pm; edited 1 time in total |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Wed Aug 10, 2016 8:50 am |
|
|
Just to triple the comment, the 45K22 I mentioned, is the same chip but only half the ROM. I should have said 45K22/46K22. |
|
|
gjs_rsdi
Joined: 06 Feb 2006 Posts: 468 Location: Bali
|
|
Posted: Thu Aug 11, 2016 6:33 am |
|
|
As all the veterans have stated, 18F26K22 & 18F46K22 is the way to go.
Still for someone who works with 16F877 will be much easier to move to 18F4520 or 18F45K20 and then to learn how to work with 18F46K22.
I had not small difficulties to migrate from 18F25K20 to 18F26K22
Best wishes
Joe |
|
|
dan king
Joined: 22 Sep 2003 Posts: 119
|
|
Posted: Thu Aug 11, 2016 6:36 am |
|
|
not to hijack the thread but ....
is anyone aware of a good breakout board with the 46k22 mounted? I have been using the 18f2553 because the Sparkfun breakout board with USB is so convenient. I would consider the 46k22 using an ftdi if a nice breakout was available.
Thanks
Dan |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
|
dan king
Joined: 22 Sep 2003 Posts: 119
|
|
Posted: Thu Aug 11, 2016 7:01 am |
|
|
thanks for the links, I'll check it out.
Dan |
|
|
|