库函数操作
库函数操作代码:
main.c
001
#include "stm32f10x.h"
002
#include "stdio.h"
003
004
005
#define PRINTF_ON 1
006
007
void
RCC_Configuration(
void
);
008
void
GPIO_Configuration(
void
);
009
void
USART_Configuration(
void
);
010
void
NVIC_Configuration(
void
);
011
void
EXTI_Configuration(
void
);
012
013
014
015
int
main(
void
)
016
{
017
RCC_Configuration();
018
GPIO_Configuration();
019
USART_Configuration();
020
NVIC_Configuration();
021
EXTI_Configuration();
022
while
(1);
023
}
024
025
026
void
NVIC_Configuration(
void
)
027
{
028
NVIC_InitTypeDef NVIC_InitStructure;
029
030
#ifdef VECT_TAB_RAM
031
NVIC_SetVectorTable(NVIC_VectTab_RAM,0x0);
032
#else
033
NVIC_SetVectorTable(NVIC_VectTab_FLASH,0x0);
034
#endif
035
036
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
037
038
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
039
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
//优先级数字越大,优先级越小
040
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
041
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
042
NVIC_Init(&NVIC_InitStructure);
043
044
NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;
045
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
046
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
047
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
048
NVIC_Init(&NVIC_InitStructure);
049
050
NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn;
051
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
052
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
053
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
054
055
NVIC_Init(&NVIC_InitStructure);
056
}
057
058
059
060
061
void
GPIO_Configuration(
void
)
062
{
063
GPIO_InitTypeDef GPIO_InitStructure;
064
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2;
065
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
066
GPIO_Init(GPIOA , &GPIO_InitStructure);
067
068
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource0);
069
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource1);
070
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource2);
071
072
073
074
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
075
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
076
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
077
GPIO_Init(GPIOA , &GPIO_InitStructure);
078
079
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
080
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
081
GPIO_Init(GPIOA , &GPIO_InitStructure);
082
}
083
084
void
EXTI_Configuration(
void
)
085
{
086
EXTI_InitTypeDef EXTI_InitStructure;
087
088
EXTI_InitStructure.EXTI_Line = EXTI_Line0 | EXTI_Line1 | EXTI_Line2;
089
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
090
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
091
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
092
EXTI_Init(&EXTI_InitStructure);
093
094
095
}
096
097
098
void
RCC_Configuration(
void
)
099
{
100
/* 定义枚举类型变量 HSEStartUpStatus */
101
ErrorStatus HSEStartUpStatus;
102
103
/* 复位系统时钟设置*/
104
RCC_DeInit();
105
/* 开启HSE*/
106
RCC_HSEConfig(RCC_HSE_ON);
107
/* 等待HSE起振并稳定*/
108
HSEStartUpStatus = RCC_WaitForHSEStartUp();
109
/* 判断HSE起是否振成功,是则进入if()内部 */
110
if
(HSEStartUpStatus == SUCCESS)
111
{
112
/* 选择HCLK(AHB)时钟源为SYSCLK 1分频 */
113
RCC_HCLKConfig(RCC_SYSCLK_Div1);
114
/* 选择PCLK2时钟源为 HCLK(AHB) 1分频 */
115
RCC_PCLK2Config(RCC_HCLK_Div1);
116
/* 选择PCLK1时钟源为 HCLK(AHB) 2分频 */
117
RCC_PCLK1Config(RCC_HCLK_Div2);
118
/* 设置FLASH延时周期数为2 */
119
FLASH_SetLatency(FLASH_Latency_2);
120
/* 使能FLASH预取缓存 */
121
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
122
/* 选择锁相环(PLL)时钟源为HSE 1分频,倍频数为9,则PLL输出频率为 8MHz * 9 = 72MHz */
123
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
124
/* 使能PLL */
125
RCC_PLLCmd(ENABLE);
126
/* 等待PLL输出稳定 */
127
while
(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
128
/* 选择SYSCLK时钟源为PLL */
129
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
130
/* 等待PLL成为SYSCLK时钟源 */
131
while
(RCC_GetSYSCLKSource() != 0x08);
132
}
133
/* 打开APB2总线上的GPIOA时钟*/
134
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1|RCC_APB2Periph_AFIO, ENABLE);
135
136
//RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 , ENABLE);
137
138
}
139
140
141
void
USART_Configuration(
void
)
142
{
143
USART_InitTypeDef USART_InitStructure;
144
USART_ClockInitTypeDef USART_ClockInitStructure;
145
146
USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
147
USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
148
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
149
USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
150
USART_ClockInit(USART1 , &USART_ClockInitStructure);
151
152
USART_InitStructure.USART_BaudRate = 9600;
153
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
154
USART_InitStructure.USART_StopBits = USART_StopBits_1;
155
USART_InitStructure.USART_Parity = USART_Parity_No;
156
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
157
USART_InitStructure.USART_Mode = USART_Mode_Rx|USART_Mode_Tx;
158
USART_Init(USART1,&USART_InitStructure);
159
160
USART_Cmd(USART1,ENABLE);
161
}
162
163
164
void
TIM_Configuration(
void
)
165
{
166
167
168
}
169
170
#if PRINTF_ON
171
172
int
fputc
(
int
ch,
FILE
*f)
173
{
174
USART_SendData(USART1,(u8) ch);
175
while
(USART_GetFlagStatus(USART1,USART_FLAG_TC) == RESET);
176
return
ch;
177
}
178
179
#endif
|