View previous topic :: View next topic |
Author |
Message |
Peter Mayhew
Joined: 31 Dec 2003 Posts: 9 Location: United Kingdom Devon
|
Toggle Port B 16f877 |
Posted: Mon May 11, 2009 4:17 am |
|
|
I have been writing code in ASM which all works fine, and now writing the same code using CCS, however I'm having trouble with the basics of simply toggling portb output pins.
When I monitor the special function registers portb in debug they do toggle, however when i blow the pic, and probe with the scope, there is no toggling.
Something basic i'm doing wrong, help please?
Code: | #include <16F877.h>
#use delay(clock=12000000)
Main ()
{
set_tris_b(0xff);
while (1)
{
output_b(0x00);
output_b(0xff);
}
} |
|
|
|
horde_fuego
Joined: 31 Mar 2009 Posts: 11
|
Re: Toggle Port B 16f877 |
Posted: Mon May 11, 2009 6:15 am |
|
|
Peter Mayhew wrote: | I have been writing code in ASM which all works fine, and now writing the same code using CCS, however I'm having trouble with the basics of simply toggling portb output pins.
When I monitor the special function registers portb in debug they do toggle, however when i blow the pic, and probe with the scope, there is no toggling.
Something basic i'm doing wrong, help please?
#include <16F877.h>
#use delay(clock=12000000)
Main ()
{
set_tris_b(0xff);
while (1)
{
output_b(0x00);
output_b(0xff);
}
} |
You need to insert a delay so that you will see the toggling effect just like this
Code: | output_b(0x00);
delay_ms(500);
output_b(0xff);
delay_ms(500); |
hope it helps |
|
|
andyfraser
Joined: 04 May 2004 Posts: 47 Location: UK
|
Toggle Port B 16f877 |
Posted: Mon May 11, 2009 6:19 am |
|
|
You don't need a delay unless you have really, really slow scope !
You are setting all the pins to INPUT with set_tris_b(0xFF);
HTH
Andy |
|
|
Guest
|
|
Posted: Mon May 11, 2009 8:05 am |
|
|
Hi,
Which is why it's a good idea to just let the compiler handle setting the TRIS settings, especially for a beginner. The only time you need to explicitly specify the TRIS settings yourself is when you use FAST_IO.
Chas |
|
|
Peter Mayhew
Joined: 31 Dec 2003 Posts: 9 Location: United Kingdom Devon
|
|
Posted: Mon May 11, 2009 2:51 pm |
|
|
Many thanks for all your advice.
Yes my scope is fast enough, thanks for pointing that out.
You are correct, I did have tris_b set to 0xff, which was a silly mistake. That happened as I was originally reading port B and outputting to port A, but when I found the program wasn't working as expected. just toggled port b but forgot to update tris_b to 0x00. Well spotted, thankyou.
I changed tris_b to an output 0x00. However this didn't solve the problem.
I went through a couple of example files and found the code
#fuses HS,NOWDT,NOPROTECT,NOLVP
So I added #fuses HS and the program works.
Complete code
Code: | #include <16F877.h>
#fuses HS
#use delay(clock=12000000)
Main ()
{
set_tris_b(0xff);
while (1)
{
output_b(0x00);
output_b(0xff);
}
} |
Purely out of interest, I experimented with all four fuse options being LP, XT, HS, RC. Only RC caused this problem. So I take it that CCS defaulted to RC mode since I didn't define it. Many thanks again for all your time. |
|
|
bungee-
Joined: 27 Jun 2007 Posts: 206
|
|
Posted: Mon May 11, 2009 3:22 pm |
|
|
Peter use the project wizard for starting a new project. One of my favorite function is setting correct fuses, so you allways know how the PIC is set. |
|
|
Peter Mayhew
Joined: 31 Dec 2003 Posts: 9 Location: United Kingdom Devon
|
|
Posted: Mon May 11, 2009 3:39 pm |
|
|
Funny you should mention that. I did use the menu Project --> Project Wizard. I just checked it again, and can't see any mention of the fuses??? I can only find it in menu configure --> configuration bits? Maybe there is something I missed?? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon May 11, 2009 3:42 pm |
|
|
The command line compilers used with MPLAB don't have any
of the CCS Wizards. |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1934 Location: Norman, OK
|
|
Posted: Mon May 11, 2009 3:51 pm |
|
|
Peter,
If you had used the "PIC Wizard" that comes with the CCS IDE, under General Category there is an Options Tab. On that tab you will find the Fuses panel on the bottom half of the tab page right below where you set the Oscillator Frequency. This section sets the fuses automatically according to the speed and type of oscillator along with the other options desired. That's the section I believe he was referring to.
Also, Charles is right, you really should avoid using the TRIS() statements unless you absolutely have to. The compiler will set the port bits automatically. |
|
|
Peter Mayhew
Joined: 31 Dec 2003 Posts: 9 Location: United Kingdom Devon
|
|
Posted: Mon May 11, 2009 4:36 pm |
|
|
I don't know about CCS IDE. My copy of CCS is quite old, which I bought back in 2003. I don't think they had CCS IDE back then?, or you had to avoid using TRIS. All I know is its PCM Software. |
|
|
|