Advertisement
Guest User

Untitled

a guest
Feb 20th, 2010
548
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.43 KB | None | 0 0
  1. #include "stm32f10x_lib.h"
  2. #include "stm32f10x_usart.h"
  3. #include "stm32f10x_gpio.h"
  4. #include "stm32f10x_map.h"
  5. #include "stm32f10x_rcc.h"
  6. #include "bits.h"
  7.  
  8. #define STACK_TOP 0x20000800
  9. #define NVIC_CCR ((volatile unsigned long *)(0xE000ED14))
  10.  
  11. void nmi_handler(void);
  12. void hardfault_handler(void);
  13. void myDelay(unsigned long delay);
  14. void out_string(char* str);
  15.  
  16. int main(void);
  17.  
  18. void Clk_Init (void);
  19.  
  20. // Define the vector table
  21.     unsigned int * myvectors[59]
  22.      __attribute__ ((section("vectors")))= {
  23.     (unsigned int *)    0x20000800, // stack pointer
  24.     (unsigned int *)    main,       // code entry point
  25.     (unsigned int *)    nmi_handler,        // NMI handler (not really)
  26.     (unsigned int *)    hardfault_handler       // hard fault handler (let's hope not)
  27.    /*(unsigned int *)   0,0,0,0,0,0,0,0,0,0
  28.                         0,0,0,0,0,0,0,0,0,0
  29.                         0,0,0,0,0,0,0,0,0,0
  30.                         0,0,0,0,0,0,0,0,0,0
  31.                         0,0,0,0,0,0,0,0,0,0
  32.                         0,0,0,0,0,0*/
  33. };
  34.  
  35.  
  36. //Definiton of Structure
  37. USART_InitTypeDef USART_InitStructure;
  38. GPIO_InitTypeDef GPIO_InitStructure;
  39.  
  40. //#define TxBufferSize   (countof(TxBuffer))
  41. //#define countof(a)   (sizeof(a) / sizeof(*(a)))
  42.  
  43. int main(void)
  44. {
  45.     //Private Variable
  46.     u8 TxBuffer[] = "Buffer Send from USART2 to USART1 using Flags";
  47.     u8 RxBuffer[45];
  48.     u8 TxCounter = 0, RxCounter = 0;
  49.  
  50.  
  51.     *NVIC_CCR = *NVIC_CCR | 0x200; /* Set STKALIGN in NVIC */
  52.  
  53.     RCC_DeInit();
  54.     RCC_HSEConfig(RCC_HSE_ON);
  55.     Clk_Init;
  56.  
  57.         //Clock Enable USART2 & GPIOA
  58.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC |RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
  59.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  60.  
  61.         // Configure PA.0 as output push-pull
  62.         GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_0;
  63.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  64.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  65.         GPIO_Init(GPIOA, &GPIO_InitStructure);
  66.  
  67.         // Configure PA.12 as output push-pull
  68.         GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_12;
  69.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  70.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  71.         GPIO_Init(GPIOC, &GPIO_InitStructure);
  72.  
  73.  
  74.         //Configure TX as Output - Alternate Function Push-Pull
  75.         GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_2;
  76.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  77.         GPIO_Init(GPIOA, &GPIO_InitStructure);
  78.  
  79.         //Configure RX as Input - Input Floating
  80.         GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_3;
  81.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  82.         GPIO_Init(GPIOA, &GPIO_InitStructure);
  83.  
  84.     //Configuration of USART2
  85.     USART_InitStructure.USART_BaudRate = 0x2580; /* 9600 Baud */
  86.     USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  87.     USART_InitStructure.USART_Parity = USART_Parity_No;
  88.     USART_InitStructure.USART_StopBits = USART_StopBits_1;
  89.     USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  90.     USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  91.     USART_Init(USART2,&USART_InitStructure);
  92.  
  93.     USART_Cmd(USART2,ENABLE);
  94.  
  95.     while(TxCounter <= 45)
  96.     {
  97.         USART_SendData(USART2,TxBuffer[TxCounter++]);
  98.         while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET)
  99.         {
  100.         }
  101.  
  102.     }
  103.     while(1)
  104.     {
  105.     }
  106.  
  107. }
  108.  
  109. void nmi_handler(void)
  110. {
  111.     return ;
  112. }
  113.  
  114. void hardfault_handler(void)
  115. {
  116.     return ;
  117. }
  118.  
  119. void myDelay(unsigned long delay)
  120. {
  121.     while(delay) delay--;
  122. }
  123.  
  124. void Clk_Init (void)
  125. {
  126.   // 1. Cloking the controller from internal HSI RC (8 MHz)
  127.   RCC_HSICmd(ENABLE);
  128.   // wait until the HSI is ready
  129.   while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET);
  130.   RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI);
  131.   // 2. Enable ext. high frequency OSC
  132.   RCC_HSEConfig(RCC_HSE_ON);
  133.   // wait until the HSE is ready
  134.   while(RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET);
  135.   // 3. Init PLL
  136.   RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9); // 72MHz
  137. //  RCC_PLLConfig(RCC_PLLSource_HSE_Div2,RCC_PLLMul_9); // 72MHz
  138.   RCC_PLLCmd(ENABLE);
  139.   // wait until the PLL is ready
  140.   while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
  141.   // 4. Set system clock divders
  142.   RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);
  143.   RCC_ADCCLKConfig(RCC_PCLK2_Div8);
  144.   RCC_PCLK2Config(RCC_HCLK_Div1);
  145.   RCC_PCLK1Config(RCC_HCLK_Div2);
  146.   RCC_HCLKConfig(RCC_SYSCLK_Div1);
  147.   // Flash 1 wait state
  148.   *(vu32 *)0x40022000 = 0x12;
  149.   // 5. Clock system from PLL
  150.   RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
  151. }
  152.  
  153.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement