打印
[其他ST产品]

newlab平台stm32总结

[复制链接]
975|59
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
一、GPIO的输出
1.时钟设置
2.调用初始化函数
3.输出函数
(1)GPIO_WriteBit(端口,引脚,(BitAction)(0));//低
GPIO_WriteBit(端口,引脚,(BitAction)(1));//高
(2)GPIO_SetBits(端口,引脚); //低
GPIO_ResetBits端口,引脚); //高
(3)PAout(n) = 1:PAn;//输出高
PAout(n) = 0;//输出低
P端口out(引脚) = 0|1
(4)PAin(n) = 1;//输入高
PAin(n) = 0;//输入低
P端口in(引脚) = 0|1

使用特权

评论回复
评论
纠结的那些年 2023-8-26 17:57 回复TA
———————————————— 版权声明:本文为CSDN博主「Baal Austin」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/weixin_44529350/article/details/109163581 
沙发
纠结的那些年|  楼主 | 2023-8-26 17:57 | 只看该作者
void Init(void){ //初始化
        GPIO_InitTypeDef  GPIO_InitStructure;        
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//参数一:GPIOB口输出,参数二:使能      
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;//B0和B1                        
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //输出频率
        GPIO_Init(LEDPORT, &GPIO_InitStructure);//初始化                       
}

使用特权

评论回复
板凳
纠结的那些年|  楼主 | 2023-8-26 17:57 | 只看该作者
用途
例一:
点亮led
步骤:
1.初始化时钟
2.初始化PB0,1
3.调用GPIO输出函数
连接方式:
位数码管连接3.3v
段数码管连接PB0

使用特权

评论回复
地板
纠结的那些年|  楼主 | 2023-8-26 17:57 | 只看该作者


#define LEDPORT        GPIOB       
#define LED1        GPIO_Pin_0       
#define LED2        GPIO_Pin_1       
void LED_Init(void){
        GPIO_InitTypeDef  GPIO_InitStructure;        
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);      
    GPIO_InitStructure.GPIO_Pin = LED1 | LED2;                       
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;     
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;     
        GPIO_Init(LEDPORT, &GPIO_InitStructure);                       
}

int Led1(){
        RCC_Configuration();
        LED_Init();
        while(1){
                GPIO_WriteBit(LEDPORT,LED1,(BitAction)(0));//低电平亮,高电平灭
                delay_ms(100);
                GPIO_WriteBit(LEDPORT,LED1,(BitAction)(1));
                delay_ms(100);
        }
}

使用特权

评论回复
5
纠结的那些年|  楼主 | 2023-8-26 17:58 | 只看该作者
按键控制Led
步骤:
1.初始化按键
2.获取按键电平
GPIO_ReadInputDataBit(KEYPORT1,KEY1)
按下返回低电平0,松开返回高电平1

使用特权

评论回复
6
纠结的那些年|  楼主 | 2023-8-26 17:58 | 只看该作者
连接方式:
//按键1 C13
#define KEYPORT1        GPIOC       
#define KEY1        GPIO_Pin_13       

//按键2 D13
#define KEYPORT2 GPIOD
#define KEY2        GPIO_Pin_13
void KEY1_Init(void){
        GPIO_InitTypeDef  GPIO_InitStructure;
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);      
    GPIO_InitStructure.GPIO_Pin = KEY1;      
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;      
//    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;   
        GPIO_Init(KEYPORT1,&GPIO_InitStructure);                       
}

void KEY2_Init(void){
        GPIO_InitTypeDef  GPIO_InitStructure;
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE);      
    GPIO_InitStructure.GPIO_Pin = KEY2;               
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;  
//    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(KEYPORT2,&GPIO_InitStructure);                       
}

使用特权

评论回复
7
纠结的那些年|  楼主 | 2023-8-26 17:58 | 只看该作者
例二:蜂鸣器
步骤:
1.初始化时钟
2.初始化PB5
3.调用GPIO输出函数
连接方式:

使用特权

