Как использовать переменную прерывания USART в основном коде? - PullRequest
0 голосов
/ 21 сентября 2019

Я использую PIC18F4550, и, следуя примеру, приведенному в https://www.electronicwings.com/pic/pic18f4550-usart,, программа работает, потому что, когда я отправляю символ через Bluetooth (HC05), он получает его, а затем передает тот же символ, проблема в том, чтосделано в функции прерывания USART, и если я пытаюсь сравнить символ, полученный в основном коде, например

if (data == 'a')

Условие никогда не выполняется, но если ясделать это в той же функции прерывания, он распознает символ.

Я использую MPLAB X v5.2 с компилятором XC8.Проблема не в Bluetooth или используемом приложении, потому что я использовал его ранее с Arduino.

                               MAIN CODE
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pic18f4550.h>
#include "Configuration_Header_File.h"
#include "LCD_16x2_8-bit_Header_File.h"

void USART_Init(int);

#define F_CPU 8000000/64

char data;

void __interrupt(low_priority) ISR(void)
{
  while(RCIF==0);
  data=RCREG;

  LCD_Char(data);

  while(TXIF==0); 
  if(data=='b')
    TXREG='b';
  if(data=='a')
      TXREG='a';


}

void main()
{
OSCCON=0x72;
MSdelay(10);
LCD_Init();
USART_Init(9600);    
LCD_String_xy(1,0,"Receive");
LCD_Command(0xC0);
while(1){

       if(data=='c')                         /* THIS IS WHAT IS WRONG */
      TXREG='c';
    }
    }



    void USART_Init(int baud_rate)
{   
float temp;
TRISC6=0;           /*Make Tx pin as output*/
TRISC7=1;           /*Make Rx pin as input*/
temp=(((float)(F_CPU)/(float)baud_rate)-1);     
SPBRG=(int)temp;    /*baud rate=9600 SPBRG=(F_CPU /(64*9600))-1*/
TXSTA=0x20;         /*TX enable; */
RCSTA=0x90;         /*RX enanle and serial port enable*/
INTCONbits.GIE=1;   /*Enable Global Interrupt */
INTCONbits.PEIE=1;  /*Enable Peripheral Interrupt */
PIE1bits.RCIE=1;    /*Enable Receive Interrupt*/
PIE1bits.TXIE=1;    /*Enable Transmit Interrupt*/
}                                 

Как вы можете видеть в коде, условия внутри функции прерывания работают, потому что, если я отправляю 'a 'via bluettoth (APP),' a '- это то, что я получил в том же приложении, и то же самое с' b ', но когда я получил' c ', ничего не происходит.Поэтому я предполагаю, что проблема в том, как объявить переменную «data» или что-то связанное с этой переменной, я не пытался сделать ее глобальной, но я уверен, что должно быть лучшее решение.Возможно, содержимое в «данных» стирается, когда прерывание выполнено.

Я ожидаю, что содержимое «данных» будет доступно в основном коде, но я не знаю, почему оно распознается только в функции прерывания USART.,

Буду признателен за вашу помощь.

1 Ответ

0 голосов
/ 21 сентября 2019

Вам необходимо захватить полученные данные в FIFO или другом решении для буферизации для использования в главном «потоке».

Идея состоит в том, чтобы постоянно буферизовать входные байты из вашего ISR и при захвате вводана досуге из основного цикла.

Вот пример простого решения для COM-порта ... Я не проверял его, но он показывает минимальные шаги, необходимые для эхо-приложения.

char rx_buffer[16];
char tx_buffer[16];

_fifo_t rx_fifo;   // need 2 buffers, one for sending, and one for receiving.
_fifo_t tx_fifo;

void __interrupt(low_priority) ISR(void)
{
    // never wait for a flag to becom set or reset within an ISR.  the ISR will be 
    // called automatically when the 
    while (RCIF)
    {
        data=RCREG;
        LCD_Char(data);  // you should considfer moving this out o the ISR.
        FifoPutByte(&rx_fifo, data); // check for overruns is handled by the fifo module.
    }

    while (TXIE && TXIF && !FifoEmpty(&tx_fifo))
         TXREG = FifoGetByte(&tx_fifo);

    if (FifoEmpty(&tx_fifo))  // if no more data to send, disable TX interrupt.
         TXIE = 0;
}

int COM_Transmit(char c)
{
    if (!FifoPutByte(&tx_fifo, c))
        return 0;                    // buffer is full!

    TXIE = 1;  // enable UART TX interrupts

    // now comes the tricky part, you need to 'prime' the transmission of bytes 
    // in the transmit fifo. This operation MUST be done 
    // with interrupts disabled.

    if (TXIF)  // can only be true if TX interrupts are not kicking in.
    {
        PEIE = 0;  // disable peripheral interrupts
        TXREG = FifoGetByte(&tx_fifo); // clears TXIF
        PEIE = 1;  // enable peripheral inrerrupts

        // ISR will be called when UART is done sending the byte in TXREG.
    }
}

