查看: 3843|回复: 5

[分享] 【LPC824lite】MRT呼吸灯

[复制链接]
  • TA的每日心情
    开心
    2018-7-23 21:04
  • 签到天数: 103 天

    连续签到: 1 天

    [LV.6]常住居民II

    228

    主题

    5379

    帖子

    0

    金牌会员

    Rank: 6Rank: 6

    积分
    16701
    最后登录
    1970-1-1
    发表于 2016-10-23 16:52:39 | 显示全部楼层 |阅读模式
    学了MRT,来个呼吸灯玩玩,当然使用SCT更好些,这里主要练习一下,分别使用了循环和单次模式。
    先来看看思路
    主要使用MRT的CH0和CH1,CH0控制周期,CH1控制占空比。
    周期设置1ms,占空比在10%-90%之间变化。
    QQ截图20161023164520.png
    定义变量,保存当前的占空比
    QQ截图20161023162848.png
    通过占空比,返回MRT_CH1的初值
    QQ截图20161023162901.png
    这是中断函数
    QQ截图20161023162924.png
    主函数中初始化MRT。
    QQ截图20161023162936.png

    完整程序:
    1. #if defined (__USE_LPCOPEN)
    2. #if defined(NO_BOARD_LIB)
    3. #include "chip.h"
    4. #else
    5. #include "board.h"
    6. #endif
    7. #endif

    8. #include <cr_section_macros.h>

    9. // TODO: insert other include files here

    10. // TODO: insert other definitions and declarations here
    11. #define LPC_MRT_CH0_1MS 1000
    12. volatile uint8_t pwm_Duty_Cycle = 10;

    13. uint32_t Set_MRT_CH1_Interval(uint8_t duty_Cycle)
    14. {
    15.         return Chip_Clock_GetSystemClockRate() / (LPC_MRT_CH0_1MS * 100) * duty_Cycle;
    16. }

    17. void MRT_IRQHandler(void)
    18. {
    19.         static volatile uint32_t count = 0;
    20.         static volatile int8_t step = 1;

    21.         if (Chip_MRT_GetIntPendingByChannel(0) == true)
    22.         {
    23.                 Chip_MRT_ClearIntPending(MRT0_INTFLAG);
    24.                 Chip_MRT_SetInterval(LPC_MRT_CH1, Set_MRT_CH1_Interval(pwm_Duty_Cycle) | MRT_INTVAL_LOAD);
    25.                 Board_LED_Set(0, true);
    26.                 count++;
    27.                 if (count == 20)
    28.                 {
    29.                         count = 0;
    30.                         if (pwm_Duty_Cycle > 90)
    31.                         {
    32.                                 step = -1;
    33.                         }
    34.                         if (pwm_Duty_Cycle < 10)
    35.                         {
    36.                                 step = 1;
    37.                         }
    38.                         pwm_Duty_Cycle += step;                }
    39.         }
    40.         if (Chip_MRT_GetIntPendingByChannel(1) == true)
    41.         {
    42.                 Chip_MRT_ClearIntPending(MRT1_INTFLAG);
    43.                 Board_LED_Set(0, false);
    44.         }
    45. }

    46. int main(void) {

    47. #if defined (__USE_LPCOPEN)
    48.     // Read clock settings and update SystemCoreClock variable
    49.     SystemCoreClockUpdate();
    50. #if !defined(NO_BOARD_LIB)
    51.     // Set up and initialize all required blocks and
    52.     // functions related to the board hardware
    53.     Board_Init();
    54.     // Set the LED to the state of "On"
    55.     Board_LED_Set(0, true);
    56. #endif
    57. #endif

    58.     // TODO: insert code here
    59.     Chip_MRT_Init();
    60.     Chip_MRT_SetDisabled(LPC_MRT_CH0);
    61.     Chip_MRT_SetDisabled(LPC_MRT_CH1);
    62.     NVIC_EnableIRQ(MRT_IRQn);
    63.     Chip_MRT_SetInterval(LPC_MRT_CH0, (Chip_Clock_GetSystemClockRate() / LPC_MRT_CH0_1MS) | MRT_INTVAL_LOAD);
    64.     Chip_MRT_SetMode(LPC_MRT_CH0, MRT_MODE_REPEAT);
    65.     Chip_MRT_SetInterval(LPC_MRT_CH1, Set_MRT_CH1_Interval(pwm_Duty_Cycle) | MRT_INTVAL_LOAD);
    66.     Chip_MRT_SetMode(LPC_MRT_CH1, MRT_MODE_ONESHOT);
    67.     Chip_MRT_SetEnabled(LPC_MRT_CH0);
    68.     Chip_MRT_SetEnabled(LPC_MRT_CH1);

    69.     // Force the counter to be placed into memory
    70.     volatile static int i = 0 ;
    71.     // Enter an infinite loop, just incrementing a counter
    72.     while(1) {
    73.         i++ ;
    74.     }
    75.     return 0 ;
    76. }
    复制代码



    回复

    使用道具 举报

  • TA的每日心情
    擦汗
    2021-9-9 22:51
  • 签到天数: 415 天

    连续签到: 1 天

    [LV.9]以坛为家II

    79

    主题

    3088

    帖子

    21

    金牌会员

    Rank: 6Rank: 6

    积分
    5181
    最后登录
    2022-5-23
    发表于 2016-10-23 21:12:31 | 显示全部楼层
      点个赞  
    该会员没有填写今日想说内容.
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    奋斗
    2023-2-15 00:12
  • 签到天数: 969 天

    连续签到: 1 天

    [LV.10]以坛为家III

    175

    主题

    2843

    帖子

    34

    金牌会员

    Rank: 6Rank: 6

    积分
    7123
    最后登录
    2023-4-20
    发表于 2016-10-24 08:24:02 | 显示全部楼层
    感谢分享!
    回复

    使用道具 举报

  • TA的每日心情
    开心
    2018-7-23 21:04
  • 签到天数: 103 天

    连续签到: 1 天

    [LV.6]常住居民II

    228

    主题

    5379

    帖子

    0

    金牌会员

    Rank: 6Rank: 6

    积分
    16701
    最后登录
    1970-1-1
     楼主| 发表于 2016-10-25 20:17:44 | 显示全部楼层

    多谢支持
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    奋斗
    2025-5-7 09:07
  • 签到天数: 353 天

    连续签到: 1 天

    [LV.8]以坛为家I

    141

    主题

    8056

    帖子

    0

    金牌会员

    Rank: 6Rank: 6

    积分
    5785
    最后登录
    2025-5-7
    发表于 2016-10-25 20:30:28 | 显示全部楼层
    用mbed比这简单多了
    该会员没有填写今日想说内容.
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2018-7-23 21:04
  • 签到天数: 103 天

    连续签到: 1 天

    [LV.6]常住居民II

    228

    主题

    5379

    帖子

    0

    金牌会员

    Rank: 6Rank: 6

    积分
    16701
    最后登录
    1970-1-1
     楼主| 发表于 2016-10-25 21:01:30 | 显示全部楼层
    wambob 发表于 2016-10-25 20:30
    用mbed比这简单多了

    是的,而且熟悉一个板子,其它的相通,mbed省心
    回复 支持 反对

    使用道具 举报

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

    本版积分规则

    关闭

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

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

    GMT+8, 2025-7-21 03:33 , Processed in 0.091204 second(s), 25 queries , MemCache On.

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.

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