打印
[范例教程]

【M0】 MG32F02A 学习笔记③ 定时器0 1ms延时

[复制链接]
1496|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">
#include "MG32x02z_DRV.H"
#include <stdio.h>

typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;


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

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


void TM00_Delay_Init(void)
{  
    TM_TimeBaseInitTypeDef TM_TimeBase_InitStruct;
    // make sure :
        
    //===Set CSC init====
    //MG32x02z_CSC_Init.h(Configuration Wizard)
    //Select CK_HS source = CK_IHRCO
    //Select IHRCO = 12M
    //Select CK_MAIN Source = CK_HS
    //Configure PLL->Select APB Prescaler = CK_MAIN/1
    //Configure Peripheral On Mode Clock->TM00 = Enable
        
    // ----------------------------------------------------
    // 1.initial TimeBase structure
    TM_TimeBaseStruct_Init(&TM_TimeBase_InitStruct);
   
    // modify parameter
    TM_TimeBase_InitStruct.TM_Period = Simple_Time_MainCounter - 1;
    TM_TimeBase_InitStruct.TM_Prescaler = Simple_Time_Prescaler - 1;
    TM_TimeBase_InitStruct.TM_CounterMode = Cascade;
   
    TM_TimeBase_Init(TM00, &TM_TimeBase_InitStruct);
               
                TM_ClearFlag(TM00, TMx_TOF);
   
    // ----------------------------------------------------
    // 3.Start TM00
    TM_Timer_Cmd(TM00, ENABLE);

    // ----------------------------------------------------
    // 4.until TOF flag event (polling)
    while(TM_GetSingleFlagStatus(TM00, TMx_TOF) == DRV_UnHappened);
    TM_ClearFlag(TM00, TMx_TOF);        // clear TOF flag
//                TM_Timer_Cmd(TM00, DISABLE);
               
}
void delay_ms(int i)
{
        int x=0;
        for(x=0;x<i;x++)
        {
                while(TM_GetSingleFlagStatus(TM00, TMx_TOF) == DRV_UnHappened);
    TM_ClearFlag(TM00, TMx_TOF);        // clear TOF flag
        }
}


