本帖最后由 hu9jj 于 2019-5-31 14:31 编辑
点亮LED灯之后就着手测试开发板上的用户按键,此按键被连接在RC2引脚上,按照用户手册的说明,先将RC2设置为输入,同时设置为弱上拉,然后在主循环中轮询按键状态。在我的测试中,设置了一个变量i,计划每按一次按键,i值加1,超过4则恢复为0,然后根据 i 不同的值对应不同的延时,达到改变LED灯闪烁频率的效果。程序代码如下:
- void main(void)
- {
- uint8_t i, j;
- /* initialize the device */
- SYSTEM_Initialize();
- /* Enable the Global Interrupts */
- // INTERRUPT_GlobalInterruptEnable();
- /* Enable the Peripheral Interrupts */
- // INTERRUPT_PeripheralInterruptEnable();
- // APP_init();
- TRISA = 0xFB; //设置RA2为输出
- PORTA = 0xFF; //端口置‘1’
- TRISC = 0x04; //设置RC2为输入
- PORTC = 0xFF; //端口置‘1’
- WPUC = 0xFF;
- // WPUC2 = 1; //设置RC2为弱上拉
-
- while (1)
- {
- if(RC2==0)
- {
- i++;
- if(i>4)
- i = 0;
- }
- RA2 = 0;
- switch (i){
- case 1:__delay_ms(100);break;
- case 2:__delay_ms(200);break;
- case 3:__delay_ms(500);break;
- case 4:__delay_ms(1000);break;
- case 5:__delay_ms(2000);
- }
-
- RA2 = 1;
- switch (i){
- case 1:__delay_ms(100);break;
- case 2:__delay_ms(200);break;
- case 3:__delay_ms(500);break;
- case 4:__delay_ms(1000);break;
- case 5:__delay_ms(2000);
- }
-
- }
- }
以下是用户手册上的截图:
但在测试时却不顺利,RC2的值一直为低电平,貌似弱上拉没有被设置好,但我反复查看用户手册和数据手册却找不到问题所在,又找不到示例代码作参考,只好在此求助各位,请帮忙看看是什么原因,谢谢!
下面是数据手册中相关的说明:
|