1. sakishrist

    Thread Starter New Member

    Dec 4, 2009
    26
    0
    Hello,

    I am trying to send a simple "Hello World!" message to a terminal running on a PC through the com port of that PC. I still have a problem.

    The code I have is:
    Code ( (Unknown Language)):
    1. #include <p18f4620.h>
    2. //#pragma config WDT = OFF
    3. #pragma config MCLRE = OFF
    4. #include <usart.h>
    5. #include <delays.h>
    6. #pragma config PWRT = OFF, WDT = OFF, LVP = OFF
    7.  
    8.  
    9. void main()
    10. {
    11.  
    12. unsigned int i;
    13. TRISC = 0x00;
    14. TRISB =0x00;
    15. OSCCONbits.IRCF0 =1;
    16. OSCCONbits.IRCF1 =1;
    17. OSCCONbits.IRCF2 =1;
    18. OSCCONbits.SCS1 =1;
    19. OpenUSART(USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_LOW,103);
    20.  
    21. while(1)
    22. {
    23. PORTB=0x00;
    24. for(i=0;i<4000;i++);
    25.  
    26. PORTB=0xff;
    27. for(i=0;i<4000;i++);
    28.  
    29. putrsUSART( (const far rom char *)"Hello World!" );
    30. Delay10KTCYx(200);
    31.  
    32.  
    33. }
    34.  
    35. }
    I set spbrg to 103 because I used this:
    Code ( (Unknown Language)):
    1. With USART_BRGH_LOW:
    2. baud rate= FOSC / (64 * (spbrg + 1))
    I have connected pin 25 to 2 of the COM port and 5 of the COM port to gnd. The problem is that I'm getting some strange characters on the terminal and not the message. What could be the problem here? Could it be the baudrate? (The thing is that when I run it in simulation everything seems to be fine)

    Thanks
     
  2. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    It could well be but since you don't post a circuit or tell us your clock frequency, your PC baud rate, or for that matter what compiler you are using, we can only guess at the cause of your problem.
     
  3. sakishrist

    Thread Starter New Member

    Dec 4, 2009
    26
    0
    Sorry. I have attached the circuit. I am using the internal osc at 8 Mhz. I set the pc baudrate at 1200 (although I have tried others as well) and I use MPLAB IDE with C18.

    Thanks
     
  4. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    We still can't see your circuit!
     
  5. sakishrist

    Thread Starter New Member

    Dec 4, 2009
    26
    0
    Oops, I think I attached it now.
     
  6. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    Ok, we can see it now.

    The on chip USART is really designed to drive a RS232 level converter/RS232 dirver chip such as a Max232 to produce the correct RS232 signal levels.

    The RS232 standard defines a mark condition as -12Volt and a space as +12Volt.

    Your chip output gives a high logic level for a mark and a low for a space.

    The PC is pretty tolerant of low level signals and will probably work down to logic levels but if you are going to feed logic level signals into your PC you have to invert the signal. The simplest way would be to use an inverter gate such as 74LS04 or similar. Of course the best way would be to use the proper driver chip such as the Max232.
     
  7. sakishrist

    Thread Starter New Member

    Dec 4, 2009
    26
    0
    Is there a site that describes those two in detail so I can see what exactly I have to do?

    Thanks both of you :)
     
  8. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    You should add a 220 to 470 ohm resistor between the PIC and the DE9 serial plug. That is all you need to drive a PC serial port.

    Then you have to stop using the PIC USART and manually generate (bit bang) the serial. This is because the PIC USART does not have an invert option.

    There is some working C code here to manually generate the serial and send direct to PC with nothing needed besides a 470 ohm resistor;
    http://www.romanblack.com/bitbangserial.htm
     
  9. sakishrist

    Thread Starter New Member

    Dec 4, 2009
    26
    0
    I managed to invert the signal but the problem now is with the formula for finding the spbrg. As far as I can see my chip is working at 8Mhz so I used 415 for 1200 baudrate but when I compared the two signals (PC and PIC) with an oscilloscope I found out that 415 works for 3231 baudrate. So is there something wrong with the formula?

    I might use the manual method.

    Thanks (especially for the link :) )
     
  10. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    The USART and how it functions are well documented in the datasheet. You can download it for free.
     
Loading...