Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何在主代码中使用USART中断字符变量?_C_Pic_Mplab - Fatal编程技术网

C 如何在主代码中使用USART中断字符变量?

C 如何在主代码中使用USART中断字符变量?,c,pic,mplab,C,Pic,Mplab,我使用的是PIC18F4550,按照中给出的示例,该程序可以工作,因为当我通过蓝牙(HC05)发送一个字符时,它接收到该字符,然后发送相同的字符,问题是,这是在中断USART函数中完成的,如果我尝试比较主代码中接收到的字符,例如 如果(数据=='a') 该条件从未满足,但如果我在同一中断函数中这样做,它确实会识别该字符 我正在使用带有XC8编译器的MPLAB X v5.2。问题不在于蓝牙或应用程序,因为我以前在arduino上使用过它

我使用的是PIC18F4550,按照中给出的示例,该程序可以工作,因为当我通过蓝牙(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*/
}                                 
主代码
#包括
#包括
#包括
#包括
#包括“配置头文件.h”
#包括“LCD_16x2_8位_头文件.h”
无效USART_Init(int);
#定义F_CPU 8000000/64
字符数据;
无效中断(低优先级)ISR(无效)
{
而(RCIF==0);
数据=RCREG;
液晶显示器字符(数据);
而(TXIF==0);
如果(数据=='b')
TXREG='b';
如果(数据=='a')
TXREG='a';
}
void main()
{
OSCCON=0x72;
MSdelay(10);
LCD_Init();
USART_Init(9600);
LCD_字符串_xy(1,0,“接收”);
LCD_命令(0xC0);
而(1){
如果(data=='c')/*这就是问题所在*/
TXREG='c';
}
}
无效USART_初始(整数波特率)
{   
浮子温度;
TRISC6=0;/*使Tx引脚作为输出*/
TRISC7=1;/*将Rx引脚作为输入*/
温度=((浮点)(F_CPU)/(浮点)波特率)-1);
SPBRG=(int)temp;/*波特率=9600 SPBRG=(F_CPU/(64*9600))-1*/
TXSTA=0x20;/*TX启用*/
RCSTA=0x90;/*RX eNALE和串行端口启用*/
INTCONbits.GIE=1;/*启用全局中断*/
INTCONbits.PEIE=1;/*启用外围中断*/
PIE1bits.RCIE=1;/*启用接收中断*/
PIE1bits.TXIE=1;/*启用传输中断*/
}                                 
正如您在代码中所看到的,中断函数内部的条件是有效的,因为如果我通过bluettoth(应用程序)发送“a”,则“a”是我在同一应用程序中收到的,与“b”相同,但是,当我收到“c”时,什么也不会发生。所以我想问题在于如何声明变量“data”或与该变量相关的东西,我没有尝试使其全局化,但我确信一定会有更好的解决方案。中断完成后,“数据”中的内容可能会被删除

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


非常感谢您的帮助。

您需要在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.c
*
*简单字节fifo定义。
*
******************************************************************************/
#include//memcpy()
#包括“fifo.h”
void FifoInit(fifo_t*fifo,无符号字符*缓冲区,无符号整数长度)
{
fifo->pStart=(无符号字符*)(缓冲区);
fifo->pEnd=(无符号字符*)((缓冲区)+(len));
fifo->pHead=(无符号字符*)(缓冲区);
fifo->pTail=(无符号字符*)(缓冲区);
fifo->nOverruns=0;
#ifdef FIFO_调试
先进先出->nWaterMark=0;
#恩迪夫
}
无效fifo设置(fifo_t*fifo)
{
fifo->pHead=fifo->pStart;
fifo->pTail=fifo->pStart;
fifo->nOverruns=0;
}
int FifoEmpty(fifo\u t*fifo)
{
返回(fifo->pHead==fifo->pTail);
}
无符号int-FifoGetFreeSpace(fifo_t*fifo)
{
返回((fifo->pEnd-fifo->pStart)-FifoGetCount(fifo))-1;
}
无符号整数fifo计数(fifo\u t*fifo)
{
无符号字符*pHead,*pTail;
无符号整数结果;
pHead=(无符号字符*)fifo->pHead;
pTail=(无符号字符*)fifo->pTail;
如果(pTail>=pHead)
{
nResult=pTail-pHead;
}
其他的
{
nResult=(pTail-fifo->pStart)+(fifo->pEnd-pHead);
}
返回结果;
}
无符号整数fifo大小(fifo_t*fifo)
{
返回fifo->pEnd-fifo->pStart;
}
无符号整数fifo(fifo_t*fifo,常量无效*数据,无符号整数长度)
{
无符号整数nMax,nLeft;
无符号字符*pTail,*pHead;
常量无符号字符*p;
nLeft=(len);
p=(常量无符号字符*)数据;
while(nLeft)
{
pHead=(无符号字符*)fifo->pHead;
如果(pHead>fifo->pTail)
{
nMax=(pHead-fifo->pTail)-1;
}
其他的
{
nMax=fifo->pEnd-fifo->pTail;
}
如果(nMax>nLeft)
{
nMax=nLeft;
}
如果(!nMax)
{
fifo->nOverruns+=nLeft;
打破
}
memcpy((void*)fifo->pTail,p,nMax);
pTail=(无符号字符*)fifo->pTail+nMax;
如果(pTail>=fifo->pEnd)
{
pTail=fifo->pStart;
}
fifo->pTail=pTail;
p+=nMax;
nLeft-=nMax;
}
#ifdef FIFO_调试
nMax=先进先出(fifo);
如果(nMax>fifo->nWaterMark)
{
先进先出->nWaterMark=nMax;
}
#恩迪夫
返回nLeft;
}
无符号整数FifoGet(fifo_t*fifo,void*数据,无符号整数长度)
{
无符号整数nMax,nLeft;
无符号字符*p,*pTail,*pHead;
nLeft=(len);
p=(无符号字符*)(数据);
while(nLeft)
{
pTail=(无符号字符*)fifo->pTail;
如果(pTail>=fifo->pHead)
{
nMax=pTail-fifo->pHead;
}
其他的
{
nMax=fifo->pEnd-fifo->pHead;
}
如果(!nMax)
{
打破
}
如果(nMax>nLeft)
{
nMax=nLeft;
}
M