打印
[STM32F1]

【转】基于stm32f103zet6的看门狗学习

[复制链接]
542|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
俺是村长他爹|  楼主 | 2016-11-12 23:43 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
看门狗,这个东西在哪都能看见,关于其中简单介绍在我的另一篇博文中有的,那是关于arm11的,不过大体一样http://blog.csdn.net/king_bingge/article/details/8510713
一、首先就是独立看门狗
直接上操作流程吧
1、三个比较重要的寄存器:键值寄存器(IWDG_KR )、预分频寄存器(IWDG_PR)、重载寄存器(IWDG_RLR)
向IWDG_KR 写入0X5555:能够去除写保护,方便我们给分频寄存器和重载寄存器进行写操作嘛!不就是类似于DS1302的写保护么。。
2、有一个知识点就是关于看门狗使用的时钟,这里使用的是内部低速时钟40k左右的样子,那么我们就能算出,定时的最大时间了。Tout=40Khz/((4*2^prer )*rlr)
向IWDG_KR 写入0XAAAA:这一步能够将我们写入的这个值重装载到IWDG_RLR这个寄存器中,类似于我们51的定时器!
3、接着就是像IWDG_KR 写入0XCCCC来启动看门狗,同时也就关闭了写保护,防止意外写入。
注意了:每次我们给IWDG_KR 写入0XAAAA的时候,他会自动重新进行喂狗!
二、分析代码
1、  IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);  //使能对寄存器IWDG_PR和IWDG_RLR的写操作,也就是带开写保护了!
不信跟踪进去看一看吧,仅仅尝试这一次,因为之前学arm11裸机的时候,全是操作寄存器的,现在烦了,这也是我第一次分析寄存器吧,反正我还是觉得库函数用着方便
Go   GO   GO  !

[html] view plain copy
print?


  • void IWDG_WriteAccessCmd(uint16_t IWDG_WriteAccess)  
  • {  
  •   /* Check the parameters */  
  •   assert_param(IS_IWDG_WRITE_ACCESS(IWDG_WriteAccess));  
  •   IWDG->KR = IWDG_WriteAccess;  
  • }  


上面是函数定义;

[html] view plain copy
print?


  • #define IWDG                ((IWDG_TypeDef *) IWDG_BASE)  


上面是原型

[html] view plain copy
print?


  • #define IWDG_BASE             (APB1PERIPH_BASE + 0x3000)  


这是跟踪到的地址,继续!

[html] view plain copy
print?


  • #define APB1PERIPH_BASE       PERIPH_BASE  

[html] view plain copy
print?


  • #define PERIPH_BASE           ((uint32_t)0x40000000) /*!< Peripheral base address in the alias region */  

[html] view plain copy
print?


  • 好的,反推回去!  

[html] view plain copy
print?


  •   

[html] view plain copy
print?


  • IWDG_BASE     = 0x40000000 <span style="white-space:pre">   </span>+<span style="white-space:pre">  </span>0x3000   

[html] view plain copy
print?


  • 最后找到这个结构体咯  

[html] view plain copy
print?


  • typedef struct  
  • {  
  •   __IO uint32_t KR;  
  •   __IO uint32_t PR;  
  •   __IO uint32_t RLR;  
  •   __IO uint32_t SR;  
  • } IWDG_TypeDef;  

[html] view plain copy
print?


  • 再看芯片手册  

[html] view plain copy
print?


  • 0x4000 3000 - 0x4000 33FF   独立看门狗(IWDG)  

继续跟踪参数。。
#define IWDG_WriteAccess_Enable     ((uint16_t)0x5555)

发现了??所以嘛    IWDG->KR = IWDG_WriteAccess;
就是相当于给
寄存器(IWDG_KR )0x40003000 地址写 0x5555。
OK分析完毕,ST32的库函数都是这样组织的,我很喜欢这种形式,开发速度相当快呀!即使有些地方的口碑不太好,但是我们在那部分就可以操作寄存器了,一举两得的事情
沙发
俺是村长他爹|  楼主 | 2016-11-12 23:45 | 只看该作者
2、直接给出初始化代码,有兴趣的自己可以分析,我是没什么兴趣了