评论回复
8
纠结的那些年|  楼主 | 2023-8-26 17:58 | 只看该作者
#define BUZZERPORT        GPIOB
#define BUZZER        GPIO_Pin_5
void BUZZER_Init(void){
        GPIO_InitTypeDef  GPIO_InitStructure;        
    GPIO_InitStructure.GPIO_Pin = BUZZER;                     
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;      
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(BUZZERPORT, &GPIO_InitStructure);       
        GPIO_WriteBit(BUZZERPORT,BUZZER,(BitAction)(1));               
}
void BUZZER_BEEP1(void){
        u16 i;
        for(i=0;i<200;i++){
                GPIO_WriteBit(BUZZERPORT,BUZZER,(BitAction)(0));
                delay_us(500);
                GPIO_WriteBit(BUZZERPORT,BUZZER,(BitAction)(1));
                delay_us(500);               
        }
}

使用特权

评论回复
9
纠结的那些年|  楼主 | 2023-8-26 17:59 | 只看该作者
例三:点亮动态数码管
步骤:
1.初始化时钟
2.初始化PB0,1,2,3,4,5,6,7
3.调用GPIO输出函数
连接方式:
数码管段选输出口为PA0-7,位选输出口为PB10-13 PC6-9

使用特权

评论回复
10
纠结的那些年|  楼主 | 2023-8-26 17:59 | 只看该作者
void GPIO_Configuration(void)//时钟配置子程序
{
        GPIO_InitTypeDef GPIO_InitStructure;//声明GPIO初始化结构变量
       
        GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
        GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_Pin=GPIO_Pin_7|GPIO_Pin_6|GPIO_Pin_5|GPIO_Pin_4|GPIO_Pin_3|GPIO_Pin_2|GPIO_Pin_1|GPIO_Pin_0;
        GPIO_Init(GPIOA,&GPIO_InitStructure);
       
        GPIO_SetBits(GPIOA,GPIO_InitStructure.GPIO_Pin);
       
        GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12|GPIO_Pin_13;
        GPIO_Init(GPIOB,&GPIO_InitStructure);
        GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9|GPIO_Pin_8|GPIO_Pin_7|GPIO_Pin_6;
        GPIO_Init(GPIOC,&GPIO_InitStructure);
       
        GPIO_SetBits(GPIOB,GPIO_InitStructure.GPIO_Pin);

}

void Show_Num(double num){
       
                double num1 = num;
                unsigned char index = 0;
                unsigned int n = 1;
                unsigned int num2;
                unsigned char i;
                while((unsigned int)num1 != num1&&index < 4){
                        index++;
                        num1 = num1*10;
                        n = n*10;
                }
                num2 = num1;
                LedOut[0]=Disp_Tab[num2/10000000%10];
                LedOut[1]=Disp_Tab[num2/1000000%10];   
                LedOut[2]=Disp_Tab[num2/100000%10];       
                LedOut[3]=Disp_Tab[num2/10000%10];         
       
                LedOut[4]=Disp_Tab[num2/1000%10];
                LedOut[5]=Disp_Tab[num2/100%10];
                LedOut[6]=Disp_Tab[num2/10%10];
                LedOut[7]=Disp_Tab[num2%10];     
                LedOut[7-index]=Disp_Tab1[num2/n%10];
               
               
               
                for(i=0; i<8; i++)
                {               
                        GPIO_Write(GPIOA , LedOut[i])        ;       
                        LED1(0);LED2(0);LED3(0);LED4(0);LED5(0);LED6(0);LED7(0);LED8(0);
                        switch(i)                                          
                        {
                                case 0:       
                                        LED1(1);LED2(0);LED3(0);LED4(0);LED5(0);LED6(0);LED7(0);LED8(0);
                                        break;         
                                case 1:
                                        LED1(0);LED2(1);LED3(0);LED4(0);LED5(0);LED6(0);LED7(0);LED8(0);
                                        break;                    
                                case 2:
                                        LED1(0);LED2(0);LED3(1);LED4(0);LED5(0);LED6(0);LED7(0);LED8(0);
                                        break;
                                case 3:
                                        LED1(0);LED2(0);LED3(0);LED4(1);LED5(0);LED6(0);LED7(0);LED8(0);
                                        break;
                                case 4:       
                                        LED1(0);LED2(0);LED3(0);LED4(0);LED5(1);LED6(0);LED7(0);LED8(0);
                                        break;
                                case 5:       
                                        LED1(0);LED2(0);LED3(0);LED4(0);LED5(0);LED6(1);LED7(0);LED8(0);
                                        break;
                                case 6:
                                        LED1(0);LED2(0);LED3(0);LED4(0);LED5(0);LED6(0);LED7(1);LED8(0);
                                        break;
                                case 7:       
                                        LED1(0);LED2(0);LED3(0);LED4(0);LED5(0);LED6(0);LED7(0);LED8(1);
                                        break;
                         }
                        delay_nms(1);
                }
       
}

