Chinaunix首页 | 论坛 | 博客
  • 博客访问: 312881
  • 博文数量: 169
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 12
  • 用 户 组: 普通用户
  • 注册时间: 2013-07-10 12:00
文章分类

全部博文(169)

文章存档

2014年(38)

2013年(131)

我的朋友

分类: C/C++

2014-02-24 22:05:42



/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx.h"


/** @addtogroup STM32F2xx_StdPeriph_Examples
  * @{
  */


/** @addtogroup IOToggle
  * @{
  */ 


void GPIO_Config(void);
void TIM_Config(void);
void NVIC_Config(void);








int main(void)
{
  GPIO_Config();
TIM_Config();
NVIC_Config();
while(1);
}


void GPIO_Config(void)
{


GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG | RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB, ENABLE);


GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;    
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;   
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;   
GPIO_Init(GPIOG, &GPIO_InitStructure);     


/*??GPIOA_Pin_8,??TIM1_Channel2 PWM??*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_10 | GPIO_Pin_11; 
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;   
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;         
GPIO_Init(GPIOA, &GPIO_InitStructure);


GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;     //GPIOB_Pin13??TIM1_CH1N??
GPIO_Init(GPIOB, &GPIO_InitStructure);


GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_TIM1); //??GPIOA_Pin7?TIM1_Ch1N  ?a??1ü?ì??óD0.5v
GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_TIM1); //??GPIOA_Pin8?TIM1_Ch1, 
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_TIM1);//??GPIOA_Pin10?TIM1_Ch3, 
GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_TIM1);//??GPIOA_Pin11?TIM1_Ch4,????CH4N 
GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_TIM1);//??GPIOB_Pin13?TIM1_Ch1N,
}   




void TIM_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
TIM_DeInit(TIM1);                 //??tim1??DD??è???3?ê???
/*·???oí?ü?ú????1?ê?
  Prescaler = (TIMxCLK / TIMx counter clock) - 1;
  Period = (TIMx counter clock / TIM3 output clock) - 1 
  TIMx counter clock?a???ùDèòa??txm???¨ê±?÷ê±?ó
  */


TIM_TimeBaseStructure.TIM_Period = 10000-1; //TIM1??ê±?ó1??¨????êy??
TIM_TimeBaseStructure.TIM_Prescaler = 16800-1;  //TIM1ê±?ó???ê3yêy???¤·?????    ?a?ù??·???oí?????éò?2úéú1s???¨ê±
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;//2éó?ê±?ó·???·?ê?1 
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//?òé???êy
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0x0; //???¨???'????êy?÷??
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);




TIM_OCStructInit(&TIM_OCInitStructure); 
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;//PWM1?a?y3?????±è??ê?
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//ê?3??êDí
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; //??21ê?3??êDí
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //????????±è ????D???'?ê±????±è?a20%
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;//??21ê?3???ó?ò?é??à·'
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;


TIM_OCInitStructure.TIM_Pulse = 5000;  //ê?è?í¨?à1????±èêy?Y 
TIM_OC1Init(TIM1, &TIM_OCInitStructure); 
TIM_OCInitStructure.TIM_Pulse = 4000;  //ê?è?í¨?à3????±èêy?Y 
TIM_OC3Init(TIM1, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse = 7000;  //ê?è?í¨?à4????±èêy?Y 
TIM_OC4Init(TIM1, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM1, ENABLE); 


TIM_ClearFlag(TIM1, TIM_FLAG_Update);
TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM1, ENABLE); 
TIM_CtrlPWMOutputs(TIM1, ENABLE);
}


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








#ifdef  USE_FULL_ASSERT


/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)

  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */


  /* Infinite loop */
  while (1)
  {


  }
}
#endif


/**
  * @}
  */ 


/**
  * @}
  */ 


/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/

阅读(761) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~