#include "stm32f30x.h"
//#incluce "stm32f30x_conf.h"
#include "stm32f30x_rcc.h"
#include "stm32f30x_gpio.h"
#include "stm32f30x_misc.h"
#include "stm32f30x_usart.h"
#include "string.h"
#include "stdio.h"
#include "stdlib.h"
/** @addtogroup IO_Toggle
* @{
*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
static __IO uint32_t TimingDelay;
uint8_t __IO BlinkSpeed = 0;
u16 led0pwmval=20;
/* Private function prototypes -----------------------------------------------*/
//RCC_ClocksTypeDef RCC_Clocks;
/* Private functions ---------------------------------------------------------*/
void delay_ms(__IO uint32_t nTime);
int fputc(int ch,FILE *f);
void RCC_Configuration(void);
void GPIO_Configuration(void);
void USART_Configuration(void);
void NVIC_Configuration(void);
void TIM3_Configuration(void);
void TIM2_Configuration(void);
void TIM4_Configuration(void);
void TIM1_Int_Init(u16 arr,u16 psc);
void TIM1_PWM_Configuration(u16 arr,u16 psc);
void pwm_set_pwm_hi(u16 phase);
void pwm_set_pwm_lo(u16 phase);
/**
* @brief Main program
* @param None
* @retval None
*/
int main(void)
{
u8 dir=1;
/* SysTick end of count event each 1ms */
// RCC_GetClocksFreq(&RCC_Clocks);
// SysTick_Config(RCC_Clocks.HCLK_Frequency / 1000);
RCC_Configuration();
GPIO_Configuration();
USART_Configuration();
TIM1_PWM_Configuration(1000,0);
pwm_set_pwm_lo(PWM_PHASE_A);
NVIC_Configuration();
// TIM2_Configuration();
// TIM4_Configuration();
// TIM3_Configuration();
/* Initialize LED2 *///PB13
// STM_EVAL_LEDInit(LED2);
/* Initialize User_Button on STM32NUCLEO *///PC13
// STM_EVAL_PBInit(BUTTON_USER, BUTTON_MODE_EXTI);
/* Initiate Blink Speed variable */
BlinkSpeed = 0;
printf("System start.......\r\n");
/* Infinite loop */
while (1)
{
delay_ms(10000); // 1000000
if(dir)led0pwmval++;
else led0pwmval--;
if(led0pwmval>800)dir=0;
if(led0pwmval==0)dir=1;
TIM_SetCompare1(TIM1,led0pwmval);
printf("STM32-CortexM4 study.......\r\n");
// Test on blink speed
/* if(BlinkSpeed == 0)
{
//LED2 Toggle each 500ms
STM_EVAL_LEDToggle(LED2);
Delay(500);
}
else if(BlinkSpeed == 1)
{
STM_EVAL_LEDToggle(LED2);
//LED2 Toggle each 1000ms
Delay(50);
}*/
}
}
/**
* @brief Inserts a delay time.
* @param nTime: specifies the delay time length, in 1 ms.
* @retval None
*/
void Delay(__IO uint32_t nTime)
{
TimingDelay = nTime;
while(TimingDelay != 0);
}
/**
* @brief Decrements the TimingDelay variable.
* @param None
* @retval None
*/
void TimingDelay_Decrement(void)
{
if (TimingDelay != 0x00)
{
TimingDelay--;
}
}
void delay_ms(__IO uint32_t nTime)
{
while(nTime--);
}
#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
/**
* @}
*/
void RCC_Configuration(void)
{
SystemInit();
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB,ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
// RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
// RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
// RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
//
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4;//TX
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;
// GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
// GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//UP
GPIO_Init(GPIOC,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;//RX
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;
// GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
// GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;//UP
GPIO_Init(GPIOC,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_13;//PB13
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF; //GPIO_Mode_OUT
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_2);//¸´Óö˿Ú:6?2
}
//TIM1_Init
void TIM1_Int_Init(u16 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_Period = arr;
TIM_TimeBaseStructure.TIM_Prescaler =psc; //
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //
TIM_ITConfig(TIM1,TIM_IT_Update,ENABLE );
TIM_Cmd(TIM1, ENABLE);
}
//PWM
void TIM1_PWM_Configuration(u16 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_BDTRInitTypeDef TIM1_BDTRInitStructure;
TIM_DeInit(TIM1);
TIM_TimeBaseStructure.TIM_Period = arr; //ÉèÖÃÏÂÒ»¸ö¸üÐÂʼþ×Ô¶¯ÖØÔص½¼Ä´æÆ÷µÄÖµ
TIM_TimeBaseStructure.TIM_Prescaler =psc; //Ô¤·ÖƵֵ
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //ʱ¼ä·Ö¸î:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //¼ÆÊýģʽ
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
// TIM_ITConfig(TIM1,TIM_IT_Update,ENABLE );
//TIM1 Channel PWMģʽ
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //ģʽ
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Disable; //TIM1_OutputStateÑ¡ÔñÊä³ö±È½Ï״̬
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;////TIM1_OutputNStateÑ¡Ôñ»¥²¹Êä³ö±È½Ï״̬
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;//»¥²¹¼«ÐÔΪ¸ß
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //Êä³ö¼«ÐÔΪ¸ß
TIM_OCInitStructure.TIM_Pulse=led0pwmval;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse=led0pwmval+20;
TIM_OC2Init(TIM1, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse=led0pwmval+40;
TIM_OC3Init(TIM1, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse=led0pwmval+60;
TIM_OC4Init(TIM1, &TIM_OCInitStructure);
/*
TIM1_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable; //ʹÄÜTIM1 OSSR״̬
TIM1_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable; //ʹÄÜTIM1 OSSI״̬
TIM1_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF; //²»ËøÈκÎλ
TIM1_BDTRInitStructure.TIM_DeadTime = 1; //Êä³ö´ò¿ªºÍ¹Ø±Õ״̬֮¼äµÄÑÓʱ£¬ËÀÇøʱ¼ä
TIM1_BDTRInitStructure.TIM_Break = TIM_Break_Disable; //ʧÄÜTIM1ɲ³µÊäÈë
TIM1_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High; //TIM1ɲ³µÊäÈë¹Ü½Å¼«ÐÔ¸ß
TIM1_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable; //×Ô¶¯Êä³ö¹¦ÄÜʹÄÜ
TIM_BDTRConfig(TIM1, &TIM1_BDTRInitStructure);
*/
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable); //ʹÄÜTIM1ÔÚCCR2 ÉϵÄÔ¤ÖØÔؼĴæÆ÷
TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable); //ʹÄÜTIM1ÔÚCCR2 ÉϵÄÔ¤ÖØÔؼĴæÆ÷
TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable); //ʹÄÜTIM1ÔÚCCR2 ÉϵÄÔ¤ÖØÔؼĴæÆ÷
TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable); //ʹÄÜTIM1ÔÚCCR2 ÉϵÄÔ¤ÖØÔؼĴæÆ÷
TIM_CtrlPWMOutputs(TIM1,ENABLE);//ʹÄÜÍâÉèTIM1µÄÖ÷Êä³ö
TIM_ARRPreloadConfig(TIM1,ENABLE);
TIM_Cmd(TIM1, ENABLE);
}
/////////////test////////////
void pwm_set_pwm_hi(u16 phase)//TIM_Channel
{
TIM_SelectOCxM(TIM1,phase,TIM_OCMode_PWM2);
TIM_CCxCmd(TIM1,phase,TIM_CCx_Enable);
TIM_CCxNCmd(TIM1,phase,TIM_CCxN_Disable);
}
/////////////test////////////
void pwm_set_pwm_lo(u16 phase)//TIM_Channel
{
TIM_SelectOCxM(TIM1,phase,TIM_OCMode_PWM2);
TIM_CCxCmd(TIM1,phase,TIM_CCx_Disable);
TIM_CCxNCmd(TIM1,phase,TIM_CCxN_Enable);
}
void TIM2_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStruct;
TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
TIM_TimeBaseStruct.TIM_Period=100;//
TIM_TimeBaseStruct.TIM_Prescaler=35999;//
TIM_TimeBaseStruct.TIM_ClockDivision=0;
TIM_TimeBaseStruct.TIM_CounterMode=TIM_CounterMode_Down;//
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStruct);
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
TIM_Cmd(TIM2,ENABLE);
}
void TIM3_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStruct;
TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
TIM_TimeBaseStruct.TIM_Period=2000;//
TIM_TimeBaseStruct.TIM_Prescaler=35999;//
TIM_TimeBaseStruct.TIM_ClockDivision=0;
TIM_TimeBaseStruct.TIM_CounterMode=TIM_CounterMode_Down;//
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStruct);
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
TIM_Cmd(TIM3,ENABLE);
}
void TIM4_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStruct;
TIM_ClearITPendingBit(TIM4,TIM_IT_Update);
TIM_TimeBaseStruct.TIM_Period=200;//
TIM_TimeBaseStruct.TIM_Prescaler=35999;//
TIM_TimeBaseStruct.TIM_ClockDivision=0;
TIM_TimeBaseStruct.TIM_CounterMode=TIM_CounterMode_Down;//
TIM_TimeBaseInit(TIM4,&TIM_TimeBaseStruct);
TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE);
TIM_Cmd(TIM4,ENABLE);
}
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDef USART_ClockInitStruct;
USART_StructInit(&USART_InitStructure);
GPIO_PinAFConfig(GPIOC,GPIO_PinSource4,GPIO_AF_7);//¶Ë¿Ú¸´ÓÃ
GPIO_PinAFConfig(GPIOC,GPIO_PinSource5,GPIO_AF_7);//¶Ë¿Ú¸´ÓÃ
USART_InitStructure.USART_BaudRate=9600;
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_ClockStructInit(&USART_ClockInitStruct);//´®¿ÚʱÖÓ³õʼ»¯
USART_ClockInit(USART1,&USART_ClockInitStruct);//´®¿ÚʱÖÓ³õʼ»¯
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
USART_Cmd(USART1,ENABLE);
USART_ClearFlag(USART1,USART_FLAG_TC);
}
void NVIC_Configuration(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 = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
#if 1
int fputc(int ch,FILE *f)
{
USART_SendData(USART1,(u8)ch);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
return ch;
}
#endif
#if 0
#pragma import(__use_no_semihosting)
struct __FILE
{
int handle;
};
FILE __stdout;
//_sys_exit()
_sys_exit(int x)
{
x = x;
}
//fputc
int fputc(int ch, FILE *f)
{
while((USART1->SR&0X40)==0);
USART1->DR = (u8) ch;
return ch;
}
#endif
|