zoukankan      html  css  js  c++  java
  • 使用寄存器点亮LED

    1. 项目:使用stm32寄存器点亮LED, 分别点亮红、绿、蓝3个灯。

    2. 代码:

      只需要编写main.c程序,stm3210x.h程序为空(只需要新建即可)。

      2.1 点亮绿灯main.c程序

    #include "stm32f10x.h"
    
    int main(void)
    {
    	//打开GPIOB端口的时钟
    	*(unsigned int * )0x40021018 |= ((1)<<3);	//置位1
    
    	//配置IO口为输出,设置PB0输出低电平,点亮绿灯
    	*(unsigned int * )0x40010c00 |= ((1)<<(4*0));	//置位1
    	//控制ODR寄存器
    	*(unsigned int *)0x40010c0c &= ~(1<<0);	//清0	
    	
    }
    
    void SystemInit(void)
    {
    	//函数体为空给,为了骗过编译器不报错
    }
    

      

       2.2 点亮红灯main.c程序

      这里只需要修改port口PB5输出低电平即可

    #include "stm32f10x.h"
    
    int main(void)
    {
    	//打开GPIOB端口的时钟
    	*(unsigned int * )0x40021018 |= ((1)<<3);	//置位1
    	//配置IO口为输出,设置PB5输出低电平,点亮红灯
    	*(unsigned int*)0x40010c00 |= ((1)<<(4*5));	//置位1
    	//控制ODR寄存器
    	*(unsigned int *)0x40010c0c &= ~(1<<0);	//清0
    }
    
    void SystemInit(void)
    {
    	//函数体为空给,为了骗过编译器不报错
    }
    

      2.3 点亮蓝灯main.c程序

    #include "stm32f10x.h"
    
    int main(void)
    {
    	//打开GPIOB端口的时钟
    	*(unsigned int * )0x40021018 |= ((1)<<3);	//置位1
    	//配置IO口为输出,设置PB1输出低电平,点亮蓝灯
    	*(unsigned int*)0x40010c00 |= ((1)<<(4*1));	//置位1
    	//控制ODR寄存器
    	*(unsigned int *)0x40010c0c &= ~(1<<0);	//清0
    }
    
    void SystemInit(void)
    {
    	//函数体为空给,为了骗过编译器不报错
    }
    

    3. 思路总结

      stm32点亮LED需要3步:

        (1)打开GPIO端口时钟

        (2)配置GPIO工作模式/速度,输入/输出

        (3)控制GPIO输出高低电平

        

       

     

         

      

  • 相关阅读:
    mac c++编译出现segmentation fault :11错误
    ssh 连接缓慢解决方法
    237. Delete Node in a Linked List
    203. Remove Linked List Elements
    Inversion of Control Containers and the Dependency Injection pattern
    82. Remove Duplicates from Sorted List II
    83. Remove Duplicates from Sorted List
    SxsTrace
    使用CCleaner卸载chrome
    decimal and double ToString problem
  • 原文地址:https://www.cnblogs.com/kevin-hou1991/p/15284309.html
Copyright © 2011-2022 走看看