软件:
IAR 4.42限制版
ST库 2.01
硬件:
万利199开发板 STM3210B-LK1
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_lib.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
ErrorStatus HSEStartUpStatus;
/* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void);
void NVIC_Configuration(void);
/* Private functions ---------------------------------------------------------*/
#define PWM_Period 120
int main(void)
{
u16 waveNumber = 10;
/* System Clocks Configuration */
RCC_Configuration();
/* Enable related peripheral clocks */
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB,ENABLE);
RCC_APB2PeriphClockCmd( RCC_APB2Periph_TIM1,ENABLE);
RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM3,ENABLE);
/* Config IO for related timers */
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Timer1 Channel 2, PA9 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Timer3 Channel 4, PB1*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
/* Setup Timer3 channel 4, Timer3 is master timer
This timer is used to control the waveform count of timer1 */
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_DeInit(TIM3);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_OCStructInit(&TIM_OCInitStructure);
TIM_TimeBaseStructure.TIM_Prescaler = PWM_Period/2 - 1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = waveNumber*2;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
/* Timer2 Channel 3 Configuration in PWM2 mode, this is used for enable Recive clcok */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = waveNumber*2 - 1;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC4Init(TIM3,&TIM_OCInitStructure);
TIM_CtrlPWMOutputs(TIM3, ENABLE);
TIM_SelectOnePulseMode(TIM3, TIM_OPMode_Single);
}
/* Setup timer1 channel 2, Timer1 is slave timer
This timer is used to output waveforms */
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_DeInit(TIM1);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_OCStructInit(&TIM_OCInitStructure);
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = PWM_Period;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure);
/* Timer2 Channel 3 Configuration in PWM2 mode, this is used for enable Recive clcok */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = PWM_Period/2;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC2Init(TIM1,&TIM_OCInitStructure);
TIM_CtrlPWMOutputs(TIM1, ENABLE);
}
/* Create relationship between timer1 and timer3, timer3 is master, timer1 is slave
timer1 is work under gate control mode, and controled by timer3
timer3's channel 4 is used as the control signal
*/
/* Enable timer's master/slave work mode */
TIM_SelectMasterSlaveMode(TIM3,TIM_MasterSlaveMode_Enable);
TIM_SelectMasterSlaveMode(TIM1,TIM_MasterSlaveMode_Enable);
/* timer3's channel 4 is used as the control signal */
TIM_SelectOutputTrigger(TIM3,TIM_TRGOSource_OC4Ref );
/* Check the master/slave is valid or not */
compile_assert((u16)GetInternalTrigger(TIM1,TIM3) != (u16)-1);
/* Config timer1's external clock */
TIM_ITRxExternalClockConfig(TIM1, GetInternalTrigger(TIM1,TIM3));
TIM_SelectSlaveMode(TIM1,TIM_SlaveMode_Gated);
/* Enable the slave tiemr*/
TIM_Cmd(TIM1,ENABLE);
//SetupAlltimers();
while(1){
/* Check whether the previous action is done or not */
if(!(TIM3->CR1 & 1)){
TIM1->CNT = 0; /* It would be very perfect if gate mode can
reset the slave timer automatically */
TIM3->ARR = waveNumber*2; /* Reload wave number*/
TIM3->CCR4 = waveNumber*2 - 1;
TIM3->CR1|=1; /* Re-enable the timer */
/* update waveform number */
waveNumber++;
if(waveNumber == 13){
waveNumber = 10;
}
}
}
}
/*******************************************************************************
* Function Name : RCC_Configuration
* Description : Configures the different system clocks.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void RCC_Configuration(void)
{
/* RCC system reset(for debug purpose) */
RCC_DeInit();
/* Enable HSE */
RCC_HSEConfig(RCC_HSE_ON);
/* Wait till HSE is ready */
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS)
{
/* Enable Prefetch Buffer */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* Flash 2 wait state */
FLASH_SetLatency(FLASH_Latency_2);
/* HCLK = SYSCLK */
RCC_HCLKConfig(RCC_SYSCLK_Div1);
/* PCLK2 = HCLK */
RCC_PCLK2Config(RCC_HCLK_Div1);
/* PCLK1 = HCLK/2 */
RCC_PCLK1Config(RCC_HCLK_Div2);
/* PLLCLK = 8MHz * 9 = 72 MHz */
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
/* Enable PLL */
RCC_PLLCmd(ENABLE);
/* Wait till PLL is ready */
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}
/* Select PLL as system clock source */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* Wait till PLL is used as system clock source */
while(RCC_GetSYSCLKSource() != 0x08)
{
}
}
}
输出波形如下图 |