2017-02-07 3 views
1

저는 RGB LED 프로젝트를 설계 중이며 PIC12F1572에 의해 제어됩니다. 내가 사용하고있는 소프트웨어는 XC8 컴파일러가있는 MPLAB IDE입니다. 계획은 직렬 통신을 사용하여 PIC에 LED RGB 조합 데이터 명령을 보내 변수가 LED 깜박임과 발광을 수행하도록 저장하는 것입니다. PC 단말기에서 RGBFF00001FF001F0을 보내고 이것을 하나의 배열로 포착하지만 배열에서 수집 한 값을보고 싶을 때 다시 EUSART_Write (배열)을 한 다음 각 문자 사이에 공백이있는 값을 얻었습니다. [RGBFF 0 0 0 0 1 FF 0 0 1 F 0 \ n] 이 배열 값에서 내 다음 작업이 의존하기 때문에 일어난다. 내 코드를 여기에서 공유합니다 : 이 정보를 찾는 위치에 대한 조언이나 조언은 크게 감사하겠습니다.PIC12F1572 UART 송신기 및 수신기 문제

감사합니다. main.c를

#include "mcc_generated_files/mcc.h" 
#include <stdlib.h> 
#include <stdio.h> 
#include "atoh.h" 
#include "LED.h" 
#define _XTAL_FREQ 16000000 
#define FRAMESIZE 18 


void main(void) 
{ 
    uint8_t data ,i,j; 

    uint16_t R_value, G_value ,B_value; 
    uint8_t value; 
    uint8_t RX_Buffer[FRAMESIZE] = {0}; 
    uint8_t RGB_data[6] ,HEX_data[6]; 

    // initialize the device 
    SYSTEM_Initialize(); 
    INTERRUPT_GlobalInterruptEnable();      // Enable the Global Interrupts 
    INTERRUPT_PeripheralInterruptEnable();     // Enable the Peripheral Interrupts 

    // while(1) 
    // { 
    // //EUSART_Write(0x61); 
     while (!RCIF) 
     { 
      data = EUSART_Read();        // Read received character 
      for (i = 0; i<FRAMESIZE ; i++) 
      { 
      RX_Buffer[i] = data; 
      } 

      EUSART_Write(data); 
      EUSART_Write(RX_Buffer);  //HERE I RECHECK MY COLLECTED VALUE 
     //check if any data is received 
      } 

      if(RX_Buffer[0]=='R' && RX_Buffer[FRAMESIZE-1] == '\n') 
      { 
       //ASCII to HEX separate values 
       for (j = 0 ; j = 5; j++)     // get the RGB value in the separate array 
       { 
        RGB_data[j] = RX_Buffer[j+3]; 
        HEX_data[value] = RGB_data[j]/16; 
        EUSART_Write(HEX_data); 
       } 

       // uint32_t number = (uint32_t)strtol(HEX_data, NULL, 16); 
       // R_value = number >>16; 
       // G_value = (number & 0xffff) >> 8; 
       // B_value = (number & 0x0000FF); 

       R_value = (uint16_t) atoh(HEX_data[0], HEX_data[1]); 
       G_value = (uint16_t) atoh(HEX_data[2], HEX_data[3]); 
       B_value = (uint16_t) atoh(HEX_data[4], HEX_data[5]); 
       EUSART_Write(R_value); 
      } 

       SetLedColor(R_value,G_value,B_value); 



    } 

그냥 UART.c 넣다

#include "eusart.h" 
#include "LED.h" 
#include <stdlib.h> 
#include <stdio.h> 
//#include "atoh.h" 
/** 
    Section: Macro Declarations 
*/ 
#define EUSART_TX_BUFFER_SIZE 8 
#define EUSART_RX_BUFFER_SIZE 8 
#define _XTAL_FREQ 16000000 
#define FRAMESIZE 18 
/** 
    Section: Global Variables 
*/ 

static uint8_t eusartTxHead = 0; 
static uint8_t eusartTxTail = 0; 
static uint8_t eusartTxBuffer[EUSART_TX_BUFFER_SIZE]; 
volatile uint8_t eusartTxBufferRemaining; 

static uint8_t eusartRxHead = 0; 
static uint8_t eusartRxTail = 0; 
static uint8_t eusartRxBuffer[EUSART_RX_BUFFER_SIZE]; 
volatile uint8_t eusartRxCount; 


/** 
    Section: EUSART APIs 
*/ 

void EUSART_Initialize(void) 
{ 
    // disable interrupts before changing states 
    PIE1bits.RCIE = 0; 
    PIE1bits.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; 
    BAUDCON = 0x08; 

    // SPEN enabled; RX9 8-bit; CREN enabled; ADDEN disabled; SREN disabled; 
    RCSTA = 0x90; 

    // TX9 8-bit; TX9D 0; SENDB sync_break_complete; TXEN enabled; SYNC asynchronous; BRGH hi_speed; CSRC slave; 
    TXSTA = 0x24; 

    // Baud Rate = 9600; SPBRGL 160; 
    SPBRGL = 0xA0; 

    // Baud Rate = 9600; SPBRGH 1; 
    SPBRGH = 0x01; 


    // initializing the driver state 
    eusartTxHead = 0; 
    eusartTxTail = 0; 
    eusartTxBufferRemaining = sizeof(eusartTxBuffer); 

    eusartRxHead = 0; 
    eusartRxTail = 0; 
    eusartRxCount = 0; 

    // enable receive interrupt 
    PIE1bits.RCIE = 1; 
} 

