要知道时间对程序来说也是生命!该延时的要延时,不该延时的不能延时,操作器件不也就是操作时序吗?,所以延时是非常重要的,但是ARM的运算速度非常快,用普通单片机似的延时恐怕。。。,很难算得很精确,这就出来了滴答时钟,他和内核在一块,方便的时钟选择,简单的寄存器,就搞定。
本程是查询程序,下一步亚用中断实现!
值得注意的是我没找到库函数!所以采用的是操作寄存器实现的!
SYSCLK=72mhz
- #include "main.h"
- GPIO_InitTypeDef GPIO_InitStructure;
- void mysysint()//系统初始化程序
- {
- ErrorStatus HSEStartUpStatus;//说明标志位
- RCC_DeInit();//所有外设全部缺省设置
- /* Enable HSE */
- RCC_HSEConfig(RCC_HSE_ON);
- /* Wait till HSE is ready and if Time out is reached exit */
- HSEStartUpStatus = RCC_WaitForHSEStartUp();
- if(HSEStartUpStatus == SUCCESS)//启动成功
- {
- /*这两条FLASH指令必须加上,不知为啥?不加上就运行几秒后出错,参照系统初始化*/
- /* Enable The Prefetch Buffer */
- FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);//FLASH缓存开启
- /* Configure the Latency cycle: Set 2 Latency cycles */
- FLASH_SetLatency(FLASH_Latency_2); //设置FLASH这些位表示SYSCLK(系统时钟)周期与闪存访问时间的比例,为010:两个等待状态,当 48MHz < SYSCLK ≤ 72MHz
- /* Set PLL clock output to 72MHz using HSE (8MHz) as entry clock */
- RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);//外部时钟为8M,PLL的输入时钟=8MHZ,倍频系数9,
- /* Configure HCLK such as HCLK = SYSCLK */
- RCC_HCLKConfig(RCC_SYSCLK_Div1);//设置了啦AHB分频器的分频系数=1,即HCLK=SYSCLK=72MHZ
- /* Configure PCLK1 such as PCLK1 = HCLK/2 */
- RCC_PCLK1Config(RCC_HCLK_Div2);//设置了APB1外设的时钟频率最大是36M这里是APB1的分频器设为2,PCLK1=HCLK/2=72/2=36MHZ正好是最大值
- /* Configure PCLK2 such as PCLK2 = HCLK */
- RCC_PCLK2Config(RCC_HCLK_Div1);//设置PLCK2=HCLK=72MHZ,的APB2分频器=1
- /* Select the PLL as system clock source */
- RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);//设置了SYSCLK的提供者为PLL,频率由上面算出=72MHZ
- /* disable PLL Ready interrupt */
- RCC_ITConfig(RCC_IT_PLLRDY, DISABLE);//PLL中断关闭
- /* disable PLL Ready interrupt */
- RCC_ITConfig(RCC_IT_HSERDY,DISABLE);//HSE中断关闭
- /* disable PLL Ready interrupt */
- RCC_ITConfig(RCC_IT_HSIRDY, DISABLE); //HSI中断关闭
- /* disable PLL Ready interrupt */
- RCC_ITConfig(RCC_IT_LSERDY, DISABLE); //LSE中断关闭
- /* disable PLL Ready interrupt */
- RCC_ITConfig(RCC_IT_LSIRDY, DISABLE); //LSI中断关闭
- /* PLL clock divided by 1.5 used as USB clock source */
- RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);//设置USB的时钟为=72、1.5=48mhz
- /* Configure ADCCLK such as ADCCLK = PCLK2/2 */
- RCC_ADCCLKConfig(RCC_PCLK2_Div2);//设置ADC时钟=PCLK2/2= 36MHZ
- /* disable the LSE */
- RCC_LSEConfig(RCC_LSE_OFF);//外部低速晶振关闭
- /*DISable the RTC clock */
- RCC_RTCCLKCmd(DISABLE);
- /* DISable the Clock Security System */
- RCC_ClockSecuritySystemCmd(DISABLE);
- /* Enable the PLL */
- RCC_PLLCmd(ENABLE);//使能PLL
-
-
-
- /* PLL ans system clock config */
- }
- else
- {
- /* Add here some code to deal with this error */
- }
-
-
- }
- /** @addtogroup STM32F10x_StdPeriph_Examples
- * @{
- */
- /** @addtogroup SysTick_TimeBase
- * @{
- */
- /* Private typedef -----------------------------------------------------------*/
- /* Private define ------------------------------------------------------------*/
- /* Private macro -------------------------------------------------------------*/
- /* Private variables ---------------------------------------------------------*/
- static __IO uint32_t TimingDelay;
- /* Private function prototypes -----------------------------------------------*/
- void Delay(__IO uint32_t nTime);
- /* Private functions ---------------------------------------------------------*/
- /**
- * @brief Main program.
- * @param None
- * @retval None
- */
- void wang_mySYStick_init()
- {
- //SysTick->LOAD=72000; //53MHZ下定时1ms
- SysTick->LOAD = 72000;
- SysTick->CTRL=0x00000005;//表示HCLK=SYStick时钟,开启滴答,关闭中断
- }
- /*
- *此函数为滴答时钟的查询函数,不断地在查询标志位!!!!
- *
- * 王均伟
- *
- * 形参为时间值,延时时间=TIME*1毫秒
- */
- void mydelay(unsigned int time) //20110725他妈的我没找到关于这个版本的库函数,直接对寄存器操作了、反而感觉更亲切了。很想51
- {
- unsigned long a,b;
- while(time)
- {
- a=SysTick->CTRL;
- b=a&0x00010000;
- if(b==0x00010000)
- {
- SysTick->LOAD = 72000-1; //用的72MHZ时钟,一直使用的话要减一!!!!
- time--;
- }
-
- }
- }
- int main(void)
- {
- mysysint();//系统初始化程序
- /* GPIOD Periph clock enable */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);//使能时钟
- /* Configure PD0 and PD2 in output pushpull mode */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9| GPIO_Pin_10| GPIO_Pin_11;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- wang_mySYStick_init();//滴答初始化!
- while(1)
- {
- /* Set the GPIOA port pin 10 and pin 15 */
- GPIO_SetBits(GPIOD, GPIO_Pin_8);//GPIOD->BSRR = 0x00000f00;//置为PD8-PD11
- mydelay(100);
- /* Clears the GPIOA port pin 10 and pin 15 */
- GPIO_ResetBits(GPIOD, GPIO_Pin_8);//GPIOD->BRR = 0x00000f00;
-
- mydelay(100);
- /* Set the GPIOA port pin 10 and pin 15 */
- GPIO_SetBits(GPIOD, GPIO_Pin_9);//GPIOD->BSRR = 0x00000f00;//置为PD8-PD11
- mydelay(100);
- /* Clears the GPIOA port pin 10 and pin 15 */
- GPIO_ResetBits(GPIOD, GPIO_Pin_9);//GPIOD->BRR = 0x00000f00;
-
- mydelay(100);
-
- /* Set the GPIOA port pin 10 and pin 15 */
- GPIO_SetBits(GPIOD, GPIO_Pin_10);//GPIOD->BSRR = 0x00000f00;//置为PD8-PD11
- mydelay(100);
- /* Clears the GPIOA port pin 10 and pin 15 */
- GPIO_ResetBits(GPIOD, GPIO_Pin_10);//GPIOD->BRR = 0x00000f00;
-
-
- mydelay(100);
- /* Set the GPIOA port pin 10 and pin 15 */
- GPIO_SetBits(GPIOD, GPIO_Pin_11);//GPIOD->BSRR = 0x00000f00;//置为PD8-PD11
- mydelay(100);
- /* Clears the GPIOA port pin 10 and pin 15 */
- GPIO_ResetBits(GPIOD, GPIO_Pin_11);//GPIOD->BRR = 0x00000f00;
-
- mydelay(100);
-
-
- }
复制代码
|