C program on PIC to send data via RS232

Thread Starter

yliu

Joined Mar 25, 2009
9
Greetings guys,

I am using PIC to send data via RS232 to a computer, and I am using C to program the chip. However, I don't know how to write this program. Is there any special fuction or instructions in C library? By the way, I am using HyperTerminal on the computer to receive data.

Also, do I need to worry about ASCII format when I send the data?

Any help will be most appreciated. Thanks guys.

Best regards,
Y.
 

thatoneguy

Joined Feb 19, 2009
6,359
What do you have so far for code and a schematic?

Are you using an external IC similar to a MAX-232 to translate Logic Level UART to RS-232 specifications?
 

Thread Starter

yliu

Joined Mar 25, 2009
9
Hi,

Thanks for reply.

I'm planning to write a method "SendData()" to perform the data transferring, but this is all I get so far. I am wondering if there is any specific syntax code to do this.

In hardware, I am using 16F877A, and I connected the TX and RX to MAX232, then it is connected to a female DB9 towards a computer. On the computer, I am running HyperTerminal to receive the data from PIC. I think the hardware part should work (hopefully :D)

Best regards,
Y.
 

Thread Starter

yliu

Joined Mar 25, 2009
9
Hi,

I was just looking through CCS C Manual, will this program work?
Say some int variables already defined, a=1, b=2

SendData ()
{
printf("#", a, ":", b);
}

With this short code, can I receive message "#1:2" on HyperTerminal?

Regards,
Y.
 
Last edited:

thatoneguy

Joined Feb 19, 2009
6,359
You need to set up the port on the PIC (USART) at the correct baud rate, then send the 0-5V logic to a "Logic booster" IC that converts 5V logic to +12V/-12V logic to talk to a computer properly. Some can transmit to computer only using logic level without the IC, others haven't had as good of luck.
 

Thread Starter

yliu

Joined Mar 25, 2009
9
Yup, the voltage shift already designed in the hardware part. Could you check the code above and tell me if it could work? Thanks a lot.

Regards,
Y.


You need to set up the port on the PIC (USART) at the correct baud rate, then send the 0-5V logic to a "Logic booster" IC that converts 5V logic to +12V/-12V logic to talk to a computer properly. Some can transmit to computer only using logic level without the IC, others haven't had as good of luck.
 

thatoneguy

Joined Feb 19, 2009
6,359
I'm not familair with CCS compiler. If the '#' device in the code is direced to USART, then it should work from a cursory glance.

However, you are looking to send the octothorpe to hyperterminal. printf needs to know "what" to print to (LCD, 7 segment, serial, etc). This is done in code setup, the manual should have more information.
 

thatoneguy

Joined Feb 19, 2009
6,359
Apparently with CCS, that IS as simple as it gets!

I'm guessing the port setup posted above is defined in u876.h or the bootloader.

From This site, also has other samples
Rich (BB code):
 1: /*
 2: *****************************************************************************
 3: *
 4: * File Name - hello.c
 5: * Function - Demonstrates serial output by printing a test string each sec.
 6: *
 7: * You will need:
 8: * (1) uFlash & CCS PIC C Compiler (PCM/PCW)
 9: * (2) PC Terminal emulator (such as Hyperterm)
10: *
11: * Instructions:
12: * (1) Compile & downloading to the uFlash
13: * (2) Setup Hyprterm for 57K6 Baud,8 bits, no parity, no echo, no handshaking
14: * (3) Remove the Pgrm Jumper from the uFlash (this stops Hyperterm from
15: * holding the uFlash in reset).
16: * (4) Run Hyperterm
17: *
18: *****************************************************************************
19: */
20: #include [SIZE=+1]<uF876.h[SIZE=+1]>[/SIZE]
21: 
22: void main[SIZE=+1]([/SIZE]void[SIZE=+1])[/SIZE]
23:     [SIZE=+1]{[/SIZE]
24:     WORD x[SIZE=+1]=[/SIZE]0[SIZE=+1];[/SIZE]                    // line counter
25: 
26:     while[SIZE=+1]([/SIZE]TRUE[SIZE=+1])[/SIZE]                    // loop forever
27:         [SIZE=+1]{[/SIZE]
28:         printf[SIZE=+1]([/SIZE]"Hello World %ld\r"[SIZE=+1],[/SIZE][SIZE=+1][SIZE=+1]+[/SIZE][SIZE=+1]+[/SIZE][/SIZE]x[SIZE=+1])[/SIZE][SIZE=+1];[/SIZE]    // print str & count (inc count)
29:         delay_ms[SIZE=+1]([/SIZE]1000[SIZE=+1])[/SIZE][SIZE=+1];[/SIZE]                // wait 1 second
30:         [SIZE=+1]}[/SIZE]
31:     [SIZE=+1]}[/SIZE]

