Jump to content
Sign in to follow this  
eckertc

Newbi Questions

Recommended Posts

Hey all,

 

First i would like to say THANK YOU sourceboost for creating this excellent software package. I have been looking for weeks for a way to get into PIC programming using C and your software has been the least painfull of all.

 

now to the meat of the post. I have been going through the beginners section doing the simple tutorials (or not so simple but getting there tutorials) and have even made a modified version of the Blinking LED source that uses a 2 state button to turn on and off the LED.

 

#include <system.h>
#include "Project1.h"

#pragma CLOCK_FREQ 4000000  // config clock to 4mhz.

// Set configuration fuse.

#pragma DATA _CONFIG, _XT_OSC & _WDT_OFF & _CP_OFF & _PWRTE_OFF

void main()
{
  trisb = 1;			//configure port B pins as input
  trisa = 0;		//configure port A pins as output
while( 1 )
{
  if(trisb.0==0)		//check If button is pushed
  {
  porta.0=1;			//if button is pushed turn on LED
  }
  if(trisb.0==1)
  {
  porta.0=0;
  }
}
}

 

with a few of these small projects under my belt now and looking forward to doing the rest, i was wondering if someone could point me in the right direction to find more info i doing something with Serial Tx and RX. the final project i have in mind is using the PIC16F628A to accomplish the following.

 

set up the PIC16F628A for full duplex.

set up the PIC16F628A for using the 9th data bit on both TX and RX. (sometime bit 9 will = 0 sometimes bit 9 will = 1) (ex. 1 start bit, 8 data bits, 1 address bit, 1 stop bit)

set up the PIC16F628A at 9600 baud rate.

monitor two seperate serial(TTL Level) lines coming from two seperate peripherals for communication.

If one of my peripherals sends data, read it. if it is one of many expected communications we are waiting for from this peripheral, send it the appropriate responce. If not, reset.

 

any suggestions on this would be wonderful help. one thing im still not clear on is if this is even possible with only one PIC16F628A do to only having one USART Tx and one RX. any ideas??

Share this post


Link to post
Share on other sites

Well, conveniently SourceBoost has a header file called "rs232_driver.h" which will do serial data communication pretty well! Now, unfortunately this is not generalised for any PIC and the 16F628A isn't one of the ones it is configured for. Don't worry. This isn't that bad an issue, but it will involve some research on your part. There is a sample rs232 project that you should look at first at SourceBoost/Samples/BoostC/SerialTest.__c. It is configured for the ... PIC18F452. Now. It looks all confusing at first with the preamble before main, but open up the datasheet for the PIC and reverse-engineer where it got the register locations, and then try to translate that to your own PIC. If you search for rs232 on this site I know someone made it work for the PIC16F627 so you might be able to use that as another reference on how it was translated.

Share this post


Link to post
Share on other sites
Well, conveniently SourceBoost has a header file called "rs232_driver.h" which will do serial data communication pretty well! Now, unfortunately this is not generalised for any PIC and the 16F628A isn't one of the ones it is configured for. Don't worry. This isn't that bad an issue, but it will involve some research on your part. There is a sample rs232 project that you should look at first at SourceBoost/Samples/BoostC/SerialTest.__c. It is configured for the ... PIC18F452. Now. It looks all confusing at first with the preamble before main, but open up the datasheet for the PIC and reverse-engineer where it got the register locations, and then try to translate that to your own PIC. If you search for rs232 on this site I know someone made it work for the PIC16F627 so you might be able to use that as another reference on how it was translated.

 

Thanks Twomers :) I think that is the littel push i needed to get going in the right direction. for anyone else reading this post, another helpfull resource I found thanks to twomers input was the file in C:\Program Files\SourceBoost\Samples\C\BoostC\ez called USARTRecieve.c.

 

on another note, is there anyway to simulate serial communication with a plugin the way you can simulate an LED or a Button? It would be very helpful to simulate serial functions in the software to save time debugging on the hardware.

Share this post


Link to post
Share on other sites

I'm not too certain if this will work, and even if it does it's not that nice a solution.

The RS232 bank is linked to an output port bank so why not monitor this with the LED block in the debugger? The only thing you're going to have to remember to do is when debugging instead of stepping over the putc() or puts() functions you must step into them (and in the puts() you'll have to step into the individual putc() calls). Just did a brief test with the LED-block pointing to portc in the SerialTest.__c sample project and some LEDs go on and off. I didn't investigate much so this may just be chance but I think not. It would be nice to have a plugin for that alright, though.

 

Edit: Note this only works for software implemented rs232, the LED bank. I think HW stuff is managed by the PIC itself and isn't really concerned with our implementation of it. This, apparently, sends data out of the PIC -- "l_txreg = tx_char;" It sets the value of the transmit register and I can only guess that there is something inside the PIC which will then trigger a serial stream of bits on the transmit port of the implementation. Same kind of thing with getc() I believe. Then again, I may be completely wrong.

Edited by twomers

Share this post


Link to post
Share on other sites

I made a little library of functions under BoostC that may help out as well. Go to:

 

http://www.geocities.com/ted_rossin/Electronics/Pic/Pic.html

 

Scroll down to Library of C Routines and grab CBLib.zip. In there you will find Serial.c and Serial.h. Just add them to your project and make use of the following functions:

 

void RS232Init(unsigned char BaudRate);

void RS232putch(unsigned char ByteVal);

char RS232getch(void);

unsigned char RS232peekch(void);

 

The RS232Init function was written for 16F873 parts so it will not work for the 16F628A part directly.

 

The 16F628 uses RB1 for RX and RB2 for Tx instead of RC7 for RX and RC6 for Tx that the 16F87x parts use. So you would have to change two lines in RS232Init from set_bit(trisc,7); to set_bit(trisb,1); and change clear_bit(trisc,6); to clear_bit(trisb,2);

 

The rest of the crap should be the same. I defined a bunch of Baud rates in Serial.h that you can pass to RS232Init.

 

For example:

 

RS232Init(RS232_20MHZ_BAUD_115000);

 

will set up the baud rate generator for 115K baud when clocking the PIC with a 20 MHz crystal. The function RS232putch(Value); will wait for the transmit buffer to be empty and then send the value you pass. The function RS232getch() will wait for a character to be recieved and then return the value as a char. The function RS232peekch() will return 1 if a character is available and 0 if one is not.

 

One day I'll add an optional type input to RS232Init so you don't have to hack the version.

Edited by trossin

Share this post


Link to post
Share on other sites

Thanks Trossin i will definatley be taking a look at that link. and as long as we are on the topic of serial communications, is the internal clock of the PIC16F628A accurate enough to handle stable communications or is it nessesary to use an external oscillator??

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...