void main()
{
    /* initialization code... */

    FifoInit(&rx_fifo, rx_buffer, sizeof(rx_buffer));
    FifoInit(&tx_fifo, tx_buffer, sizeof(tx_buffer));

    while(1) 
    {
        // simple UART echo code

        while (!FifoEmpty(&rx_fifo) && !FifoFull(&tx_fifo))
            COM_Transmit(FifoGetByte(&rx_fifo));

        /* doing the other stuff that your controller must do may take some time */
        /* You must make sure that rx_buffer is large enough to capture any bytes */
        /* that could be received during this delay, at 9600 bauds, count 1ms per char */
        MSdelay(15);  // for example...  A larger processing time will warrant larger buffer size for rx_fifo.  
    }
}

включаемый файл:

#ifndef _fifo_h
#define _fifo_h
/* *****************************************************************************
 *
 *  file: fifo.h
 *
 *  Simple byte fifo definitions.
 *
 ******************************************************************************/

#define FIFO_DEBUG  0

typedef struct _fifo_t
{
    volatile unsigned char* pHead;          //  head of data to extract.
    volatile unsigned char* pTail;          //  tail of data for insertion.
    unsigned char* pStart;                          //  start of buffer.
    unsigned char* pEnd;                                //  end of buffer.
    volatile unsigned int nOverruns;        //  overrun counter, in bytes.
    volatile unsigned int nUnderruns;       //  underrun counter, in bytes.
#ifdef FIFO_DEBUG
        volatile unsigned int nWaterMark;   //  Water mark indicating the maximum number of bytes ever held.
#endif
} fifo_t;


void FifoInit(fifo_t* fifo, unsigned char* buffer, unsigned int len);
void FifoReset(fifo_t* fifo);
int FifoEmpty(fifo_t* fifo);
unsigned int FifoGetFreeSpace(fifo_t* fifo);

// returns nuèmber of bytes available for GET op
unsigned int FifoGetCount(fifo_t* fifo);

// returns total buffer length
unsigned int FifoGetSize(fifo_t* fifo);

// returns the number of bytes in overrun
unsigned int FifoPut(fifo_t* fifo, const void* data, unsigned int len);

//  return the number of bytes copied to data buffer.
unsigned int FifoGet(fifo_t* fifo, void* data, unsigned int len);

//  returns negative value if fifo was empty
int FifoGetByte(fifo_t* fifo);

// returns non zetro value on success.
int FifoPutByte(fifo_t* fifo, unsigned char c);


// returns a contiguous buffer inside fifo, the length of contiguous part is returned in availLen. 
//  caller is responsible for advancing get/put pointer when the data is consumed.
unsigned char* FifoGetContiguousPutBuffer(fifo_t* fifo, unsigned int reqLen, unsigned int* availLen);
unsigned char* FifoGetContiguousGetBuffer(fifo_t* fifo, unsigned int reqLen, unsigned int* availLen);

// - peeks at data inside fifo, with optional offset from GET pointer, 
//   the length of contiguous part is returned in availLen. 
// - buffer must be large enough to hold reqLen bytes.
// - the value returned is either a direct pointer to fifo buffer, if the data is contuguous, or buffer.
//    if the data was copied to it, or NULL if the data is not avialable in buffer.
// - caller is responsible for advancing get/put pointer when the data is consumed.
void* FifoPeekBuffer(fifo_t* fifo, unsigned int offset, unsigned int reqLen, void* buffer);

// - Modifies a single byte at offset from GET pointer.
// - returns non-zero if successful.
char FifoPoke(fifo_t* fifo, unsigned int offset, unsigned char c);

// NOTE: interrupts should be disabled around calls to this function.
void FifoSetCount(fifo_t* fifo, unsigned int count);

unsigned int FifoAdvancePutPtr(fifo_t* fifo, unsigned int len);
unsigned int FifoAdvanceGetPtr(fifo_t* fifo, unsigned int len);

#endif

.c модуль:

/* *****************************************************************************
 *
 *  file: fifo.c
 *
 *  Simple byte fifo definitions.
 *
 ******************************************************************************/

#include <string.h> //  memcpy()
#include "fifo.h"

