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

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

文章分类

全部博文(223)

文章存档

2017年(56)

2016年(118)

2015年(3)

2014年(46)

我的朋友

分类: 嵌入式

2017-02-22 21:43:15

一、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. }
从手册上可知,USART1是PA9(TX)PA10(RX)。所以设置pin9和pin10的GPIO输入输出状态。
同时需要开启复用功能的时钟,和USART1的时钟。

二、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;
  12.     USART_InitStructure.USART_StopBits = USART_StopBits_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;
  16.     USART_Init(USART1, &USART_InitStructure);
  17.     USART_Cmd(USART1, ENABLE);
  18. }
初始化主要包括,波特率,发送的长度,停止位,校验位,硬件流控。

三、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.     while(1)
  19.     {
  20.         GPIO_WriteBit(GPIOB, GPIO_Pin_8, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_8)));
  21.         fPutString("Hello World!", 13);
  22.         Delay(0xffffff);
  23.     }    
  24. }
简单的发送程序。

  1. /****************************************************************************
  2. * Function Name : fPutString
  3. * Description : Send a string.
  4. * Input : None
  5. * Output : None
  6. * Return : None
  7. ****************************************************************************/
  8. void fPutString(u8 *buf, u8 len)
  9. {
  10.     u8 i;
  11.     for(i=0;i<len;i++)
  12.     {
  13.         fPutChar(*buf++);
  14.     }
  15. }

  16. /****************************************************************************
  17. * Function Name : fPutChar
  18. * Description : Send a byte
  19. * Input : None
  20. * Output : None
  21. * Return : None
  22. ****************************************************************************/
  23. u8 fPutChar(u8 ch)
  24. {
  25.     USART_SendData(USART1, (u8) ch);
  26.     while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
  27.     {
  28.         
  29.     }
  30.     return ch;
  31. }

四、补充NVIC

  1. /**************************实现函数********************************************
  2. *函数原型:        void U1NVIC_Configuration(void)
  3. *功  能:        串口1中断配置
  4. 输入参数:无
  5. 输出参数:没有    
  6. *******************************************************************************/
  7. void UART1NVIC_Configuration(void)
  8. {
  9.         NVIC_InitTypeDef NVIC_InitStructure;
  10.         /* Enable the USART1 Interrupt */
  11.         NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  12.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  13.         NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  14.         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  15.         NVIC_Init(&NVIC_InitStructure);
  16. }

五、支持printf输出,不用选择use MicroLIB


  1. //////////////////////////////////////////////////////////////////
  2. //加入以下代码,支持printf函数,而不需要选择use MicroLIB    
  3. #if 1
  4. #pragma import(__use_no_semihosting)
  5. //标准库需要的支持函数
  6. struct __FILE
  7. {
  8.     int handle;
  9.     /* Whatever you require here. If the only file you are using is */
  10.     /* standard output using printf() for debugging, no file handling */
  11.     /* is required. */
  12. };
  13. /* FILE is typedef’ d in stdio.h. */
  14. FILE __stdout;
  15. //定义_sys_exit()以避免使用半主机模式
  16. _sys_exit(int x)
  17. {
  18.     x = x;
  19. }
  20. //重定义fputc函数
  21. int fputc(int ch, FILE *f)
  22. {
  23.     while((USART1->SR&0X40)==0);//循环发送,直到发送完毕
  24.     USART1->DR = (u8) ch;
  25.     return ch;
  26. }
  27. #endif

阅读(629) | 评论(0) | 转发(0) |
0

上一篇:按键中断总结

下一篇:DMA实验总结

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