查看: 3933|回复: 0

[原创] [在线活动 0元购OM40001L LPC804] NFC门锁之一

[复制链接]
  • TA的每日心情
    擦汗
    2024-11-7 09:48
  • 签到天数: 1 天

    连续签到: 1 天

    [LV.1]初来乍到

    35

    主题

    83

    帖子

    0

    金牌会员

    Rank: 6Rank: 6

    积分
    1265
    最后登录
    2025-9-8
    发表于 2019-12-23 15:44:17 | 显示全部楼层 |阅读模式
    1、开发板收到,直接上图,详细资料在网站上很详尽,OM40001L,自行前往浏览。

    597350838.jpg

    这个项目主要开发是用NFC读取NFC卡数据,认证后开锁的演示程序。需要用到的参数是,
    处理器       采用TSSOP24封装的LPC804Arm®Cortex®-M0+ MCU,运行速度高达15 MHz
    记忆       1 Mb Winbond SPI闪存
    五按钮电容式触摸屏,每个按钮带有LED指示灯, 三个用户LED,复位按钮、PLU扩展板包括LED,开关和振荡器。

    2、开发IDE采用KEIL,从网站下载范例程序后,打开程序就自动启动开发板支持硬件的Package安装。程序可以启动了。
    3、首先测试范例电容触摸功能,显示以下效果,
    启动先自检硬件,然后等待触摸输入,触摸值超过阈值就可以点亮对应的LED的灯。
    bms.gif
    4. 在测试这个程序前,需要按照下图重新去掉跳线,才能正确执行功能。
    1.jpg 2.jpg

    5. 具体的范例程序如下,
    1. const uint32_t cLEDxIdxArr[LEDx_COUNT] =
    2. {
    3.     LED0_IDX,
    4.     LED1_IDX,
    5.     LED2_IDX,
    6.     LED3_IDX,
    7.     LED4_IDX
    8. };

    9. const uint32_t cLEDxMaskArr[LEDx_COUNT] =
    10. {
    11.     LED0_MASK,
    12.     LED1_MASK,
    13.     LED2_MASK,
    14.     LED3_MASK,
    15.     LED4_MASK
    16. };

    17. /*
    18. * MRTTicker.
    19. */
    20. void MRTTicker_Init(void);
    21. void MRTTicker_BlockingDelay10ms(uint32_t ticks);

    22. /*
    23. * CapTouch.
    24. */
    25. void App_CapTouch_Init(void);
    26. void App_CapTouch_StartPollContinuous(uint32_t chnMasks);
    27. void App_CapTouch_WaitPollDoneBlocking(uint32_t waitPollCount);
    28. uint32_t App_CapTouch_GetTouchedIndex(uint32_t *buf, uint32_t len, uint32_t *touchThresholdVals);

    29. /* User application entry. */
    30. int main(void)
    31. {
    32.     uint32_t key_value_pre;
    33.    
    34.     BOARD_InitBootClock();
    35.     BOARD_InitBootPins();

    36.     LEDx_Init();
    37.     MRTTicker_Init();

    38.     App_ProbeLEDx();
    39.    
    40.     bAppCapTouchCalibrationDone = false;
    41.     LEDx_On(LEDx_ALL_MASK); /* turn on all the leds here, they would be trun off when the calibraiton is done. */

    42.     /* CapTouch. */
    43.     key_value_pre = gAppCapTouchKeyValue;
    44.     App_CapTouch_Init();
    45.     App_CapTouch_StartPollContinuous(0x1F); /* Active all the five X channels. */

    46.     while (1)
    47.     {

    48.         /* gAppCapTouchKeyValue; */
    49.         if (key_value_pre != gAppCapTouchKeyValue)
    50.         {
    51.             /* disable the leds for old key_value_pre */
    52.             if (key_value_pre >= LEDx_COUNT)
    53.             {
    54.                 LEDx_Off(LEDx_ALL_MASK);
    55.             }
    56.             else
    57.             {
    58.                 LEDx_Off(cLEDxMaskArr[key_value_pre]);
    59.             }
    60.             
    61.             key_value_pre = gAppCapTouchKeyValue;
    62.             
    63.             /* enable led for new gAppCapTouchKeyValue. */
    64.             
    65.             if (key_value_pre >= LEDx_COUNT)
    66.             {
    67.                 LEDx_Off(LEDx_ALL_MASK);
    68.             }
    69.             else
    70.             {
    71.                 LEDx_On(cLEDxMaskArr[key_value_pre]);
    72.             }

    73.         }
    74.     }
    75. }


    76. void BOARD_InitBootClock(void)
    77. {
    78.     /* Enable clocks to relevant peripherals. */
    79.     SystemCoreClockUpdate();
    80.     /* Enable the clock to access the modules. */
    81.     LPC_SYSCON->SYSAHBCLKCTRL0 |= (SWM | IOCON | GPIO0 | MRT);
    82. }

    83. void BOARD_InitBootPins(void)
    84. {
    85.     /* GPIO - LEDx. */
    86.     /* - PIO0_20 */ LPC_IOCON->PIO0_20  = IOCON_RESERVED789; /* Disable internal pull up and pull down. */
    87.     /* - PIO0_18 */ LPC_IOCON->PIO0_18  = IOCON_RESERVED789; /* Disable internal pull up and pull down. */
    88.     /* - PIO0_15 */ LPC_IOCON->PIO0_15  = IOCON_RESERVED789; /* Disable internal pull up and pull down. */
    89.     /* - PIO0_8  */ LPC_IOCON->PIO0_8   = IOCON_RESERVED789; /* Disable internal pull up and pull down. */
    90.     /* - PIO0_9  */ LPC_IOCON->PIO0_9   = IOCON_RESERVED789; /* Disable internal pull up and pull down. */

    91.     /* ACMP. */
    92.     /* CAPT. */
    93. }

    94. /******************************************************************************
    95. * LEDx Functions.
    96. ******************************************************************************/
    97. void App_ProbeLEDx(void)
    98. {
    99.     uint32_t i;
    100.     uint32_t ledxMaskAll = 0U;

    101.     for (i = 0U; i < LEDx_COUNT; i++)
    102.     {
    103.         LEDx_On(cLEDxMaskArr[i]);
    104.         MRTTicker_BlockingDelay10ms(20U);
    105.         LEDx_Off(cLEDxMaskArr[i]);
    106.         MRTTicker_BlockingDelay10ms(20U);

    107.         ledxMaskAll |= cLEDxMaskArr[i];
    108.     }

    109.     for (i = 0U; i < 3U; i++)
    110.     {
    111.         LEDx_On(ledxMaskAll);
    112.         MRTTicker_BlockingDelay10ms(20U);
    113.         LEDx_Off(ledxMaskAll);
    114.         MRTTicker_BlockingDelay10ms(20U);
    115.     }
    116. }

    117. void LEDx_Init(void)
    118. {
    119.     uint32_t i;

    120.     for (i = 0U; i < LEDx_COUNT; i++)
    121.     {
    122.         GPIOSetBitValue(0U, cLEDxIdxArr[i], 1U);
    123.         GPIOSetDir(0U, cLEDxIdxArr[i], OUTPUT);
    124.     }
    125. }

    126. void LEDx_On(uint32_t ledMask)
    127. {
    128.     uint32_t i;

    129.     for (i = 0U; i < LEDx_COUNT; i++)
    130.     {
    131.         if (0U != (cLEDxMaskArr[i] & ledMask))
    132.         {
    133.             GPIOSetBitValue(0U, cLEDxIdxArr[i], 0U);
    134.         }
    135.     }
    136. }

    137. void LEDx_Off(uint32_t ledMask)
    138. {
    139.     uint32_t i;

    140.     for (i = 0U; i < LEDx_COUNT; i++)
    141.     {
    142.         if (0U != (cLEDxMaskArr[i] & ledMask))
    143.         {
    144.             GPIOSetBitValue(0U, cLEDxIdxArr[i], 1U);
    145.         }
    146.     }
    147. }

    148. void LEDx_OnOnly(uint32_t ledMask)
    149. {
    150.     uint32_t i;

    151.     for (i = 0U; i < LEDx_COUNT; i++)
    152.     {
    153.         if (0U != (cLEDxMaskArr[i] & ledMask))
    154.         {
    155.             GPIOSetBitValue(0U, cLEDxIdxArr[i], 0U);
    156.         }
    157.         else
    158.         {
    159.             GPIOSetBitValue(0U, cLEDxIdxArr[i], 1U);
    160.         }
    161.     }
    162. }
    163. /*****************************************************************************/

    164. /******************************************************************************
    165. * MRT Timer Functions.
    166. ******************************************************************************/
    167. volatile uint32_t gAppTickerCounter;
    168. void MRTTicker_Init(void)
    169. {
    170.     LPC_MRT->Channel[0].CTRL = (MRT_Repeat<<MRT_MODE)
    171.                              | (1U <<MRT_INTEN)
    172.                              ;
    173.     NVIC_EnableIRQ(MRT_IRQn);

    174.     /* Start the channel 0. */
    175.     LPC_MRT->Channel[0].INTVAL = 15000000UL / 100UL; /* 100Hz */
    176. }

    177. /* 10ms per tick. */
    178. void MRTTicker_BlockingDelay10ms(uint32_t ticks)
    179. {
    180.     gAppTickerCounter = ticks;
    181.     while (gAppTickerCounter > 0U)
    182.     {}
    183. }

    184. void MRT_IRQHandler(void)
    185. {
    186.     uint32_t flags = LPC_MRT->IRQ_FLAG;

    187.     if (gAppTickerCounter > 0U)
    188.     {
    189.         gAppTickerCounter--;
    190.     }

    191.     LPC_MRT->IRQ_FLAG = flags;
    192. }
    193. /*****************************************************************************/

    194. /******************************************************************************
    195. * CapTouch.
    196. ******************************************************************************/
    197. void App_CapTouch_InitClock(void)
    198. {
    199.     /* Enable clocks to relevant peripherals. */
    200.     LPC_SYSCON->SYSAHBCLKCTRL0 |= (1U << CLK_GPIO0)
    201.                                 | (1U << CLK_IOCON)
    202.                                 | (1U << CLK_SWM)
    203.                                 ;
    204.     LPC_SYSCON->SYSAHBCLKCTRL1 |= (1U << 0U); /* CLK_CAPT */

    205.     /* Reset the CAPT module. */
    206.     Do_Periph_Reset(RESET_CAPT);

    207.     SystemCoreClockUpdate();

    208.     /* Setup the FCLK for the CAP Touch block. */
    209.     LPC_SYSCON->CAPTCLKSEL = 0U; /* CAPTCLKSEL_FRO_CLK. Use FRO 15MHz clock. */
    210. }

    211. #define IOCON_PIO_RESERVED (0x1 << 7U)
    212. #define IOCON_PIO_MODE(x)  (((x) & 0x3) << 3U)
    213. #define IOCON_PIO_OD_MASK  (0x1 << 10U)

    214. void App_CapTouch_InitPins(void)
    215. {
    216.     // Enable the CAP Touch functions on their pins in the SWM
    217.     ConfigSWM(CAPT_X0, P0_12);
    218.     ConfigSWM(CAPT_X1, P0_21);
    219.     ConfigSWM(CAPT_X2, P0_13);
    220.     ConfigSWM(CAPT_X3, P0_11);
    221.     ConfigSWM(CAPT_X4, P0_19);
    222.     ConfigSWM(CAPT_YH, P0_17);
    223.     ConfigSWM(CAPT_YL, P0_7 );

    224.     LPC_IOCON->PIO0_12 = IOCON_PIO_MODE(0) /* | IOCON_PIO_OD_MASK */ | IOCON_PIO_RESERVED; /* CAPT_X0. */
    225.     LPC_IOCON->PIO0_21 = IOCON_PIO_MODE(0) /* | IOCON_PIO_OD_MASK */ | IOCON_PIO_RESERVED; /* CAPT_X1. */
    226.     LPC_IOCON->PIO0_13 = IOCON_PIO_MODE(0) /* | IOCON_PIO_OD_MASK */ | IOCON_PIO_RESERVED; /* CAPT_X2. */
    227.     LPC_IOCON->PIO0_11 = IOCON_PIO_MODE(0) /* | IOCON_PIO_OD_MASK */ | IOCON_PIO_RESERVED; /* CAPT_X3. */
    228.     LPC_IOCON->PIO0_19 = IOCON_PIO_MODE(0) /* | IOCON_PIO_OD_MASK */ | IOCON_PIO_RESERVED; /* CAPT_X4. */
    229.     LPC_IOCON->PIO0_17 = IOCON_PIO_MODE(0) /* | IOCON_PIO_OD_MASK */ | IOCON_PIO_RESERVED; /* CAPT_YH. */
    230.     LPC_IOCON->PIO0_7  = IOCON_PIO_MODE(0) /* | IOCON_PIO_OD_MASK */ | IOCON_PIO_RESERVED; /* CAPT_YL */
    231. }

    232. /* Initialize the CapTouch application with interrupts enabled */
    233. void App_CapTouch_Init(void)
    234. {
    235.     App_CapTouch_InitClock();
    236.     App_CapTouch_InitPins();

    237.     /* CAPT_CTRL - Setup the working condition of CapTouch.
    238.      *  0: 1 - POLLMODE : Scan method including: Inactive (stop the scan right now, control and status)
    239.      *                                            PollNow (one time scan and trigger by software)
    240.      *                                            Continuous (continuously autumatically by hardware)
    241.      *  2: 3 - TYPE     : No other selection, the only available value is 0, can be reserved.
    242.      *  4    - TRIGGER  : Assert the scan event done by checking the YH GPIO logic level or from the external ACMP module.
    243.      *  5    - WAIT     : Enable blocking the scan before the available touch value is read.
    244.      *  6: 8
    245.      *  8:11 - FDIV     : Divider from CAPT clock source to FCLK.
    246.      * 12:13 - XPINUSE  : Logic state for unactivated X pins: High-Z (0x0) or Grounded (0x1).
    247.      * 15    - INCHANGE : Check if this CAPT_CTRL resiger is accept by hardware.
    248.      * 31:16 - XPINSEL  : Enable the activated X pins in the scan (or the sequence for continuous mode)
    249.      */
    250.     while (0U != (INCHANGE & LPC_CAPT->CTRL)) /* Wait while the LPC_CAPT is in change. */
    251.     {}
    252.     LPC_CAPT->CTRL = POLLMODE_INACTIVE /* Would launch the scan later. */
    253.                    | TYPE_TRIGGER_YH /* Use YH trigger as the simplest use case. */
    254.                    /* | WAIT)*/  /* NO wait, since no continuous mode is used. */
    255.                    | ((15000000UL/3000000UL)-1) << FDIV /* Divider for FCLK from clock source. */
    256.                    /* | XPINUSE_HIGHZ */ /* X pins state when they are not active. */
    257.                    | (XPINUSE_LOW)
    258.                    | (0UL << XPINSEL) /* Would indicated the channel when launch the scan later. */
    259.                    ;

    260.     /* CAPT_POLL_TCNT - Setup the timing and its judging parameters during the scan.
    261.      * 0 :11 - TCNT    : Hardware threshold value between YES_TOUCH and NO_TOUCH.
    262.      * 12:15 - TOUT    : Timeout count before a scan for one X pad is done.
    263.      * 16:23 - POLL    : x4096 count PCLK between the scan for each X pad when in continuous scan mode.
    264.      * 24:25 - MDELAY  : Delay PCLK when in Measure step.
    265.      * 27:26 - RDELAY  : Delay PCLK when in Reset step.
    266.      * 31    - TCHLOWER: Assert touch event when sensing value is higher (0) / lower (1) than the threshold (TCNT) value.
    267.      */
    268.     LPC_CAPT->POLL_TCNT = (0U  << TCNT) /* No hardware judgment for YES/NO touch. Software would check the sensing value after PollDone event. */
    269.                         | (12U << TOUT) /* Timeout value. */
    270.                         | (5U << POLL)  /* x4096 FCLK between poll round in continuous scan mode. */
    271.                         | (3U  << RDELAY) /* Reset delay. */
    272.                         | (3U  << MDELAY) /* Measurement delay. */
    273.                         | (1U  << TCHLOWER)
    274.                         ;

    275.     /* Enable the interrupts. */
    276.     LPC_CAPT->INTENSET = YESTOUCH | NOTOUCH | TIMEOUT | OVERRUN;
    277.     NVIC_EnableIRQ(CAPT_IRQn);
    278. }

    279. void App_CapTouch_StartPollNow(uint32_t chnIdx)
    280. {
    281.     while (0U != (INCHANGE & LPC_CAPT->CTRL)) /* Wait while the LPC_CAPT is in change. */
    282.     {}
    283.     LPC_CAPT->CTRL |= ( POLLMODE_NOW | ((1U << chnIdx) << XPINSEL) );
    284. }

    285. void App_CapTouch_StartPollContinuous(uint32_t chnMasks)
    286. {
    287.     while (0U != (INCHANGE & LPC_CAPT->CTRL)) /* Wait while the LPC_CAPT is in change. */
    288.     {}
    289.     LPC_CAPT->CTRL |= ( POLLMODE_CONTINUOUS | (chnMasks << XPINSEL) );
    290. }

    291. uint32_t get_min_idx(uint32_t *buf, uint32_t len)
    292. {
    293.     uint32_t i, min_idx = 0u;
    294.    
    295.     if (len > 1)
    296.     {
    297.         for (i = 1u; i < len; i++)
    298.         {
    299.             if ( buf[i] < buf[min_idx])
    300.             {
    301.                 min_idx = i;
    302.             }
    303.         }
    304.     }

    305.     return min_idx;
    306. }

    307. uint32_t get_max_idx(uint32_t *buf, uint32_t len)
    308. {
    309.     uint32_t i, max_idx = 0u;
    310.    
    311.     if (len > 1)
    312.     {
    313.         for (i = 1u; i < len; i++)
    314.         {
    315.             if ( buf[i] > buf[max_idx])
    316.             {
    317.                 max_idx = i;
    318.             }
    319.         }
    320.     }

    321.     return max_idx;
    322. }


    323. /* EOF. */
    复制代码
    这个程序需要启动对应的电容触摸库,就可以直接调用对应的API快速实现触摸感应输入的功能。


    该会员没有填写今日想说内容.
    回复

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /3 下一条

    Archiver|手机版|小黑屋|恩智浦技术社区

    GMT+8, 2025-9-10 18:13 , Processed in 0.084372 second(s), 20 queries , MemCache On.

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.

    快速回复 返回顶部 返回列表