LED灯

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// main.c
#include "stm32f10x.h"
#include "led.h"
#include "delay.h"
int main()
{
LED_Init();
delay_init();
LED1 = LED2 = LED3 = LED4 = 1;
while(1){
LED1 = LED2 = 0;
LED3 = LED4 = 1;
int t = 30000;
while(t--) delay_ms(1000);
LED1 = LED2 = 1;
LED3 = LED4 = 0;
t = 30000;
while(t--) delay_ms(1000);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// led.h
#ifndef __LED_H
#define __LED_H

#include "sys.h"

#define LED1 PBout(8)
#define LED2 PBout(9)
#define LED3 PBout(10)
#define LED4 PBout(11)

void LED_Init(void);

#endif
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//led.c
#include "stm32f10x.h"
#include "led.h"

void LED_Init()
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}

数码管 + 按键中断

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// main.c
#include "stm32f10x.h"
#include "key.h"
#include "led.h"
int num = 0;

int main()
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);

Key_Init();
Smg_Init();

GPIO_Write(GPIOB,~(0x20>>0));
GPIO_Write(GPIOC,0x3f);

while(1){}

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//smg.c
#include "stm32f10x.h"
#include "led.h"

void Smg_Init()
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_Init(GPIOC, &GPIO_InitStructure);
}

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
// key.c
#include "stm32f10x.h"
#include "key.h"
#include "led.h"
#include "delay.h"

uint16_t table[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
extern int num;

void Key_Init()
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);

GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource4);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource5);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource6);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource7);

EXTI_InitTypeDef EXTI_InitStructure;
EXTI_InitStructure.EXTI_Line = EXTI_Line4|EXTI_Line5|EXTI_Line6|EXTI_Line7;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);

NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);

NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
NVIC_Init(&NVIC_InitStructure);

}

void EXTI4_IRQHandler()
{
GPIO_Write(GPIOB,~(0x20>>0));
EXTI_ClearITPendingBit(EXTI_Line0);
}

void EXTI9_5_IRQHandler()
{
if(EXTI_GetFlagStatus(EXTI_Line5))
{
GPIO_Write(GPIOB,~(0x20>>1));
EXTI_ClearITPendingBit(EXTI_Line5);
}

if(EXTI_GetFlagStatus(EXTI_Line6))
{
GPIO_Write(GPIOB,~(0x20>>2));
EXTI_ClearITPendingBit(EXTI_Line6);
}

if(EXTI_GetFlagStatus(EXTI_Line7))
{
num ++;
GPIO_Write(GPIOC,table[num%10]);
EXTI_ClearITPendingBit(EXTI_Line7);
}

}

定时器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// main.c
#include "stm32f10x.h"
#include "delay.h"
#include "led.h"
#include "timer.h"

int main()
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);

delay_init();
led_init();
TIM1_init(500-1,7200-1);
TIM2_init(1000-1,7200-1);
LED1 = LED2 = LED3 = LED4 = 1;
}

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
// timer.c
#include "stm32f10x.h"
#include "timer.h"
#include "led.h"

void TIM1_init(u16 arr,u16 psc)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_Period = arr;
TIM_TimeBaseStructure.TIM_Prescaler = psc;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, & TIM_TimeBaseStructure);

TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);

NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);

TIM_Cmd(TIM2,ENABLE);
}

void TIM2_init(u16 arr,u16 psc)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_Period = arr;
TIM_TimeBaseStructure.TIM_Prescaler = psc;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, & TIM_TimeBaseStructure);

TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);

NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);

TIM_Cmd(TIM3,ENABLE);
}

void TIM2_IRQHandler()
{
LED1 = ~LED1;
TIM_ClearFlag(TIM2,TIM_IT_Update);
}

void TIM3_IRQHandler()
{
LED2 = ~LED2;
TIM_ClearFlag(TIM3,TIM_IT_Update);
}

PWM

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//main.c
#include "stm32f10x.h"
#include "motor.h"
#include "key.h"
#include "timer.h"

int CMP = 100;
int main()
{

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);

motor_init();

timer_init(500-1,7200-1);
TIM_SetCompare1(TIM1,CMP-1);

key_init();
while(1){}


}

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
//motor.h
#ifndef __MOTOR_H
#define __MOTOR_H

#include "sys.h"

#define Motor1 PBout(13)
#define Motor2 PBout(14)

void motor_init(void);

#endif

//motor.c
#include "stm32f10x.h"
#include "motor.h"

void motor_init(void){

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);

GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);

