- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 PIC18F4550,并遵循 https://www.electronicwings.com/pic/pic18f4550-usart 中给出的示例,该程序可以工作,因为当我通过蓝牙(HC05)发送一个字符时,它收到了它,然后传输了相同的字符,问题是,这是在中断 USART 函数中完成的,如果我尝试比较主代码中接收到的字符例如
if(数据=='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”是我在同一个应用程序中收到的,与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
}
请注意,对于小型 PIC,此 fifo 结构并不是最有效的,但它仍然可以正常工作。编写自己的 FIFO 是一个非常好的练习。上面的代码取自生产代码,我想我上次在 STM32 上使用它。它还被用于 dsPIC 和 Arduino 的生产中。
关于c - 如何在主代码中使用USART中断char变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58036412/
#include int main() { int i; for( i = 0; i< 10 ; i++ ) { printf("test1 &d", i);
我有一个连接到远程服务器以查询数据的 C# 程序。数据很大,所以查询大约需要 2 分钟才能完成。在这 2 分钟的时间内,互联网中断了。这导致作业无法完成,程序卡在获取数据例程中。 它建立了连接,但在选
语句 1: [2,4,6,7,8].each do |i| (i % 2 == 0) || (puts "Not even" && break) puts i end 声明 2: [2
我想知道 C++ 是否有任何处理中断的方法。我希望一个程序将信息存储在文本文件中,而另一个程序根据文本文件中的内容打印一条语句。由于我希望它尽可能准确,因此我需要在更新程序更新文件时中断打印程序。 最
我正在尝试终止一个线程,但它不会中断或停止。所有这些都是名为 Webots 的软件 Controller 的一部分。我用它来模拟多机器人系统。在每个机器人的 Controller 中,我启动了一个线程
中断10h可以使用的服务有很多, 我想问一下关于其中两个我很困惑的问题 首先是, AH = 06H SCROLL UP WINDOW AH = 07H SCROLL DOWN WINDOW 在 D
我有一个基本的 SQL 问题,如果有两个事件连接,"A"和 "B"到 SQL 服务器,假设两者之间发生死锁,那么为了避免死锁,SQL 服务器将回滚其中一个连接“A”或“B”的交易。假设 SQL Ser
我有一个 mapkit/ View ,它工作正常 - 但我滚动并在 2 - 10 次移动后我的应用程序崩溃了......而且这只是一个“中断”。 这是我的代码的一部分。我认为这是后台线程和数组释放/覆
有什么办法可以做到,比如 C/C# ? 例如(C#风格) for (int i = 0; i Seq.tryFind (fun i -> printfn "%d" i i=66) 在实践中,
我的问题是Haskell如何与系统交互。 例如,IO如何发生? 我对IO monad不感兴趣。或者如何使Haskell打印字符串或读取文件。 我对Haskell和底层操作系统之间的层(如果是这样的话)
你能确认下一个break是否取消了内部for循环吗? for (out in 1:n_old){ id_velho <- old_table_df$id[out] for
我有一个程序可以进行一些数据分析,并且有几百行长。 在程序的早期,我想做一些质量控制,如果没有足够的数据,我希望程序终止并返回到 R 控制台。否则,我希望执行其余代码。 我尝试过break、brows
我有一个 if 语句,用于检查我的对象(向导)是否命中了项目。如果他这样做了,巫师的大小将会改变,他应该能够在与障碍物的 1 次碰撞中幸存。 现在我陷入了“在 1 个障碍物碰撞中幸存”,因为在我的碰撞
我正在尝试使用迭代器来显示很多内容。我不断收到“break;”错误线。它说这是一个无法访问的声明。如有任何帮助,我们将不胜感激。 public Lot getLot(int number) {
我正在创建一个应用程序,我需要在其中处理可能非常庞大且可能需要一些时间的数据。 现在我阅读了很多关于 IntentService 的资料,实际上我已经将它实现为处理 REST 调用的通信类,但现在我试
我有一个自定义的 UITableViewCell。该单元具有三个标签。最左边的“金额”标签具有以下约束。 在单元格的右侧,我有另一个标签,“Label Dollar Amount”。它具有以下约束:
我有以下不和谐嵌入: message.reply({ content: '', embed: { color: 11416728, author
JavaScript 不是我最擅长的技能,但我会尽力解释,所以就这样吧。我有人在我的网站上创建了一个幻灯片菜单,我也使用 jplayer 音乐播放器。 现在一切正常,直到我在顶部添加此脚本。由于某种原
我已经在 Ubuntu 上安装了 android studio,有一个带有损坏图像的 API,我也尝试过重新安装。我应该怎么做才能克服这个问题。 删除它后,它没有在 sdk 管理器中显示 提前致谢。
假设我的站点上有大约 10 个 css 文件。我想把它们合二为一。但是当我组合它们时(只是“连接”文件,以便将它们包含到 html 中),我的样式/布局中断了。这不是路径问题或其他问题,只是选择器无法
我是一名优秀的程序员,十分优秀!