音乐喷泉:
#include "stm32f10x_lib.h"
//#include "stdio.h"
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void TIM_Configuration(void);
u8 TIM2_Flag=0;
main(void)
{
#ifdef DEBUG
debug();
#endif
RCC_Configuration();
GPIO_Configuration();
TIM_Configuration();
NVIC_Configuration();
while(1)
{
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_10)==0)
{
TIM_Cmd(TIM2, ENABLE);
if(TIM2_Flag==1)
GPIOD->ODR=0x01<<8;
if(TIM2_Flag==2)
GPIOD->ODR=0x01<<9;//GPIOD->ODR<<1;
if(TIM2_Flag==3)
GPIOD->ODR=0x01<<10;//GPIOD->ODR<<1;
if(TIM2_Flag==4)
GPIOD->ODR=0x01<<11;//GPIOD->ODR<<1;
if(TIM2_Flag==5)
GPIOD->ODR=0x01<<12;//GPIOD->ODR<<1;
if(TIM2_Flag==6)
GPIOD->ODR=0x01<<13;//GPIOD->ODR<<1;
if(TIM2_Flag==7)
GPIOD->ODR=0x01<<14;//GPIOD->ODR<<1;
if(TIM2_Flag==8)
GPIOD->ODR=0x01<<15;//GPIOD->ODR<<1;
if(TIM2_Flag==9)
GPIOD->ODR=0x03<<8;
if(TIM2_Flag==10)
GPIOD->ODR=0x03<<10;//GPIOD->ODR<<2;
if(TIM2_Flag==11)
GPIOD->ODR=0x03<<12;//GPIOD->ODR<<2;
if(TIM2_Flag==12)
GPIOD->ODR=0x03<<14;//GPIOD->ODR<<2;音乐喷泉
if(TIM2_Flag==13)
GPIOD->ODR=0x07<<8;
if(TIM2_Flag==14)
GPIOD->ODR=0x07<<11;
if(TIM2_Flag==15)
GPIOD->ODR=0x03<<14;
if(TIM2_Flag==16)
GPIOD->ODR=0x0F<<8;
if(TIM2_Flag==17)
{
GPIOD->ODR=0x0F<<12;
TIM2_Flag=0;
}
}
else
{
TIM_Cmd(TIM2, DISABLE);
GPIOD->ODR=0x00;
TIM2_Flag=0;
}
}
}
void RCC_Configuration(void)
{
ErrorStatus HSEStartUpStatus;
u8 SYSCLKSourceValue;
RCC_DeInit();
RCC_HSEConfig(RCC_HSE_ON);
HSEStartUpStatus=RCC_WaitForHSEStartUp();
if(HSEStartUpStatus==SUCCESS)
{
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
FLASH_SetLatency(FLASH_Latency_2);
RCC_HCLKConfig(RCC_SYSCLK_Div1);
RCC_PCLK1Config(RCC_HCLK_Div2);
RCC_PCLK2Config(RCC_HCLK_Div2); RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);
RCC_PLLCmd(ENABLE); while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
SYSCLKSourceValue=RCC_GetSYSCLKSource();
while(SYSCLKSourceValue != 0x08) {
}
}
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|
RCC_APB2Periph_GPIOB, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
}
void NVIC_Configuration(void)
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel= TIM2_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11|
GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
}
void TIM_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_TimeBaseInitStructure.TIM_Prescaler=17999;
TIM_TimeBaseInitStructure.TIM_Period=2000; TIM_TimeBaseInitStructure.TIM_ClockDivision = 0x0;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Down;
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);
TIM_ITConfig(TIM2,TIM_IT_Update, ENABLE );
}
发布评论