70,020
社区成员




// 在读串口时,根据接收到的不同数据而执行不同的function();
//这样写只能读取一次,随后再怎么写给串口数据,也读取不到了。
//怎么修改才可以实现:一旦有数据来,就中断当前的funtion(),重新读取并执行新的function呢。
// 串口本身不是IRQ操作么?我用的是cortex m0.
while (1)
{
data = ReceiveByte();
switch(data)
{
case 'a':
function1()...
break;
case 'b':
...
break;
case 'z':
...
break;
default:
break;
}
}
//需要定义一个串口接收缓冲区:
char buf[SIZE];
char *read = buf;
char *write = buf;
int cnt = 0;
//中断处理程序, :
void GetDataFromSerial(void)
{
if (cnt < SIZE)//事实上,应该是cnt < SIZE - num, 因为响应速度问题,需要提前禁止,num根据实际情况自定义大小
{
if (write == buf + SIZE)
{
write = buf;//刚才不小心多按了一个=
}
//DI 禁止中断
*write++ = DATA;//假设DATA是从端口读的数据
cnt++;
//EI 恢复中断
}
else
{
//需要在这里禁止再接收数据
}
}
char GetDataFromSerialBuf()
{
char data;
if (cnt > 0)
{
if (read == buf + SIZE)
{
read = buf;
}
//DI 禁止中断
data = *read++;
cnt--;
//EI 恢复中断
}
else
{
while (cnt == 0);//等待数据
}
return data;//刚才忘记返回了,哈哈
}
//主控程序中这么写
while (1)
{
data = GetDataFromSerialBuf();
switch(data)
{
case 'a':
function1();
break;
case 'b':
...
break;
case 'z':
...
break;
default:
break;
}
}
//需要定义一个串口接收缓冲区:
char buf[SIZE];
char *read = buf;
char *write = buf;
int cnt = 0;
//中断处理程序, :
void GetDataFromSerial(void)
{
if (cnt < SIZE)//事实上,应该是cnt < SIZE - num, 因为响应速度问题,需要提前禁止,num根据实际情况自定义大小
{
if (write == buf + SIZE)
{
write = buf;//刚才不小心多按了一个=
}
//DI 禁止中断
*write++ = DATA;//假设DATA是从端口读的数据
cnt++;
//EI 恢复中断
}
else
{
//需要在这里禁止再接收数据
}
}
char GetDataFromSerialBuf()
{
char data;
if (cnt > 0)
{
if (read == buf + SIZE)
{
read = buf;
}
//DI 禁止中断
data = *read++;
cnt--;
//EI 恢复中断
}
else
{
while (cnt == 0);//等待数据
}
}
//主控程序中这么写
while (1)
{
data = GetDataFromSerialBuf();
switch(data)
{
case 'a':
function1();
break;
case 'b':
...
break;
case 'z':
...
break;
default:
break;
}
}
//需要定义一个串口接收缓冲区:
char buf[SIZE];
char *read = buf;
char *write = buf;
int cnt = 0;
//中断处理程序, :
void GetDataFromSerial(void)
{
if (cnt < SIZE)//事实上,应该是cnt < SIZE - num, 因为响应速度问题,需要提前禁止,num根据实际情况自定义大小
{
if (write == buf + SIZE)
{
write == buf;
}
//DI 禁止中断
*write++ = DATA;//假设DATA是从端口读的数据
cnt++;
//EI 恢复中断
}
else
{
//需要在这里禁止再接收数据
}
}
char GetDataFromSerialBuf()
{
char data;
if (cnt > 0)
{
if (read == buf + SIZE)
{
read = buf;
}
//DI 禁止中断
data = *read++;
cnt--;
//EI 恢复中断
}
else
{
while(cnt == 0);//等待数据
}
}
//主控程序中这么写
while (1)
{
data = GetDataFromSerialBuf();
switch(data)
{
case 'a':
function1();
break;
case 'b':
...
break;
case 'z':
...
break;
default:
break;
}
}