Migrate PIC 16f877A to 18F4520 !!!

Please check the frequently asked questions before posting to the forum.
Post Reply
Author
Message
djanib
Posts: 6
Joined: 31 Mar 2006 08:17
Contact:

Migrate PIC 16f877A to 18F4520 !!!

#1 Post by djanib » 17 Mar 2011 23:31

I'm tired about organizing memory gaps for 16f877A and
I decide to migrate my programs on newer PIC 18f4520.

Please, I need few words advice from You, about migrating to this MCU.

First of all,,, is it 100% copmatible with old one 16f877A disregarding on upgrades in 4520
, and what I need to change especially in my working 877A programs to use 4520 instead of them?

Best Regards
Beginner.

User avatar
tihomir.losic
mikroElektronika team
Posts: 2138
Joined: 02 Dec 2009 14:16
Location: Serbia
Contact:

Re: Migrate PIC 16f877A to 18F4520 !!!

#2 Post by tihomir.losic » 18 Mar 2011 10:40

Hello,

please, attach here the piece of code which you need to migrate to PIC18F4520,
and we will give you some useful tips.

Best regards,

Losic Tihomir
mikroElektronika [Support team]

djanib
Posts: 6
Joined: 31 Mar 2006 08:17
Contact:

Re: Migrate PIC 16f877A to 18F4520 !!!

#3 Post by djanib » 18 Mar 2011 16:15

It is not one prog.
I have it a lot. Most of them use 95% of 16f877a but,,,,

I2c bus - Reading eeprom's and RTC chip
UART - Comm with terminal software.
LCD - 4x20 or 2x16 on port D or B or C 4bit BUS
GLCD - KS108 controller
Inputs - KeyMatrix or buttons
Interupt flags - RBIF and TMR0 overflow etc...
...

And other simple functions, used through MikroPascal,,,
Is it compatible for 18f4520 with simple steps (edit projects and change type of MCU)?

I'm asking because 18F452 - 18F4520 and 16F877A, are pin to pin similar, but different size of RAM and ROM and 18F452x have all function built in 16F877A and much more.

Wich kind of problems i can expect?
I'm already order 50pcs 4520, and just want to know, can I simply replace 877A with 4520?

Thx, for your fast responce, and sorry for my bad english.
Beginner.

User avatar
tihomir.losic
mikroElektronika team
Posts: 2138
Joined: 02 Dec 2009 14:16
Location: Serbia
Contact:

Re: Migrate PIC 16f877A to 18F4520 !!!

#4 Post by tihomir.losic » 21 Mar 2011 13:48

Hello,

I am recommending you to open your Support Ticket on:
http://www.mikroe.com/esupport
and contact me. I will help you as much as I can in migrating
from PIC16F877A to PIC18F4520.

Best regards,

Losic Tihomir
mikroElektronika [Support team]

piort
Posts: 1379
Joined: 28 Dec 2005 16:42
Location: Laval,Québec,Canada,Earth... :-)
Contact:

Re: Migrate PIC 16f877A to 18F4520 !!!

#5 Post by piort » 22 Mar 2011 00:58

djanib wrote:Wich kind of problems i can expect?
I'm already order 50pcs 4520, and just want to know, can I simply replace 877A with 4520?
Hi,
For all the use of ME lib, no change to do...
If you use ADC and interrupt, some register name arent the same (ansel vs cmcon) or timer size (8 bit vs 16 bit).

Just change MCU in edit project and try to compile... If some register arent reconize, just chk in datasheet... Very easy and educational to do ;-)

For one of my project now , i change the MCU from a 18f4620 to a 18f46K22 ... Beside isnt the same voltage, the migration was very easy to do... just have to read about crystal setting (32 MHZ to 64), ADC ( not same register name) and some difference in timer ( lot more of ) ... took only 45 minutes to migrate the 1200 lines of code ... now just have to concentrate in why i have change of MCU ;-)

HTH a bit ;-)

djanib
Posts: 6
Joined: 31 Mar 2006 08:17
Contact:

