一、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 = ; //设置波特率
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
NVIC_InitStructure.NVIC_IRQChannelSubPriority = ; //子优先级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()
{
GPIO_WriteBit(GPIOB, GPIO_Pin_8, (BitAction)( - GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_8)));
fPutString("Hello World!", );
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=;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);
  • }

最新文章

  1. CSS3_01之选择器、Hack
  2. PowerDesigner 15设置mysql主键自动增长及基数
  3. AEAI Portal V3.5.4升级说明,门户集成平台
  4. 【.NET实战教程】北风网基于ASP.NET多层架构下的企业级进销存软件全程培训
  5. 转载关于KeyPress和KeyDown事件的区别和联系
  6. 为移动Web应用创建快速响应按钮
  7. JavaMail组件实现邮件功能
  8. 犯罪团伙 codevs 3554
  9. [Oracle]查看和修改连接数
  10. jQuery第九章
  11. 运维route语法
  12. Oracle EBS 查看执行计划
  13. python rabbitmq的库,rabbitpy代替pika
  14. Shell实现判断进程是否存在并重新启动脚本
  15. hdu5229
  16. ruby 生成随机字符串
  17. 最优布线问题(wire.cpp)
  18. Hadoop中HDFS工作原理
  19. linux系统如何安装vmware Tools(下面以CentOS为例)
  20. 基于C#的控制台的进度提示实现

热门文章

  1. Codeforces 803F - Coprime Subsequences(数论)
  2. ID3,C4.5和CART三种决策树的区别
  3. 【HDOJ6611】K Subsequence(费用流)
  4. net core 发布docker镜像的官方写法
  5. DB-概念-数据库:数据库/Database
  6. 杂项-报表-Minitab:Minitab百科
  7. Oracle -操作数据库
  8. Windows下使用Composer安装yii2
  9. JumpServer堡垒机安装笔记
  10. 在线常用库 + API手册