Connect with us

UART problem for PIC microcontroller

Discussion in 'Microcontrollers, Programming and IoT' started by saur, Oct 29, 2010.

  1. saur

    saur

    8
    0
    Sep 28, 2010
    I am using pic24fj64ga002 controller UART. I am able to communicate with PC when I transmit/receive one character to PC. But my intention is to get a string from PC and process it in microcontroller. However, I am getting a weird problem. When I am sending more than 5 characters, eg. abcdefg, I am able to store 'ab' but I get some special character out of cdefg might be like $ or (.
    similarly if i enter "i am a microcontroller", i am able to store "i am a microcontr" followed by some special character.
    If the number of characters is less than 5, eg abcd, i am getting a special character or null value.



    #include <p24FJ64GA002.h>
    #define FCY 16000000UL // instruction cycle rate (ignore)
    #include <libpic30.h> // __delay32
    // __delay_ms and __delay_us
    // note: use only small values
    #include <stdio.h>
    #define USE_AND_OR // enable AND_OR mask setting
    #include <string.h>

    #define BAUD_19200 51 // brg for low-speed, 32 MHz clock
    #define TotalCharacters 50 //total number of characters to be entered
    //-----------------------------------------------------------------------------
    // Subroutines
    //-----------------------------------------------------------------------------

    // Initialize Hardware
    void init_hw()
    {
    LATBbits.LATB3 = 0; // write 0 into output latches
    LATBbits.LATB5 = 0;
    TRISBbits.TRISB3 = 0; // make green led pin an output
    TRISBbits.TRISB5 = 0; // make red led pin an output
    AD1PCFGbits.PCFG9 = 1; // make push button a digital input
    CNPU1bits.CN11PUE = 1; // enable pull-up for push button
    RPINR18bits.U1RXR = 11; // assign U1RX to RP11
    RPOR5bits.RP10R = 3; // assign U1TX to RP10
    }

    void serial_init(int baud_rate)
    {
    // set baud rate
    U1BRG = baud_rate;
    // enable uarts, 8N1, low speed brg
    U1MODE = 0x8000;
    // enable tx and rx
    U1STA = 0x0400;
    }

    void serial_puts(char str[])
    {
    int i;
    for (i = 0; i < strlen(str); i++)
    {
    // make sure buffer is empty
    while(U1STAbits.UTXBF);
    // write character
    U1TXREG = str;
    }
    }

    char serial_getc()
    {
    // clear out any overflow error condition
    if (U1STAbits.OERR == 1)
    U1STAbits.OERR = 0;
    // wait until character is ready
    while(!U1STAbits.URXDA);
    return U1RXREG;
    }

    // Wait for PB Press
    void wait_pb()
    {
    while (PORTBbits.RB15 == 1); // wait until pb pressed
    }

    //-----------------------------------------------------------------------------
    // Main
    //-----------------------------------------------------------------------------

    int c;
    char gReceivedData,gReceivedString4mPC[TotalCharacters];


    int gParameterTypes[10][5];
    int main(void)
    {

    init_hw(); // initialize hardware
    serial_init(BAUD_19200); // configure uart
    //step 1
    LATBbits.LATB3 = 1; // blink green LED for 500ms
    __delay32(8000000);
    LATBbits.LATB3 = 0;
    /*
    wait_pb(); // wait for pb press
    */
    serial_puts("Enter command "); // print greeting


    //step 2
    checkReceivedDataForCRnBS();
    //step 2.5
    // testStorageOf1DArray();


    //flush global variables
    flushGlobalVariables();


    return 0;
    }
    /*
    This function gets characters entered by user using PC and reads it in microcontroller. Then, it checks for carrage return,backspace, and a valid character. It forms
    a string and stores it in global variable gReceivedString4mPC.

    */
    void checkReceivedDataForCRnBS(){

    //step 2 start
    int lCounterNumberOfCharactersReceived = 0;
    //receiving character from UART
    while(1){
    gReceivedData = serial_getc();
    if(gReceivedData == 13){ break; }
    //checking for backspace
    if(gReceivedData == 8 ){
    if(lCounterNumberOfCharactersReceived>0){
    lCounterNumberOfCharactersReceived--;
    }
    }else{
    //if character is greater than or equal to ''(ascii 32) then, its alphabet or number or
    // special character such as space , comma, colon. Store it in array.
    if(gReceivedData >=32){
    gReceivedString4mPC[lCounterNumberOfCharactersReceived] = gReceivedData;
    //serial_puts(gReceivedString4mPC[lCounterNumberOfCharactersReceived]);
    lCounterNumberOfCharactersReceived++;
    }

    }

    }
    // if it is carrage return, add a null terminator
    gReceivedString4mPC[lCounterNumberOfCharactersReceived] = 0;


    }
     
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-