void FifoInit(fifo_t* fifo, unsigned char* buffer, unsigned int len)
{
    fifo->pStart = (unsigned char*)(buffer);
    fifo->pEnd = (unsigned char*)((buffer) + (len));
    fifo->pHead = (unsigned char*)(buffer);
    fifo->pTail = (unsigned char*)(buffer);
    fifo->nOverruns = 0;
#ifdef FIFO_DEBUG
    fifo->nWaterMark = 0;
#endif
}

void FifoReset(fifo_t* fifo)
{
    fifo->pHead = fifo->pStart;
    fifo->pTail = fifo->pStart;
    fifo->nOverruns = 0;
}

int FifoEmpty(fifo_t* fifo)
{
    return (fifo->pHead == fifo->pTail);
}

unsigned int FifoGetFreeSpace(fifo_t* fifo)
{
    return ((fifo->pEnd - fifo->pStart) - FifoGetCount(fifo)) - 1;
}

unsigned int FifoGetCount(fifo_t* fifo)
{
    unsigned char* pHead, * pTail;
    unsigned int nResult;

    pHead = (unsigned char*)fifo->pHead;
    pTail = (unsigned char*)fifo->pTail;

    if (pTail >= pHead)
    {
        nResult = pTail - pHead;
    }
    else
    {
        nResult = (pTail - fifo->pStart) + (fifo->pEnd - pHead);
    }

    return nResult;
}

unsigned int FifoGetSize(fifo_t* fifo)
{
    return fifo->pEnd - fifo->pStart;
}

unsigned int FifoPut(fifo_t* fifo, const void* data, unsigned int len)
{
    unsigned int nMax, nLeft;
    unsigned char* pTail, * pHead;
    const unsigned char* p;

    nLeft = (len);
    p = (const unsigned char*)data;

    while(nLeft)
    {
        pHead = (unsigned char*)fifo->pHead;
        if (pHead > fifo->pTail)
        {
            nMax = (pHead - fifo->pTail) - 1;
        }
        else
        {
            nMax = fifo->pEnd - fifo->pTail;
        }

        if (nMax > nLeft)
        {
            nMax = nLeft;
        }

        if (!nMax)
        {
            fifo->nOverruns += nLeft;
            break;
        }

        memcpy((void*)fifo->pTail, p, nMax);

        pTail = (unsigned char*)fifo->pTail + nMax;
        if (pTail >= fifo->pEnd)
        {
            pTail = fifo->pStart;
        }
        fifo->pTail = pTail;
        p += nMax;
        nLeft -= nMax;
    }
#ifdef FIFO_DEBUG
    nMax = FifoGetCount(fifo);
    if (nMax > fifo->nWaterMark)
    {
        fifo->nWaterMark = nMax;
    }
#endif
    return nLeft;
}

unsigned int FifoGet(fifo_t* fifo, void* data, unsigned int len)
{
    unsigned int nMax, nLeft;
    unsigned char* p, * pTail, * pHead;

    nLeft = (len);
    p = (unsigned char*)(data);

    while(nLeft)
    {
        pTail = (unsigned char*)fifo->pTail;
        if (pTail >= fifo->pHead)
        {
            nMax = pTail - fifo->pHead;
        }
        else
        {
            nMax = fifo->pEnd - fifo->pHead;
        }

        if (!nMax)
        {
            break;
        }

        if (nMax > nLeft)
        {
            nMax = nLeft;
        }

        memcpy(p, (void*)fifo->pHead, nMax);

        pHead = (unsigned char*)fifo->pHead + nMax;
        if (pHead >= fifo->pEnd)
        {
            pHead = fifo->pStart;
        }
        fifo->pHead = pHead;
        p += nMax;
        nLeft -= nMax;
    }
    return len - nLeft;
}

int FifoPutByte(fifo_t* fifo, unsigned char c)
{
    int result = 0;

    if (FifoGetFreeSpace(fifo))
    {
        unsigned char* pTail = (unsigned char*)fifo->pTail;

        *pTail++ = c;

        if (pTail >= fifo->pEnd)
        {
            pTail = fifo->pStart;
        }

        fifo->pTail = pTail;

        result = 1;
    }
    return result;
}


int FifoGetByte(fifo_t* fifo)
{
    int result = -1;
    if (fifo->pHead != fifo->pTail)
    {
        unsigned char* pHead = (unsigned char*)fifo->pHead;

        result = *(fifo->pHead);

        if (++pHead >= fifo->pEnd)
        {
            pHead = fifo->pStart;
        }

        fifo->pHead = pHead;
    }
    return result;
}

