[Help] IC18F45K22 EUSART serial communication(transmit only)

Discussion in 'Embedded Systems and Microcontrollers' started by VoltVolt, Sep 24, 2013.

  1. VoltVolt

    Thread Starter New Member

    Jan 25, 2013
    12
    0
    hi everyone, just want to ask is there any problem from the below codes?
    I just want to print out "helloword" by using serial communication
    I'm using PIC18F45K22 and MPLABX IDE compiler...
    The codes can be smoothly run but it cannot be print out...
    Btw, i don't have interrupt, it is just a test code....

    Code ( (Unknown Language)):
    1. #include <htc.h>
    2. #include <string.h>
    3. char sendBuffer[11];
    4. char sendPosition=0;
    5. void sendUartString(const char string[10]);
    6. void init()
    7. {
    8. // Asynchronous Transmission Setup
    9. // 1. Initialize the SPBRGHx:SPBRGx register pair and the BRGH and BRG16 bits to achieve the desired baud rate
    10.         SPBRGH2=0X01;
    11.         SPBRG2=0XA0;
    12.     TXSTA2bits.BRGH=1;
    13.     BAUDCON2bits.BRG16=1;
    14. // 2. Set the RXx/DTx and TXx/CKx TRIS controls to ?1?.
    15.     TRISB = 0b11000000; //set TX2 and RX2 as output
    16.  
    17. // 3. Enable the asynchronous serial port by clearing the SYNC bit and setting the SPEN bit.
    18.     ANSELB = 0; //disable ANSEL
    19.     SYNC2 = 0;  //asynchronous mode
    20.     SPEN2 = 1;  //enable EUSART
    21.  
    22. // 4. If 9-bit transmission is desired, set the TX9 control bit. A set ninth data bit will indicate that the eight Least Significant data bits are an address when the receiver is set for address detection.
    23. // 5. Set the CKTXP control bit if inverted transmit data polarity is desired.
    24. // 6. Enable the transmission by setting the TXEN control bit. This will cause the TXxIF interrupt bit to be set.
    25.     TXEN2 = 1;
    26. // 7. If interrupts are desired, set the TXxIE interrupt enable bit. An interrupt will occur immediately provided that the GIE/GIEH and PEIE/GIEL bits of the INTCON register are also set.
    27.  
    28. // 8. If 9-bit transmission is selected, the ninth bit should be loaded into the TX9D data bit.
    29. // 9. Load 8-bit data into the TXREGx register. This will start the transmission.
    30. }
    31. void main()
    32. {
    33.     init();
    34.     //now you want to send something, so first load a string into the sendBuffer
    35.         while(1)
    36.         {
    37.         //sendUartString("helloworld");
    38.         }
    39. }
    40.  
    41.  
    42. void sendUartString(const char string[10])
    43. {
    44.     strcpy(sendBuffer,string); //please check the correct command,copy string[] to sendBuffer[]
    45.  
    46.     sendPosition=0; //start sending first char
    47.     while((sendPosition!= strlen(sendBuffer)) && sendPosition<10 )
    48.     {
    49.         if(TX2IF==1)//if Transmit register is empty
    50.         {
    51.             TX2REG=sendBuffer[sendPosition]; //send one more char
    52.             sendPosition++;
    53.         }
    54.     }
    55. }
     
  2. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    How is your setup between your PIC and the unit that receive your data. Describe it with so many details that we who are not sitting besides you on your workbench can understand it.
    By the way have debugged your code. The MPLAB simulator may be a big help here
     
  3. VoltVolt

    Thread Starter New Member

    Jan 25, 2013
    12
    0
    I'm using cytron UC00B USB to serial port converter...
    I connect the RX and TX from PIC to TX and RX from serial port respectively...
    and I only connect the 5 ICSP headers(MCLR,Vdd,Vss,PGC,PGD)...
    That's all my circuit...
     
  4. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Ah.. that may be error. Then a signal is going out in one end (TX) it evidently has to go to the receive (RX) on the other end. The most simple setup is
    RX-TX
    TX-RX
    GND-GND
    In order to get this working. You have to turn off hardware and software handshaking in your PC program
     
  5. VoltVolt

    Thread Starter New Member

    Jan 25, 2013
    12
    0
    Yes I did connect in this way:
    RX-TX
    TX-RX
    GND-GND
    Vcc-Vdd

    But somehow still couldn't work... :(
     
  6. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,534
    2,369
    Are you receiving on a PC Could it be your modem program?
    What program are you using?
    Does anything at all come through?
    I have found USART use on a PIC very simple to set up, but I have only used the assembly examples from Picmicro.
     
  7. techristian

    New Member

    Aug 27, 2013
    26
    2
    You must also make sure that the BAUD rate is the same on both ends.

    Dan
     
  8. VoltVolt

    Thread Starter New Member

    Jan 25, 2013
    12
    0
    receiving on a PC
    I am using MPLABX IDE

    Wow...
    I found assembly more hard to write...
    My school using assembly to teach intel processor 808x...
    I got that kind of phobia on write assemble haha...
    not confident at all....
     
  9. VoltVolt

    Thread Starter New Member

    Jan 25, 2013
    12
    0
    Yup...
    both PIC and computer has same baud rate(9600)
     
  10. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,534
    2,369
    If your port software has identified the USB port as a simulated RS232 you could try one of the free RS232 modem software such as RS232 HEX COM
    http://www.rs232pro.com/
    It has an auto search for ports and if it recognizes your USB port as valid, then this program should work.
    Max.
     
  11. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,387
    1,605
    What hardware do you have on your PIC? The signals on the PIC pins are the correct data, but not the correct levels for RS232.
     
  12. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,534
    2,369
    Good catch, I thought the OP was using a USB only, if using a hardware convertor to RS232, then the PIC end requires a MAX232 or equivalent IC.
    Max.
     
  13. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    As fas as I can see. The USB dongle that the OP use. Are made for directly interfacing with TTL levels. 3.3 or 5 volt depending on the settings
     
  14. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    Try connecting TX-TX and RX-RX.

    Put a 220 ohm series resistor between them, like this; TX-220-TX.

    Some modules say "TX" as that is the pin you connect to the micro's TX pin.
     
  15. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,534
    2,369
    The manual appears to make it quite clear as to the hook up, and it is now clear that the RS232 levels are not needed with this unit.
    What terminal program is being used? Hyperterminal or ?
    Have you tried the HEX COM program? If so set it to 8 bit receive and HEX, this way you should see anything that is coming over the link.
    Max.
     
  16. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    Thanks Max.

    One easy test is to hook the module's TX to its RX pin (PIC circuit disconnected), then send a byte from hyperterminal and see that it is echoed back to hyperterminal.
     
Loading...