1 /*Include---------------------------*/ 2 #include"stm32f10x_lib.h" //包含所有的头文件 3 #include<stdio.h> 4 5 //----------------函数声明-------------------- 6 void Delay_MS(u16 dly); 7 void RCC_Configuration(void); 8 void GPIO_Configuration(void); 9 void USART3_Configuration(void); 10 11 12 u8 tab[] = "hello welcome to class !"; 13 14 /******************************************************************************* 15 * Function Name : main 16 * Description : Main program. 17 * Input : None 18 * Output : None 19 * Return : None 20 *******************************************************************************/ 21 int main(void) 22 { 23 u8 i ,id_num[12];; 24 #ifdef DEBUG 25 debug(); 26 #endif 27 //------------初始化------------ 28 RCC_Configuration(); 29 GPIO_Configuration(); 30 USART3_Configuration(); 31 32 id_num[0] = *((u8*)(0x1FFFF7E9)); //stm32芯片id的放置地址,96位id;12字节 33 printf("hello world\n"); 34 //------------向usart3发送数据----------- 35 for(i=0;i<12;i++) 36 { 37 USART_SendData(USART3, tab[i]); 38 id_num[i] = *((u8*)(0x1FFFF7E9+i)); 39 USART_SendData(USART3, id_num[i]); //将id发送给串口 40 Delay_MS(2000); 41 // while(USART_ GetFlagStatus(USART_FLAG_TC)==RESET) 42 // ; 43 } 44 45 } 46 47 /******************************************************************************* 48 * Function Name : Delay_Ms 49 * Description : delay 1 ms. 50 * Input : dly (ms) 51 * Output : None 52 * Return : None 53 *******************************************************************************/ 54 void Delay_MS(u16 dly) 55 { 56 u16 i,j; 57 for(i=0;i<dly;i++) 58 for(j=1000;j>0;j--); 59 } 60 61 /******************************************************************************* 62 * Function Name : RCC_Configuration 63 * Description : Configures the different system clocks. 64 * Input : None 65 * Output : None 66 * Return : None 67 *******************************************************************************/ 68 void RCC_Configuration(void) 69 { 70 //----------使用外部RC晶振----------- 71 RCC_DeInit(); //初始化为缺省值 72 RCC_HSEConfig(RCC_HSE_ON); //使能外部的高速时钟 73 while(RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET); //等待外部高速时钟使能就绪 74 75 FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); //Enable Prefetch Buffer 76 FLASH_SetLatency(FLASH_Latency_2); //Flash 2 wait state 77 78 RCC_HCLKConfig(RCC_SYSCLK_Div1); //HCLK = SYSCLK 79 RCC_PCLK2Config(RCC_HCLK_Div1); //PCLK2 = HCLK 80 RCC_PCLK1Config(RCC_HCLK_Div2); //PCLK1 = HCLK/2 81 RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9); //PLLCLK = 8MHZ * 9 =72MHZ 82 RCC_PLLCmd(ENABLE); //Enable PLLCLK 83 84 while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET); //Wait till PLLCLK is ready 85 RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //Select PLL as system clock 86 while(RCC_GetSYSCLKSource()!=0x08); //Wait till PLL is used as system clock source 87 88 //---------打开相应外设时钟-------------------- 89 RCC_APB2PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE); //使能APB2外设的GPIOC的时钟 90 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO, ENABLE); //复用功能时钟开启 91 } 92 93 /******************************************************************************* 94 * Function Name : GPIO_Configuration 95 * Description : 初始化GPIO外设 96 * Input : None 97 * Output : None 98 * Return : None 99 *******************************************************************************/ 100 //由电路图可知;配置PC10,PC11引脚 101 void GPIO_Configuration(void) 102 { 103 //CLK:PB5 CLR:PE11 DATA:PE10 104 GPIO_InitTypeDef GPIO_InitStructure; //声明一个结构体变量 105 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //选择PB.1- 106 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //管脚频率为50MHZ 107 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //输出模式为 "复用推挽输出" 108 GPIO_Init(GPIOC,&GPIO_InitStructure); //初始化GPIOB寄存器 109 110 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 |GPIO_Pin_11 ; //选择PE.10 PE.11 111 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //管脚频率为50MHZ 112 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //输出模式为 "浮空输入" 113 GPIO_Init(GPIOC,&GPIO_InitStructure); //初始化GPIOE寄存器 114 115 //开启时钟 必须在RCC_Configuration中设置 116 117 118 //端口重映射:PC10/PC11 119 GPIO_PinRemapConfig(GPIO_PartialRemap_USART3, ENABLE); 120 } 121 122 void USART3_Configuration(void) 123 { 124 USART_InitTypeDef USART_InitStructure; 125 USART_InitStructure.USART_BaudRate = 9600; 126 USART_InitStructure.USART_WordLength = USART_WordLength_8b; 127 USART_InitStructure.USART_StopBits = USART_StopBits_1; 128 USART_InitStructure.USART_Parity = USART_Parity_No; 129 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; 130 USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; 131 132 USART_Init(USART3, &USART_InitStructure); 133 134 USART_Cmd(USART3, ENABLE); //开启使能 135 } 136 137 /*********************************************************** 138 name: fputc * 139 Description:重定向这个c库printf函数,文件流--->串口USART * 140 * 141 ************************************************************/ 142 int fputc(int ch,FILE *f) 143 { 144 //ch发送给USART1 145 USART_SendData(USART3, ch); 146 //等待发送完毕 147 while(USART_GetFlagStatus(USART3, USART_FLAG_TC)==RESET) 148 ; 149 return ch; 150 }