[范例教程] 【M0】 MG32F02A 学习笔记③ 定时器0 1ms延时

[复制链接]
1955|4
 楼主| noctor 发表于 2018-9-14 09:00 | 显示全部楼层 |阅读模式
本帖最后由 noctor 于 2018-9-14 09:34 编辑

      上回我们说到了MG32F02A的GPIO的使用来进行点亮LED的实验,这次,我们来为这个点亮LED做一点改进,变成流水灯吧。
      上次的GPIO实验帖子链接:https://bbs.21ic.com/icview-2553822-1-1.html"code_div">
  1. #include "MG32x02z_DRV.H"
  2. #include <stdio.h>

  3. typedef uint8_t u8;
  4. typedef uint16_t u16;
  5. typedef uint32_t u32;
  6. typedef uint64_t u64;


  7. void ChipInit(void);
  8. //  <h> Delay Period Setting
  9. //  <o0> Clock Prescaler (1~256)  <1-256>
  10. //  <o1> Main Counter (1~256) <1-256>

  11. //if user wants to delay 1ms and CK_TM00_PR is 12MHz.
  12. //The Total clocks is 12M*1ms = 12000.
  13. //User can set "clock prescaler"=100 and "pulse width"=120 .   
  14. #define Simple_Time_Prescaler       100
  15. #define Simple_Time_MainCounter     120


  16. void TM00_Delay_Init(void)
  17. {  
  18.     TM_TimeBaseInitTypeDef TM_TimeBase_InitStruct;
  19.     // make sure :
  20.         
  21.     //===Set CSC init====
  22.     //MG32x02z_CSC_Init.h(Configuration Wizard)
  23.     //Select CK_HS source = CK_IHRCO
  24.     //Select IHRCO = 12M
  25.     //Select CK_MAIN Source = CK_HS
  26.     //Configure PLL->Select APB Prescaler = CK_MAIN/1
  27.     //Configure Peripheral On Mode Clock->TM00 = Enable
  28.         
  29.     // ----------------------------------------------------
  30.     // 1.initial TimeBase structure
  31.     TM_TimeBaseStruct_Init(&TM_TimeBase_InitStruct);
  32.    
  33.     // modify parameter
  34.     TM_TimeBase_InitStruct.TM_Period = Simple_Time_MainCounter - 1;
  35.     TM_TimeBase_InitStruct.TM_Prescaler = Simple_Time_Prescaler - 1;
  36.     TM_TimeBase_InitStruct.TM_CounterMode = Cascade;
  37.    
  38.     TM_TimeBase_Init(TM00, &TM_TimeBase_InitStruct);
  39.                
  40.                 TM_ClearFlag(TM00, TMx_TOF);
  41.    
  42.     // ----------------------------------------------------
  43.     // 3.Start TM00
  44.     TM_Timer_Cmd(TM00, ENABLE);

  45.     // ----------------------------------------------------
  46.     // 4.until TOF flag event (polling)
  47.     while(TM_GetSingleFlagStatus(TM00, TMx_TOF) == DRV_UnHappened);
  48.     TM_ClearFlag(TM00, TMx_TOF);        // clear TOF flag
  49. //                TM_Timer_Cmd(TM00, DISABLE);
  50.                
  51. }
  52. void delay_ms(int i)
  53. {
  54.         int x=0;
  55.         for(x=0;x<i;x++)
  56.         {
  57.                 while(TM_GetSingleFlagStatus(TM00, TMx_TOF) == DRV_UnHappened);
  58.     TM_ClearFlag(TM00, TMx_TOF);        // clear TOF flag
  59.         }
  60. }


  61. int main()
  62. {

  63.         PIN_InitTypeDef PINX_InitStruct;

  64.         ChipInit();
  65.         
  66.         PINX_InitStruct.PINX_Mode                                 = PINX_Mode_PushPull_O;          // Pin select digital input mode
  67.         PINX_InitStruct.PINX_PUResistant                 = PINX_PUResistant_Enable;  // Enable pull up resistor
  68.         PINX_InitStruct.PINX_Speed                                   = PINX_Speed_Low;                        
  69.         PINX_InitStruct.PINX_OUTDrive                         = PINX_OUTDrive_Level0;         // Pin output driver full strength.
  70.         PINX_InitStruct.PINX_FilterDivider                   = PINX_FilterDivider_Bypass;// Pin input deglitch filter clock divider bypass
  71.         PINX_InitStruct.PINX_Inverse                         = PINX_Inverse_Disable;         // Pin input data not inverse
  72.         PINX_InitStruct.PINX_Alternate_Function = 0;                                                 // Pin AFS = 0
  73.   GPIO_PinMode_Config(PINE(15),&PINX_InitStruct);                                          // D6 setup at PE15
  74.         TM00_Delay_Init();
  75.     while(1)
  76.     {
  77.                
  78.                 delay_ms(1000);
  79.                         

  80.                 PE15=~PE15;

  81.     }

  82.                
  83. }
下面是CSC_Init.h下的设置:
  

       后面是不使用Wizard的版本,此处要注意一点,我的初始化函数CSC_Init()与系统使用的初始化函数同名,要是用的话可以改名或者直接把mg32x02z_Init目录删除:
  1. #include "MG32x02z_DRV.H"
  2. #include <stdio.h>

  3. typedef uint8_t u8;
  4. typedef uint16_t u16;
  5. typedef uint32_t u32;
  6. typedef uint64_t u64;

  7. //  <h> Delay Period Setting
  8. //  <o0> Clock Prescaler (1~256)  <1-256>
  9. //  <o1> Main Counter (1~256) <1-256>

  10. //if user wants to delay 1ms and CK_TM00_PR is 12MHz.
  11. //The Total clocks is 12M*1ms = 12000.
  12. //User can set "clock prescaler"=100 and "pulse width"=120 .   
  13. #define Simple_Time_Prescaler       100
  14. #define Simple_Time_MainCounter     120

  15. void CSC_Init (void)
  16. {
  17.         CSC_PLL_TyprDef CSC_PLL_CFG;
  18.    
  19.         
  20.   UnProtectModuleReg(MEMprotect);             // Setting flash wait state
  21.   MEM_SetFlashWaitState(MEM_FWAIT_ONE);        // 50MHz> Sysclk >=25MHz
  22.   ProtectModuleReg(MEMprotect);

  23.   UnProtectModuleReg(CSCprotect);
  24.         CSC_CK_APB_Divider_Select(APB_DIV_1);        // Modify CK_APB divider        APB=CK_MAIN/1
  25.         CSC_CK_AHB_Divider_Select(AHB_DIV_1);        // Modify CK_AHB divider        AHB=APB/1

  26.         
  27.         /* CK_HS selection */
  28.         CSC_IHRCO_Select(IHRCO_12MHz);                        // IHRCO Sel 12MHz
  29.         CSC_IHRCO_Cmd(ENABLE);
  30.         while(CSC_GetSingleFlagStatus(CSC_IHRCOF) == DRV_Normal);
  31.         CSC_ClearFlag(CSC_IHRCOF);
  32.         CSC_CK_HS_Select(HS_CK_IHRCO);                        // CK_HS select IHRCO

  33.         
  34.         /* CK_MAIN */
  35.         CSC_CK_MAIN_Select(MAIN_CK_HS);        


  36.         /* Configure ICKO function */
  37.                
  38.         /* Configure peripheral clock */

  39.          CSC_PeriphOnModeClock_Config(CSC_ON_PortE,ENABLE);
  40.         
  41.         CSC_PeriphProcessClockSource_Config(CSC_TM00_CKS, CK_APB);
  42.         CSC_PeriphOnModeClock_Config(CSC_ON_TM00, ENABLE);                                          // Enable IIC0 module clock


  43.         

  44.     ProtectModuleReg(CSCprotect);
  45.    
  46. }


  47. void TM00_Delay_Init(void)
  48. {  
  49.     TM_TimeBaseInitTypeDef TM_TimeBase_InitStruct;
  50.     // make sure :
  51.         
  52.     //===Set CSC init====
  53.     //MG32x02z_CSC_Init.h(Configuration Wizard)
  54.     //Select CK_HS source = CK_IHRCO
  55.     //Select IHRCO = 12M
  56.     //Select CK_MAIN Source = CK_HS
  57.     //Configure PLL->Select APB Prescaler = CK_MAIN/1
  58.     //Configure Peripheral On Mode Clock->TM00 = Enable
  59.         
  60.     // ----------------------------------------------------
  61.     // 1.initial TimeBase structure
  62.     TM_TimeBaseStruct_Init(&TM_TimeBase_InitStruct);
  63.    
  64.     // modify parameter
  65.     TM_TimeBase_InitStruct.TM_Period = Simple_Time_MainCounter - 1;
  66.     TM_TimeBase_InitStruct.TM_Prescaler = Simple_Time_Prescaler - 1;
  67.     TM_TimeBase_InitStruct.TM_CounterMode = Cascade;
  68.    
  69.     TM_TimeBase_Init(TM00, &TM_TimeBase_InitStruct);
  70.                
  71.                 TM_ClearFlag(TM00, TMx_TOF);
  72.    
  73.     // ----------------------------------------------------
  74.     // 3.Start TM00
  75.     TM_Timer_Cmd(TM00, ENABLE);

  76.     // ----------------------------------------------------
  77.     // 4.until TOF flag event (polling)
  78.     while(TM_GetSingleFlagStatus(TM00, TMx_TOF) == DRV_UnHappened);
  79.     TM_ClearFlag(TM00, TMx_TOF);        // clear TOF flag
  80. //                TM_Timer_Cmd(TM00, DISABLE);
  81.                
  82. }
  83. void delay_ms(int i)
  84. {
  85.         int x=0;
  86.         for(x=0;x<i;x++)
  87.         {
  88.                 while(TM_GetSingleFlagStatus(TM00, TMx_TOF) == DRV_UnHappened);
  89.     TM_ClearFlag(TM00, TMx_TOF);        // clear TOF flag
  90.         }
  91. }


  92. int main()
  93. {

  94.         PIN_InitTypeDef PINX_InitStruct;
  95.         CSC_Init();
  96.         PINX_InitStruct.PINX_Mode                                 = PINX_Mode_PushPull_O;          // Pin select digital input mode
  97.         PINX_InitStruct.PINX_PUResistant                 = PINX_PUResistant_Enable;  // Enable pull up resistor
  98.         PINX_InitStruct.PINX_Speed                                   = PINX_Speed_Low;                        
  99.         PINX_InitStruct.PINX_OUTDrive                         = PINX_OUTDrive_Level0;         // Pin output driver full strength.
  100.         PINX_InitStruct.PINX_FilterDivider                   = PINX_FilterDivider_Bypass;// Pin input deglitch filter clock divider bypass
  101.         PINX_InitStruct.PINX_Inverse                         = PINX_Inverse_Disable;         // Pin input data not inverse
  102.         PINX_InitStruct.PINX_Alternate_Function = 0;                                                 // Pin AFS = 0
  103.   GPIO_PinMode_Config(PINE(15),&PINX_InitStruct);                                          // D6 setup at PE15
  104.         TM00_Delay_Init();
  105.     while(1)
  106.     {
  107.                
  108.                 delay_ms(1000);
  109.                         

  110.                 PE15=~PE15;

  111.     }

  112.                
  113. }






本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
344864311 发表于 2018-9-14 17:28 | 显示全部楼层
写的不错,顶一个。。
 楼主| noctor 发表于 2018-9-17 13:23 | 显示全部楼层
344864311 发表于 2018-9-14 17:28
写的不错,顶一个。。

哈哈终于有回帖了,好耶
344864311 发表于 2018-10-12 16:42 | 显示全部楼层
已经验证   工作OK。
 楼主| noctor 发表于 2018-10-12 16:48 | 显示全部楼层
344864311 发表于 2018-10-12 16:42
已经验证   工作OK。

我可都是验证能用我才发出来的
您需要登录后才可以回帖 登录 | 注册

本版积分规则

26

主题

82

帖子

3

粉丝
快速回复 在线客服 返回列表 返回顶部