Funpack第7期:Microchip Curiocity
基于Microchip的8位PIC MCU开发板DM164137,实现按键扩展功能,快速点击按钮获取模拟电压量,短按按钮1~5秒实现二极管D4的亮灭,长按按钮大于5秒实现另一个二极管的亮灭D7。
标签
嵌入式系统
Undefined_User
更新2021-04-21
659

任务要求:

通过按键扩展功能,分为按键点击(快速点击),短按(大于1秒),长按(大于5秒)用来控制板上LED显示。同时外接串口模块打印信息(包括按键信息,电位计信息)

功能实现:

快速点击按钮获取模拟电压量,短按按钮1~5秒实现二极管D4的亮灭,长按按钮大于5秒实现另一个二极管的亮灭D7。

主频:

Fio8ZX7p8P6hnhERpTPbfuihr5dp

主要代码如下:

#include "mcc_generated_files/mcc.h"


static uint16_t adcResult; // Used to store the result of the ADC


//typedef 
typedef enum{
    BTN_RELEASE= 0,
    BTN_TOUCH = 1,
    CLICK  = 2,
    DOUBLE_CLICK = 3,
    SHORT_PUSH = 4,
    LONG_PUSH = 5,
    WAIT_DOUBLE = 6,
}BTN_STATE;

bool Button0Touch = 0;
//void processButtonTouch(enum mtouch_button_names button)
//{    
//    switch(button)
//    {
//        case Button0: 
//            //LED_D7_SetHigh();
//            Button0Touch = 1;
//            break;
//        //case Button1: LED1_SetLow();break;
//        default: break;
//    }
//}
// 
//void processButtonRelease(enum mtouch_button_names button)
//{
//    switch(button)
//    {
//        case Button0:
//            //LED_D7_SetLow();
//            Button0Touch = 0;
//            break;
//        //case Button1: LED1_SetHigh();break;
//        default: break;
//    }
//}

#define SHORT_PUSH_CNT 1000
#define LONG_PUSH_CNT 4000
#define WAIT_DOUBLE_CNT 200

BTN_STATE Button0_state[1];//[MTOUCH_BUTTONS];
uint16_t Button0_cnt;


BTN_STATE button_check(uint8_t button)//enum mtouch_button_names button)
{
    switch(Button0_state[button])
    {
        case BTN_RELEASE:
        {
            if(Button0Touch)
            {
                Button0_state[button] = BTN_TOUCH;
            }
            break;
        }
        case BTN_TOUCH:
        {
            if(Button0Touch)
            {
                if(Button0_cnt>SHORT_PUSH_CNT)
                {
                    Button0_state[button] = SHORT_PUSH;
                    //printf("SHORT\n\r");
                    Button0_cnt = 0;
                }
                Button0_cnt++;
            }
            else
            {
                Button0_state[button] = CLICK;
                Button0_cnt = 0;
            }
            break;
        }
        case CLICK:
        {
            if(!Button0Touch)
            {
                LED_D5_Toggle();//??????????D4,5,6,7
                //Get the top 4 MSBs and display it on the LEDs
                adcResult = ADCC_GetSingleConversion(ANC0);

                //Printing ADC result on Serial port
                printf("ADC Result: %d\n\r", adcResult); 
                //???????????,??????????
                //printf("voltage: %d\n\r", adcResult); 
                //printf("POT VAL  %d\n\r", adcResult);
                
                Button0_state[button] = WAIT_DOUBLE;
                Button0_cnt = 0;
            }
            break;
        }
        case DOUBLE_CLICK:
        {
            if(!Button0Touch)
            {
                Button0_state[button] = BTN_RELEASE;
                Button0_cnt = 0;
            }
            break;
        }
        case SHORT_PUSH:
        {
            if(Button0Touch)
            {
                if(Button0_cnt>LONG_PUSH_CNT)
                {
                    Button0_state[button] = LONG_PUSH;
                    //printf("LONG");
                    LED_D7_Toggle();
                }
                Button0_cnt++;
            }
            if(!Button0Touch)
            {
                LED_D4_Toggle();
                Button0_state[button] = BTN_RELEASE;
                Button0_cnt = 0;
            }
            break;
        }
        case LONG_PUSH:
        {
            if(!Button0Touch)
            {
                Button0_state[button] = BTN_RELEASE;
                Button0_cnt = 0;
            }
            break;
        }
        case WAIT_DOUBLE:
        {
            if(Button0Touch)
            {
                LED_D6_Toggle();
                Button0_state[button] = DOUBLE_CLICK;
            }
            else
            {
                if(Button0_cnt>WAIT_DOUBLE_CNT)
                {
                    Button0_state[button] = BTN_RELEASE;
                    Button0_cnt = 0;
                }
                Button0_cnt++;
            }
            break;
        }
        default:break;
    }
}

void TMR0ISR()
{
    //MTOUCH_Service_Mainloop();  
    //LED_D4_Toggle();
    if(S1_GetValue() == 0)
    {
        Button0Touch = 1;
    }
    else
    {
        Button0Touch = 0;
    }
    button_check(0);
}


/*
                         Main application
 */
void main(void)
{
    // initialize the device
    SYSTEM_Initialize();

    // When using interrupts, you need to set the Global and Peripheral Interrupt Enable bits
    // Use the following macros to:
    
    // Enable the Global Interrupts
    INTERRUPT_GlobalInterruptEnable();

    // Enable the Peripheral Interrupts
    INTERRUPT_PeripheralInterruptEnable();

    // Disable the Global Interrupts
    //INTERRUPT_GlobalInterruptDisable();

    // Disable the Peripheral Interrupts
    //INTERRUPT_PeripheralInterruptDisable();
    //MTOUCH_Button_SetPressedCallback(processButtonTouch);
    //MTOUCH_Button_SetNotPressedCallback(processButtonRelease);
    TMR0_SetInterruptHandler(TMR0ISR);

 
    while (1)
    {
 
    }
}
/**
 End of File
*/

心得体会:经过本次活动,接触到了平时难以接触的板子。经历了开发环境配置,配置初始化,编程调试,感觉很有收获。十分感谢本次活动!

团队介绍
个人
团队成员
Undefined_User
评论
0 / 100
查看更多
目录
硬禾服务号
关注最新动态
0512-67862536
info@eetree.cn
江苏省苏州市苏州工业园区新平街388号腾飞创新园A2幢815室
苏州硬禾信息科技有限公司
Copyright © 2023 苏州硬禾信息科技有限公司 All Rights Reserved 苏ICP备19040198号