GPIO_InitTypeDef GPIO_InitStructure;
ErrorStatus HSEStartUpStatus;
USART_InitTypeDef USART_InitStructure; //定义一个结构体
NVIC_InitTypeDef NVIC_InitStructure;
/* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void);
void NVIC_Configuration(void);
void Uart2_PutChar(unsigned char ch)
{
USART_SendData(USART3, ch);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);//等待数据发送完毕
}
void HardwareInit(void);
void Delay(void) //延时函数
{
unsigned long ik;
for(ik=0;ik<0xffff8;ik++) ;
}
/* Private functions ---------------------------------------------------------*/
/*******************************************************************************
* Function Name : main
* Description : Main program.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
int main(void) //main函数
{
// unsigned short RxData=0;
RCC_Configuration(); //配置系统时钟,设置系统时钟为72M
NVIC_Configuration(); //配置中断
HardwareInit();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); //重映射时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE); //GPIOC 时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3 , ENABLE); //USART3时钟
GPIO_PinRemapConfig(GPIO_PartialRemap_USART3, ENABLE); //将USART3局部重映射到PC10,PC11
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //管脚10
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOC, &GPIO_InitStructure); //TX初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; //管脚11
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入
GPIO_Init(GPIOC, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 9600; //波特率9600
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //字长8位
USART_InitStructure.USART_StopBits = USART_StopBits_1; //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;//打开Rx接收和Tx发送功能
USART_Init(USART3, &USART_InitStructure); //初始化
USART_Cmd(USART3, ENABLE);
while (1)
{
Uart2_PutChar(0xaa);
//RxData = USART_ReceiveData(USART2);
Delay();Delay(); //延时
}
}
//----------------------------------------------------------------------------
//以下为子函数
void HardwareInit()
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD , ENABLE); //打开外设D的时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE); //打开外设B的时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE , ENABLE); //打开外设E的时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOG , ENABLE); //打开外设G的时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF , ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC , ENABLE);
//配置控制LED所在外设PD0--7脚为输出
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7|
GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //I/O口的方向
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //I/O口50MHz输出速度
GPIO_Init(GPIOD, &GPIO_InitStructure); //初始化外设D端口
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //I/O口的方向
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //I/O口50MHz输出速度
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 |
GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //I/O口的方向
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //I/O口50MHz输出速度
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 |
GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //I/O口的方向
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //I/O口50MHz输出速度
GPIO_Init(GPIOG, &GPIO_InitStructure);
//配置输入口
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //I/O口的方向,输入
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //I/O口的最高输出速度
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //I/O口的方向,输入
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //I/O口的最高输出速度
GPIO_Init(GPIOG, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //I/O口的方向,输入
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //I/O口的最高输出速度
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //I/O口的方向,输入
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //I/O口的最高输出速度
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //I/O口的方向,输入
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //I/O口的最高输出速度
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //I/O口的方向,输入
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //I/O口的最高输出速度
GPIO_Init(GPIOF, &GPIO_InitStructure);
GPIO_Write(GPIOD, 0);
GPIO_Write(GPIOB, 0);
GPIO_Write(GPIOE, 0);
GPIO_Write(GPIOG, 0);
}
/*******************************************************************************
* 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)
{
}
}
}
/*******************************************************************************
* Function Name : NVIC_Configuration
* Description : Configures Vector Table base location.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void NVIC_Configuration(void)
{
#ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
}
/*******************************************************************************
* Function Name : Delay
* Description : Inserts a delay time.
* Input : nCount: specifies the delay time length.
* Output : None
* Return : None
*******************************************************************************/
#ifdef DEBUG
/*******************************************************************************
* Function Name : assert_failed
* Description : Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* Input : - file: pointer to the source file name
* - line: assert_param error line source number
* Output : None
* Return : None
*******************************************************************************/
void assert_failed(u8* file, u32 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
", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
此帖出自
小平头技术问答
一周热门 更多>