CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

16f877 rs232 problem and some Matlab
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
strumpfm



Joined: 23 Nov 2003
Posts: 7
Location: Seattle, WA

View user's profile Send private message AIM Address

16f877 rs232 problem and some Matlab
PostPosted: Sun Nov 23, 2003 10:46 pm     Reply with quote

Hi all- I know this has been brought up a million times before, but it seems like there's one small barrier that everyone needs to get past, and I'm stuck at one.

I'm trying to eventually collect data in matlab to plot off my PIC, but for the time being I would just like to see something in HyperTerm or something. I'm using an 877 with the Tx on pin B7 (pin 2 of serial port) and Rx on B6 (pin 3 of serial port) connected to the computer with a 22k resistor, pin 5 of the serial port grounded, using the internal clock, not an external crystal, and attempting to use software USART. Here's the code that I've been trying:

#include <16F877.h>
#include <String.h>
#include <Math.h>
//#device PIC16F877
//#device adc=10
#FUSES HS,NOWDT,NOPROTECT,NOPUT,BROWNOUT,NOLVP,NOCPD,WRT
#USE DELAY(CLOCK=10000000)
#USE RS232 (BAUD=9600, xmit=PIN_B7, rcv=PIN_B6, PARITY=N, BITS=8,ERRORS, invert)
#byte port_b = 6
#byte port_c = 7
#byte port_d = 8


void output_int(int var);
void cycle();
void clear_out();


void main()
{
int temp;
int i=0;
int toggle=0;
set_tris_b(0x00); //set b as outputs
set_tris_d(0x00); //set d as outputs
delay_ms(50);
do{

for (i=0; i<1000; i++) {

printf( "Hello!" );

} //end for

}while(1);
}

I tried using HyperTerminal set up with 9600 baud, 8 data bits, no parity, stop bits of 1, and hardware flow control. The com port will open, but nothing comes out on the screen. I also tried writing a Matlab function to read in data from the serial port that looks like this:

ser_obj=serial('COM2','baudrate',9600);
fopen(ser_obj);
rctime=fscanf(ser_obj,'%s'); % receive sensor data
fclose(ser_obj);
y=rctime;

I don't know if any of you know Matlab serial interfacing well, but I just keep getting a message saying "Warning: The input buffer was filled before the Terminator was reached."

Am I overlooking something really basic? Any little bits of guidance would be greatly appreciated. Thanks,

Mike
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Nov 24, 2003 12:25 am     Reply with quote

Code:
using the internal clock, not an external crystal

Just to clarify, does this statement refer to the A/D clock,
or are you actually running the 16F877 without any crystal
connected to it ?
strumpfm



Joined: 23 Nov 2003
Posts: 7
Location: Seattle, WA

View user's profile Send private message AIM Address

PostPosted: Mon Nov 24, 2003 3:41 am     Reply with quote

PCM programmer wrote:

Just to clarify, does this statement refer to the A/D clock,
or are you actually running the 16F877 without any crystal
connected to it ?


All I had used a PIC for before was A/D, so that is what I'm referring to. As you can see, I'm not really familiar with using an external clock. Are there any frequencies or part numbers that people are fond of for rs232? Do you need extra code to set bits to use the external clock, or is it just expected in the #use command that an external clock is used. Is the XP type crystal/resonator the best to use (compared to a low power crystal, high speed crystal, or capacitor/resistor)? Thanks for the reply, I'll do some more forum searching.

Mike
Pete Smith



Joined: 17 Sep 2003
Posts: 55
Location: Chester, UK

View user's profile Send private message Visit poster's website MSN Messenger ICQ Number

Re: 16f877 rs232 problem and some Matlab
PostPosted: Mon Nov 24, 2003 4:13 am     Reply with quote

strumpfm wrote:
Hi all- I know this has been brought up a million times before, but it seems like there's one small barrier that everyone needs to get past, and I'm stuck at one.

I'm trying to eventually collect data in matlab to plot off my PIC, but for the time being I would just like to see something in HyperTerm or something. I'm using an 877 with the Tx on pin B7 (pin 2 of serial port) and Rx on B6 (pin 3 of serial port) connected to the computer with a 22k resistor, pin 5 of the serial port grounded, using the internal clock, not an external crystal, and attempting to use software USART.

I tried using HyperTerminal set up with 9600 baud, 8 data bits, no parity, stop bits of 1, and hardware flow control. The com port will open, but nothing comes out on the screen. I also tried writing a Matlab function to read in data from the serial port that looks like this:

Am I overlooking something really basic? Any little bits of guidance would be greatly appreciated. Thanks,

Mike


What does your PC end of the cable look like?

I always connect together pins 7 & 8 on the D type shell I plug into the serial port (or serial port extension), otherwise the flow control gets a bit messed up. For some reason, I also seem to connect pins 5 & 9 and 1 & 6. Can't think why though!

