+ Post New Thread
Results 1 to 4 of 4
  1. #1
    Member level 3
    Points: 624, Level: 5

    Join Date
    Oct 2017
    Posts
    58
    Helped
    1 / 1
    Points
    624
    Level
    5

    REceive uart data as hex value

    I am using Mplab x Ide with v3.61 on Xc8 compiler, PIC18F24K40. I used MCC generated UART code for send and receive data. For understanding I am clubling my code and posting part of it.

    My main code look like this.

    Code:
    #include "mcc_generated_files/mcc.h"
    #define EUSART_TX_BUFFER_SIZE 8
    #define EUSART_RX_BUFFER_SIZE 8
     
    /**
      Section: Global Variables
    */
     
    volatile uint8_t eusartTxHead = 0;
    volatile uint8_t eusartTxTail = 0;
    volatile uint8_t eusartTxBuffer[EUSART_TX_BUFFER_SIZE];
    volatile uint8_t eusartTxBufferRemaining;
     
    volatile uint8_t eusartRxHead = 0;
    volatile uint8_t eusartRxTail = 0;
    volatile uint8_t eusartRxBuffer[EUSART_RX_BUFFER_SIZE];
    volatile uint8_t eusartRxCount;
     
    
    #define LED_RX RC7 // Pin assigned RX LED
    #define LED_TX RC6 // Pin assigned TX LED
    #define LED RC2 // Pin assigned for LED 
    #define DE RC5
     
    
    unsigned int count=0;
    char data=0;
    static int DEVICE_ID=1;
    char rxbuf[50]=" ";
    unsigned char Function_code=0X03;
    static int index=0;
    static int rec_flag = 0;
    static int check_flag = 1;
     
    unsigned char buff[10];
     
    char data1[10];
    unsigned char buf[20]; //array to hold twenty bytes
     
    
     
    
     
    void EUSART_Initialize(void)
    {
        // disable interrupts before changing states
        PIE3bits.RCIE = 0;
        PIE3bits.TXIE = 0;
     
        // Set the EUSART module to the options selected in the user interface.
     
        // ABDOVF no_overflow; SCKP Non-Inverted; BRG16 16bit_generator; WUE disabled; ABDEN disabled; 
        BAUD1CON = 0x08;
     
        // SPEN enabled; RX9 8-bit; CREN enabled; ADDEN disabled; SREN enabled; 
        RC1STA = 0xB0;
     
        // TX9 8-bit; TX9D 0; SENDB sync_break_complete; TXEN enabled; SYNC asynchronous; BRGH hi_speed; CSRC master; 
        TX1STA = 0xA4;
     
        TRISCbits.TRISC7 = 1; //As Prescribed in Datasheet
        TRISCbits.TRISC6 = 1; 
        RC1STAbits.CREN=1;
        TX1STAbits.TXEN=1;
     
        // Baud Rate = 9600; SP1BRGL 207; 
        SP1BRGL = 0xCF;
     
        // Baud Rate = 9600; SP1BRGH 0; 
        SP1BRGH = 0x00;
     
    
        // initializing the driver state
        eusartTxHead = 0;
        eusartTxTail = 0;
        eusartTxBufferRemaining = sizeof(eusartTxBuffer);
     
        eusartRxHead = 0;
        eusartRxTail = 0;
        eusartRxCount = 0;
     
        // enable receive interrupt
        PIE3bits.RCIE = 1;
    }
     
    
     
    
     
    
     
    uint8_t EUSART_Read(void)
    {
        uint8_t readValue = 0;
     
        while(0 == eusartRxCount)
        {
        }
     
        readValue = eusartRxBuffer[eusartRxTail++];
        if(sizeof(eusartRxBuffer) <= eusartRxTail)
        {
            eusartRxTail = 0;
        }
        PIE3bits.RCIE = 0;
        eusartRxCount--;
        PIE3bits.RCIE = 1;
        eusartRxBuffer[8];
     
        return readValue;
    }
     
    void EUSART_Write(uint8_t txData)
    {
        while(0 == eusartTxBufferRemaining)
        {
        }
     
        if(0 == PIE3bits.TXIE)
        {
            TX1REG = txData;
        }
        else
        {
            PIE3bits.TXIE = 0;
            eusartTxBuffer[eusartTxHead++] = txData;
            if(sizeof(eusartTxBuffer) <= eusartTxHead)
            {
                eusartTxHead = 0;
            }
            eusartTxBufferRemaining--;
        }
        PIE3bits.TXIE = 1;
    }
     
    char getch(void)
    {
        return EUSART_Read();
    }
     
    void putch(char txData)
    {
        EUSART_Write(txData);
    }
     
    void EUSART_Transmit_ISR(void)
    {
     
        // add your EUSART interrupt custom code
        if(sizeof(eusartTxBuffer) > eusartTxBufferRemaining)
        {
            TX1REG = eusartTxBuffer[eusartTxTail++];
            if(sizeof(eusartTxBuffer) <= eusartTxTail)
            {
                eusartTxTail = 0;
            }
            eusartTxBufferRemaining++;
        }
        else
        {
            PIE3bits.TXIE = 0;
        }
    }
     
    void EUSART_Receive_ISR(void)
    {
     
        if(1 == RC1STAbits.OERR)
        {
            // EUSART error - restart
     
            RC1STAbits.CREN = 0;
            RC1STAbits.CREN = 1;
        }
     
        // buffer overruns are ignored
        eusartRxBuffer[eusartRxHead++] = RC1REG;
        if(sizeof(eusartRxBuffer) <= eusartRxHead)
        {
            eusartRxHead = 0;
        }
        eusartRxCount++;
    }
     
    
     
    
     
    
     
    
     
    
    /* Send Data Serially */
     void send_string(const char *x)
     {
         while(*x)
         {
            EUSART_Write(*x++); 
         }
     }
     
    
     
    
     
    
    /* Timer Interrupt Service Routine Program*/
    void Blink_Count()
    {
       if(PIR0bits.TMR0IF == 1)
       { 
           PIR0bits.TMR0IF =0;
           count=count+1;
           if(count>=15)
           {
              // LED=!LED;
               count=0;
     
           }
     
       }
     
    }
     
    void main(void)
    {
     
        while (1)
        {
       data= EUSART_Read();
         rxbuf[10]=data;
         printf("%s\n",&rxbuf[10]);
         __delay_ms(150);
     
        }
     
    }
    And tested Serial communication. with this code i could print welcome on Serial Monitor

    Code:
    void main(void)
    {
      EUSART_Initialize();
        while (1)
        {
         char array[20] = "Hello World";
         printf("%s\n",array);
          __delay_ms(150);
     
        }
     
    }
    The problem i am facing is. When i try to send data 0103000000Ac5CD In ASCii format i receice as charcter ,but when i try to send in hex format i could not able to receive nothing. If am printing data like i used in working hello print example i got garbage value.Click image for larger version. 

