C program on PIC to send data via RS232

Discussion in 'Programmer's Corner' started by yliu, Mar 29, 2009.

  1. yliu

    Thread Starter New Member

    Mar 25, 2009
    9
    0
    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.
     
  2. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    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?
     
  3. yliu

    Thread Starter New Member

    Mar 25, 2009
    9
    0
    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.
     
  4. yliu

    Thread Starter New Member

    Mar 25, 2009
    9
    0
    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: Mar 29, 2009
  5. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    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.
     
  6. yliu

    Thread Starter New Member

    Mar 25, 2009
    9
    0
    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.


     
  7. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    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.
     
  8. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    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
    Code ( (Unknown Language)):
    1.  
    2. [FONT=Arial][SIZE=2][COLOR=#cccccc] 1: [/COLOR][COLOR=GREEN][I]/*
    3. [COLOR=#eeeeee] 2: [/COLOR]*****************************************************************************
    4. [COLOR=#eeeeee] 3: [/COLOR]*
    5. [COLOR=#eeeeee] 4: [/COLOR]* File Name - hello.c
    6. [COLOR=#cccccc] 5: [/COLOR]* Function - Demonstrates serial output by printing a test string each sec.
    7. [COLOR=#eeeeee] 6: [/COLOR]*
    8. [COLOR=#eeeeee] 7: [/COLOR]* You will need:
    9. [COLOR=#eeeeee] 8: [/COLOR]* (1) uFlash & CCS PIC C Compiler (PCM/PCW)
    10. [COLOR=#eeeeee] 9: [/COLOR]* (2) PC Terminal emulator (such as Hyperterm)
    11. [COLOR=#cccccc]10: [/COLOR]*
    12. [COLOR=#eeeeee]11: [/COLOR]* Instructions:
    13. [COLOR=#eeeeee]12: [/COLOR]* (1) Compile & downloading to the uFlash
    14. [COLOR=#eeeeee]13: [/COLOR]* (2) Setup Hyprterm for 57K6 Baud,8 bits, no parity, no echo, no handshaking
    15. [COLOR=#eeeeee]14: [/COLOR]* (3) Remove the Pgrm Jumper from the uFlash (this stops Hyperterm from
    16. [COLOR=#cccccc]15: [/COLOR]* holding the uFlash in reset).
    17. [COLOR=#eeeeee]16: [/COLOR]* (4) Run Hyperterm
    18. [COLOR=#eeeeee]17: [/COLOR]*
    19. [COLOR=#eeeeee]18: [/COLOR]*****************************************************************************
    20. [COLOR=#eeeeee]19: [/COLOR]*/[/I][/COLOR]
    21. [COLOR=#cccccc]20: [/COLOR]#include [SIZE=+1][COLOR=BLUE]<[/COLOR][/SIZE]uF876.h[SIZE=+1][COLOR=BLUE]>[/COLOR][/SIZE]
    22. [COLOR=#eeeeee]21: [/COLOR]
    23. [COLOR=#eeeeee]22: [/COLOR][COLOR=RED][B]void[/B][/COLOR] main[SIZE=+1][COLOR=BLUE][B]([/B][/COLOR][/SIZE][COLOR=RED][B]void[/B][/COLOR][SIZE=+1][COLOR=BLUE][B])[/B][/COLOR][/SIZE]
    24. [COLOR=#eeeeee]23: [/COLOR]    [SIZE=+1][COLOR=BLUE][B]{[/B][/COLOR][/SIZE]
    25. [COLOR=#eeeeee]24: [/COLOR]    WORD x[SIZE=+1][COLOR=BLUE]=[/COLOR][/SIZE][COLOR=BROWN]0[/COLOR][SIZE=+1][COLOR=BLUE][B];[/B][/COLOR][/SIZE]                    [COLOR=GREEN][I]// line counter
    26. [COLOR=#cccccc]25: [/COLOR][/I][/COLOR]
    27. [COLOR=#eeeeee]26: [/COLOR]    [COLOR=RED][B]while[/B][/COLOR][SIZE=+1][COLOR=BLUE][B]([/B][/COLOR][/SIZE]TRUE[SIZE=+1][COLOR=BLUE][B])[/B][/COLOR][/SIZE]                    [COLOR=GREEN][I]// loop forever
    28. [COLOR=#eeeeee]27: [/COLOR][/I][/COLOR]        [SIZE=+1][COLOR=BLUE][B]{[/B][/COLOR][/SIZE]
    29. [COLOR=#eeeeee]28: [/COLOR]        printf[SIZE=+1][COLOR=BLUE][B]([/B][/COLOR][/SIZE][COLOR=PURPLE]"Hello World %ld\r"[/COLOR][SIZE=+1][COLOR=BLUE][B],[/B][/COLOR][/SIZE][SIZE=+1][COLOR=BLUE][SIZE=+1][COLOR=BLUE]+[/COLOR][/SIZE][SIZE=+1][COLOR=BLUE]+[/COLOR][/SIZE][/COLOR][/SIZE]x[SIZE=+1][COLOR=BLUE][B])[/B][/COLOR][/SIZE][SIZE=+1][COLOR=BLUE][B];[/B][/COLOR][/SIZE]    [COLOR=GREEN][I]// print str & count (inc count)
    30. [COLOR=#eeeeee]29: [/COLOR][/I][/COLOR]        delay_ms[SIZE=+1][COLOR=BLUE][B]([/B][/COLOR][/SIZE][COLOR=BROWN]1000[/COLOR][SIZE=+1][COLOR=BLUE][B])[/B][/COLOR][/SIZE][SIZE=+1][COLOR=BLUE][B];[/B][/COLOR][/SIZE]                [COLOR=GREEN][I]// wait 1 second
    31. [COLOR=#cccccc]30: [/COLOR][/I][/COLOR]        [SIZE=+1][COLOR=BLUE][B]}[/B][/COLOR][/SIZE]
    32. [COLOR=#eeeeee]31: [/COLOR]    [SIZE=+1][COLOR=BLUE][B]}[/B][/COLOR][/SIZE]
    33.  
    34. [/SIZE][/FONT]
    35.  
    Code formatting/colorization by http://www.chamisplace.com/colorizer/cc.asp
     
    Last edited: Mar 29, 2009
  9. thatoneguy

    AAC Fanatic!

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

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


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

    Thread Starter New Member

    Mar 25, 2009
    9
    0
    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.
     
  11. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    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.
     
  12. yliu

    Thread Starter New Member

    Mar 25, 2009
    9
    0
    Hi,

    Here is the hardware connection of my circuit.
    [​IMG]
    Cheers,
    Y.
    [​IMG][​IMG]
     
  13. yliu

    Thread Starter New Member

    Mar 25, 2009
    9
    0
    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.


     
  14. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    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.
     
  15. yliu

    Thread Starter New Member

    Mar 25, 2009
    9
    0
    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.


     
  16. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    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
     
  17. yliu

    Thread Starter New Member

    Mar 25, 2009
    9
    0
    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.
     
  18. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    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...
     
  19. xushirui

    New Member

    Apr 7, 2009
    8
    0
    You may try Comm Operator instead of Hyper Term. Comm Operator support send data in ascii, hex and dec, as well as send a file to serial port. It can also show received data in ascii, hex or decimal.

    Here is the link.
    http://www.serialporttool.com/CommOpInfo.htm
     
Loading...