使用特权

评论回复
11
纠结的那些年|  楼主 | 2023-8-26 17:59 | 只看该作者
#include "stm32f10x.h"                  // Device header
#include "dynamic_seg_led.h"
int main(void)
{
        RCC_Configuration1();//配置时钟
        GPIO_Configuration();//配置GPIO口
        LED1(0);LED2(0);LED3(0);LED4(0);LED5(0);LED6(0);LED7(0);LED8(0);//熄灭所有数码管
        while(1){
          Show_Num(1234.5678);//显示想显示的数
        }
}

使用特权

评论回复
12
纠结的那些年|  楼主 | 2023-8-26 17:59 | 只看该作者
例四: 驱动灯泡,风扇,门锁
步骤:
与例一相同
线路连接:
GPIO输出口连接继电器J2(J5),J9(J12)连接设备正极,J8(J11)连接电源正极12V,设备负极连接电源负极

使用特权

评论回复
13
纠结的那些年|  楼主 | 2023-8-26 18:00 | 只看该作者
例五: 键盘模块
步骤:
1.时钟设置
2.44或55键盘初始化
3.获取按键信息,如将获取的信息显示在数码管上
连线:
4*4键盘
COL0-COL3 PB10-PB13
ROW0-ROW3 PC6-PC9

使用特权

评论回复
14
纠结的那些年|  楼主 | 2023-8-26 18:00 | 只看该作者

使用特权

评论回复
15
纠结的那些年|  楼主 | 2023-8-26 18:00 | 只看该作者
5*5键盘
ROW0-ROW4 PA0-PA4
COL4-COL0 PB14-PB10

使用特权

评论回复
16
纠结的那些年|  楼主 | 2023-8-26 18:00 | 只看该作者

使用特权

评论回复
17
纠结的那些年|  楼主 | 2023-8-26 18:00 | 只看该作者
//键盘初始化
void GPIO_Configuration55(void)
{

       
                GPIO_InitTypeDef GPIO_InitStructure;//声明GPIO初始化结构变量
                RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //开启PA时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //开启PB时钟
                RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); //开启PC时钟
               
                GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_3|GPIO_Pin_2|GPIO_Pin_1|GPIO_Pin_0;
                GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;  //配置成推挽输出
                GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //输出速度 50M HZ
                GPIO_Init(GPIOA, &GPIO_InitStructure);  //初始化PA口

          GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 |  GPIO_Pin_11 |  GPIO_Pin_12 |  GPIO_Pin_13 |  GPIO_Pin_14;  
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;  //上拉输入
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //
    GPIO_Init(GPIOB, &GPIO_InitStructure);  //初始化PB口
                GPIO_Write(GPIOB, 0x7C00); //上拉输入需要把对应的ODR置1

}

