找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 5357|回复: 0
打印 上一主题 下一主题
收起左侧

STM32之SYSCLK滴答精确延时程序 寄存器实现

[复制链接]
跳转到指定楼层
楼主
ID:82781 发表于 2015-6-14 03:17 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
要知道时间对程序来说也是生命!该延时的要延时,不该延时的不能延时,操作器件不也就是操作时序吗?,所以延时是非常重要的,但是ARM的运算速度非常快,用普通单片机似的延时恐怕。。。,很难算得很精确,这就出来了滴答时钟,他和内核在一块,方便的时钟选择,简单的寄存器,就搞定。
本程是查询程序,下一步亚用中断实现!
值得注意的是我没找到库函数!所以采用的是操作寄存器实现的!

SYSCLK=72mhz

  1. #include "main.h"
  2. GPIO_InitTypeDef GPIO_InitStructure;
  3. void mysysint()//系统初始化程序
  4. {
  5. ErrorStatus HSEStartUpStatus;//说明标志位
  6. RCC_DeInit();//所有外设全部缺省设置

  7. /* Enable HSE */
  8. RCC_HSEConfig(RCC_HSE_ON);
  9. /* Wait till HSE is ready and if Time out is reached exit */
  10. HSEStartUpStatus = RCC_WaitForHSEStartUp();
  11. if(HSEStartUpStatus == SUCCESS)//启动成功
  12. {
  13. /*这两条FLASH指令必须加上,不知为啥?不加上就运行几秒后出错,参照系统初始化*/
  14. /* Enable The Prefetch Buffer */
  15. FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);//FLASH缓存开启
  16. /* Configure the Latency cycle: Set 2 Latency cycles */
  17.   FLASH_SetLatency(FLASH_Latency_2);  //设置FLASH这些位表示SYSCLK(系统时钟)周期与闪存访问时间的比例,为010:两个等待状态,当 48MHz < SYSCLK ≤ 72MHz
  18. /* Set PLL clock output to 72MHz using HSE (8MHz) as entry clock */
  19. RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);//外部时钟为8M,PLL的输入时钟=8MHZ,倍频系数9,

  20. /* Configure HCLK such as HCLK = SYSCLK */
  21. RCC_HCLKConfig(RCC_SYSCLK_Div1);//设置了啦AHB分频器的分频系数=1,即HCLK=SYSCLK=72MHZ
  22. /* Configure PCLK1 such as PCLK1 = HCLK/2 */
  23. RCC_PCLK1Config(RCC_HCLK_Div2);//设置了APB1外设的时钟频率最大是36M这里是APB1的分频器设为2,PCLK1=HCLK/2=72/2=36MHZ正好是最大值
  24. /* Configure PCLK2 such as PCLK2 = HCLK */
  25. RCC_PCLK2Config(RCC_HCLK_Div1);//设置PLCK2=HCLK=72MHZ,的APB2分频器=1
  26. /* Select the PLL as system clock source */
  27. RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);//设置了SYSCLK的提供者为PLL,频率由上面算出=72MHZ
  28. /* disable PLL Ready interrupt */
  29. RCC_ITConfig(RCC_IT_PLLRDY, DISABLE);//PLL中断关闭
  30. /* disable PLL Ready interrupt */
  31. RCC_ITConfig(RCC_IT_HSERDY,DISABLE);//HSE中断关闭
  32. /* disable PLL Ready interrupt */
  33. RCC_ITConfig(RCC_IT_HSIRDY, DISABLE); //HSI中断关闭
  34. /* disable PLL Ready interrupt */
  35. RCC_ITConfig(RCC_IT_LSERDY, DISABLE); //LSE中断关闭
  36. /* disable PLL Ready interrupt */
  37. RCC_ITConfig(RCC_IT_LSIRDY, DISABLE); //LSI中断关闭

  38. /* PLL clock divided by 1.5 used as USB clock source */
  39. RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);//设置USB的时钟为=72、1.5=48mhz
  40. /* Configure ADCCLK such as ADCCLK = PCLK2/2 */
  41. RCC_ADCCLKConfig(RCC_PCLK2_Div2);//设置ADC时钟=PCLK2/2= 36MHZ
  42. /* disable the LSE */
  43. RCC_LSEConfig(RCC_LSE_OFF);//外部低速晶振关闭

  44. /*DISable the RTC clock */
  45. RCC_RTCCLKCmd(DISABLE);
  46. /* DISable the Clock Security System */
  47. RCC_ClockSecuritySystemCmd(DISABLE);
  48. /* Enable the PLL */
  49. RCC_PLLCmd(ENABLE);//使能PLL







  50. /* PLL ans system clock config */
  51. }
  52. else
  53. {
  54. /* Add here some code to deal with this error */
  55. }





  56. }

  57. /** @addtogroup STM32F10x_StdPeriph_Examples
  58.   * @{
  59.   */

  60. /** @addtogroup SysTick_TimeBase
  61.   * @{
  62.   */

  63. /* Private typedef -----------------------------------------------------------*/
  64. /* Private define ------------------------------------------------------------*/
  65. /* Private macro -------------------------------------------------------------*/
  66. /* Private variables ---------------------------------------------------------*/
  67. static __IO uint32_t TimingDelay;

  68. /* Private function prototypes -----------------------------------------------*/
  69. void Delay(__IO uint32_t nTime);

  70. /* Private functions ---------------------------------------------------------*/

  71. /**
  72.   * @brief  Main program.
  73.   * @param  None
  74.   * @retval None
  75.   */


  76. void wang_mySYStick_init()
  77. {

  78. //SysTick->LOAD=72000; //53MHZ下定时1ms
  79. SysTick->LOAD  = 72000;

  80. SysTick->CTRL=0x00000005;//表示HCLK=SYStick时钟,开启滴答,关闭中断


  81. }

  82. /*
  83. *此函数为滴答时钟的查询函数,不断地在查询标志位!!!!
  84. *
  85. *  王均伟
  86. *
  87. * 形参为时间值,延时时间=TIME*1毫秒

  88. */
  89. void mydelay(unsigned int time) //20110725他妈的我没找到关于这个版本的库函数,直接对寄存器操作了、反而感觉更亲切了。很想51
  90. {
  91. unsigned long a,b;
  92.    while(time)
  93.    {
  94.      a=SysTick->CTRL;
  95.   b=a&0x00010000;
  96.    if(b==0x00010000)
  97.    {
  98.     SysTick->LOAD  = 72000-1;  //用的72MHZ时钟,一直使用的话要减一!!!!
  99.   time--;
  100.    }



  101.    }

  102. }
  103. int main(void)
  104. {

  105. mysysint();//系统初始化程序

  106. /* GPIOD Periph clock enable */
  107.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);//使能时钟
  108. /* Configure PD0 and PD2 in output pushpull mode */
  109.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9| GPIO_Pin_10| GPIO_Pin_11;
  110.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  111.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  112.   GPIO_Init(GPIOD, &GPIO_InitStructure);


  113. wang_mySYStick_init();//滴答初始化!

  114.   while(1)
  115.   {
  116.       /* Set the GPIOA port pin 10 and pin 15 */
  117.   GPIO_SetBits(GPIOD,  GPIO_Pin_8);//GPIOD->BSRR = 0x00000f00;//置为PD8-PD11
  118. mydelay(100);

  119.    /* Clears the GPIOA port pin 10 and pin 15 */
  120.     GPIO_ResetBits(GPIOD,  GPIO_Pin_8);//GPIOD->BRR  = 0x00000f00;

  121. mydelay(100);
  122.   /* Set the GPIOA port pin 10 and pin 15 */
  123.   GPIO_SetBits(GPIOD, GPIO_Pin_9);//GPIOD->BSRR = 0x00000f00;//置为PD8-PD11
  124. mydelay(100);

  125.    /* Clears the GPIOA port pin 10 and pin 15 */
  126.     GPIO_ResetBits(GPIOD,  GPIO_Pin_9);//GPIOD->BRR  = 0x00000f00;

  127. mydelay(100);

  128.   /* Set the GPIOA port pin 10 and pin 15 */
  129.   GPIO_SetBits(GPIOD, GPIO_Pin_10);//GPIOD->BSRR = 0x00000f00;//置为PD8-PD11
  130. mydelay(100);

  131.    /* Clears the GPIOA port pin 10 and pin 15 */
  132.     GPIO_ResetBits(GPIOD,  GPIO_Pin_10);//GPIOD->BRR  = 0x00000f00;


  133.   mydelay(100);
  134.   /* Set the GPIOA port pin 10 and pin 15 */
  135.   GPIO_SetBits(GPIOD, GPIO_Pin_11);//GPIOD->BSRR = 0x00000f00;//置为PD8-PD11
  136. mydelay(100);

  137.    /* Clears the GPIOA port pin 10 and pin 15 */
  138.     GPIO_ResetBits(GPIOD,  GPIO_Pin_11);//GPIOD->BRR  = 0x00000f00;

  139. mydelay(100);


  140.   }
复制代码





分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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