Chinaunix首页 | 论坛 | 博客
  • 博客访问: 473831
  • 博文数量: 223
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2145
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-01 10:23
个人简介

该坚持的时候坚持,该妥协的时候妥协,该放弃的时候放弃

文章分类

全部博文(223)

文章存档

2017年(56)

2016年(118)

2015年(3)

2014年(46)

我的朋友

分类: 嵌入式

2017-02-26 22:20:40

一、RCC设置

没什么好写的之前USART的基本一样
  1. /****************************************************************************
  2. * Function Name : RCC_Configuration
  3. * Description : Sets System clock frequency to 72MHz and configure HCLK, PCLK2
  4. * and PCLK1 prescalers.
  5. * Input : None
  6. * Output : None
  7. * Return : None
  8. ****************************************************************************/
  9. void RCC_Configuration(void)
  10. {
  11.     /* Deinitialize the RCC registers */
  12.     RCC_DeInit();
  13.     
  14.     /* Enable the HSE */
  15.     RCC_HSEConfig(RCC_HSE_ON);
  16.     
  17.     /* Wait till HSE is ready and if Time out is reached exit */
  18.     HSEStartUpStatus = RCC_WaitForHSEStartUp();
  19.     if(HSEStartUpStatus == SUCCESS)
  20.     {
  21.         /* Add here PLL ans system clock config */
  22.         
  23.         /* Enable The Prefetch Buffer */
  24.         FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
  25.         
  26.         /* Configure Tthe Latency cycle: Set 0 Latency cycles */
  27.         FLASH_SetLatency(FLASH_Latency_2);
  28.         
  29.         /* Configure HCLK such as HCLK = SYSCLK */
  30.         RCC_HCLKConfig(RCC_SYSCLK_Div1);
  31.         
  32.         /* PCLK2 = HCLK */
  33.         RCC_PCLK2Config(RCC_HCLK_Div1);
  34.         
  35.         /* PCLK1 = HCLK/2 */
  36.         RCC_PCLK1Config(RCC_HCLK_Div2);
  37.         
  38.         /* ADCCLK = PCLK2/4 */
  39.         RCC_ADCCLKConfig(RCC_PCLK2_Div4);
  40.         
  41.         /* PLLCLK = 8MHz * 9 = 72MHz */
  42.         RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
  43.         
  44.         /* Enable PLL */
  45.         RCC_PLLCmd(ENABLE);
  46.         
  47.         /* Wait till PLL is ready */
  48.         while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
  49.         
  50.         /* Select PLL as system clock source */
  51.         RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); /* Wait till HSE is used as system clock source */
  52.         while(RCC_GetSYSCLKSource() != 0x08)
  53.         {
  54.             
  55.         }
  56.     }
  57. }


二、GPIO设置

  1. /****************************************************************************
  2. * Function Name : GPIO_Configuration
  3. * Description :
  4. * Input : None
  5. * Output : None
  6. * Return : None
  7. ****************************************************************************/
  8. void GPIO_Configuration(void)
  9. {
  10.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO | RCC_APB2Periph_USART1, ENABLE);

  11.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  12.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  13.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  14.     GPIO_Init(GPIOA, &GPIO_InitStructure);
  15.     
  16.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  17.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  18.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  19.     GPIO_Init(GPIOA, &GPIO_InitStructure);
  20.     
  21.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
  22.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  23.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  24.     GPIO_Init(GPIOB, &GPIO_InitStructure);
  25.     
  26. }
设置AP9,AP10为串口。

三、USART设置

  1. /*******************************************************************************
  2. * Function Name : USART_Configure
  3. * Description : Configures the USART
  4. * Input : None
  5. * Output : None
  6. * Return : None
  7. *******************************************************************************/
  8. void USART_Configuration(void)
  9. {
  10.     USART_InitStructure.USART_BaudRate = 115200;                            //设置波特率
  11.     USART_InitStructure.USART_WordLength = USART_WordLength_8b;             //设置为8位
  12.     USART_InitStructure.USART_StopBits = USART_StopBits_1;                  //停止为1位
  13.     USART_InitStructure.USART_Parity = USART_Parity_No;                     //没有校验位
  14.     USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;          //没有流控
  15.     USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;         //设置RX和TX模式
  16.     USART_Init(USART1, &USART_InitStructure);                               //初始化结构体
  17.     USART_Cmd(USART1, ENABLE);                                              //开启串口
  18. }