int main()
{

        PIN_InitTypeDef PINX_InitStruct;

        ChipInit();
        
        PINX_InitStruct.PINX_Mode                                 = PINX_Mode_PushPull_O;          // Pin select digital input mode
        PINX_InitStruct.PINX_PUResistant                 = PINX_PUResistant_Enable;  // Enable pull up resistor
        PINX_InitStruct.PINX_Speed                                   = PINX_Speed_Low;                        
        PINX_InitStruct.PINX_OUTDrive                         = PINX_OUTDrive_Level0;         // Pin output driver full strength.
        PINX_InitStruct.PINX_FilterDivider                   = PINX_FilterDivider_Bypass;// Pin input deglitch filter clock divider bypass
        PINX_InitStruct.PINX_Inverse                         = PINX_Inverse_Disable;         // Pin input data not inverse
        PINX_InitStruct.PINX_Alternate_Function = 0;                                                 // Pin AFS = 0
  GPIO_PinMode_Config(PINE(15),&PINX_InitStruct);                                          // D6 setup at PE15
        TM00_Delay_Init();
    while(1)
    {
               
                delay_ms(1000);
                        

                PE15=~PE15;

    }

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

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

typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;

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

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

void CSC_Init (void)
{
        CSC_PLL_TyprDef CSC_PLL_CFG;
   
        
  UnProtectModuleReg(MEMprotect);             // Setting flash wait state
  MEM_SetFlashWaitState(MEM_FWAIT_ONE);        // 50MHz> Sysclk >=25MHz
  ProtectModuleReg(MEMprotect);

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

        
        /* CK_HS selection */
        CSC_IHRCO_Select(IHRCO_12MHz);                        // IHRCO Sel 12MHz
        CSC_IHRCO_Cmd(ENABLE);
        while(CSC_GetSingleFlagStatus(CSC_IHRCOF) == DRV_Normal);
        CSC_ClearFlag(CSC_IHRCOF);
        CSC_CK_HS_Select(HS_CK_IHRCO);                        // CK_HS select IHRCO

        
        /* CK_MAIN */
        CSC_CK_MAIN_Select(MAIN_CK_HS);        


        /* Configure ICKO function */
               
        /* Configure peripheral clock */

         CSC_PeriphOnModeClock_Config(CSC_ON_PortE,ENABLE);
        
        CSC_PeriphProcessClockSource_Config(CSC_TM00_CKS, CK_APB);
        CSC_PeriphOnModeClock_Config(CSC_ON_TM00, ENABLE);                                          // Enable IIC0 module clock


        

    ProtectModuleReg(CSCprotect);
   
}


void TM00_Delay_Init(void)
{  
    TM_TimeBaseInitTypeDef TM_TimeBase_InitStruct;
    // make sure :
        
    //===Set CSC init====
    //MG32x02z_CSC_Init.h(Configuration Wizard)
    //Select CK_HS source = CK_IHRCO
    //Select IHRCO = 12M
    //Select CK_MAIN Source = CK_HS
    //Configure PLL->Select APB Prescaler = CK_MAIN/1
    //Configure Peripheral On Mode Clock->TM00 = Enable
        
    // ----------------------------------------------------
    // 1.initial TimeBase structure
    TM_TimeBaseStruct_Init(&TM_TimeBase_InitStruct);
   
    // modify parameter
    TM_TimeBase_InitStruct.TM_Period = Simple_Time_MainCounter - 1;
    TM_TimeBase_InitStruct.TM_Prescaler = Simple_Time_Prescaler - 1;
    TM_TimeBase_InitStruct.TM_CounterMode = Cascade;
   
    TM_TimeBase_Init(TM00, &TM_TimeBase_InitStruct);
               
                TM_ClearFlag(TM00, TMx_TOF);
   
    // ----------------------------------------------------
    // 3.Start TM00
    TM_Timer_Cmd(TM00, ENABLE);

    // ----------------------------------------------------
    // 4.until TOF flag event (polling)
    while(TM_GetSingleFlagStatus(TM00, TMx_TOF) == DRV_UnHappened);
    TM_ClearFlag(TM00, TMx_TOF);        // clear TOF flag
//                TM_Timer_Cmd(TM00, DISABLE);
               
}
void delay_ms(int i)
{
        int x=0;
        for(x=0;x<i;x++)
        {
                while(TM_GetSingleFlagStatus(TM00, TMx_TOF) == DRV_UnHappened);
    TM_ClearFlag(TM00, TMx_TOF);        // clear TOF flag
        }
}


int main()
{

        PIN_InitTypeDef PINX_InitStruct;
        CSC_Init();
        PINX_InitStruct.PINX_Mode                                 = PINX_Mode_PushPull_O;          // Pin select digital input mode
        PINX_InitStruct.PINX_PUResistant                 = PINX_PUResistant_Enable;  // Enable pull up resistor
        PINX_InitStruct.PINX_Speed                                   = PINX_Speed_Low;                        
        PINX_InitStruct.PINX_OUTDrive                         = PINX_OUTDrive_Level0;         // Pin output driver full strength.
        PINX_InitStruct.PINX_FilterDivider                   = PINX_FilterDivider_Bypass;// Pin input deglitch filter clock divider bypass
        PINX_InitStruct.PINX_Inverse                         = PINX_Inverse_Disable;         // Pin input data not inverse
        PINX_InitStruct.PINX_Alternate_Function = 0;                                                 // Pin AFS = 0
  GPIO_PinMode_Config(PINE(15),&PINX_InitStruct);                                          // D6 setup at PE15
        TM00_Delay_Init();
    while(1)
    {
               
                delay_ms(1000);
                        

                PE15=~PE15;

    }

               
}






沙发
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。

使用特权

评论回复
5
noctor|  楼主 | 2018-10-12 16:48 | 只看该作者
344864311 发表于 2018-10-12 16:42
已经验证   工作OK。

我可都是验证能用我才发出来的

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

26

主题

82

帖子

3

粉丝