[/SIZE]
Code formatting/colorization by http://www.chamisplace.com/colorizer/cc.asp
 
Last edited:

thatoneguy

Joined Feb 19, 2009
6,359
This is from This Site for Reading RS-232 input:

Rich (BB code):
1: //
 2: // RS232 input with timeouts. 9600 8N1
 3: // This code is a section of code written by Peter Anderson
 4: // I modified it - any bugs are mine.
 5: //
 6: // Inverted version.
 7: // Non-inverted pulses start high and drop low
 8: // Inverted pulses start low and go high
 9: //
10: // Meant to be a direct hookup to the PC (hook the
11: // transmit line (pin 3 on a 9 pin DIN) through a
12: // 47K resistor to the interrupt line. You can also
13: // hook up to an MCU by monitoring the DTR line (a2)
14: // until it goes high and then transmitting to B0
15: //
16: 
17: int get_ch(long t_wait)
18: // returns 0xff if no char recived within t_wait ms
19: {
20:    int one_ms_loop,  ser_loop, ser_data, ser_time;
21: 
22:    do
23:    {
24:         one_ms_loop = 100;	// 100 times 10 usecs
25: #asm
26: 
27: SCAN_1:
28: 	CLRWDT
29:    NOP
30: 	NOP
31: 	NOP
32: 	NOP
33: 	BTFSC	PORTB, RxData	// check serial in - for inverted data
34: 	GOTO 	SERIN_1		// if start bit (change BTFSC to BTFSS for non-inverted)
35: 	DECFSZ 	one_ms_loop, F
36: 	GOTO SCAN_1
37: #endasm
38:    }while(--t_wait);
39:    return(0xff);
40: 
41: #asm
42: SERIN_1:
43: 
44: SERIN_INV:
45: 	MOVLW 8
46: 	MOVWF ser_loop
47: 	CLRF ser_data
48: 
49: 	MOVLW 51         	// delay for 1.5 bits
50: 	MOVWF ser_time		// 3 + 51 * 3 = 156 usecs for 9600
51: 
52: SERIN_INV_1:
53: 	DECFSZ ser_time, F
54: 	GOTO SERIN_INV_1
55: 
56: SERIN_INV_2:
57: 	BTFSS PORTB, RxData
58: 	BSF STATUS, C		// reverse for non-inverted (BSF, etc)
59: 	BTFSC PORTB, RxData
60: 	BCF STATUS, C           // reverse for non-inverted (BCF, etc)
61: 
62: 	RRF ser_data, F
63: 
64: 	MOVLW 23      		// one bit delay
65: 	MOVWF ser_time		// 10 + 23 * 4 =  102 usecs
66: 
67: SERIN_INV_3:
68: 	CLRWDT
69: 	DECFSZ ser_time, F
70: 	GOTO SERIN_INV_3
71: 
72: 	DECFSZ ser_loop, F	// done?
73: 	GOTO SERIN_INV_2	// get next bit
74: 
75: 	MOVLW 10
76: 	MOVWF ser_time		// wait for at least 1/2 bit
77: 
78: SERIN_INV_4:
79: 	CLRWDT
80: 	DECFSZ ser_time, F
81: 	GOTO SERIN_INV_4
82: #endasm
83:    return(ser_data);
84: }



