一、RCC设置
没什么好写的之前USART的基本一样
/**************************************************************************** * Function Name : RCC_Configuration * Description : Sets System clock frequency to 72MHz and configure HCLK, PCLK2 * and PCLK1 prescalers. * Input : None * Output : None * Return : None ****************************************************************************/ void RCC_Configuration(void) { /* Deinitialize the RCC registers */ RCC_DeInit(); /* Enable the HSE */ RCC_HSEConfig(RCC_HSE_ON); /* Wait till HSE is ready and if Time out is reached exit */ HSEStartUpStatus = RCC_WaitForHSEStartUp(); if(HSEStartUpStatus == SUCCESS) { /* Add here PLL ans system clock config */ /* Enable The Prefetch Buffer */ FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); /* Configure Tthe Latency cycle: Set 0 Latency cycles */ FLASH_SetLatency(FLASH_Latency_2); /* Configure HCLK such as HCLK = SYSCLK */ RCC_HCLKConfig(RCC_SYSCLK_Div1); /* PCLK2 = HCLK */ RCC_PCLK2Config(RCC_HCLK_Div1); /* PCLK1 = HCLK/2 */ RCC_PCLK1Config(RCC_HCLK_Div2); /* ADCCLK = PCLK2/4 */ RCC_ADCCLKConfig(RCC_PCLK2_Div4); /* PLLCLK = 8MHz * 9 = 72MHz */ 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 HSE is used as system clock source */ while(RCC_GetSYSCLKSource() != 0x08) { } } }
二、GPIO设置
设置AP9,AP10为串口。
/**************************************************************************** * Function Name : GPIO_Configuration * Description : * Input : None * Output : None * Return : None ****************************************************************************/ void GPIO_Configuration(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO | RCC_APB2Periph_USART1, ENABLE); 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); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); }
三、USART设置
/******************************************************************************* * Function Name : USART_Configure * Description : Configures the USART * Input : None * Output : None * Return : None *******************************************************************************/ void USART_Configuration(void) { USART_InitStructure.USART_BaudRate = 115200; //设置波特率 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(USART1, &USART_InitStructure); //初始化结构体 USART_Cmd(USART1, ENABLE); //开启串口 }
四、DMA设置
/******************************************************************************* * Function Name : DMA_Configure * Description : Configures the DMA * Input : None * Output : None * Return : None *******************************************************************************/ void DMA_Configuration(void) { DMA_InitTypeDef DMA_InitStructure; //定义DMA结构体 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); //开启DMA的RCC DMA_InitStructure.DMA_PeripheralBaseAddr = APB2PERIPH_BASE + 0X3804; //设置DMA外设基地址:串口1的地址+串口1的数据寄存器偏移量 DMA_InitStructure.DMA_MemoryBaseAddr = (u32)SendBuff; //要发送的内容地址 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; //设置外设作为数据传输的目的地 DMA_InitStructure.DMA_BufferSize = SENDBUFF_SIZE; //指定DMA缓存大小 DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //设置外设地址寄存器不递增 DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //内存地址寄存器递增 DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; //数据宽度为8位 DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; //数据宽度为8位 DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; //工作在正常缓存模式 DMA_InitStructure.DMA_Priority = DMA_Priority_Medium; //设置DMA为中优先级 DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //不设置为内存到内存传输 DMA_Init(DMA1_Channel4, &DMA_InitStructure); //初始化DMA结构体 DMA_Cmd(DMA1_Channel4, ENABLE); //开启DMA DMA_ITConfig(DMA1_Channel4, DMA_IT_TC, ENABLE); //使能DMA1_Channel4作为中断通道,传输完成后中断屏蔽,使能 }
五、设置中断
/**************************************************************************** * Function Name : NVIC_Configuration * Description : Configures Vector Table base location. * Input : None * Output : None * Return : None ****************************************************************************/ void NVIC_Configuration(void) { NVIC_InitTypeDef NVIC_InitStructure; #ifdef VET_TAB_RAM /* Set the Vector Table base location at 0x2000 0000 */ NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0); #else /* Set the Vector Table base location at 0x8000 0000 */ NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0); #endif NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); //设置为组1 NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel4_IRQn; //设置DMA1_Channel4为中断源 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //优先级1 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //子优先级1 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能 NVIC_Init(&NVIC_InitStructure); //初始化 }
在stm32f10x_it.c中加入:
void DMA1_Channel4_IRQHandler(void) { if(DMA_GetFlagStatus(DMA1_FLAG_TC4) == SET) //如果中断源是设置了 { DMA_ClearFlag(DMA1_FLAG_TC4); //清除中断 } }
六、main函数
/**************************************************************************** * Function Name : main * Description : Main program. * Input : None * Output : None * Return : None ****************************************************************************/ int main(void) { RCC_Configuration(); NVIC_Configuration(); GPIO_Configuration(); USART_Configuration(); DMA_Configuration(); while(1) { GPIO_WriteBit(GPIOB, GPIO_Pin_8, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_8))); fPutString("Hello World!", 13); Delay(0xffffff); } } /**************************************************************************** * Function Name : fPutString * Description : Send a string. * Input : None * Output : None * Return : None ****************************************************************************/ void fPutString(u8 *buf, u8 len) { u8 i; for(i=0;i<len;i++) { fPutChar(*buf++); } } /**************************************************************************** * Function Name : fPutChar * Description : Send a byte * Input : None * Output : None * Return : None ****************************************************************************/ u8 fPutChar(u8 ch) { USART_SendData(USART1, (u8) ch); while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET) { } return ch; }
- /*******************************************************************************
- * Function Name : USART_Configure
- * Description : Configures the USART
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- void USART_Configuration(void)
- {
- USART_InitStructure.USART_BaudRate = 115200;
- 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_Cmd(USART1, ENABLE);
- }