void KEY_scanningtimer_over_proc(void) { static uint16_t singleKeyNo = 0; if(singleKeyNo == 0 && GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_6) == KEY_DOWN) //如果按键按下 singleKeyNo = 1; else if(singleKeyNo == 1 && GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_6) == KEY_UP) //干扰信号 singleKeyNo = 0; // else if(singleKeyNo == 1 && GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_6) == KEY_DOWN) //如果不是干扰信号,不等待按键抬起 // { // singleKeyNo = 0; // singleKeyCode = isA6Down; // } else if(singleKeyNo == 1 && GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_6) == KEY_DOWN) //如果不是干扰信号 { singleKeyNo = 2; } else if(singleKeyNo == 2 && GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_6) == KEY_UP) //如果不是干扰信号,等待按键抬起 { singleKeyNo = 0; singleKeyCode = isA6Down; } //矩阵按钮 static uint16_t matrixKeyNo = 0; static uint8_t newKey = 0, oldKey = 0; newKey = GetMatrixKeyCode(); if(matrixKeyNo == 0 && newKey != 0) //如果键按下 { matrixKeyNo = 1; oldKey = newKey; } else if(matrixKeyNo == 1 && newKey != oldKey) //是干扰信号 { matrixKeyNo = 0; oldKey = 0; } // else if(matrixKeyNo == 1 && newKey == oldKey) //不是干扰信号,不等待按键抬起 // { // matrixKeyNo = 0; // oldKey = 0; // matrixKeyCode = newKey; // newKey = 0; // } else if(matrixKeyNo == 1 && newKey == oldKey) //不是干扰信号 { matrixKeyNo = 2; } else if(matrixKeyNo == 2 && newKey == 0) //不是干扰信号,等待按键抬起 { matrixKeyCode = oldKey; matrixKeyNo = 0; oldKey = 0; } //end 矩阵按钮 reload_timer(KEY_scanning_timer,30); start_timer(KEY_scanning_timer); }
void KEY_GPIO_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); //独立按键,使能 GPIOA 时钟; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOA, &GPIO_InitStructure); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); //矩阵键盘行的时钟 GPIO_InitStructure.GPIO_Pin = Row1|Row2|Row3|Row4; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_Init(GPIO_Row, &GPIO_InitStructure); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); //矩阵键盘列的时钟 GPIO_InitStructure.GPIO_Pin = Column1|Column2|Column3|Column4; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIO_Column, &GPIO_InitStructure); add_timer(KEY_scanning_timer,KEY_scanningtimer_over_proc,30); reload_timer(KEY_scanning_timer,30); start_timer(KEY_scanning_timer); }
void USART1_send_chars(uint8_t * pbuf) //发送数据帧函数 { uint8_t pbuf_len = *(pbuf + 10) + 12; uint32_t i = 0; uint32_t temp = 0; for(i = 0; i < pbuf_len-1; i++) //将发送数组的最后一位改成校验和的低8位 { temp += *(pbuf + i); } *(pbuf + pbuf_len - 1) = (uint8_t) temp % 256; if(pbuf_len >= SEND_LOWRE_LIMIT) { for(i = 0; i < pbuf_len; i++) //发送数据 { USART1->TDR = (*(pbuf + i) & (uint16_t)0x01FF); while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET); usart1_send_backup[i] = *(pbuf + i); //备份发送的数据 } if(usart1_send_backup[9] >= NEED_BACK_MSG_DOWN && usart1_send_backup[9] <= NEED_BACK_MSG_UP) //判断是否需要回复 { reload_timer(usart1_waiterback_timer,WAITER_BACK_TIME); start_timer(usart1_waiterback_timer); } } }
int clock_int_handler(void * data) { ticks++; // if( (ticks & 0x1F) == 0) // cons_putc('A'); run_timer_list(); reload_timer(); return 0; }
void clock_init_arm(uint32_t base, int irq) { // ARM_TIMER_DIV: pre-divider: clock = apb_clock / (x+1), 1MHz outw(ARM_TIMER_DIV,0x000000F9); // ARM_TIMER_CTL: control: // 1: 23-bit counter, 5: interrupt on, 7: timer on // 3..2: prescale off, 23..16: prescale, freq = clock / (x+1) outw(ARM_TIMER_CTL,0x003E00A2); clock_clear(); reload_timer(); register_irq(TIMER0_IRQ, clock_int_handler, 0); pic_enable(TIMER0_IRQ); }
void USART1_isr(void) //串口1中断函数 接收完的数据帧保存在 usart1_receive[] 中 { uint32_t i = 0; uint32_t temp = 0; uint8_t SUMValue = 0; if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) { reload_timer(usart1_receive_timer,RECEIVE_TIME);//若有字符收到,装初值并启动定时器,若字节超时,自动调用定时器超时函数 start_timer(usart1_receive_timer); if(usart1_receive_index >= BUF_SIZE) //若接收位置越界,那么重头开始存放 usart1_receive_index = 0; usart1_receive_buf[usart1_receive_index++] = (uint8_t)USART_ReceiveData(USART1); //接收字符,并位置加1 if(usart1_receive_index == 1 && usart1_receive_buf[0] != 0x7E) usart1_receive_index = 0; //有效数据帧从 0x7e 开始 if(usart1_receive_index >= SEND_LOWRE_LIMIT) if(usart1_receive_index == usart1_receive_buf[10] + 12) { stop_timer(usart1_receive_timer); for(i = 0; i < usart1_receive_index-1; i++) //将发送数组的最后一位改成校验和的低8位 { temp += usart1_receive_buf[i]; } SUMValue = (uint8_t) temp % 256; if(usart1_receive_buf[usart1_receive_index - 1] == SUMValue) { for(i = 0;i < usart1_receive_index; i++) //接收缓冲区数据移至数据接收数组。保存数据 usart1_receive[i] = usart1_receive_buf[i]; if(usart1_receive[9] > BACK_MSG_DOWN && usart1_receive[9] < BACK_MSG_UP) //判断是否是回复信息 { ReSendCount = 0; stop_timer(usart1_waiterback_timer); } } usart1_receive_index=0; //接收缓冲区接收位置复位 } } if (USART_GetFlagStatus(USART1, USART_FLAG_ORE) != RESET) //过载处理,只要读状态寄存器和数据寄存器就可自动清理此标志 { (void)USART_ReceiveData(USART1); usart1_receive_index = 0; //接收缓冲区接收位置复位 } }
void clock_init(void) { reload_timer(); pic_enable(TIMER0_IRQ); kprintf("++setup timer interrupts\n"); }