Code formatting/colorized by http://www.chamisplace.com/colorizer/cc.asp
 

Thread Starter

yliu

Joined Mar 25, 2009
9
Hi guys,

Thanks so much for help.

I am using 16F877A (40Pin) chip with 20MHz clock.
Here is the C program I wrote:

#include <16F877A.h>
#use delay(clock=20000000)
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use rs232(baud=9600, uart1, bits=8, parity=N, stop=1, ERRORS)

#include <string.h>
#include <stdio.h>
#include <stdlibm.h>

void main ()
{
while (TRUE)
{
printf("Hello there");
}
}

Sadly, I didn't receive anything on HyperTerminal window. Any ideas?
Help is most appreciated!

Regards,
Y.
 

thatoneguy

Joined Feb 19, 2009
6,359
Make sure to set it for no handshaking, software or hardware, hyperterterm has same settings for speed/parity/stop, and you are using the correct cable - Either standard RS-232 or null modem depending on wiring.
 

Thread Starter

yliu

Joined Mar 25, 2009
9
Hi,

Thanks for reply.
I double checked my HyperTerminal setup, it is the same as I did in the program. I am using a USB to DB9 cable with proper driver installed on my computer. I can't see anything wrong with it ...

Regards,
Y.


Make sure to set it for no handshaking, software or hardware, hyperterterm has same settings for speed/parity/stop, and you are using the correct cable - Either standard RS-232 or null modem depending on wiring.
 

thatoneguy

Joined Feb 19, 2009
6,359
If you have a logic probe, check to make sure there is voltage on the Tx pin of the uC. A Voltmeter would also work if you have the code running in a loop. If not, something in the CCS manual should help out.

If there are signals, then it is something with the cable or hyperterminal, assuming valid ASCII is being sent.
 

Thread Starter

yliu

Joined Mar 25, 2009
9
Hi,

Thanks again for the very good testing idea.

There's a signal present on TX pin of uC, it is constantly 2.19v when I run a loop. The corresponding signal comes out from MAX232 is 0.35v, is this normal? If it is, then my cable or hyperterminal got problem.

Can you explain a bit on ASCII? If I just write "printf("Hello");" in my code, then MAX232 will transform the string to ASCII format?

I really appreciate your help! Thanks a lot.

Regards,
Y.


If you have a logic probe, check to make sure there is voltage on the Tx pin of the uC. A Voltmeter would also work if you have the code running in a loop. If not, something in the CCS manual should help out.

If there are signals, then it is something with the cable or hyperterminal, assuming valid ASCII is being sent.
 

thatoneguy

Joined Feb 19, 2009
6,359
Try sending hex 0x34 to the serial port in a loop.

I don't know if CCS translates or not. MAX232 only does level changing.

From the examples I found, it looks like it should work.

As far as the voltages, a 1 is -12V, and a 0 is +12V, so the average of about 2.5V out of the PIC would correspond to an average of about 0V from the MAX232
 

Thread Starter

yliu

Joined Mar 25, 2009
9
Hi thatoneguy,

Really appreciate for your help and sorry for the late reply. But good news, I finally got it working :p

The circuit that I built is definitely working, the problem was HyperTerminal, which has some compatible issues with Vista. I searched for other serial port logging software and I found this:
http://www.com-port-monitoring.com/datalogger.html
It is called RS232 Data Logger and it is free.

Now, for people who is new with Vista and if you are using a USB-to-Serial(DB9) cable, make sure you have the cable on Prolific brand. It provides driver on Vista system and it is definitely working.

Thank you all for your kind help.

Regards,
Y.
 

thatoneguy

Joined Feb 19, 2009
6,359
I was going to blame Microsoft several posts back, but thought it would be bad form. :p

Do you use a PicKit 2 Programmer? In addition to being a triggered logic probe/recorder, they are a USART Diagnostic tool, AND a PIC Programmer! Not bad for $35...
 
Top