首页 > 解决方案 > 如何在主代码中使用 USART 中断字符变量?

问题描述

我正在使用 PIC18F4550,并按照https://www.electronicwings.com/pic/pic18f4550-usart中给出的示例,该程序有效,因为当我通过蓝牙(HC05)发送一个字符时,它接收到它然后传输相同的字符,问题是,这是在中断 USART 函数中完成的,如果我尝试比较在主代码中接收到的字符,例如

如果(数据 == 'a')

条件从未满足,但如果我在同一个中断函数中这样做,它确实可以识别字符。

我正在使用带有 XC8 编译器的 MPLAB X v5.2。问题不在于蓝牙或使用的应用程序,因为我以前在 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*/
}                                 

正如您在代码中看到的,中断函数内部的条件有效,因为如果我通过蓝牙(APP)发送一个“a”,一个“a”是我在同一个应用程序中收到的,与一个“b”相同,但是,当我收到“c”时,什么也没有发生。所以我想问题是如何声明变量“数据”或与该变量相关的东西,我没有尝试将其设为全局但我很确定必须有更好的解决方案。中断完成后,可能会擦除“数据”中的内容。

我希望“数据”内容在主代码中可用,但我不知道为什么它只能在 USART 中断函数中识别。

我会很感激你的帮助。

标签: cpicmplab

解决方案


您需要在 FIFO 或其他缓冲解决方案中捕获接收到的数据,以便在主“线程”中使用。

这个想法是从你的 ISR 中不断地缓冲输入字节,同时在你闲暇时从主循环中获取输入。

这是一个简单的 COM 端口解决方案的示例……我没有对其进行测试,但它显示了 echo 应用程序所需的最少步骤。

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 上的生产。


推荐阅读