[html] view plain copy
print?


  • void IWDG_Init(u8 prer,u16 rlr)   
  • {     
  •     IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);   //使能对寄存器IWDG_PR和IWDG_RLR的写操作  
  •       
  •     IWDG_SetPrescaler(prer);            //设置IWDG预分频值:设置IWDG预分频值为64  
  •       
  •     IWDG_SetReload(rlr);                //设置IWDG重装载值  
  •       
  •     IWDG_ReloadCounter();               //按照IWDG重装载寄存器的值重装载IWDG计数器  
  •       
  •     IWDG_Enable();                  //使能IWDG  
  • }  


//喂独立看门狗

[html] view plain copy
print?


  • void IWDG_Feed(void)  
  • {     
  •     IWDG_ReloadCounter();                                            
  •   
  • }  


下面看代码吧!
1、直接给初始化函数

[html] view plain copy
print?


  • void WWDG_Init(u8 tr,u8 wr,u32 fprer)  
  • {   
  •     RCC_APB1PeriphClockCmd(RCC_APB1Periph_WWDG, ENABLE);    //   WWDG时钟使能  
  •   
  •     WWDG_SetPrescaler(fprer);               //设置IWDG预分频值  
  •   
  •     WWDG_SetWindowValue(wr);                //设置窗口值  
  •   
  •     WWDG_Enable(tr);                    //使能看门狗 ,   设置 counter .                    
  •   
  •     WWDG_ClearFlag();  
  •   
  •     WWDG_NVIC_Init();                   //初始化窗口看门狗 NVIC  
  •   
  •     WWDG_EnableIT();                    //开启窗口看门狗中断  
  • }  

2、同样也有喂狗函数
//喂狗

[html] view plain copy
print?


  • void WWDG_Set_Counter(u8 cnt)  
  • {  
  •     WWDG_Enable(cnt);      
  • }  

3、上面提到了当技术到0x40的时候就会长生中断,那么中断怎么实现?
//窗口看门狗中断服务程序

[html] view plain copy
print?


  • void WWDG_NVIC_Init()  
  • {  
  •     NVIC_InitTypeDef NVIC_InitStructure;  
  •     NVIC_InitStructure.NVIC_IRQChannel = WWDG_IRQn; //WWDG中断  
  •     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //抢占2,子优先级3,组2      
  •     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;//抢占2,子优先级3,组2      
  •     NVIC_Init(&NVIC_InitStructure); //NVIC初始化  
  • }  
  • void WWDG_IRQHandler(void)  
  • {  
  •     // Update WWDG counter  
  •     WWDG_SetCounter(0x7F);//当禁掉此句后,窗口看门狗将产生复位  
  •     // Clear EWI flag */  
  •     WWDG_ClearFlag();//清除提前唤醒中断标志位  
  • }  

就是这样配置了!这样就能实现我们的两只狗看着程序这个大门了!!!
[html] view plain copy
print?


  •   

[html] view plain copy
print?


  • <pre></pre>  
  • <pre></pre>  
  • <p></p>  
  • <pre name="code" class="html"><pre name="code" class="html"><pre name="code" class="html"><pre name="code" class="html"><pre name="code" class="html"><pre></pre>  
  • <pre></pre>  
  • <pre></pre>  
  • <pre></pre>  
  • <pre></pre>  
  • <pre></pre>  
  • <pre></pre>  
  • <pre></pre>  
  • <pre></pre>  
  • <pre></pre>  
  • <pre></pre>  
  • <pre></pre>  
  • <pre></pre>  
  • <pre></pre>  
  • <pre></pre>  
  • <pre></pre>  
  • <pre></pre>  
  • <pre></pre>  
  • <pre></pre>  
  • <pre></pre>  
  • <pre></pre>  
  • <pre></pre>  
  • <pre></pre>  
  • <pre></pre>  
  • <pre></pre>  
  • <pre></pre>  
  • <pre></pre>  
  • <pre></pre>  
  • <pre></pre>  
  • <pre></pre>  
  • <pre></pre>  
  • <pre></pre>  
  • <pre></pre>  
  • <pre></pre>  
  • <pre></pre>  
  • <pre></pre>  
  • <pre></pre>  
  •      
  • </pre></pre></pre></pre></pre>  

使用特权

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

本版积分规则

66

主题

168

帖子

2

粉丝