使用mplab X读取十六进制值 [英] Reading hex value using mplab X

查看:407
本文介绍了使用mplab X读取十六进制值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#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);
    }
} 





void main(void)
{
  EUSART_Initialize();
    while (1)
    {
     char array[20] = "Hello World";
     printf("%s\n",array);
      __delay_ms(150);
    }
}



我尝试过的事情:

我在Xc8编译器PIC18F24K40上的v3.61上使用了Mplab x Ide.我使用MCC生成的UART代码发送和接收数据.为了理解,我将我的代码合并并发布其中的一部分.

我的主要代码如下所示.


我面临的问题是.当我尝试以ASCii格式发送数据0103000000Ac5CD时,我以字符形式接收,但是当我尝试以十六进制格式发送时,我什么也收不到.如果正在像我在工作打招呼示例中使用的那样打印数据,则我得到了垃圾值.



What I have tried:

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.


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.

推荐答案

您必须更好地了解发送过程.您发送单个字节(通过访问char数组,因此字符串是char数组,它可以直接使用.但是要发送int,必须将整数复制到带有终止零的字节数组中.
You must better understand the send process. You send single bytes (by accessing an char array, so a string is a char array and it works out of the box. But for sending an int you must copy integer into byte array with a terminating zero.
char buffer[5] = {0};//memzero
memcpy( buffer, &intValue, 0);


并发送该缓冲区.在接收方,您必须将缓冲区反向复制为整数.


and send that buffer. On the receiver side you must reversly copy the buffer into an integer.


这篇关于使用mplab X读取十六进制值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