Re: Migrate PIC 16f877A to 18F4520 !!!

#6 Post by djanib » 01 Apr 2011 08:28

Well, here we are!
I got 18F4520 yesterday, then, now we can make couple of tests together.

Some of smaller program work without big changes, and transition is going well, until I began change programs with i2c function inside. And not working on 18F4520. Actualy, some time it happend to work, without explanation.

I'm using my own development board connected to controled power supply. Usualy, MCU, with all peripheral modules, use 50-70mA, but this one (18F4520), if decide to work, use 120-130mA, until I get out power supply and plug it again. Then again use 50mA but allways stuck on i2c.

I'm separate here, one small program module, and this perfectly working with 16F877A. All day i'm reading PDF docs of both MCUs looking for what is going wrong.

Please if some one, with more expirience, know solution, help me and point me, what i'm doing wrong.

For all of you, good people, apologise me for my bad english. I hope it's enough for some simple conversation, like this.

Here is working 16F877A code for Dallas RTC DS1307:

Code: Select all

// Hardware description
//
// PortB - out - LCD 16x2 or 20x4
// PortE.2 - input - SQW 1Hz from RTC (Clock,data,sqw pull up 10k)
// PortC.3..4 - I2C bus
// PortD.7 - out - Led blink, 1Hz

program DS1307;
var
                                   DateStr,TimeStr : string[8];
                                                cnvstr : string[5];
i,GSec,Gmin,Ghr,Gday,Gdt,Gmn,Gye, tGsec : byte;

Procedure Read_time;
begin
   I2C_Start;
   I2C_Wr($D0);
   I2C_Wr($00);
   I2C_Repeated_Start;
   I2C_Wr($D1);
   Gsec :=I2C_Rd(1);
   Gmin :=I2C_Rd(1);
   Ghr  :=I2C_Rd(1);
   Gday :=I2C_Rd(1);
   Gdt  :=I2C_Rd(1);
   Gmn  :=I2C_Rd(1);
   Gye  :=I2C_Rd(0);
   I2C_Stop;

   GSec := Bcd2Dec(Gsec);
   Gmin := Bcd2Dec(Gmin);
   Ghr  := Bcd2Dec(Ghr);
   Gdt  := Bcd2Dec(Gdt);
   Gmn  := Bcd2Dec(Gmn);
   Gye  := Bcd2Dec(Gye);

WordToStrWithZeros(Ghr, CnvStr);
     TimeStr[0]:=CnvStr[3];
     TimeStr[1]:=CnvStr[4];
     TimeStr[2]:=':';
WordToStrWithZeros(Gmin, CnvStr);
     TimeStr[3]:=CnvStr[3];
     TimeStr[4]:=CnvStr[4];
     TimeStr[5]:=':';
WordToStrWithZeros(Gsec, CnvStr);
     TimeStr[6]:=CnvStr[3];
     TimeStr[7]:=CnvStr[4];

WordToStrWithZeros(Gdt, CnvStr);
     DateStr[0]:=CnvStr[3];
     DateStr[1]:=CnvStr[4];
     DateStr[2]:='.';
WordToStrWithZeros(Gmn, CnvStr);
     DateStr[3]:=CnvStr[3];
     DateStr[4]:=CnvStr[4];
     DateStr[5]:='.';
WordToStrWithZeros(Gye, CnvStr);
     DateStr[6]:=CnvStr[3];
     DateStr[7]:=CnvStr[4];
end;

begin
  TRISB := $00;
  TRISD := $00;
  TRISE := $07;
//  ADCON1:= $07; // 16F877A - Disable AD
  ADCON1:= $0F; // 18F4520 - Disable AD
  CMCON := $07; // Disable Comp...
  PORTD:=0;
  I2C_init(100000);

  Lcd_Init(PORTB);
  Lcd_Cmd(LCD_CURSOR_OFF);
  Lcd_Cmd(Lcd_Clear);

  tGsec   :=0;
  while TRUE do
  begin
       PortD.7:=PortE.2;
       if (PortD.7=1) and (tGsec=1) and (I2C_Is_Idle) then
       begin
            tGsec:=0;
            Read_time;
            For i:=0 to 7 do Lcd_chr(1,1+i,TimeStr[i]);
            For i:=0 to 7 do Lcd_chr(2,1+i,DateStr[i]);
       end;
       if PortD.7=0 then tGsec:=1;
  end;