uint8_t EUSART_Read(void) 
{ 
    uint8_t readValue = 0; 

    while(0 == eusartRxCount) 
    { 
    } 

    readValue = eusartRxBuffer[eusartRxTail++]; 
    if(sizeof(eusartRxBuffer) <= eusartRxTail) 
    { 
     eusartRxTail = 0; 
    } 
    PIE1bits.RCIE = 0; 
    eusartRxCount--; 
    PIE1bits.RCIE = 1; 

    return readValue; 
} 



void EUSART_Write(uint8_t txData) 
{ 
    while(0 == eusartTxBufferRemaining) 
    { 
    } 

    if(0 == PIE1bits.TXIE) 
    { 
     TXREG = txData; 
    } 
    else 
    { 
     PIE1bits.TXIE = 0; 
     eusartTxBuffer[eusartTxHead++] = txData; 
     if(sizeof(eusartTxBuffer) <= eusartTxHead) 
     { 
      eusartTxHead = 0; 
     } 
     eusartTxBufferRemaining--; 
    } 
    PIE1bits.TXIE = 1; 
} 

void EUSART_Transmit_ISR(void) 
{ 

    // add your EUSART interrupt custom code 
    if(sizeof(eusartTxBuffer) > eusartTxBufferRemaining) 
    { 
     TXREG = eusartTxBuffer[eusartTxTail++]; 
     if(sizeof(eusartTxBuffer) <= eusartTxTail) 
     { 
      eusartTxTail = 0; 
     } 
     eusartTxBufferRemaining++; 
    } 
    else 
    { 
     PIE1bits.TXIE = 0; 
    } 
} 

void EUSART_Receive_ISR(void) 
{ 

    if(1 == RCSTAbits.OERR) 
    { 
     // EUSART error - restart 

     RCSTAbits.CREN = 0; 
     RCSTAbits.CREN = 1; 
    } 

    // buffer overruns are ignored 
    eusartRxBuffer[eusartRxHead++] = RCREG; 
    if(sizeof(eusartRxBuffer) <= eusartRxHead) 
    { 
     eusartRxHead = 0; 
    } 
    eusartRxCount++; 
} 

void EUSART_Put(const unsigned char *string) 
{ 
    //int i; 
    for (i=0;string[i]!='\0';i++) 
    { 
     EUSART_Write(string[i]); 
    } 
} 

답변

0

그래서 UART 프레임을 저장하고 에코 백을 내 문제가 여기 내 방식으로 해결했습니다. 여기에 대한 대답은 입니다. 코드 :

void main(void) 
{ 
    uint8_t data,i,j,got_char; 

    uint8_t R_value, G_value ,B_value; 
    uint8_t value; 
    uint8_t RX_Buffer[FRAMESIZE]; 
    uint8_t RGB_data[6] ,HEX_data[6]; 

    // initialize the device 
    SYSTEM_Initialize(); 
    INTERRUPT_GlobalInterruptEnable();      // Enable the Global Interrupts 
    INTERRUPT_PeripheralInterruptEnable();     // Enable the Peripheral Interrupts 

    while (1) 
{ 

     if (EUSART_DataReady) 
     { 
      for (i = 0; i<FRAMESIZE; i++) 
      { 
       RX_Buffer[i] = EUSART_Read(); 
       if (RX_Buffer[i] == '\n') 
        break; 
      } 

      RX_Buffer[i] = '\n';      //append '\n' at the end of stoaring array for detection of frame 
      RX_Buffer[i+1] = '\0';      // End of an array 
      EUSART_WriteAnArrayOfBytes(RX_Buffer); 


     if(RX_Buffer[0]=='R' && RX_Buffer[FRAMESIZE-2] == '\n') //check for correct frame 
      { 

      LATAbits.LATA2 = 1; 
      __delay_ms(2000); 
      LATAbits.LATA2 = 0; 
      __delay_ms(1000); 
      } 
     } 

    }  

} 
0

그냥 EUSART_Put() 같은 당신이해야 별도의 EUSART_Write()을 가지고, 당신은 UART를 통해 데이터를 전송하는 데 사용하는 기능을 포장 항상 일 그 곳 곳곳에 전화하는 것은 나쁜 습관입니다. 이 여기에 정의되어 있기 때문에, 어쩌면 EUSART_Write(R_value);에서 오류가 발생합니다 :

uint16_t R_value, G_value ,B_value; 

실제로는 2 바이트 (비어있는 바이트가 나온 곳)을 보냈습니다.

+0

EUSART_Write 하지만 여전히 동일한 결과 .. 그리고 나는이 EUSART_Write (RX_Buffer [i])에 대해 더 많은 것을 시도한다. 그럼 다른 문자가 가르쳐 charatcter 사이 appraing .. – Garryp

+0

귀하의 UART가 8 비트 단어 (바이트) 대신 16 비트 단어를 보내도록 설정되어 있는지 확인, 나는 잘 모르겠지만'BRG16 16bit_generator 비트에서 귀하의 초기화 좀 봐 암호 – mrbm