unsigned char* FifoGetContiguousPutBuffer(fifo_t* fifo, unsigned int reqLen, unsigned int* availLen)
{
    unsigned int nMax;
    unsigned char* pHead;

    pHead = (unsigned char*)fifo->pHead;

    if (pHead > fifo->pTail)
    {
        nMax = (pHead - fifo->pTail) - 1;
    }
    else
    {
        nMax = fifo->pEnd - fifo->pTail;
        if (pHead == fifo->pStart)
        {
            --nMax;
        }
    }

    if (nMax > reqLen)
    {
        nMax = reqLen;
    }

    *availLen = nMax;

    return (unsigned char*)fifo->pTail;
}

unsigned char* FifoGetContiguousGetBuffer(fifo_t* fifo, unsigned int reqLen, unsigned int* availLen)
{
    unsigned int nMax;
    unsigned char* pTail;

    pTail = (unsigned char*)fifo->pTail;
    if (pTail >= fifo->pHead)
    {
        nMax = pTail - fifo->pHead;
    }
    else
    {
        nMax = fifo->pEnd - fifo->pHead;
    }

    if (nMax > reqLen)
    {
        nMax = reqLen;
    }

    *availLen = nMax;

    return (unsigned char*)fifo->pHead;
}

void* FifoPeekBuffer(fifo_t* fifo, unsigned int offset, unsigned int reqLen, void* buffer)
{
    void* result;

    result = NULL;

    if ((reqLen + offset) <= FifoGetCount(fifo))
    {
        unsigned char* pHead, * pTail;

        pHead = (unsigned char*)fifo->pHead + offset;

        if (pHead >= fifo->pEnd)
        {
            pHead -= (fifo->pEnd - fifo->pStart);
        }

        pTail = pHead + reqLen;

        if (pTail <= fifo->pEnd)
        {
            result = pHead;
        }
        else
        {
            // need to copy, as requested data wraps around 
            unsigned char* p;
            unsigned int n;

            p = (unsigned char*)buffer;
            n = fifo->pEnd - pHead;

            memcpy(p, pHead, n);
            memcpy(p + n, fifo->pStart, reqLen - n);

            result = buffer;
        }
    }
    return result;
}

char FifoPoke(fifo_t* fifo, unsigned int offset, unsigned char c)
{
    unsigned char* p;
    char result = 0;

    if (offset < FifoGetCount(fifo))
    {
        p = (unsigned char*)fifo->pHead + offset;

        if (p >= fifo->pEnd)
        {
            p -= (fifo->pEnd - fifo->pStart);
        }

        *p = c;

        result = 1;
    }

    return result;
}

unsigned int FifoAdvancePutPtr(fifo_t* fifo, unsigned int len)
{
    unsigned int n;
    unsigned char* pTail;

    n = FifoGetFreeSpace(fifo);

    if (len > n)
    {
        fifo->nOverruns += (len - n);       
        len = n;
    }

    pTail = (unsigned char*)fifo->pTail + len;

    while (pTail >= fifo->pEnd)
    {
        pTail -= (fifo->pEnd - fifo->pStart);
    }

    fifo->pTail = pTail;

#ifdef FIFO_DEBUG
    n = FifoGetCount(fifo);
    if (n > fifo->nWaterMark)
    {
        fifo->nWaterMark = n;
    }
#endif

    return len;
}

unsigned int FifoAdvanceGetPtr(fifo_t* fifo, unsigned int len)
{
    unsigned int n;
    unsigned char* pHead;

    n = FifoGetCount(fifo);
    if (len > n)
    {
        fifo->nUnderruns += (len - n);
        len = n;
    }

    pHead = (unsigned char*)fifo->pHead + len;

    while (pHead >= fifo->pEnd)
    {
        pHead -= (fifo->pEnd - fifo->pStart);
    }

    fifo->pHead = pHead;

    return len;
}

void FifoSetCount(fifo_t* fifo, unsigned int count)
{
    unsigned int n;
    unsigned char* pTail;

    n = FifoGetSize(fifo);
    if (count > n)
    {
        fifo->nOverruns += (count - n);     
        count = n;
    }

    pTail = (unsigned char*)fifo->pHead + count;

    while (pTail >= fifo->pEnd)
    {
        pTail -= (fifo->pEnd - fifo->pStart);
    }

    fifo->pTail = pTail;

#ifdef FIFO_DEBUG
    n = FifoGetCount(fifo);
    if (n > fifo->nWaterMark)
    {
        fifo->nWaterMark = n;
    }
#endif
}

Обратите внимание, что эта структура fifo не самая эффективная для небольшой PIC, но, тем не менее, она должна работать нормально.Написание собственного FIFO - очень хорошее упражнение.Приведенный выше взят из производственного кода, я думаю, что последний раз использовал его на STM32.Это также использовалось в производстве на dsPIC и на Arduino.

...