2022/12/26 ARM点灯实验(C语言实现)

头文件

#ifndef __LED_H__                                              
#define __LED_H__                                              
                                                               
typedef struct                                                 
{                                                              
    volatile unsigned int MODER;                               
    volatile unsigned int OTYPER;                              
    volatile unsigned int OSPEEDR;                             
    volatile unsigned int PUPDR;                               
    volatile unsigned int IDR;                                 
    volatile unsigned int ODR;                                 
}gpio_t;                                                       
#define RCC_AHB4_ENSETE (*(volatile unsigned int*)0x50000A28)  
#define GPIOE ((gpio_t*)0x50006000)                            
#define GPIOF ((gpio_t*)0x50007000)                            
                                                               
//LED1初始化                                                   
void led1_init();                                              
//LED1点亮                                                     
void led1_on();                                                
//LED1熄灭                                                     
void led1_off();                                               
                                                               
//LED2初始化                                                   
void led2_init();                                              
//LED2点亮                                                     
void led2_on();                                                
//LED2熄灭                                                     
void led2_off();                                               
                                                               
//LED3初始化                                                   
void led3_init();                                              
//LED3点亮                                                     
void led3_on();                                                
//LED3熄灭                                                     
void led3_off();                                               
#endif                                                         

功能文件

#include "led.h"                           
                                           
//LED1初始化                               
void led1_init()                           
{                                          
    RCC_AHB4_ENSETE |= (0x1 << 4);         
    GPIOE->MODER &= (~(0x3 << 20));        
    GPIOE->MODER |= (0x1 << 20);           
    GPIOE->OTYPER &= (~(0x1 << 10));       
    GPIOE->OSPEEDR &= (~(0x3 << 20));      
}                                          
//LED1点亮                                 
void led1_on()                             
{                                          
    GPIOE->ODR |= (0x1 << 10);             
}                                          
//LED1熄灭                                 
void led1_off()                            
{                                          
    GPIOE->ODR &= (~(0x1 << 10));          
}                                          
                                           
//LED2初始化                               
void led2_init()                           
{                                          
    RCC_AHB4_ENSETE |= (0x1 << 5);         
    GPIOF->MODER &= (~(0x3 << 20));        
    GPIOF->MODER |= (0x1 << 20);           
    GPIOF->OTYPER &= (~(0x1 << 10));       
    GPIOF->OSPEEDR &= (~(0x3 << 20));      
}                                          
//LED2点亮                                 
void led2_on()                             
{                                          
    GPIOF->ODR |= (0x1 << 10);             
}                                          
//LED2熄灭                                 
void led2_off()                            
{                                          
    GPIOF->ODR &= (~(0x1 << 10));          
}                                          
                                           
//LED3初始化                               
void led3_init()                           
{                                          
    RCC_AHB4_ENSETE |= (0x1 << 4);         
    GPIOE->MODER &= (~(0x3 << 16));        
    GPIOE->MODER |= (0x1 << 16);           
    GPIOE->OTYPER &= (~(0x1 << 10));       
    GPIOE->OSPEEDR &= (~(0x3 << 16));      
}                                          
//LED3点亮                                 
void led3_on()                             
{                                          
    GPIOE->ODR |= (0x1 << 8);              
}                                          
//LED3熄灭                                 
void led3_off()                            
{                                          
    GPIOE->ODR &= (~(0x1 << 8));           
}                                          

测试文件

#include "led.h"
extern void printf(const char *fmt, ...);
void delay_ms(int ms)
{
    int i,j;
    for(i = 0; i < ms;i++)
        for (j = 0; j < 1800; j++);
}


int main()
{
    led1_init();
    led2_init();
    led3_init();
    while(1)
    {
        led1_on();
        delay_ms(1000);
        led1_off();

        led2_on();
        delay_ms(1000);
        led2_off();

        led3_on();
        delay_ms(1000);
        led3_off();
    }
    return 0;
}                                             

你可能感兴趣的:(c语言,开发语言)