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

Need help, Serial COM, PIC6F628.

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







Need help, Serial COM, PIC6F628.
PostPosted: Thu Jul 15, 2004 2:53 pm     Reply with quote

Newbie alert!

The biggest problem here is that don't know the first thing about what I want to do! - Thats a bit of a generalisation, I'll explain.

Basically I've made a switch box, I've got some triacs that switch on and off some lights.
At the minute I've got some cheap push switches connected to a 16f628. one push switches the lights on (at output b) a second push (of the asme switch) switches them off. (So it's a very simple latch, there is a little pause to debounce the switches, nothing too teechnical). - so thats my control board.

Now I want to make a PC interface board. - thats where my problems start.
Idealy I wanted to use the parallel port of the computer, I'd have just had a simple buffer. I have a program written in C that toggles the port bits for a second, which is enough to switch on the control board outputs.
The only problem is that it not possible to communicate with the hardware directly on an NT system. (it works fine in windows 98).

So I've decided that I should use the serial port, since there is a component in visual basic that can use the serial port without trouble.
also this gives a way of opperating the serial port via PHP, so I could have my switch box connected to a webserver and control the lights from a distance.

The only problem is I don't have a clue about anything to do with serial communications, I've got a pinout of the rs232 port, and know there is a transmit and recieve line. I won't be sending any data back to the PC so only need to use the transmit line.
I don't intend to use a level shifter so I don't need to do that. (i'm only using the output, so I don't need to shift levels for the input)

What i really need is some sample code to get me started (if I send a charector from a VB program to the com port how do get that charactor to the output port of the PIC,) and I need to know where I'd connect the serial line to the PIC.

I intend to use the 16f628 for my computer interface board.
Any help would be greatly appreciated.
I've been searching for days and can't seem to find anything.
dyeatman



Joined: 06 Sep 2003
Posts: 1912
Location: Norman, OK

View user's profile Send private message

RS232
PostPosted: Thu Jul 15, 2004 3:30 pm     Reply with quote

Where have you been searching? Certainly not here because there are HUNDREDS of entries on that subject on this board. Almost every one of them has code with it.

The basics:

1. You will have to have a #use rs232 statement

2. If you use the hardware UART in the chip you will need a MAX232 inverter or simiilar. If you use a software UART you can include the INVERT option to get around the inverter requirement. You can search for MAX232 and/or inverter in this forum for more info.

3. You can use hyperterminal or a similar terminal program on the PC end to test send and receive.

4. If you use the H/W UART and want to use interrupts there is code in the CCS code library as well for that.

Search this board for #use RS232 and you will find an incredible amount of info on the subject.

Once you do some research and get your code started we will help with the debug if you run into problems.
that damn newb is back
Guest







PostPosted: Thu Jul 15, 2004 4:13 pm     Reply with quote

OK, thanks very much.
My searching was a few days of googleing.
IO searched here for 16f628 but didn't seem to find much that I could recognise and use as a starting point.

I searched for LCD driver. thinking that that would be a popular subject, and it would be doing exactly what I wanted - recieving chars and outputting them.
But I am getting to grasps a bit with some of that code, but am still unsure of the actual physical circuit that I will need. Does it actually matter where I attach the serial conection -so long as it's connected to an input.
My background is mostly in the motorolla 68hc11 where some pins have dedicated functions that cannot be performed on other pins. on this chip serial has to be done on the serial enable ports. - is this the same on the pic16f628?
dyeatman



Joined: 06 Sep 2003
Posts: 1912
Location: Norman, OK

View user's profile Send private message

Finding RS232 code
PostPosted: Thu Jul 15, 2004 4:25 pm     Reply with quote

Dont search for 16F628. The rs232 routines are pretty much the same across the board so any RS232 stuff you find will be relevant.

OK, first, are you going to use the HW UART or bit bang the rs232?
Back again
Guest







PostPosted: Thu Jul 15, 2004 4:38 pm     Reply with quote

That search did turn up a lot of results. now I have (I think) figured out a few things... here goes...

this is my code
[code]#include <16F628.h>
#fuses intrc_io,nomclr,NOPROTECT,NOWDT,NOBROWNOUT,NOLVP /*fuses, turn pretty much everything off!*/
#use delay(clock=4000000)
#use rs232(baud=9600,parity=N,rcv=PIN_A0,bits=8)
#byte PORTB = 6 /*map ports */
#byte PORTA = 5

void main()
{
unsigned char j;
set_tris_A (1);
set_tris_B (0);
setup_uart(TRUE);
while(TRUE)
{
j = getc();
PORTB = j;
delay_ms(500); /*long pause to make sure on period outweighs debounce*/
PORTB = 0xff; /*reset port*/
}
}[code]
now, this is what I think should happen,
I should connect the serial line to port A0. (I'm still not sure what is meant by hardware and serial invert - But I'll keep researching).
and I should connect my circuit to port B (all eight pins) now if I send 0x01 I should see that at port b, if I send 0xff I should see all pins on at port B.
then after a short pause, port B is reset and it all starts again.

