![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
Guest_X44 Guest
|
Use of 16F84...Helppppp |
Posted: Mon Apr 19, 2004 7:02 pm |
|
|
This is a very simple one:
I'm using a 16F84. I've never used this device for srial comms - only a 877, so I'm not really that familiar.
The intention is to monitor the input on pin B6 and, if low, send some values serially.
What I noticed when simulating within MPLAB is that the pin A0 is set HIGH as soon as it enters main().
the code below is the full code. I'm probably missing something here.
Also, I wonder if my simple circuit will work, at all.
The switch is connected to pin 12, tied to Vcc through a 10K resistor.
RA0 is set as the serial transmit line
RA1 is configured for serial receive but not used
A resonator is connected between pins 15 and 16
A +5V regulator's input is taken from the serial com port (serial com port pins 4, 7, and 8 tied together). Hence, no external power supply is used.
No max232 or equivalent is used at all. The 16f84's pin 18 (RA1) is directly connected to the PC's serial com port's pin2 (receive pin).
Pleeeeeeese help me on this.
Code: |
#include <16f84.h>
#use delay(clock=10000000)
#use rs232 (baud=9600, xmit=PIN_A1, rcv=PIN_A0)
main()
{
while(1)
{
if(input(PIN_B6))
{
putc(0xB3); //id
putc(0x00); //state
}
}
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
Guest_X44 Guest
|
|
Posted: Mon Apr 19, 2004 7:57 pm |
|
|
By the way, I'm using CCS Ver. 3.173. I believe 16f84 is supported on this version. |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Apr 19, 2004 8:59 pm |
|
|
Quote: | The 16f84's pin 18 (RA1) is directly connected to the PC's serial com port's pin2 (receive pin).
#use rs232 (baud=9600, xmit=PIN_A1, rcv=PIN_A0) |
If you have no MAX232 chip, then you need to use the INVERT
parameter with your #use RS232 statement. Also, on the
PIC's RS232 receive pin (RA0), you will need a 22K series resistor.
Do you have that ? |
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
|
Posted: Mon Apr 19, 2004 11:44 pm |
|
|
Do I need a 22K resistor in series with the receive pin even though I will not use it? Can I just leave out the rcv=PIN_A0 parameter in the #use rs232 statement?
#use rs232 (baud=9600, xmit=PIN_A1, INVERT)
What's the INVERT for?
What about the problem of the xmt pin being set immediately as it enters main() when I do a simulation within MPLAB?
Do I have to add any more code for my intended task of simply reading the state of a single switch?
I'd appreciate to see someone in this forum help me out on my code and circuit. |
|
![](templates/subSilver/images/spacer.gif) |
Guest_X44 Guest
|
|
Posted: Mon Apr 19, 2004 11:48 pm |
|
|
Oops, sorry. The previous reply (query) was from me, Guest_X44. |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Apr 20, 2004 12:47 am |
|
|
Quote: | Do I need a 22K resistor in series with the receive pin even though I will not use it? Can I just leave out the rcv=PIN_A0 parameter in the #use rs232 statement?
#use rs232 (baud=9600, xmit=PIN_A1, INVERT) |
No, you don't need the resistor if you're not using the RCV pin.
Yes, you can leave out the rcv=PIN_A0, as you've done.
Quote: | What's the INVERT for? |
The CCS code normally expects you to use a MAX232 type chip.
That chip does an inversion to the signal. So if you don't use
the MAX232, you have to tell the compiler to invert the data before
transmitting it (and also when receiving it).
Quote: | What about the problem of the xmt pin being set immediately as it enters main() when I do a simulation within MPLAB? |
When the RS232 signals come out of the PIC in normal mode,
a high level is the idle state. So the CCS code was acting
properly. This caused you a problem because you had no
MAX232 chip there to invert it. It was seen by your PC as
a constant start bit. But now, if you add the INVERT parameter
it will fix the problem. You should see the xmit pin go low upon
start-up.
Quote: | Do I have to add any more code for my intended task of simply reading the state of a single switch? |
Well, normally you put a pull-up resistor on the PIC pin which is
used to read the switch. (Or you enable Port B pullups).
Then you connect one side of the switch to Ground. The switch
is normally-open, so the idle state will be read as a high level.
When the switch is pressed you read a low level, and then perform
some action. There are other aspects to it, such as debouncing
the switch, or looking for a transition (an edge) instead of a level,
but perhaps it's best if you leave those for later.
A 10K pull-up to VDD on PIN_B6 should work fine -- or use the CCS
function: port_b_pullups(TRUE);
Quote: | I'd appreciate to see someone in this forum help me out on my code and circuit. |
Perhaps someone can point you to a schematic, or give you some other
tips. |
|
![](templates/subSilver/images/spacer.gif) |
Haplo
Joined: 06 Sep 2003 Posts: 659 Location: Sydney, Australia
|
|
Posted: Tue Apr 20, 2004 1:47 am |
|
|
Regarding that 22k, what is the minimum recommended value for that resistor and how do you calculate it? |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Apr 20, 2004 12:01 pm |
|
|
Quote: | Regarding that 22k, what is the minimum recommended value
for that resistor and how do you calculate it? |
I didn't calculate it. I don't normally do it, but I know that PIC
users do it a lot, so I did a quick web search and got the value
off the net. Based on the data sheet, in the Absolute Maximum
Ratings section, you can supposedly put 20ma through those
diodes. That seems excessive. Let's say you have a swing
of +/- 10v on your RS-232 input. 10v/22K = about .5 ma.
They love to discuss this issue on Piclist. Go to this link and
click on View Thread for the Simple RS232 hardware topic.
They have strong opinions on both sides. I'm with Dale Botkin --
I'd use it in the lab, but be wary about using it in a product.
If management insisted on a super cheap solution, I'd probably
use the approach of external schottky diodes, where you know
they can handle the current.
http://www.google.com/groups?as_epq=protection%20diodes&as_ugroup=comp.arch.embedded.piclist |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|