I would also (personally) use a hardware UART and use a hardware level switcher. I find the hardware UART much more reliable than the software ones.

Finally, I never touch Hyperterm with someone elses 10 foot pole. I always use Teraterm instead.

HTH

Pete.
TSchultz



Joined: 08 Sep 2003
Posts: 66
Location: Toronto, Canada

View user's profile Send private message

PostPosted: Mon Nov 24, 2003 5:52 am     Reply with quote

For RS-232 to work reliably the baud rate has to be maintained within +/-3% at both ends. You should be using an external crystal, some of the ceramic resonators are OK as well. Then you need to pick a crystal frequency that minimizes the errors for the baud rate you want to use, a frequency easily divisible by powers of 2 are the best. Check the data sheet and read the section on the USART, you will find most of the details you need in there.

Next you should always use a hadware level shift device for the RS-232 signals, they are supposed to be bipolar 12V signals. Although many devices will accept unipolar low level signals you are not guaranteed of this. Use a MAX232 or one of the many equivalent parts and things will be much more reliable.

Also use the hardware USART on the PIC16F877, it will be much more reilable than the software one you are currently using. Most of the problems people encounter with the RS-232 is not understanding the interface. Using the hardware USART and the hardware level shifting takes most of the guesse work out.

Start of with a simple "Hello world" before you start sending data. If you can't send a simple string then you won't be able to send data.

If you search this forum you should find a great deal of information on the subject.
rwyoung



Joined: 12 Nov 2003
Posts: 563
Location: Lawrence, KS USA

View user's profile Send private message Send e-mail

Re: 16f877 rs232 problem and some Matlab
PostPosted: Mon Nov 24, 2003 8:39 am     Reply with quote

strumpfm wrote:
I tried using HyperTerminal set up with 9600 baud, 8 data bits, no parity, stop bits of 1, and hardware flow control. The com port will open, but nothing comes out on the screen.


You don't have the CTS, RTS etc pins hooked up and driven in your connector so hardware flow control probably won't work. Flow control should be set to "none".

Also, just like all the other posts say, use the hardware USART and a level converter chip like the MAX232 or cousins or the DS1275. Some PCs work OK with the resistor trick but just as many or more won't.
_________________
Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Nov 24, 2003 12:55 pm     Reply with quote

Quote:
All I had used a PIC for before was A/D, so that is what I'm referring to. As you can see, I'm not really familiar with using an external clock. Are there any frequencies or part numbers that people are fond of for rs232? Do you need extra code to set bits to use the external clock, or is it just expected in the #use command that an external clock is used. Is the XP type crystal/resonator the best to use (compared to a low power crystal, high speed crystal, or capacitor/resistor)? Thanks for the reply, I'll do some more forum searching.

Based on the above, I'll assume that you don't have a crystal on your
PIC.

You're running at 9600 baud. Probably the minimum crystal freq you
should use for that would be 4 MHz, at least with the hardware USART.
I mention that, because for some reason you're not using the hardware
USART. If possible, you should do so. It's on pins C6 and C7.
Here is the setup for that:

Code:
#include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000)
#use rs232(baud = 9600, xmit=PIN_C6, rcv = PIN_C7, ERRORS)

Note that XT mode is used with a 4 MHz crystal.

With regard to part numbers, you need a parallel resonant crystal.
Digikey p/n X405 would work. That's made by ECS.
I believe the recommended capacitors for a 4 MHz oscillator on the PIC
are 15 pf. If you only have 22 pf in stock, those would work OK.

Here is the Digikey page for that crystal. Click on Catalog / Technical
information to get the specs.
http://www.digikey.com/scripts/DkSearch/dksus.dll?Detail?Ref=90219&Row=382897&Site=US
strumpfm



Joined: 23 Nov 2003
Posts: 7
Location: Seattle, WA

View user's profile Send private message AIM Address

PostPosted: Mon Nov 24, 2003 2:04 pm     Reply with quote

Thanks for all of your help, I certainly know more now. After realizing that using a resistor for driving the clock isn't accurate enough, I was able to get an XTAL-20MHz crystal. I've been coming up dry when searching for info on these crystals, though. Do these crystals have a polarity? I've seen designs that have both ends of a crystal tied to ground, and the inputs in OSC1 and OSC2. Most ranges that I've seen for capacitance were between 15 and 30 pf. I had some 22pF capacitors, so I connected them up, but wasn't able to get oscillation. Is that how you are supposed to connect up the oscillator? One should be able to hook up a scope and view the oscillation at OSC1, right? Thanks again,

Mike
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Nov 24, 2003 2:34 pm     Reply with quote

Look at the following schematic. Zoom in on the crystal circuit
on the left side of the 16F877. Do you have your board wired
in the same way ?
http://www.melabs.com/downloads/labx2sch.pdf

Also, since you're using a 20 MHz crystal, you need to use HS mode,
and you need to set the #use delay value to 20 MHz. Example:

