江科大STM32-3-3,3-4

本文详细介绍了STM32微控制器中GPIO的使用,包括可变电阻和滤波电容的作用,上拉和下拉模式在不同硬件电路中的应用,以及C语言中数据类型、宏定义、typedef和结构体、枚举的运用,展示了LED闪烁、流水灯、蜂鸣器控制和按键与光敏传感器交互的示例代码。
摘要由CSDN通过智能技术生成

一、

图3
N1为可变电阻,阻值可根据环境的光线温度等进行变化,C2是滤波电容,给中间电压输出进行滤波,用来滤出一些干扰保持电压波形稳定。

当N1阻值变小时,下拉作用会增强,中间的AO端电压会拉低。极端情况下,N1阻值为0,AO输出被完全下拉,输出0V;当N1阻值变大,下拉作用减弱,中间的引脚由于R1的上拉作用,电压会升高;极端情况下,N1阻值无穷大相当于断路,输出电压被R1拉高至VCC。

二、硬件电路

1、按键按下PAO下拉到GND,PAO口读取低电平。(PAO悬空会出现引脚电压不确定)

PAO为上拉输入模式,引脚悬空,PAO为高电平。

上面两种接法按键按下时为低电平(采用上拉)

下面面两种接法按键按下时为高电平(采用下拉)

左边两种接法必须为上拉或下拉模式,右边两种接法可为悬空状态。

三、C语言数据类型

注意stm32中int占32位,如果要用16位数据,要用short表示。

C语言宏定义

关键字:#define

用途:用一个字符串代替一个数字,便于理解,防止出错;提取程序中经常出现的参数,便于快速修改

定义宏定义:

#define ABC 12345

引用宏定义:

int a = ABC; //等效于int a = 12345;

C语言typedef
关键字:typedef

用途:将一个比较长的变量类型名换个名字,便于使用

定义typedef:typedef unsigned char uint8_t;

引用typedef:uint8_t a; //等效于unsigned char a;

C语言结构体

C语言枚举
关键字:enum

用途:定义一个取值受限制的整型变量,用于限制变量取值范围;宏定义的集合。

•定义枚举变量:enum{FALSE = 0, TRUE = 1} EnumName;

因为枚举变量类型较长,所以通常用typedef更改变量类型名。

•引用枚举成员:

EnumName = FALSE;

EnumName = TRUE;

代码

LED闪烁

#include "stm32f10x.h" // Device header #include "Delay.h"

int main(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);

while (1)
{
    GPIO_ResetBits(GPIOA, GPIO_Pin_0);
    Delay_ms(500);
    GPIO_SetBits(GPIOA, GPIO_Pin_0);
    Delay_ms(500);
    
    GPIO_WriteBit(GPIOA, GPIO_Pin_0, Bit_RESET);
    Delay_ms(500);
    GPIO_WriteBit(GPIOA, GPIO_Pin_0, Bit_SET);
    Delay_ms(500);
    
    GPIO_WriteBit(GPIOA, GPIO_Pin_0, (BitAction)0);
    Delay_ms(500);
    GPIO_WriteBit(GPIOA, GPIO_Pin_0, (BitAction)1);
    Delay_ms(500);
}
}

LED流水灯
#include "stm32f10x.h" // Device header #include "Delay.h"

int main(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

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

while (1)
{
    GPIO_Write(GPIOA, ~0x0001); //0000 0000 0000 0001
    Delay_ms(100);
    GPIO_Write(GPIOA, ~0x0002); //0000 0000 0000 0010
    Delay_ms(100);
    GPIO_Write(GPIOA, ~0x0004); //0000 0000 0000 0100
    Delay_ms(100);
    GPIO_Write(GPIOA, ~0x0008); //0000 0000 0000 1000
    Delay_ms(100);
    GPIO_Write(GPIOA, ~0x0010); //0000 0000 0001 0000
    Delay_ms(100);
    GPIO_Write(GPIOA, ~0x0020); //0000 0000 0010 0000
    Delay_ms(100);
    GPIO_Write(GPIOA, ~0x0040); //0000 0000 0100 0000
    Delay_ms(100);
    GPIO_Write(GPIOA, ~0x0080); //0000 0000 1000 0000
    Delay_ms(100);
}
}

蜂鸣器
#include "stm32f10x.h" // Device header #include "Delay.h"

int main(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

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

while (1)
{
    GPIO_ResetBits(GPIOB, GPIO_Pin_12);
    Delay_ms(100);
    GPIO_SetBits(GPIOB, GPIO_Pin_12);
    Delay_ms(100);
    GPIO_ResetBits(GPIOB, GPIO_Pin_12);
    Delay_ms(100);
    GPIO_SetBits(GPIOB, GPIO_Pin_12);
    Delay_ms(700);
}
}

按键控制LED
#include "stm32f10x.h" // Device header #include "Delay.h" #include "LED.h" #include "Key.h"

uint8_t KeyNum;

int main(void) { LED_Init(); Key_Init();

while (1)
{
    KeyNum = Key_GetNum();
    if (KeyNum == 1)
    {
        LED1_Turn();
    }
    if (KeyNum == 2)
    {
        LED2_Turn();
    }
}
}

光敏传感器控制蜂鸣器
#include "stm32f10x.h" // Device header #include "Delay.h" #include "Buzzer.h" #include "LightSensor.h"

int main(void) { Buzzer_Init(); LightSensor_Init();

while (1)
{
    if (LightSensor_Get() == 1)
    {
        Buzzer_ON();
    }
    else
    {
        Buzzer_OFF();
    }
}
}

  • 43
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值