/*
5*5矩阵按键设置
*/
int  keyscan55(void)
{        
         ROW14(0);ROW13(0);ROW12(0);ROW11(0);ROW10(0);//ÐÐÏßÀ­µÍ
//                      |                        |                        |                        |     |
// 0 PA0 ---0-----1-----2-----3-----4-- |                  |--------|
//                      |                        |                        |                        |     |   --------
// 0 PA1 ---5-----6-----7-----8-----9--
//                      |                        |                        |                        |     |
// 0 PA2 ---A-----B-----C-----D-----E--
//                      |                        |                        |                        |     |
// 0 PA3 ---F-----H-----I-----J-----K--
//                      |                        |                        |                        |     |
// 0 PA4 ---L-----M-----N-----O-----P--
//          |     |     |     |     |
//                           PB14 PB13  PB12   PB11  PB10
//   
                if(COL14&&COL13&&COL12&&COL11&&COL10){
                        return -1;
                }
                else
                {
                        delay_nms(5);
                        if(COL14&&COL13&&COL12&&COL11&&COL10)
                                return -1;
                }
               
//                      |                        |                        |                        |     |
// 0 PA0 ---0-----1-----2-----3-----4-- |                  |--------|
//                      |                        |                        |                        |     |   --------
// 1 PA1 ---5-----6-----7-----8-----9--
//                      |                        |                        |                        |     |
// 1 PA2 ---A-----B-----C-----D-----E--
//                      |                        |                        |                        |     |
// 1 PA3 ---F-----H-----I-----J-----K--
//                      |                        |                        |                        |     |
// 1 PA4 ---L-----M-----N-----O-----P--
//          |     |     |     |     |
//                           PB14 PB13  PB12   PB11  PB10
//   
                ROW14(1);ROW13(1);ROW12(1);ROW11(1);ROW10(0);
                if(!COL14){
                        return 0;
                }
                if(!COL13){
                        return 1;
                }
                if(!COL12){
                        return 2;
                }
                if(!COL11){
                        return 3;
                }
                if(!COL10){
                        return 4;
                }
//                      |                        |                        |                        |     |
// 1 PA0 ---0-----1-----2-----3-----4-- |                  |--------|
//                      |                        |                        |                        |     |   --------
// 0 PA1 ---5-----6-----7-----8-----9--
//                      |                        |                        |                        |     |
// 1 PA2 ---A-----B-----C-----D-----E--
//                      |                        |                        |                        |     |
// 1 PA3 ---F-----H-----I-----J-----K--
//                      |                        |                        |                        |     |
// 1 PA4 ---L-----M-----N-----O-----P--
//          |     |     |     |     |
//                           PB14 PB13  PB12   PB11  PB10
//   
                ROW14(1);ROW13(1);ROW12(1);ROW11(0);ROW10(1);
                if(!COL14){
                        return 5;
                }
                if(!COL13){
                        return 6;
                }
                if(!COL12){
                        return 7;
                }
                if(!COL11){
                        return 8;
                }
                if(!COL10){
                        return 9;
                }
               
//                      |                        |                        |                        |     |
// 1 PA0 ---0-----1-----2-----3-----4-- |                  |--------|
//                      |                        |                        |                        |     |   --------
// 1 PA1 ---5-----6-----7-----8-----9--
//                      |                        |                        |                        |     |
// 0 PA2 ---A-----B-----C-----D-----E--
//                      |                        |                        |                        |     |
// 1 PA3 ---F-----H-----I-----J-----K--
//                      |                        |                        |                        |     |
// 1 PA4 ---L-----M-----N-----O-----P--
//          |     |     |     |     |
//                           PB14 PB13  PB12   PB11  PB10
//   

                ROW14(1);ROW13(1);ROW12(0);ROW11(1);ROW10(1);
                if(!COL14){
                        return 10;
                }
                if(!COL13){
                        return 11;
                }
                if(!COL12){
                        return 12;
                }
                if(!COL11){
                        return 13;
                }
                if(!COL10){
                        return 14;
                }
//                      |                        |                        |                        |     |
// 1 PA0 ---0-----1-----2-----3-----4-- |                  |--------|
//                      |                        |                        |                        |     |   --------
// 1 PA1 ---5-----6-----7-----8-----9--
//                      |                        |                        |                        |     |
// 1 PA2 ---A-----B-----C-----D-----E--
//                      |                        |                        |                        |     |
// 0 PA3 ---F-----H-----I-----J-----K--
//                      |                        |                        |                        |     |
// 1 PA4 ---L-----M-----N-----O-----P--
//          |     |     |     |     |
//                           PB14 PB13  PB12   PB11  PB10
//   

                ROW14(1);ROW13(0);ROW12(1);ROW11(1);ROW10(1);
                if(!COL14){
                        return 15;
                }
                if(!COL13){
                        return 16;
                }
                if(!COL12){
                        return 17;
                }
                if(!COL11){
                        return 18;
                }
                if(!COL10){
                        return 19;
                }
//                      |                        |                        |                        |     |
// 1 PA0 ---0-----1-----2-----3-----4-- |                  |--------|
//                      |                        |                        |                        |     |   --------
// 1 PA1 ---5-----6-----7-----8-----9--
//                      |                        |                        |                        |     |
// 1 PA2 ---A-----B-----C-----D-----E--
//                      |                        |                        |                        |     |
// 1 PA3 ---F-----H-----I-----J-----K--
//                      |                        |                        |                        |     |
// 0 PA4 ---L-----M-----N-----O-----P--
//          |     |     |     |     |
//                           PB14 PB13  PB12   PB11  PB10
//   

                ROW14(0);ROW13(1);ROW12(1);ROW11(1);ROW10(1);
                if(!COL14){
                        return 20;
                }
                if(!COL13){
                        return 21;
                }
                if(!COL12){
                        return 22;
                }
                if(!COL11){
                        return 23;
                }
                if(!COL10){
                        return 24;
                }
                return -1;
}
void delay_nms(u16 time)//ÑÓʱ×Ó³ÌÐò
{  u16 i=0;  
   while(time--)
   {  i=12000;  //×Ô¼º¶¨Òå
      while(i--) ;
   }
}

