• 嵌入式实验四


    #include "stm32f10x.h"
    #include <stdio.h>
    #include <string.h>

    void RCC_cfg(void);
    void GPIO_cfg(void);
    void NVIC_cfg(void);
    void TIMER_cfg(void);

    int main(void)
    {
     RCC_cfg();
     GPIO_cfg();
     NVIC_cfg();
     TIMER_cfg();
     TIM_Cmd(TIM2,ENABLE);
     while(1);
    }

    void RCC_cfg(void)
    {
     RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF,ENABLE);
    }
    void GPIO_cfg(void)
    {
     GPIO_InitTypeDef GPIO_InitStructure;
     GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;//
     GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
     GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
     GPIO_Init(GPIOF,&GPIO_InitStructure);
    }

    void NVIC_cfg(void)
    {
     NVIC_InitTypeDef NVIC_InitStructure;
     NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
     NVIC_InitStructure.NVIC_IRQChannel=TIM2_IRQn;
     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
     NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
     NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
     NVIC_Init(&NVIC_InitStructure);
    }

    void TIMER_cfg(void)
    {
     TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
     TIM_DeInit(TIM2);
     TIM_InternalClockConfig(TIM2);
     TIM_TimeBaseStructure.TIM_Prescaler=36000-1;
     TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
     TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
     TIM_TimeBaseStructure.TIM_Period=3000-1;
     TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);
     
     TIM_ClearFlag(TIM2,TIM_FLAG_Update);
     TIM_ARRPreloadConfig(TIM2,DISABLE);
     TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
    }

    void TIM2_IRQHandler(void)
    {
     u8 ReadValue;
     if(TIM_GetITStatus(TIM2,TIM_IT_Update)!=RESET)
     {
      TIM_ClearITPendingBit(TIM2,TIM_FLAG_Update);
      ReadValue=GPIO_ReadOutputDataBit(GPIOF,GPIO_Pin_9);//
      if(ReadValue==0)
      {
       GPIO_SetBits(GPIOF,GPIO_Pin_9);
       //GPIO_ResetBits(GPIOF,GPIO_Pin_7);
      }
      else
      {
       GPIO_ResetBits(GPIOF,GPIO_Pin_9);
       //GPIO_SetBits(GPIOF,GPIO_Pin_7);
      }
     }
    }
     
     

  • 相关阅读:
    Alpha发布——美工+文案展示博客
    021_func
    020_with
    018_IO
    017_set
    016_dict
    015_string
    012_list
    010_ternaryOperation
    008_standard_lib_os
  • 原文地址:https://www.cnblogs.com/rourou123/p/8053444.html
Copyright © 2020-2023  润新知