0

I am currently working on a project to select different outputs based on a text input from a laptop via USB.

What I am currently trying to do is send a character out from my PIC18F1320 to HyperTerminal, I have tried using variables but was recieveing rnadom characters such as smiley faces etc. I simplified my code to simply print a 1 however I am still recieving smily face symbols. I have checked that I am using the correct baud rate and there is definite communication between my laptop and the PIC, however at this point I am completely stuck as I have no idea why random characters are being output.

I have extremely little knowledge of C and any help would be appreciated

    #include <p18f1320.h>
    #include <usart.h>
    #include <stdio.h>
    #include <stdlib.h>

    void main()
    { 
        OSCCON = 0x70;  // 8MHz internal clock

        // Configure USART
        OpenUSART(  USART_TX_INT_OFF    &
                    USART_RX_INT_OFF    &
                    USART_ASYNCH_MODE   &
                    USART_EIGHT_BIT     &
                    USART_CONT_RX       &
                    USART_BRGH_LOW,
                    12);

        while (1)
        {
            putrsUSART("1");
        }
        CloseUSART();
    }
  • If you try to print only one time (try with `putrsUSART("Hello world\r\n")`), does it work? If yes, you should add a delay between two `putrsUSART` invocations. – ouah Feb 04 '14 at 23:24
  • @ouah thanks for your quick response. Tryint the Hello world line you suggested unfortunately yielded the same results. However changing the `putrsUSART("1")` to `putcUSART('1')` works. Although I'm not too sure about the difference between these functions – user2887182 Feb 04 '14 at 23:35

2 Answers2

0

The internal RC oscillator on the PIC is not precise enough to run the USART at the desired speed - it can vary by ±2% from the expected frequency, which is probably far out enough to be causing the errors you're seeing. You will need to attach and configure an external crystal for correct results.

0

Put your data in RAM and send. See Harvard Architecture

char buf[10];
strcpy(buf, "Hello\n");  // This should cal special ROM to RAM strcpy();
putrsUSART(buf);

Note: "I have checked that I am using the correct baud rate" may not be right. Until you received valid data, I would not be too confident about this.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • Thanks for your help, I've managed to recieve data however I've had to make `char buf[10] = "Hello\n"` and then use `putsUSART(&buf)`. I'm unfortunately extremely new to C, do you have any idea why this works but trying to print a string any other way gives random characters? (Printing a single character causes no issue, it's only when it's a string) – user2887182 Feb 05 '14 at 03:58
  • @user2887182 Note: `putsUSART(&buf[0])` may also work. The other ways have problems because with "Harvard Architecture", variable data is in one address space and fixed data (program and constant string like "Hello\n") are in another. Only select routines like `strcpy()` (or so I thought) and special constructions like `char buf[10] = "Hello\n"` move data from constant space to variable address space. This is all a lot to take in for a learner. Advice: Keep reading the compiler's documentation and good luck. – chux - Reinstate Monica Feb 05 '14 at 04:12