Name:	Helloword.jpg 
Views:	6 
Size:	122.7 KB 
ID:	142447Click image for larger version. 

Name:	Uart_out.jpg 
Views:	7 
Size:	232.7 KB 
ID:	142446Click image for larger version. 

Name:	Rs485+format.jpg 
Views:	6 
Size:	110.2 KB 
ID:	142445

    my questions:

    1) how to receive data from uart as string
    2) how to receive hex value.

    •   AltAdvertisement

        
       

  2. #2
    Super Moderator
    Points: 77,827, Level: 68
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    15,771
    Helped
    3590 / 3590
    Points
    77,827
    Level
    68

    Re: REceive uart data as hex value

    Hi,

    When i try to send data 0103000000Ac5CD In ASCii
    What is "0103000000Ac5CD"?
    I assume it is a string...and a string already is a array of ASCII characters.

    I don't kniw if this is your problem..
    But there are (at least) two types of how a string is stored:
    * one type has only one byte per character
    * and the other has two bytes per character.

    You need to read compiler documentation. Maye you can adjust it.

    Klaus
    Please don´t contact me via PM, because there is no time to respond to them. No friend requests. Thank you.



    •   AltAdvertisement

        
       

  3. #3
    Advanced Member level 4
    Points: 8,209, Level: 21
    Achievements:
    7 years registered
    paulfjujo's Avatar
    Join Date
    Jun 2008
    Location
    France 01120
    Posts
    1,324
    Helped
    269 / 269
    Points
    8,209
    Level
    21

    Re: REceive uart data as hex value

    hello,



    Where is defined Lt ?

    i don't undestand this

    Code:
    if(sizeof(eusartRxBuffer) <= eusartRxTail)

    maybe this could be more correct ?

    Code:
    if((sizeof(eusartRxBuffer) & lt )== eusartRxTail)



    •   AltAdvertisement

        
       

  4. #4
    Newbie level 5
    Points: 583, Level: 5

    Join Date
    Jan 2015
    Posts
    10
    Helped
    0 / 0
    Points
    583
    Level
    5

    Re: REceive uart data as hex value

    use sprintf of c stdio library



--[[ ]]--