|
|
View previous topic :: View next topic |
Author |
Message |
fiasgardone
Joined: 19 Jan 2010 Posts: 71
|
Why not run the same code on two PICS |
Posted: Thu Oct 15, 2015 4:19 am |
|
|
Hi!
I have this code and want to experiment with the pic 16f887 and pic 18F4550!
With pic 16f887 it works well, but the pic 18F4550 does not work, where the error is?!
I appreciate all the help
works well.
Code: |
#include <16F887.h>
#fuses XT,NOPROTECT,NOWDT,NOBROWNOUT,PUT,NOLVP
#use delay(clock=4000000)
#byte portb=6
#byte portc=7
#define SW portc,0
int cont;
byte const ario[4]={0b1100,0b0110,0b0011,0b1001};
byte const antiario[4]={0b1001,0b0011,0b0110,0b1100};
void main()
{
set_tris_c(0b11111111);
set_tris_b(0b00000000);
while(true)
{
if(bit_test(SW))
{
cont=0;
while((cont<4)&&(bit_test(SW)))
{
portb=(ario[cont]);
delay_ms(100);
cont++;
}
}
else
{
cont=0;
while((cont<4)&&(!bit_test(SW)))
{
portb =(antiario[cont]);
delay_ms(100);
cont++;
}
}
}
}
|
does not work
Code: |
#include <18F4550.h>
#use delay( clock=20000000)
#fuses HS,NOPROTECT,NOWDT,NOBROWNOUT,PUT,NOLVP
#byte portb=6
#byte portc=7
#define SW portc,0
int cont;
byte const ario[4]={0b1100,0b0110,0b0011,0b1001};
byte const antiario[4]={0b1001,0b0011,0b0110,0b1100};
void main()
{
set_tris_c(0b11111111);
set_tris_b(0b00000000);
while(true)
{
if(bit_test(SW))
{
cont=0;
while((cont<4)&&(bit_test(SW)))
{
portb=(ario[cont]);
delay_ms(100);
cont++;
}
}
else
{
cont=0;
while((cont<4)&&(!bit_test(SW)))
{
portb =(antiario[cont]);
delay_ms(100);
cont++;
}
}
}
} |
Last edited by fiasgardone on Thu Oct 15, 2015 6:35 am; edited 1 time in total |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9246 Location: Greensville,Ontario
|
|
Posted: Thu Oct 15, 2015 4:45 am |
|
|
Simply put the PICs are NOT the same ! Why do you think the code will run?
Chevy rims do not fit on Ford cars.
It's down to basic hardware. Download the datasheets and look at the 'memory map','register map','fuses', etc. and you'll soon see why the code doesn't run.
Also you should (though not necessary) set both 'clocks' at the same speed when doing comparison testing. As shown the 4550 is running 5X faster than the 887, so at the very least you'd need to change scope settings to see what's happening.
Jay |
|
|
fiasgardone
Joined: 19 Jan 2010 Posts: 71
|
|
Posted: Thu Oct 15, 2015 6:34 am |
|
|
Hi!! thanks for help
I do not yet have much experience with pics 18F,
almost I worked with 16F.
so my question to ask for help! I've seen the datasheet of the pic and still did not get that pic 18f comp if it works.
Thank you anyway |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9246 Location: Greensville,Ontario
|
|
Posted: Thu Oct 15, 2015 6:42 am |
|
|
re:
#byte portb=6
This tells the PIC that I/O port B is located at memory address #6 for the PIC16F887
If you look at the 4550 datasheet, in the 'memory organization' chapter, table 5-1 shows that I/O port B is not at address 6, rather F8Ax.
So your program isn't accessing the I/O port instead it's using a RAM location.
This is why it's important to look at the 'memory maps' of any micro.
Jay |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Thu Oct 15, 2015 8:14 am |
|
|
This is also why the compiler now supports using names for such registers. So:
Code: |
#byte portb=getenv("SFR:PORTB")
#byte portc=getenv("SFR:PORTC")
|
Will automatically 'look up' the special function registers 'PORTB', and 'PORTC' in the environment data held for the chips, and fill the values in. |
|
|
fiasgardone
Joined: 19 Jan 2010 Posts: 71
|
|
Posted: Thu Oct 15, 2015 9:05 am |
|
|
Hi!! Thank you for your help!
I really had not noticed this detail, I have a doubt you said that the address I / O PORTB was F8a of 4550, but from what I saw in datasheet is F81h! or am I doing confusion!,plus any help is welcome
I did the tests with the code below and now works well!
Code: |
#include <18F4550.h>
#use delay( clock=20000000)
#fuses HS,NOPROTECT,NOWDT,NOBROWNOUT,PUT,NOLVP
#byte portb=3969
#byte portc=3970
#define SW portc,0
int cont;
byte const ario[4]={0b1100,0b0110,0b0011,0b1001};
byte const antiario[4]={0b1001,0b0011,0b0110,0b1100};
void main()
{
set_tris_c(0b11111111);
set_tris_b(0b00000000);
while(true)
{
if(bit_test(SW))
{
cont=0;
while((cont<4)&&(bit_test(SW)))
{
portb=(ario[cont]);
delay_ms(100);
cont++;
}
}
else
{
cont=0;
while((cont<4)&&(!bit_test(SW)))
{
portb =(antiario[cont]);
delay_ms(100);
cont++;
}
}
}
}
|
|
|
|
fiasgardone
Joined: 19 Jan 2010 Posts: 71
|
|
Posted: Thu Oct 15, 2015 9:09 am |
|
|
Ttelmah wrote: | This is also why the compiler now supports using names for such registers. So:
Code: |
#byte portb=getenv("SFR:PORTB")
#byte portc=getenv("SFR:PORTC")
|
Will automatically 'look up' the special function registers 'PORTB', and 'PORTC' in the environment data held for the chips, and fill the values in. |
Hi!
I also tested the code you posted and also works
Unknown to me until now.
thanks for helping.
Code: |
#include <18F4550.h>
#use delay( clock=20000000)
#fuses HS,NOPROTECT,NOWDT,NOBROWNOUT,PUT,NOLVP
#byte portb=getenv("SFR:PORTB")
#byte portc=getenv("SFR:PORTC")
#define SW portc,0
int cont;
byte const ario[4]={0b1100,0b0110,0b0011,0b1001};
byte const antiario[4]={0b1001,0b0011,0b0110,0b1100};
void main()
{
set_tris_c(0b11111111);
set_tris_b(0b00000000);
while(true)
{
if(bit_test(SW))
{
cont=0;
while((cont<4)&&(bit_test(SW)))
{
portb=(ario[cont]);
delay_ms(100);
cont++;
}
}
else
{
cont=0;
while((cont<4)&&(!bit_test(SW)))
{
portb =(antiario[cont]);
delay_ms(100);
cont++;
}
}
}
}
|
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9246 Location: Greensville,Ontario
|
|
Posted: Thu Oct 15, 2015 10:00 am |
|
|
just a comment...
What Mr. T says....
#byte portb=getenv("SFR:PORTB")
is the 'best' way to handle SFRs as he says the compiler will generate the correct addresses.
your way...
#byte portb=3969
is awkward since the datasheet refers to address in HEX not DECIMAL, so it would be better to use F81 the same as the datasheet.
Also it's better to use descriptive names when possible, especially when asking for help.
I think 'SW' means a SWitch on port c but...SW also means SoftWare, SouthWest, etc. While you know NOW what it means, 3 days, 3 months from now you'll ask yourself ,'hmmmm... what does SW mean'?
Jay |
|
|
fiasgardone
Joined: 19 Jan 2010 Posts: 71
|
|
Posted: Thu Oct 15, 2015 11:13 am |
|
|
temtronic wrote: |
your way...
#byte portb=3969
is awkward since the datasheet refers to address in HEX not DECIMAL, so it would be better to use F81 the same as the datasheet.
Also it's better to use descriptive names when possible, especially when asking for help.
I think 'SW' means a SWitch on port c but...SW also means SoftWare, SouthWest, etc. While you know NOW what it means, 3 days, 3 months from now you'll ask yourself ,'hmmmm... what does SW mean'?
Jay |
Ok, I will pay attention to these his notes!
Yes, PORTC is placed a button called SW, was by chance that I chose this setting.
It really is more elegant writing the code in HEX than in decimal, my mistake.
Thanks for the help |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|