#include <16F877.H>
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 20000000)
#use rs232(baud = 9600, xmit=PIN_C6, rcv = PIN_C7, ERRORS)
strumpfm



Joined: 23 Nov 2003
Posts: 7
Location: Seattle, WA

View user's profile Send private message AIM Address

PostPosted: Mon Nov 24, 2003 2:45 pm     Reply with quote

PCM programmer wrote:
Look at the following schematic. Zoom in on the crystal circuit
on the left side of the 16F877. Do you have your board wired
in the same way ?
http://www.melabs.com/downloads/labx2sch.pdf


Thanks for the link, that is a great diagram. That is how I have my crystal set up. You can only view the output of a crystal if it has a load on it, right? Time for more snooping.

Mike
strumpfm



Joined: 23 Nov 2003
Posts: 7
Location: Seattle, WA

View user's profile Send private message AIM Address

PostPosted: Mon Nov 24, 2003 4:24 pm     Reply with quote

I think that I'm confused about how the crystal operates. I had a basic led test working previously with just a resistor for the clock, but with the crystal set up as in the previous .pdf file, I'm not seeming to get any clock, so none of my program is working. Here is the code that i'm running, but I don't think the code really effects clock generation. Do I need to specify any bits set to use a crystal instead of a resistor?
Code:
#include <16F877.h>
#include <String.h>
#include <Math.h>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000)
#byte      port_b = 6
#byte    port_c = 7
#byte    port_d = 8

void main()
{
int temp;
int i=0;
int toggle=0;
set_tris_b(0x00);  //set b as outputs
set_tris_d(0x00);  //set d as outputs
delay_ms(50);
do{

   delay_ms(50);

for (i=0; i<30; i++)
{
   toggle=!toggle;
   output_bit(PIN_d0,toggle);
   output_bit(PIN_d1,toggle);
   output_bit(PIN_d2,toggle);
   output_bit(PIN_d3,toggle);
   output_bit(PIN_d4,toggle);
   output_bit(PIN_d5,toggle);
   output_bit(PIN_d6,toggle);
   output_bit(PIN_d7,toggle);
}

}while(1);
}


Thanks again,

Mike
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Nov 24, 2003 4:40 pm     Reply with quote

That isn't really a simple LED test. The following code is a simple
LED test.

Before, you said that you found a 20 MHz crystal, and some 22 pf
capacitors. But the code you posted is setup for a 4 MHz crystal.
Do you know why ?

If you have a 20 MHz crystal, please use the following code, verbatim.

Code:
#include <16F877.h>
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 20000000)

void main()
{

while(1)
  {
   output_low(PIN_D0);
   delay_ms(500);
   output_high(PIN_D0);
   delay_ms(500);
  }

strumpfm



Joined: 23 Nov 2003
Posts: 7
Location: Seattle, WA

View user's profile Send private message AIM Address

PostPosted: Mon Nov 24, 2003 11:22 pm     Reply with quote

PCM programmer wrote:

Before, you said that you found a 20 MHz crystal, and some 22 pf
capacitors. But the code you posted is setup for a 4 MHz crystal.
Do you know why ?


That was a mistake on my part which I didn't realize until about an hour after posting, and I wasn't near a computer at the time. I had started typing up a response, and took a break from the computer to go get a 4 MHz crystal to see if it would respond in the same way as the 20 MHz crystal. So, I changed my code, and forgot to update.

I ran the code that you gave, but I don't think that my problem lies in the code, I think it is mostly a crystal problem. I can't seem to view any sort of waveform at the clock inputs on the PIC from the crystal, and I think this is causing the PIC to be non-functional. Here is a picture of my circuit setup: http://students.washington.edu/strumpfm/crystal_setup.GIF. I thought that I had it set up as in the link you sent before, but the crystal still seems to not be doing anything. Is using an oscillator with a TTL output advised against? Thanks again,

Mike
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Nov 25, 2003 2:06 am     Reply with quote

1. In your schematic, I don't see any connection to the MCLR pin.
That pin should be connected to VDD. In some cases a resistor
to VDD is recommended. What are you using as a programmer ?

2. Your schematic shows a battery as the power supply. What's the
voltage ?

If you're running at less than 5 volts, there are other issues
that become important. For example, say you're running
at 3.3 volts. Then you can't enable the Brownout feature.
That's because it triggers a reset if the voltage falls below
approximately 4.0 volts.

Also, there are limitations on the oscillator frequency, based on
the Vdd voltage. Vdd must be at least 4.5v for 20 MHz operation.

During these tests, you should be using a +5v regulated power
supply. It would make things a lot easier.
adrian



Joined: 08 Sep 2003
Posts: 92
Location: Glasgow, UK

View user's profile Send private message

PostPosted: Tue Nov 25, 2003 6:41 am     Reply with quote

If you have wired it as per your drawing, your LED is in back to front
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
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