使用特权

评论回复
18
纠结的那些年|  楼主 | 2023-8-26 18:01 | 只看该作者

#include "stm32f10x.h" //STM32
#include "matrix_key.h"
#include "usart.h"
// 此表为LED的字模                    0          1        2    3           4         5        6    7           8         9     a     b           c         d     e     f
unsigned char  LED7Code[] = {~0x3F,~0x06,~0x5B,~0x4F,~0x66,~0x6D,~0x7D,~0x07,~0x7F,~0x6F,~0x77,~0x7C,~0x39,~0x5E,~0x79,~0x71};
int x;
int main(void){
        RCC_Configuration(); //时钟设置
        USART1_Init(115200); //串口初始化波特率
        GPIO_Configuration55();//GPIO_Configuration44();
        GPIO_Write(GPIOA, LED7Code[8]);//数码管全亮
  while(1)
  {
                x=keyscan55();//扫描按键,返回按键值
          //x=keyscan44();)
                printf("%d ",x);//串口输出
                if (x>=0)
                {
                        GPIO_Write(GPIOA, LED7Code[x]);        //显示按键值;                               
                }
  }
}

使用特权

评论回复
19
纠结的那些年|  楼主 | 2023-8-26 18:01 | 只看该作者
二、LCD输出
步骤:
1.初始化LCD显示屏
2.清屏
3.写数据

LCD点阵大小 12864 即8行16列
每一个英文字符占816个字节,即2行1列,两行可以显示16个英文字符
每一个中文字符占16*16个字节,即2行2列,两行可以显示8个中文字符
如果超出不显示,以上为默认情况,其他函数详见LCD12864.c文档
可使用PCTOLCD生成需要的点阵

使用特权

评论回复
20
纠结的那些年|  楼主 | 2023-8-26 18:01 | 只看该作者
//LCD主函数
#include "stm32f10x.h"                  // Device header
#include "stm32f103_config.h"
#include "lcd12864.h"
#include "Font.h"
#include "Image.h"
int main(void)
{
        delay_ms(100);//延时100毫秒
        LCD_Init();/初始化显示屏
        LCD_Clr(); //清屏
        LCD_WriteEnglish(2,0,1+'0');
        LCD_WriteEnglish(2,8,2+'0');
        LCD_WriteEnglish(2,2*8,3+'0');
        LCD_WriteEnglish(2,3*8,4+'0');
        LCD_WriteEnglish(2,4*8,5+'0');
        LCD_WriteEnglishString(2,5*8,(unsigned char *)" end");
        LCD_WriteEnglishString(4,0,(unsigned char *)"0123456789ABCDEF");*/
        LCD_WriteEnglishString(0,0,(unsigned char *)" HUNAN CITY");
        LCD_WriteEnglishString(2,0,(unsigned char *)"  University");
        LCD_WriteChineseString(4,0,(unsigned char *)hanzi,6);
  //LCD_DispImg(2,48,60,60,(unsigned char *)Fig0);       
}

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

36

主题

466

帖子

0

粉丝