Motor1 = 1;
Motor2 = 0;

}

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
//key.c
#include "stm32f10x.h"
#include "key.h"
#include "motor.h"
extern int CMP;

void key_init()
{

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);

GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOC, &GPIO_InitStructure);

GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource0);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource1);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource2);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource3);


EXTI_InitTypeDef EXTI_InitStructure;
EXTI_InitStructure.EXTI_Line = EXTI_Line0 | EXTI_Line1 | EXTI_Line2 | EXTI_Line3;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);

NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);

NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;
NVIC_Init(&NVIC_InitStructure);

NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn;
NVIC_Init(&NVIC_InitStructure);

NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQn;
NVIC_Init(&NVIC_InitStructure);

}

void EXTI0_IRQHandler(){
Motor1 = 1;
Motor2 = 0;
EXTI_ClearITPendingBit(EXTI_Line0);
}

void EXTI1_IRQHandler(){
Motor1 = 0;
Motor2 = 1;
EXTI_ClearITPendingBit(EXTI_Line1);
}

void EXTI2_IRQHandler(){
CMP += 100;
CMP = CMP > 500 ? 500 : CMP;
TIM_SetCompare1(TIM1,CMP-1);
EXTI_ClearITPendingBit(EXTI_Line2);
}

void EXTI3_IRQHandler(){
CMP -= 100;
CMP = CMP < 1 ? 1 : CMP;
TIM_SetCompare1(TIM1,CMP-1);
EXTI_ClearITPendingBit(EXTI_Line3);
}


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
//timer.c
#include "stm32f10x.h"
#include "timer.h"

void timer_init(u16 arr,u16 psc)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_Period = arr;
TIM_TimeBaseStructure.TIM_Prescaler = psc;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure);

TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_Pulse = 250;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OC1Init(TIM1,&TIM_OCInitStructure);

TIM_OC1PreloadConfig(TIM1,TIM_OCPreload_Enable);
TIM_CtrlPWMOutputs(TIM1,ENABLE);

TIM_Cmd(TIM1,ENABLE);

}

串口输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// main.c
#include "stm32f10x.h"
#include "usart1.h"
#include "delay.h"
#include "bsp_clkconfig.h"

int main()
{

HSE_SetSysClock(RCC_PLLMul_7);
delay_init();
usart1_init(9600);
while(1){
usart1_sendString("221305030\r\n");
delay_ms(200);
}
}

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
// usart1.c
#include "stm32f10x.h"
#include "usart1.h"

void usart1_init(u32 bound)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO |
RCC_APB2Periph_USART1 , ENABLE);

GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);

USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = bound;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl =
USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
USART_Init(USART1, &USART_InitStructure);

USART_Cmd(USART1,ENABLE);
}

void usart1_sendChar(char ch)
{
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==0) {;}
USART_SendData(USART1,ch);
}

void usart1_sendString(char ch[])
{
for(int i = 0;ch[i];i++){
usart1_sendChar(ch[i]);
}
}

ADC

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// main.c
#include "stm32f10x.h"
#include "adc.h"
#include "OLED.h"
int main()
{
u16 adc_val;
delay_init();
adcInit();
LCD_Init();

while(1)
{
adc_val = get_ADC1_Avg(ADC_Channel_1,5);
Draw_number(adc_val,0,0);
delay_ms(20);
}
}

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
// adc.c
#include "stm32f10x.h"
#include "adc.h"
#include "delay.h"
void adcInit(){

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_ADC1,ENABLE);

GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
RCC_ADCCLKConfig(RCC_PCLK2_Div6);

ADC_DeInit(ADC1);

ADC_InitTypeDef ADC_InitStructure;
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConv =
ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);

ADC_Cmd(ADC1,ENABLE);

ADC_ResetCalibration(ADC1);
// while(!ADC_GetResetCalibrationStatus(ADC1));
delay_ms(20);

ADC_StartCalibration(ADC1);
//while(!ADC_GetCalibrationStatus(ADC1));
delay_ms(20);

ADC_SoftwareStartConvCmd(ADC1,ENABLE);
}

u16 get_ADC1_Val(u8 ch){
ADC_RegularChannelConfig(ADC1, ch, 1, ADC_SampleTime_239Cycles5);
ADC_SoftwareStartConvCmd(ADC1,ENABLE);
//while(!ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC));
delay_ms(20);
return ADC_GetConversionValue(ADC1);
}

u16 get_ADC1_Avg(u8 ch,u8 t){
u16 avg = 0;
for(int i = 0;i < t;i++){
avg += get_ADC1_Val(ch);
delay_ms(20);
}
return avg/t;
}