1--以STM32F103ZET6芯片为核心,在系统时钟为36M的情况下,使用通用定时器TIM3实现红、绿、蓝三色灯光依次点亮1s。并通过串口调试助手实时输出LED灯的状态。

4.以STM32F103ZET6芯片为核心,在系统时钟为36M的情况下,使用通用定时器TIM3实现红、绿、蓝三色灯光依次点亮1s。并通过串口调试助手实时输出LED灯的状态。

#include "stm32f10x.h"                  // Device header
#include "stdio.h"


GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
USART_InitTypeDef USART_InitStructure;

uint16_t flag;  //直接定义个标志位
uint16_t avoid_untilflag;   //避免持续发送

void Rcc_alter()   //时钟修改36MHZ
{
    ErrorStatus HSEStartUpStatus;         //固件库中文手册196页 复制过来就能用
    /* Deinitialize the RCC registers */ 
    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) 
    { 
        /* Add here PLL ans system clock config */ 
        
        /* Set PLL clock output to 72MHz using HSE (8MHz) as entry clock */ 
        RCC_PLLConfig(RCC_PLLSource_HSE_Div2, RCC_PLLMul_9);
        /* Enable the PLL */ 
        RCC_PLLCmd(ENABLE);
        RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
    } 
    else 
    { 
        /* Add here some code to deal with this error */ 
    }
}

void Rcc_Init()
{
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE); //初始化GPIO时钟和串口引脚
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);  //初始化定时器时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);  //初始化串口时钟
    
}

void LED_init()            //初始化灯的GPIO  直接打开例程中GPIO选择main.c  复制过来都可以用了
{
    
    
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_0 | GPIO_Pin_1 ;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
    
    GPIO_SetBits(GPIOB,GPIO_Pin_All);  //先关掉所有灯
    
}
 
void TIM4_init()   //初始化定时器4   直接找例程中TIMEBASE  复制粘贴过来

    NVIC_InitTypeDef NVIC_InitStructure;  //NVIC结构体初始化   直接找例程中TIMEBASE  复制粘贴过来
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
    /* Time base configuration */
  TIM_TimeBaseStructure.TIM_Period = 2*(10000-1);           //直接计数20000次  自己可以算等于2s 不会自己慢慢琢磨
  TIM_TimeBaseStructure.TIM_Prescaler = 3600-1;                    //36MHZ直接分频一次100us
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;  //时钟不分频
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
    
    TIM_ITConfig(TIM4,  TIM_IT_Update,  ENABLE);    //配置中断模式
    TIM_Cmd(TIM4,ENABLE);
    
    /* Enable the TIM4 global Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
    
}


void USART_init()    //直接找例程中USART  复制粘贴过来  最需要改引脚即可    这个等于串口PA9,PA10固定不会变
{
    
    //     GPIO_InitTypeDef GPIO_InitStructure;   //可有可无  最上边已经定义全局GPIO
    
  /* Configure USARTy TX  */
  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_9;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /* Configure USARTy RX as input floating */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
    
  USART_InitStructure.USART_BaudRate = 115200;   
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  USART_InitStructure.USART_StopBits = USART_StopBits_1;
  USART_InitStructure.USART_Parity = USART_Parity_No ;
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  USART_Init(USART1, &USART_InitStructure);
    
    USART_Cmd(USART1, ENABLE);

}


int fputc(int ch/*c*/, FILE * f/*stream*/)     //直接从USART例程中的printf中找出来拉过来
{
    USART_ClearFlag(USART1,USART_FLAG_TC);
    USART_SendData(USART1, (uint8_t)ch);

  /* Loop until the end of transmission */
  while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
  {}

  return ch;
}

int main()
{
    Rcc_alter(); 
    Rcc_Init();
    LED_init();
    TIM4_init(); 
    USART_init();
    
    while(1)
    {
                if(avoid_untilflag==0&&flag==1)   //只让执行一次 防止串口卡死
                {
                    avoid_untilflag=1;   
                    GPIO_SetBits(GPIOB,GPIO_Pin_All);   //全灭
                    GPIO_ResetBits(GPIOB,GPIO_Pin_5);  //红灯亮
                    printf("红灯亮\r\n");
                }
                if(avoid_untilflag==1&&flag==2)   //只让执行一次 防止串口卡死
                {
                    avoid_untilflag=2; 
                    GPIO_SetBits(GPIOB,GPIO_Pin_All);        //全灭
                    GPIO_ResetBits(GPIOB,GPIO_Pin_0);  //绿灯亮
                    printf("绿灯亮\r\n");
                }
                if(avoid_untilflag==2&&flag==0)   //只让执行一次 防止串口卡死
                {
                    avoid_untilflag=0; 
                    GPIO_SetBits(GPIOB,GPIO_Pin_All);        //全灭
                    GPIO_ResetBits(GPIOB,GPIO_Pin_1);  //蓝灯亮
                    printf("蓝灯亮\r\n");
                }
    }
}

void TIM4_IRQHandler()  //中断处理函数   例程中也有
{
    if(TIM_GetITStatus(TIM4,TIM_IT_Update)!=RESET)
    {
        if(++flag==3)
            flag=0;
        
        TIM_ClearITPendingBit(TIM4,TIM_IT_Update);
        
    }
}

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值