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

[solved] beginner serial comm #use rs232

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
jin_yeugh



Joined: 07 Mar 2013
Posts: 3
Location: Orange County, CA

View user's profile Send private message

[solved] beginner serial comm #use rs232
PostPosted: Wed Jul 10, 2013 3:01 pm     Reply with quote

I'm having trouble understanding how to use realterm with my 16F1937 development board. I've installed and set up the bits/baud rate on realterm and am having trouble setting up the PIC in PCW. Can you please point me in the right direction to get started communicating between my PC and PIC?

Code:
#include <16F1937.h> //declares which PIC chip is being used
#include "protoalone.h"
#include <stdlib.h>
#device ICD = TRUE //decares the ICD is being used
#fuses HS, PLL_SW, NOLVP, NOWDT
#use DELAY ( CLOCK = 10000000 ) //stating the clock speed
#use rs232 ( baud = 9600, xmit = PIN_C6, rcv = PIN_C7 )

void main()
{
  printf("Did this work?");
}


Last edited by jin_yeugh on Thu Jul 25, 2013 11:38 am; edited 1 time in total
Ttelmah



Joined: 11 Mar 2010
Posts: 19350

View user's profile Send private message

PostPosted: Wed Jul 10, 2013 3:20 pm     Reply with quote

First comment, fuses etc., want to be above the general includes. Layout should be:

1) Processor include
2) Fuses
3) Clock rate
4) RS232 setup, I2C setup etc.
5) General includes
6) Code

The wizard generally puts 1,2 & 3 together in a 'project.h' file, and includes this, but the basic order must be this way.

Reason is that things like delay code, RS232 code etc., which depend on the defines, must have these declared _before_ they load.

Second comment. You have got an RS232 level translator chip?. Max232 or similar. Needed to talk to RS232....

Third, when using the hardware UART, you should _always_ have 'ERRORS' declared in the RS232 setup. If you don't, and data arrives and is not received, the UART _will_ be hung.

Obvious comments then follow. You have a 10MHz Crystal, and are using the ICD?.

Best Wishes
ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Wed Jul 10, 2013 8:05 pm     Reply with quote

Hi,

You also need to add a while(1); statement after your printf to prevent the PIC from going to sleep before it finishes sending data out the UART!

John
jin_yeugh



Joined: 07 Mar 2013
Posts: 3
Location: Orange County, CA

View user's profile Send private message

PostPosted: Thu Jul 11, 2013 12:21 pm     Reply with quote

Thanks for the replies, guys. I'm very new to programming/PIC/RS232 so if you could clarify and give me a bit more of a guideline, Ttelmah, I'm not sure how to put 'ERRORS' in my #use RS232 directive or which ones to include.

I also don't know the problem(s) with using ICD at 10MHz. I did change it to
#use DELAY ( internal = 4MHz ) and got it to send a printf() statement to my PC through realterm but have very little idea how to do anything else. I'm learning all of this a bit blind
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Fri Jul 12, 2013 3:56 pm     Reply with quote

Just a short elaboration:
Do you have an external crystal for the clock circuit?
If yes, then there are two options:
1) <=4MHz requires the XT fuse
2) >= 4MHz requires the HS fuse
Or you can use the internal clock oscillator, saves $0.50 on the crystal but is less accurate. If you want to use RS232 this can be a problem. Requires fuse INTRC_IO and actual clock speed is selected in the #delay statement, see datasheet 5.2.2.5 for one of the 11 possible speeds.
There are more speed related fuse settings but the above set will do for 98% of the programs on this forum.

Adding 'ERRORS' to the RS232 setup is nothing more than what it says:
Code:
#use rs232 (baud = 9600, xmit = PIN_C6, rcv = PIN_C7, ERRORS)


The line with ICD=TRUE should only be present when you are using an In Circuit Debugging device. It will change some compiler settings to allow for debugging, but without ICD present these cause your program to fail start up.

Don't include header files you are not using (stdlib.h and protoalone.h)

For a 4MHz internal clock:
Code:
#include <16F1937.h> //declares which PIC chip is being used
#fuses HS, INTRC_IO, NOLVP, NOWDT
#use DELAY ( CLOCK = 4MHz ) //stating the clock speed
#use rs232 ( baud = 9600, xmit = PIN_C6, rcv = PIN_C7, ERRORS )

void main()
{
  printf("Did this work?");

  while();   // loop forever here to prevent the processor from going to sleep and UART stop transmitting the data in buffer
}


Quote:
and got it to send a printf() statement to my PC through realterm but have very little idea how to do anything else.
Sounds like you have it working. What more do you want our help for?
jin_yeugh



Joined: 07 Mar 2013
Posts: 3
Location: Orange County, CA

View user's profile Send private message

PostPosted: Tue Jul 16, 2013 3:21 pm     Reply with quote

Thanks for the clarification, ckielstra; it helps to make sense of a few more things and why to do them. I have a bad habit of getting stuck trying to understand the inner workings of EVERYTHING instead of taking processes for granted until I understand the broader, simpler concepts.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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