Advertisement
Guest User

Untitled

a guest
Aug 8th, 2012
461
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.78 KB | None | 0 0
  1. #include <string.h>
  2. #include "stm32f4xx.h"
  3.  
  4.  
  5. void Delay(__IO uint32_t nCount) {
  6.     while(nCount--) {
  7.     }
  8. }
  9.  
  10.  
  11. void initUSART6()
  12. {
  13.   GPIO_InitTypeDef GPIO_InitStructure;
  14.   USART_InitTypeDef USART_InitStruct;
  15.  
  16.   USART_StructInit(&USART_InitStruct);
  17.   //USART_InitStruct.USART_BaudRate = 115200;
  18.   // The following line works and produces the targeted baud rate of 115200kbps
  19.   USART_InitStruct.USART_BaudRate = 115200 / 3;
  20.  
  21.   /* Enable GPIO clock */
  22.   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
  23.  
  24.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE);
  25.  
  26.  
  27.   /* Connect PXx to USARTx_Tx*/
  28.   GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_USART6);
  29.  
  30.   /* Connect PXx to USARTx_Rx*/
  31.   GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_USART6);
  32.  
  33.   /* Configure USART Tx as alternate function  */
  34.   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  35.   GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;
  36.   GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
  37.  
  38.   GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_6;
  39.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  40.   GPIO_Init(GPIOC, &GPIO_InitStructure);
  41.  
  42.   /* Configure USART Rx as alternate function  */
  43.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  44.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
  45.   GPIO_Init(GPIOC, &GPIO_InitStructure);
  46.  
  47.   /* USART configuration */
  48.   USART_Init(USART6, &USART_InitStruct);
  49.  
  50.   /* Enable USART */
  51.   USART_Cmd(USART6, ENABLE);
  52. }
  53.  
  54.  
  55. void myPrint(uint8_t *msg, uint16_t len) {
  56.     uint16_t n;
  57.     for (n = 0; n < len; n++) {
  58.         while (USART_GetFlagStatus(USART6, USART_FLAG_TXE) == RESET) {}
  59.         USART_SendData(USART6, *(msg++));
  60.     }
  61. }
  62.  
  63.  
  64. int main(void)
  65. {
  66.     uint32_t cnt = 0;
  67.     uint8_t *msg = "hello world\r\n";
  68.  
  69.     initUSART6();
  70.  
  71.     while(1) {
  72. //      Delay(100L);
  73.         myPrint(msg, strlen(msg));
  74.     }
  75.  
  76.     return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement