uint8_t key_flag = 0;这个如何改写为void uart_rx_isr(uint8_t rx){ c = (char)rx; // careful: no buffering flag = 1; // set a flag for main }
时间: 2024-03-10 07:44:41 浏览: 39
将 `uint8_t key_flag = 0;` 改写为 `void uart_rx_isr(uint8_t rx){ c = (char)rx; // careful: no buffering flag = 1; // set a flag for main }` 的话,应该是这样的:
```
volatile char c;
volatile char flag;
void uart_rx_isr(uint8_t rx) {
c = (char)rx; // 将接收到的数据存放在全局变量 c 中
flag = 1; // 设置标志位,表示接收到了数据
}
```
这里使用了 `volatile` 关键字来修饰 `c` 和 `flag` 变量,因为它们会被中断服务程序和主程序共享,需要在每次使用时重新读取变量的值,以确保正确性。而 `key_flag` 变量在原代码中并没有被使用和修改过,所以在改写后的代码中也就没有了。
相关问题
void UART1_Isr() interrupt 4 { uint8 res; static uint8 dwon_count; if(UART1_GET_TX_FLAG) { UART1_CLEAR_TX_FLAG; busy[1] = 0; } if(UART1_GET_RX_FLAG) { UART1_CLEAR_RX_FLAG; res = SBUF; //ウフミヤカッマツヤリ if(res == 0x7F) { if(dwon_count++ > 20) IAP_CONTR = 0x60; } else { dwon_count = 0; } } }
这段代码是一个UART1的中断服务函数,当UART1发送或接收到数据时,会触发中断,执行该函数。
函数中有一个变量dwon_count,用于记录连续接收到0x7F字符的次数,当连续接收到20次以上时,会执行IAP_CONTR = 0x60,即进入ISP模式,用于升级程序。这是一种防止程序升级失败的机制。
另外,当UART1发送完成时,busy[1]会被置为0,表示UART1可以继续发送数据。
把代码里面的queue换为charbuff和charbuff的head和tail int main(void) { // hardware/peripheral initialisation uint8_t Q; queue_init(&charBuf); gpio_set_mode(P_LD2, Output); gpio_set_mode(P_B1, PullUp); uart_init(9600); uart_set_rx_callback(uart_rx_isr); uart_enable(); while (1) { if (flag){ // there is a new character ReadChar(c); if(!queue_enqueue(&charBuf,X)){ while(1){ if(gpio_get(P_B1) == 0){ uart_enable(); queue_init(&charBuf); } } }else{ if(queue_dequeue(&charBuf,&Q)){ uart_tx(Q); BlinkMorseCode(Q); } } } flag = 0; } }
根据你的要求,我可以把代码里面的queue换为charbuff,并且使用charbuff的head和tail。
下面是修改后的代码:
```c
int main(void) {
// hardware/peripheral initialisation
uint8_t Q;
charbuff_init(&charBuf);
gpio_set_mode(P_LD2, Output);
gpio_set_mode(P_B1, PullUp);
uart_init(9600);
uart_set_rx_callback(uart_rx_isr);
uart_enable();
while (1) {
if (flag) { // there is a new character
char c;
ReadChar(c);
if (!charbuff_enqueue(&charBuf, c)) {
while (1) {
if (gpio_get(P_B1) == 0) {
uart_enable();
charbuff_init(&charBuf);
}
}
} else {
if (charbuff_dequeue(&charBuf, &Q)) {
uart_tx(Q);
BlinkMorseCode(Q);
}
}
}
flag = 0;
}
}
```
注意,这里还需要定义charbuff_init、charbuff_enqueue和charbuff_dequeue这三个函数,来初始化、入队和出队charbuff。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)