View previous topic :: View next topic |
Author |
Message |
small_chick
Joined: 17 Jul 2012 Posts: 53
|
IO problems ? |
Posted: Fri Dec 21, 2012 8:15 pm |
|
|
I program on dsPIC30F4011 and my code as follow:
Code: |
#include <30F4011.h>
#fuses XT_PLL4,NOWDT
#word TRISB = 0x02C6
#word PORTB = 0x02C8
#word LATB = 0x02CA
#bit TRISB8 = 0x02C6.8
#bit TRISB7 = 0x02C6.7
#bit TRISB6 = 0x02C6.6
#bit TRISB5 = 0x02C6.5
#bit TRISB4 = 0x02C6.4
#bit TRISB3 = 0x02C6.3
#bit TRISB2 = 0x02C6.2
#bit TRISB1 = 0x02C6.1
#bit TRISB0 = 0x02C6.0
#bit RB0 = 0x02C8.0
#bit RB1 = 0x02C8.1
#bit RB2 = 0x02C8.2
#bit RB3 = 0x02C8.3
#bit RB4 = 0x02C8.4
#bit RB5 = 0x02C8.5
#bit RB6 = 0x02C8.6
#bit RB7 = 0x02C8.7
#bit RB8 = 0x02C8.8
void main()
{
ADPCFG = 0xFFFF;
TRISB0 = 0;
TRISB1 = 0;
TRISB2 = 0;
TRISB3 = 0;
TRISB4 = 0;
TRISB5 = 0;
TRISB6 = 0;
TRISB7 = 0;
TRISB8 = 0;
RB0 =1;
RB1 =1;
RB2 =1;
RB3 =1;
RB4 =1;
RB5 =1;
RB5 =1;
RB6 =1;
RB7 =1;
RB8 =1;
}
|
If I code as above, some pins on PORTB do not output high logic level, but if I change as follow, they work well:
Code: |
void main()
{
ADPCFG = 0xFFFF;
TRISB0 = 0;
RB0 =1;
TRISB1 = 0;
RB1 =1;
TRISB2 = 0;
RB2 =1;
TRISB3 = 0;
RB3 =1;
TRISB4 = 0;
RB4 =1;
TRISB5 = 0;
RB5 =1;
TRISB6 = 0;
RB6 =1;
TRISB7 = 0;
RB7 =1;
TRISB8 = 0;
RB8 =1;
}
|
I mean they only work well if before RBx =1, there's a line TRISBx = 0.
However, I'd like to use the first case, not the second one. How can I do this ? ^~^ |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Dec 21, 2012 9:20 pm |
|
|
Change it so you're writing to LATB instead of PORTB. |
|
|
small_chick
Joined: 17 Jul 2012 Posts: 53
|
|
Posted: Fri Dec 21, 2012 10:29 pm |
|
|
PCM programmer wrote: | Change it so you're writing to LATB instead of PORTB. |
You mean I should use LATBx instead of RBx ? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Sat Dec 22, 2012 12:56 am |
|
|
Yes.
Do a search on PIC RMW problem, and it will explain what is happening.
Writing to the latch avoids this.
Best Wishes |
|
|
asmallri
Joined: 12 Aug 2004 Posts: 1634 Location: Perth, Australia
|
|
Posted: Sat Dec 22, 2012 2:32 am |
|
|
PCM programmer wrote: | Change it so you're writing to LATB instead of PORTB. |
You also need to specify #use fast_io when you are handling I/O direction otherwise the compiler can change it. _________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
|
small_chick
Joined: 17 Jul 2012 Posts: 53
|
|
Posted: Sat Dec 22, 2012 2:46 am |
|
|
thanks everybody!
now. it's well - done! i think i've got a big mistake about LATx and PORTx ! |
|
|
|