end.
No need extra explanation. It's pretty simple.

Best Regards
Beginner.

gokul
Posts: 4
Joined: 22 May 2013 04:44

Re: Migrate PIC 16f877A to 18F4520 !!!

#7 Post by gokul » 03 Jun 2013 04:27

hello,
i have a program of IR remote control for pic 16f887.
Anybody please help me to convert it so that i can use it in pic 18f4520.



// Lcd pinout settings
sbit LCD_RS at RD2_bit;
sbit LCD_EN at RD3_bit;
sbit LCD_D7 at RD7_bit;
sbit LCD_D6 at RD6_bit;
sbit LCD_D5 at RD5_bit;
sbit LCD_D4 at RD4_bit;

// Pin direction
sbit LCD_RS_Direction at TRISD2_bit;
sbit LCD_EN_Direction at TRISD3_bit;
sbit LCD_D7_Direction at TRISD7_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D4_Direction at TRISd4_bit;
char *text = "ER-4 Remote"; // Define message
unsigned char ir_cmd=0; // Keep Command button from ER-4 Remote
//------------------ Interrupt service routine INT -----------------//
void interrupt()
{
unsigned char i; // Keep counter
if(INTCON.INTF) // Check interrupt flag RB0 (Falling edge)
{
Delay_us(416); // Delay 1/2 of 1 bit timing
// (baudrate 1200 bps)
for(i=0;i<8;i++) // Loop 8 times for keep data from ER-4
{
Delay_us(833); // Delay of 1 bit timing
// (baudrate 1200 bps)
ir_cmd = ir_cmd>>1; // **** bit 1 time
if((PORTB & 0x01)==1) // Get logic @ RB0 = '1'?
ir_cmd = ir_cmd | 0x80; // Inset bit data is '1'
}
Delay_us(833); // Delay of 1 bit timing
// (baudrate 1200 bps)
INTCON.INTF =0; // Clear interrupt flag
}
}
//------------------ Function for get character from Remote ---------//
unsigned char get_remote()
{
unsigned char _key=ir_cmd; // Get character to buffer
ir_cmd=0; // Clear old data
return(_key); // Return character from Remote
}
//------------------ Main Program ----------------------------------//
void main()
{
unsigned char key; // Save Remote Key Press
ANSELH.F4=0; // RB0 ==> Digital IO
OPTION_REG.INTEDG = 0; // INT falling edge
INTCON.INTE =1; // Enable INT/PB0
INTCON.GIE =1; // Enable Global interrupt
Lcd_Init(&PORTD); // Initialize LCD connected to PORTD
Lcd_Cmd(Lcd_CLEAR); // Clear display
Lcd_Cmd(Lcd_CURSOR_OFF); // Turn cursor off
Lcd_Out(1, 1, text); // Print text to LCD,2nd row,1st column
Sound_Init(&PORTC,0);
while(1) // Infinite loop
{
key = get_remote(); // Get Remote
if(key=='a' || key=='A') // Button A press?
{
Lcd_Out(2, 1, "Button A Press "); // Display message Button A press
Sound_Play(100,500);
}
else if(key=='b' || key=='B') // Button B press?
{
Lcd_Out(2, 1, "Button B Press "); // Display message Button B press
Sound_Play(110,500);
}
else if(key=='c' || key=='C') // Button C press?
{
Lcd_Out(2, 1, "Button C Press "); // Display message Button C press
Sound_Play(120,500);
}
else if(key=='d' || key=='D') // Button D press?
{
Lcd_Out(2, 1, "Button D Press "); // Display message Button D press
Sound_Play(130,500);
}
}
}

Post Reply

Return to “mikroPascal FAQ”