2021/10/13 智能家具嵌入式实训第三天 位操作(2)

位操作原理

 

 

 

 

 

 哪些区域支持位操作

 

 

 sys.h

 

 

 https://download.csdn.net/download/qq_35629971/32114714

stm32工程模板里面有sys.h

 

编写跑马灯 位操作代码

 

 

 

 

 

 

先led.c

运用寄存器初始化使能

 

#include "led.h"


//初始化PE5,4,3,2为输出口.并使能这四个口的时钟            
//LED IO初始化
void LED_Init(void)
{
 
 GPIO_InitTypeDef  GPIO_InitStructure;
     
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);     //使能PB,PE端口时钟
    
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;                 //LED1-->PE.5 端口配置, 推挽输出
 GPIO_Init(GPIOE, &GPIO_InitStructure);                       //推挽输出 ,IO口速度为50MHz
 GPIO_SetBits(GPIOE,GPIO_Pin_5);                          //PE.5 输出高 
    
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;                 //LED1-->PE.4 端口配置, 推挽输出
 GPIO_Init(GPIOE, &GPIO_InitStructure);                       //推挽输出 ,IO口速度为50MHz
 GPIO_SetBits(GPIOE,GPIO_Pin_4);                          //PE.4 输出高 
    
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;                 //LED1-->PE.3 端口配置, 推挽输出
 GPIO_Init(GPIOE, &GPIO_InitStructure);                       //推挽输出 ,IO口速度为50MHz
 GPIO_SetBits(GPIOE,GPIO_Pin_3);                          //PE.3 输出高 
    
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;                 //LED1-->PE.2 端口配置, 推挽输出
 GPIO_Init(GPIOE, &GPIO_InitStructure);                       //推挽输出 ,IO口速度为50MHz
 GPIO_SetBits(GPIOE,GPIO_Pin_2);                          //PE.2 输出高 
}
 

 

led.h

运用位操作

定义out

 

#ifndef __LED_H
#define __LED_H     
#include "sys.h"

#define LED1 PEout(2)// PE2    
#define LED2 PEout(3)// PE3    
#define LED3 PEout(4)// PE4    
#define LED4 PEout(5)// PE5    

void LED_Init(void);//初始化

                             
#endif

 

最后编写main函数

#include "stm32f10x.h"
#include "led.h"
#include "delay.h"

int main(void){
    delay_init(10);
    LED_Init();
    while(1){
        
        PEout(2)=1;  //LED1  低电平 灭
        PEout(3)=1;
        PEout(4)=1;
        PEout(5)=1;
        
    delay_ms(500);
        
        PEout(2)=0;  //LED1   高电平 亮
        PEout(3)=0;
        PEout(4)=0;
        PEout(5)=0;
        
            delay_ms(500);
    }
}

 

源代码:

https://wwa.lanzoui.com/i2mOxvcjcsh

 

posted @ 2021-10-14 21:11  halfup  阅读(41)  评论(0编辑  收藏  举报