-
Notifications
You must be signed in to change notification settings - Fork 2
/
STM32.txt
657 lines (606 loc) · 15.1 KB
/
STM32.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
配置:
Define: USE_STDPERIPH_DRIVER,STM32F10X_MD
Paths: .\Baisc;.\CMSIS;.\Hardware;.\Lib\src;.\Startup;.\User;.\Lib\inc
STM32
项目结构:
Target
SMSIS:
core_cm3.c
system_stm32f10x.c
Lib
misc.c
stm32f10x.gpio.c
...
Startup
Startup_stm32f10x.md.s
User
stm32f10x_it.c
main.c
编程:
1.结构体说明
1.1端口初始化结构体:
GPIO_InitTypeDef {
uint16_t GPIO_Pin; 端口号(stm32f10x_gpio.h中存在宏定义)
GPIOSpeed_TypeDef GPIO_Speed; 速度(仅输出模式需要设置)
GPIOMode_TypeDef GPIO_Mode; 模式
}
2.宏定义说明:
2.1选择IO接口工作方式:
typedef enum
{ GPIO_Mode_AIN = 0x0, //模拟输入
GPIO_Mode_IN_FLOATING = 0x04, //浮空输入
GPIO_Mode_IPD = 0x28, //下拉输入
GPIO_Mode_IPU = 0x48, //上拉输入
GPIO_Mode_Out_OD = 0x14, //开漏输出
GPIO_Mode_Out_PP = 0x10, //推挽输出
GPIO_Mode_AF_OD = 0x1C, //复用开漏输出
GPIO_Mode_AF_PP = 0x18 //复用推挽输出
}GPIOMode_TypeDef;
2.2速度定义
typedef enum
{
GPIO_Speed_10MHz = 1,
GPIO_Speed_2MHz,
GPIO_Speed_50MHz
}GPIOSpeed_TypeDef;
3.函数:
3.1 RCC_APB2PeriphClockCmd() 在总线上启动端口
例: RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC,ENABLE);
3.2 GPIO_SetBits 设置端口输出
GPIO_WriteBit(哪一组,哪一个,设置/清零)
例: GPIO_WriteBit(GPIOB,GPIO_Pin_0,Bit_SET);
GPIO_ResetBits /GPIO_SetBits
例: GPIO_SetBits(GPIOB,GPIO_Pin_0);
GPIO_Write 设置整组端口输出
GPIO_Write(GPIOB,0x0001);
//案例
#[PWM呼吸灯]####################################################################
#include "stm32f10x.h" //STM 头文件
#include "led.h"
#include "delay.h"
int main (void){//主程序
u8 ENUM,i;
u32 time;
RCC_Configuration();
LED_init();
i = 0;
ENUM = 1;
time = 1;
while(1) {
if(ENUM) {
time++;
if(time == 500) {
ENUM = 0;
}
}else {
time--;
if(time == 1) {
ENUM = 1;
}
}
for(i=0;i<10;i++) {
GPIO_WriteBit(GPIOB,LED,Bit_RESET);
delay_us(time);
GPIO_WriteBit(GPIOB,LED,Bit_SET);
delay_us(501-time);
}
}
}
#[按键控制LED灯]####################################################################
#include "stm32f10x.h"
#include "led.h"
#include "delay.h"
#include "simplekey.h"
int main (void){
RCC_Configuration();
LED_init();
KEY_init();
while(1) {
if(!GPIO_ReadInputDataBit(GPIOA,SIMPLE_KEY1)) {
delay_ms(20);
if(!GPIO_ReadInputDataBit(GPIOA,SIMPLE_KEY1)) {
GPIO_WriteBit(GPIOB,LED,(BitAction) 1-GPIO_ReadInputDataBit(GPIOB,LED));
while(!GPIO_ReadInputDataBit(GPIOA,SIMPLE_KEY1));
}
}
}
}
#[FLASH存储]####################################################################
#include "stm32f10x.h"
#include "led.h"
#include "delay.h"
#include "simplekey.h"
#include "flash.h"
int main (void){
u16 a;
u32 addr;
RCC_Configuration();
LED_init();
KEY_init();
addr = FLASH_page(127);
a = FLASH_read(addr);
GPIO_WriteBit(GPIOB,LED,(BitAction) a);
while(1) {
if(!GPIO_ReadInputDataBit(GPIOA,SIMPLE_KEY1)) {
delay_ms(20);
if(!GPIO_ReadInputDataBit(GPIOA,SIMPLE_KEY1)) {
a = 1-GPIO_ReadInputDataBit(GPIOB,LED);
FLASH_write(addr,a);
GPIO_WriteBit(GPIOB,LED,(BitAction) a);
while(!GPIO_ReadInputDataBit(GPIOA,SIMPLE_KEY1));
}
}
}
}
#[无源蜂鸣器]####################################################################
#include "stm32f10x.h"
#include "led.h"
#include "delay.h"
#include "simplekey.h"
#include "buzzer.h"
int main (void){
RCC_Configuration();
LED_init();
KEY_init();
BUZZER_init();
BUZZER_play(300,554);
while(1) {
if(!GPIO_ReadInputDataBit(GPIOA,SIMPLE_KEY1)) {
delay_ms(20);
if(!GPIO_ReadInputDataBit(GPIOA,SIMPLE_KEY1)) {
BUZZER_play(300,1000);
GPIO_WriteBit(GPIOB,LED,(BitAction)(1-GPIO_ReadInputDataBit(GPIOB,LED)));
while(!GPIO_ReadInputDataBit(GPIOA,SIMPLE_KEY1));
}
}
}
}
#[串口发送数据]####################################################################
#include "stm32f10x.h"
#include "led.h"
#include "delay.h"
#include "simplekey.h"
#include "buzzer.h"
#include "usart.h"
int main (void){
RCC_Configuration();
LED_init();
KEY_init();
BUZZER_init();
USART1_init(115200,DISABLE);
BUZZER_play(300,554);
while(1) {
if(!GPIO_ReadInputDataBit(GPIOA,SIMPLE_KEY1)) {
delay_ms(20);
if(!GPIO_ReadInputDataBit(GPIOA,SIMPLE_KEY1)) {
BUZZER_play(300,1000);
GPIO_WriteBit(GPIOB,LED,(BitAction)(1-GPIO_ReadInputDataBit(GPIOB,LED)));
USART1_printf("hello%s\n","world");
while(!GPIO_ReadInputDataBit(GPIOA,SIMPLE_KEY1));
}
}
}
}
#[查询式串口接收数据]####################################################################
#include "stm32f10x.h"
#include "buzzer.h"
#include "usart.h"
#include "led.h"
int main (void){
u8 buffer;
RCC_Configuration();
LED_init();
BUZZER_init();
USART1_init(115200,DISABLE);
BUZZER_play(300,554);
while(1) {
if(USART_GetFlagStatus(USART1,USART_FLAG_RXNE) != RESET) {
buffer = USART_ReceiveData(USART1);
switch(buffer) {
case '1':
BUZZER_play(300,1000);
GPIO_WriteBit(GPIOB,LED,(BitAction)(1-GPIO_ReadInputDataBit(GPIOB,LED)));
USART1_printf("Switch Success");
break;
default:
break;
}
}
}
}
#[RTC走时程序 ]####################################################################
#include "stm32f10x.h"
#include "led.h"
#include "rtc.h"
#include "usart.h"
int main (void){
u8 buffer;
u32 val;
struct DateTime datetime;
RCC_Configuration();
datetime.year = 2018;
datetime.month = 8;
datetime.day = 22;
datetime.hour = 19;
datetime.minute = 40;
datetime.second = 0;
USART1_init(115200,DISABLE);
USART1_printf("INIT USART1");
LED_init();
USART1_printf("INIT LED");
RTC_init();
val = RTC_Set(datetime);
USART1_printf("INIT RTC[%d]",val);
val=RTC_GetCounter();
USART1_printf("INIT RTC[%d]",val);
while(1) {
datetime = RTC_dateTime();
if(buffer != datetime.second) {
buffer = datetime.second;
val=RTC_GetCounter();
USART1_printf("READ RTC[%d]\n",val);
USART1_printf("%d-%d-%d %d:%d:%d (%d)\n",datetime.year,datetime.month,datetime.day,datetime.hour,datetime.minute,datetime.second,datetime.week);
GPIO_WriteBit(GPIOB,LED,(BitAction)(datetime.second % 2));
}
}
}
#[触摸按键单击 ]####################################################################
#include "stm32f10x.h"
#include "led.h"
#include "touchkey.h"
#include "buzzer.h"
int main (void){
RCC_Configuration();
TOUCH_KEY_init();
LED_init();
BUZZER_init();
while(1) {
if(!GPIO_ReadInputDataBit(GPIOA,TOUCHKEY_A)) {
GPIO_WriteBit(GPIOB,LED,(BitAction)(1-GPIO_ReadInputDataBit(GPIOB,LED)));
BUZZER_play(300,1000);
}
if(!GPIO_ReadInputDataBit(GPIOA,TOUCHKEY_B)) {
GPIO_WriteBit(GPIOB,LED,Bit_SET);
BUZZER_play(300,1500);
}
if(!GPIO_ReadInputDataBit(GPIOA,TOUCHKEY_C)) {
GPIO_WriteBit(GPIOB,LED,Bit_RESET);
BUZZER_play(300,2000);
}
}
}
#[触摸按键单击 双击 长按 ]####################################################################
#include "stm32f10x.h"
#include "led.h"
#include "touchkey.h"
#include "buzzer.h"
#include "delay.h"
int main (void){
u16 keyFlag;
u8 i;
RCC_Configuration();
TOUCH_KEY_init();
LED_init();
BUZZER_init();
while(1) {
if(!GPIO_ReadInputDataBit(GPIOA,TOUCHKEY_D)) {
delay_ms(20);
if(!GPIO_ReadInputDataBit(GPIOA,TOUCHKEY_D)) {
keyFlag = 0;
i = 0;
//长按判断时长 220
while(!GPIO_ReadInputDataBit(GPIOA,TOUCHKEY_D) && keyFlag < 220) {
keyFlag++;
delay_ms(10);
}
if(keyFlag >= 220) {
//长按
BUZZER_play(300,2000);
GPIO_WriteBit(GPIOB,LED1,Bit_RESET);
GPIO_WriteBit(GPIOB,LED2,Bit_RESET);
while(!GPIO_ReadInputDataBit(GPIOA,TOUCHKEY_D));
}else {
//双击判断时长 30
for(i=0;i<30;i++) {
delay_ms(10);
if(!GPIO_ReadInputDataBit(GPIOA,TOUCHKEY_D)) {
keyFlag = 50000;
//双击
GPIO_WriteBit(GPIOB,LED2,Bit_SET);
BUZZER_play(300,1500);
while(!GPIO_ReadInputDataBit(GPIOA,TOUCHKEY_D));
}
}
if(keyFlag < 50000) {
//单击
GPIO_WriteBit(GPIOB,LED1,Bit_SET);
BUZZER_play(300,1000);
}
}
}
}
}
}
#[TM1640驱动数码管显示RTC走时 ]####################################################################
#include "stm32f10x.h"
#include "led.h"
#include "rtc.h"
#include "usart.h"
#include "tm1640.h"
int main (void){
u8 buffer,sm1,sm2,sm3,sm4,sm5,sm6,sm7,sm0;
u32 val;
const u8 ledd[] = {0x40,0x01,0x02,0x04,0x08,0x10,0x20};
struct DateTime datetime;
RCC_Configuration();
datetime.year = 2018;
datetime.month = 8;
datetime.day = 22;
datetime.hour = 19;
datetime.minute = 40;
datetime.second = 0;
USART1_init(115200,DISABLE);
USART1_printf("INIT USART1");
LED_init();
USART1_printf("INIT LED");
RTC_init();
USART1_printf("INIT RTC");
TM1640_init();
USART1_printf("INIT TM1640");
while(1) {
datetime = RTC_dateTime();
if(buffer != datetime.second) {
buffer = datetime.second;
val=RTC_GetCounter();
USART1_printf("READ RTC[%d]\n",val);
USART1_printf("%d-%d-%d %d:%d:%d (%d)\n",datetime.year,datetime.month,datetime.day,datetime.hour,datetime.minute,datetime.second,datetime.week);
sm0 = datetime.month / 10;
sm1 = datetime.month % 10;
sm2 = datetime.day / 10;
sm3 = datetime.day % 10;
sm4 = datetime.hour / 10;
sm5 = datetime.hour % 10;
sm6 = datetime.minute / 10;
sm7 = datetime.minute % 10;
if(datetime.second%2) {
sm7 += 10;
}
TM1640_display(0,sm0);
TM1640_display(1,sm1);
TM1640_display(2,sm2);
TM1640_display(3,sm3);
TM1640_display(4,sm4);
TM1640_display(5,sm5);
TM1640_display(6,sm6);
TM1640_display(7,sm7);
TM1640_led(ledd[datetime.week]);
GPIO_WriteBit(GPIOB,LED1,(BitAction)(datetime.second % 2));
GPIO_WriteBit(GPIOB,LED2,(BitAction)(1-(datetime.second % 2)));
}
}
}
#[旋转编码器示例]####################################################################
#include "stm32f10x.h"
#include "led.h"
#include "rtc.h"
#include "usart.h"
#include "tm1640.h"
#include "rotaryencoder.h"
#include "delay.h"
int main (void){
u8 encoder,sm0,sm1,sm2;
u16 time;
time = 0;
RCC_Configuration();
TM1640_init();
USART1_init(115200,DISABLE);
ENCODER_init();
USART1_printf("%s","init success");
while(1) {
encoder = ENCODER_read();
if(encoder == 2 && time > 0) {
time -= 5;
}
if(encoder ==1 && time <500) {
time += 5;
}
if(encoder == 10) {
time = 0;
}
if(encoder != 0) {
USART1_printf("%d",encoder);
}
sm0 = time / 100;
sm1 = (time / 10) % 10;
sm2 = time % 10;
TM1640_display(0,sm0);
TM1640_display(1,sm1);
TM1640_display(2,sm2);
TM1640_display(3,20);
TM1640_display(4,20);
TM1640_display(5,20);
TM1640_display(6,20);
TM1640_display(7,20);
}
}
#[数码管显示LM75A温度传感器读取]####################################################################
#include "stm32f10x.h"
#include "tm1640.h"
#include "lm75a.h"
int main (void){
u8 buff[3];
RCC_Configuration();
IIC_init();
TM1640_init();
TM1640_display(0,20);
TM1640_display(1,20);
TM1640_display(2,20);
TM1640_display(3,20);
TM1640_display(4,20);
TM1640_display(5,20);
TM1640_display(6,20);
TM1640_display(7,20);
while(1) {
LM75A_read(buff);
TM1640_display(1,buff[1]/10);
TM1640_display(2,buff[1]%10+10);
TM1640_display(3,buff[2]/10);
TM1640_display(4,buff[2]%10);
}
}
#[OLD显示LM75A温度传感器读取]####################################################################
#include "stm32f10x.h"
#include "lm75a.h"
#include "oled0561.h"
#include "delay.h"
int main (void){
u8 buff[3];
delay_ms(100);
RCC_Configuration();
IIC_init();
LM75A_read(buff);
OLED_init();
OLED_display8x16_str(0," Temp:");
while(1){
LM75A_read(buff);
OLED_display8x16(16,2,buff[1]/10+0x30);
OLED_display8x16(24,2,buff[1]%10+0x30);
OLED_display8x16(32,2,'.');
OLED_display8x16(40,2,buff[2]/10+0x30);
OLED_display8x16(48,2,buff[2]%10+0x30);
delay_ms(100);
}
}
#[OLD显示图片 温度]####################################################################
#include "stm32f10x.h"
#include "lm75a.h"
#include "oled0561.h"
#include "delay.h"
#include "oleddata.h"
int main (void){
u8 buff[3];
delay_ms(100);
RCC_Configuration();
IIC_init();
OLED_init();
OLED_display128x64(IMGTAB);
while(1){
LM75A_read(buff);
OLED_display8x16(72,6,buff[1]/10+0x30);
OLED_display8x16(80,6,buff[1]%10+0x30);
OLED_display8x16(88,6,'.');
OLED_display8x16(96,6,buff[2]/10+0x30);
OLED_display8x16(104,6,buff[2]%10+0x30);
delay_ms(100);
}
}
#[触摸键控制继电器]####################################################################
#include "stm32f10x.h"
#include "delay.h"
#include "relay.h"
#include "touchkey.h"
int main (void){
TOUCH_KEY_init();
Relay_init();
while(1){
if(!GPIO_ReadInputDataBit(GPIOA,TOUCHKEY_A)) {
setRelay1(Bit_SET);
}
if(!GPIO_ReadInputDataBit(GPIOA,TOUCHKEY_B)) {
setRelay1(Bit_RESET);
}
if(!GPIO_ReadInputDataBit(GPIOA,TOUCHKEY_C)) {
setRelay2(Bit_SET);
}
if(!GPIO_ReadInputDataBit(GPIOA,TOUCHKEY_D)) {
setRelay2(Bit_RESET);
}
}
}
#[XXXXXXXX ]####################################################################
#include "stm32f10x.h"
#include "led.h"
#include "rtc.h"
#include "usart.h"
#include "tm1640.h"
#include "rotaryencoder.h"
#include "delay.h"
int main (void){
u8 buffer,sm1,sm2,sm3,sm4,sm5,sm6,sm7,sm0,encoder,i;
u16 time;
u32 val;
const u8 ledd[] = {0x40,0x01,0x02,0x04,0x08,0x10,0x20};
struct DateTime datetime;
time = 0;
RCC_Configuration();
datetime.year = 2018;
datetime.month = 8;
datetime.day = 22;
datetime.hour = 19;
datetime.minute = 40;
datetime.second = 0;
USART1_init(115200,DISABLE);
USART1_printf("INIT USART1");
LED_init();
USART1_printf("INIT LED");
RTC_init();
USART1_printf("INIT RTC");
TM1640_init();
USART1_printf("INIT TM1640");
ENCODER_init();
while(1) {
datetime = RTC_dateTime();
if(buffer != datetime.second) {
buffer = datetime.second;
val=RTC_GetCounter();
USART1_printf("READ RTC[%d]\n",val);
USART1_printf("%d-%d-%d %d:%d:%d (%d)\n",datetime.year,datetime.month,datetime.day,datetime.hour,datetime.minute,datetime.second,datetime.week);
sm0 = datetime.month / 10;
sm1 = datetime.month % 10;
sm2 = datetime.day / 10;
sm3 = datetime.day % 10;
sm4 = datetime.hour / 10;
sm5 = datetime.hour % 10;
sm6 = datetime.minute / 10;
sm7 = datetime.minute % 10;
if(datetime.second%2) {
sm7 += 10;
}
TM1640_display(0,sm0);
TM1640_display(1,sm1);
TM1640_display(2,sm2);
TM1640_display(3,sm3);
TM1640_display(4,sm4);
TM1640_display(5,sm5);
TM1640_display(6,sm6);
TM1640_display(7,sm7);
TM1640_led(ledd[datetime.week]);
GPIO_WriteBit(GPIOB,LED1,(BitAction)(datetime.second % 2));
}
encoder = ENCODER_read();
if(encoder == 1) {
time -= 5;
}
if(encoder ==2) {
time += 5;
}
if(encoder ==11) {
time -= 25;
}
if(encoder ==12) {
time += 25;
}
if(time >= 500) {
time = 500;
}
if(time <= 25) {
time = 0;
}
for(i=0;i<10;i++) {
GPIO_WriteBit(GPIOB,LED2,Bit_RESET);
delay_us(time);
GPIO_WriteBit(GPIOB,LED2,Bit_SET);
delay_us(501-time);
}
}
}