接下来会长时间更新与单片机控制相关的代码 元件使用方案 首先是目前所学到的知识的整理 :
1.引脚查询 读表 不做更多的赘述 未来会使用STMCUBE 简化读表和对引脚调度的方案
2.引脚定义 也不做更多的赘述
3.引脚使能方案

1)点灯引脚 对引脚高低电平使能方案

注 本代码实现的为市面上常见的stm32c8t6最小系统板上自带LED模块的简单使能以及点亮/熄灭操作

 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
#include "stm32f10x.h"                  // Device header
 
void LEDonStm32_Init(void)
{
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);    开启时钟 PB2 GPIOC
    GPIO_InitTypeDef GPIO_InitStructure;                                 结构体声明
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;     推挽输出      
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;                      该片区引脚号
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;    引脚速度(时钟相关)
    GPIO_Init(GPIOC, &GPIO_InitStructure);                  //以上六行代码为配置单片机上的LED的基本配置文件
    GPIO_SetBits(GPIOC,GPIO_Pin_13); 
}
 
        *2下方的代码基本上为对上方代码的使用 不必详究。
void LEDonStm32_off(void)
{
    GPIO_SetBits(GPIOC,GPIO_Pin_13);        //setbits设置为高电平熄灭
}
 
void LEDonStm32_on(void)
{
    GPIO_ResetBits(GPIOC,GPIO_Pin_13);   // reset重置 点亮
}
 
void LEDonStm32_turn(void)         //if else 函数控制下的翻转操作
{
    if (GPIO_ReadOutputDataBit(GPIOC,GPIO_Pin_13) == 0)
    {
        GPIO_SetBits(GPIOC,GPIO_Pin_13);
    }
    else
    {
        GPIO_ResetBits(GPIOC,GPIO_Pin_13);
    }
}

2)pwm 脉宽调制波 定时器输出

 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
#include "stm32f10x.h"                  // Device header
 
// 配置pwm
void Pwm_Init(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);               使能TIM2定时器          
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);             使能GPIOA区时钟信号
 
GPIO_InitTypeDef GPIO_InitStructure1;
GPIO_InitStructure1.GPIO_Mode = GPIO_Mode_AF_PP;                           复用推挽输出
GPIO_InitStructure1.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 |  GPIO_Pin_2 | GPIO_Pin_3;   
使能四个端口
 
//此处更改位置
GPIO_InitStructure1.GPIO_Speed = GPIO_Speed_50MHz;                         速度
GPIO_Init(GPIOA, &GPIO_InitStructure1);
 
以下为定时器结构体 
TIM_InternalClockConfig(TIM2);
 
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;                                       
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_Period = 20000 - 1;       //ARR
TIM_TimeBaseInitStructure.TIM_Prescaler = 72 - 1;       //PSC
TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure);
 
TIM_OCInitTypeDef TIM_OCInitStructure1;
TIM_OCStructInit(&TIM_OCInitStructure1);
TIM_OCInitStructure1.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure1.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure1.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure1.TIM_Pulse = 0;                     //CCR
TIM_OC1Init(TIM2, &TIM_OCInitStructure1);     //此处更改定时器频道    ********
TIM_OC1PreloadConfig(TIM2,TIM_OCPreload_Enable);
 
TIM_OCInitTypeDef TIM_OCInitStructure2;
TIM_OCStructInit(&TIM_OCInitStructure2);
TIM_OCInitStructure2.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure2.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure2.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure2.TIM_Pulse = 0;                     //CCR
TIM_OC2Init(TIM2, &TIM_OCInitStructure2);     //此处更改定时器频道
TIM_OC2PreloadConfig(TIM2,TIM_OCPreload_Enable);     //这行没啥用
 
TIM_OCInitTypeDef TIM_OCInitStructure3;
TIM_OCStructInit(&TIM_OCInitStructure3);
TIM_OCInitStructure3.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure3.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure3.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure3.TIM_Pulse = 0;                     //CCR
TIM_OC3Init(TIM2, &TIM_OCInitStructure3);     //此处更改定时器频道
TIM_OC3PreloadConfig(TIM2,TIM_OCPreload_Enable);
 
TIM_OCInitTypeDef TIM_OCInitStructure4;
TIM_OCStructInit(&TIM_OCInitStructure4);
TIM_OCInitStructure4.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure4.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure4.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure4.TIM_Pulse = 0;                     //CCR
TIM_OC4Init(TIM2, &TIM_OCInitStructure4);     //此处更改定时器频道
TIM_OC4PreloadConfig(TIM2,TIM_OCPreload_Enable);
 
TIM_Cmd(TIM2, ENABLE);
 
}

/* *分界线 */

以下为封装后的定时器结果导向型函数,不必深究

 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
void PWM_SetCompare11(uint16_t Compare)
{
    TIM_SetCompare1(TIM2, Compare);
}
 
void PWM_SetCompare12(uint16_t Compare)
{
    TIM_SetCompare2(TIM2, Compare);
}
 
void PWM_SetCompare13(uint16_t Compare)
{
    TIM_SetCompare3(TIM2, Compare);
}
 
void PWM_SetCompare14(uint16_t Compare)
{
    TIM_SetCompare4(TIM2, Compare);
}
 
void SG90_SetAngle1(float Angle)
{
    PWM_SetCompare11(Angle / 180 * 2000 + 500);
}
 
void SG90_SetAngle2(float Angle)
{
    PWM_SetCompare12(Angle / 180 * 2000 + 500);
}
 
void SG90_SetAngle3(float Angle)
{
    PWM_SetCompare13(Angle / 180 * 2000 + 500);
}
 
void SG90_SetAngle4(float Angle)
{
    PWM_SetCompare14(Angle / 180 * 2000 + 500);
}