四、DMA设置

  1. /*******************************************************************************
  2. * Function Name : DMA_Configure
  3. * Description : Configures the DMA
  4. * Input : None
  5. * Output : None
  6. * Return : None
  7. *******************************************************************************/
  8. void DMA_Configuration(void)
  9. {
  10.     DMA_InitTypeDef DMA_InitStructure;                                         //定义DMA结构体
  11.      
  12.     RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);                         //开启DMA的RCC
  13.     
  14.     DMA_InitStructure.DMA_PeripheralBaseAddr = APB2PERIPH_BASE + 0X3804;       //设置DMA外设基地址:串口1的地址+串口1的数据寄存器偏移量
  15.     
  16.     DMA_InitStructure.DMA_MemoryBaseAddr = (u32)SendBuff;                      //要发送的内容地址
  17.     
  18.     DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;                         //设置外设作为数据传输的目的地
  19.     
  20.     DMA_InitStructure.DMA_BufferSize = SENDBUFF_SIZE;                          //指定DMA缓存大小
  21.     
  22.     DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;           //设置外设地址寄存器不递增
  23.     
  24.     DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;                    //内存地址寄存器递增
  25.     
  26.     DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;    //数据宽度为8位
  27.     
  28.     DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;            //数据宽度为8位
  29.     
  30.     DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;                              //工作在正常缓存模式
  31.     
  32.     DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;                      //设置DMA为中优先级
  33.     
  34.     DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;                               //不设置为内存到内存传输
  35.     
  36.     DMA_Init(DMA1_Channel4, &DMA_InitStructure);                               //初始化DMA结构体
  37.     
  38.     DMA_Cmd(DMA1_Channel4, ENABLE);                                            //开启DMA
  39.     DMA_ITConfig(DMA1_Channel4, DMA_IT_TC, ENABLE);                            //使能DMA1_Channel4作为中断通道,传输完成后中断屏蔽,使能
  40. }

五、设置中断


  1. /****************************************************************************
  2. * Function Name : NVIC_Configuration
  3. * Description : Configures Vector Table base location.
  4. * Input : None
  5. * Output : None
  6. * Return : None
  7. ****************************************************************************/
  8. void NVIC_Configuration(void)
  9. {
  10.     NVIC_InitTypeDef NVIC_InitStructure;

  11. #ifdef VET_TAB_RAM
  12.     /* Set the Vector Table base location at 0x2000 0000 */
  13.     NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
  14. #else
  15.     /* Set the Vector Table base location at 0x8000 0000 */
  16.     NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
  17. #endif
  18.     
  19.     NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);                           //设置为组1
  20.     
  21.     NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel4_IRQn;                  //设置DMA1_Channel4为中断源
  22.     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;                 //优先级1
  23.     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;                        //子优先级1
  24.     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;                           //使能
  25.     NVIC_Init(&NVIC_InitStructure);                                           //初始化
  26. }
在stm32f10x_it.c中加入:
  1. void DMA1_Channel4_IRQHandler(void)
  2. {
  3.     if(DMA_GetFlagStatus(DMA1_FLAG_TC4) == SET)          //如果中断源是设置了
  4.     {
  5.         DMA_ClearFlag(DMA1_FLAG_TC4);                    //清除中断
  6.     }
  7. }

六、main函数

  1. /****************************************************************************
  2. * Function Name : main
  3. * Description : Main program.
  4. * Input : None
  5. * Output : None
  6. * Return : None
  7. ****************************************************************************/
  8. int main(void)
  9. {
  10.     RCC_Configuration();
  11.     
  12.     NVIC_Configuration();
  13.     
  14.     GPIO_Configuration();
  15.     
  16.     USART_Configuration();
  17.     
  18.     DMA_Configuration();
  19.     
  20.     while(1)
  21.     {
  22.         GPIO_WriteBit(GPIOB, GPIO_Pin_8, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_8)));
  23.         fPutString("Hello World!", 13);
  24.         Delay(0xffffff);
  25.     }    
  26. }

  27. /****************************************************************************
  28. * Function Name : fPutString
  29. * Description : Send a string.
  30. * Input : None
  31. * Output : None
  32. * Return : None
  33. ****************************************************************************/
  34. void fPutString(u8 *buf, u8 len)
  35. {
  36.     u8 i;
  37.     for(i=0;i<len;i++)
  38.     {
  39.         fPutChar(*buf++);
  40.     }
  41. }

  42. /****************************************************************************
  43. * Function Name : fPutChar
  44. * Description : Send a byte
  45. * Input : None
  46. * Output : None
  47. * Return : None
  48. ****************************************************************************/
  49. u8 fPutChar(u8 ch)
  50. {
  51.     USART_SendData(USART1, (u8) ch);
  52.     while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
  53.     {
  54.         
  55.     }
  56.     return ch;
  57. }







  • /*******************************************************************************
  • * 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);
  • }
  • 阅读(1041) | 评论(0) | 转发(0) |
    0

    上一篇:STM32串口USART通信总结

    下一篇:I2C_24c02实验

    给主人留下些什么吧!~~