can anyone see any problems with this, I'm in the unfortunate position of being scared to break my PC, I'm off to buy a cheap IO card at the weekend, and want to get some code sorted so I can dive straight into it as soon as I get back from the shops. (which is why I haven't tested it.)
me again
Guest







PostPosted: Thu Jul 15, 2004 4:41 pm     Reply with quote

I was away composing that reply, and did't see that last post.

I'm not sure what I want to do, which ever is most reliable/easiest I guess.

If the h/w UA acts as I think it does. (takes a serial stream of 8 bits and presents it as parallel data on 8 pins) then I'll use the H/W UART.

having said that, I suppose using check bits in my serial stream I could just look at the incoming flow.

What does everyone else do, what is the tried and tested bes way to do it?
dyeatman



Joined: 06 Sep 2003
Posts: 1912
Location: Norman, OK

View user's profile Send private message

RS232
PostPosted: Thu Jul 15, 2004 4:58 pm     Reply with quote

It is unlikely you will break your PC port but if you want the peace of mind of an add-on card..OK.

First I assume you are wanting to use a SW UART since you are setting up on PIN_A0 rather than pins B1 and B2 for the HW UART built into the chip.

Since that is the case change your RS232 line to this:
#use rs232(baud=9600,parity=N,rcv=PIN_A0,bits=8, INVERT)

Next, since you are not using Fast_IO you can delete the TRIS line for port a.

Change to setup_uart(FALSE) since you are not using the HW UART

Next, you need to test KBHIT() before a serial getc() or it will hang waiting for the input. Do it like this:

if kbhit()
{
j = getc();

... do whatever at this point with j
}
dyeatman



Joined: 06 Sep 2003
Posts: 1912
Location: Norman, OK

View user's profile Send private message

RS232
PostPosted: Thu Jul 15, 2004 5:04 pm     Reply with quote

The HW UART receives the characters and presents them one at a time to the software in byte form.

The big advantages to using the HW UART are that you dont tie up the processor doing a char receive and you can use interrupts which you cannot do with SW RS232.

In your case, right now use the SW UART on A0 just to get your feet wet...it is the simplest method to get started.

UPDATE:
Oh yeah I almost forgot, you will need to add the ERRORS keyword like this:

#use rs232(baud=9600,parity=N,rcv=PIN_A0,bits=8, INVERT, ERRORS)

This is to handle recieve errors and automatically reset the receive routine when they occur
Guest








PostPosted: Sun Jul 18, 2004 9:56 am     Reply with quote

Ok, I now have the code as above (including suggested changes) but I am getting errors when compiling.
Code:
#include <16F628.h>
#fuses intrc_io,nomclr,NOPROTECT,NOWDT,NOBROWNOUT,NOLVP /*fuses, turn pretty much everything off!*/
#use delay(clock=4000000)
#use rs232(baud=9600,parity=N,rcv=PIN_A0,bits=8, INVERT, ERRORS)
#byte PORTB = 6 /*map ports */
#byte PORTA = 5

void main()
{
unsigned char j;
set_tris_A (1);
set_tris_B (0);
SETUP_UART(FALSE);
while(TRUE)
{
if kbhit()
{
j = getc();
PORTB = j;
delay_ms(500); /*long pause to make sure on period outweighs debounce*/
PORTB = 0xff; /*reset port*/
}
}
}


I am using MPLAB version 6.50 and I have version 3.099 of the ccs compiler.
the errors I am getting are
Code:
Error[12]   seriali.c 13 : Undefined identifier 

Code:
SETUP_UART(FALSE);

I've looked in the manual and can't find the SETUP_UART command in the manual. Is this command included in the version I have?
Guest








PostPosted: Sun Jul 18, 2004 11:49 am     Reply with quote

No, it's not. Time to upgrade?
Guest








PostPosted: Sun Jul 18, 2004 12:10 pm     Reply with quote

OK, Cheers then.
I guess I'll start saving for a new version!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jul 18, 2004 12:14 pm     Reply with quote

Two things:

1. The setup_uart() function is only accepted by the compiler if
you have declared that the hardware USART will be used.
To do this, you MUST use pin B2 for xmit, and pin B1 for receive.
That's because the hardware usart in the 16F628 is only on those
two pins. Example:

#use rs232(baud=9600, xmit=PIN_B2, rcv=PIN_B1)

Since your #use rs232 statement uses pin A0, you are using
a soft USART and the setup_uart() function is not available.
The compiler will give an error message if you try to use it.


2. The ERRORS statement will only have effect if the hardware
usart is used. The compiler doesn't give an error message
if you put it in a #use rs232 statement for a soft usart, but
it's just ignored.
greatful
Guest







PostPosted: Tue Jul 20, 2004 12:28 pm     Reply with quote

I've just come back to say cheers very much, thanks for all your help I've now got it working.
Incase anyone is looking for the same sort of thing, I'll post the end code]
Code:
#include <16F628.h>
#fuses intrc_io,nomclr,NOPROTECT,NOWDT,NOBROWNOUT,NOLVP /*fuses, turn pretty much everything off!*/
#use delay(clock=4000000)
#use rs232(baud=9600,parity=N,rcv=PIN_A0,bits=8, INVERT)
#byte PORTB = 6 /*map ports */
#byte PORTA = 5

void main()
{
unsigned char j;
set_tris_A (1);
set_tris_B (0);

while(TRUE)
{
if (kbhit())
{
j = getc();
PORTB = j;
delay_ms(500); /*long pause to make sure on period outweighs debounce*/
PORTB = 0x00; /*reset port back to zero*/
}
}
}
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