STM32_project:led_beep

本文详细介绍了如何在STM32F10x微控制器上使用C语言进行GPIO配置,包括时钟初始化、GPIO模式设置、输出操作以及延时处理,展示了基础的硬件控制功能实现。

代码: 

主要部分:

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

// 给蜂鸣器IO口输出低电平,响,高,不向。
//int main (void)
//{
//    // 开启时钟
//    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,  ENABLE); // rcc外设时钟控制的APB2外设接口, 第一个参数选择点亮那个口,第二个参数使能或者失能
//    // 初始化gpioA的PIN0口
//    GPIO_InitTypeDef GPIO_InitStruct;
//    //GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP; // 推挽输出
//    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_OD ; // 开漏输出,高电平没有驱动能力
//    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
//    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
//    GPIO_Init(GPIOA, &GPIO_InitStruct); // 用结构体初始化gpio口
//    
////  GPIO_SetBits(GPIOA, GPIO_Pin_0);
////  GPIO_ResetBits(GPIOA, GPIO_Pin_0);
////  GPIO_WriteBit(GPIOA, GPIO_Pin_0, Bit_SET); // Bit_RESET
////    GPIO_Write;
//    const int delay_time = 100; // 用来定义常量,有作用域的限制。具有类型检查。
//                                              // const 定义的常量在程序运行时会分配内存,并且具有类型信息不是简单的文本替换。
//   while(1)
//   {
//       GPIO_ResetBits(GPIOA, GPIO_Pin_0);
//       Delay_ms(delay_time);
//       GPIO_SetBits(GPIOA, GPIO_Pin_0);
//       Delay_ms(delay_time);
//       GPIO_WriteBit(GPIOA, GPIO_Pin_0, Bit_RESET); 
//       Delay_ms(delay_time);
//       GPIO_WriteBit(GPIOA, GPIO_Pin_0, Bit_SET); 
//       Delay_ms(delay_time);
//       
//       GPIO_WriteBit(GPIOA, GPIO_Pin_0, (BitAction)0); // Bit_RESET 强制类型转换
//       Delay_ms(delay_time);
//       GPIO_WriteBit(GPIOA, GPIO_Pin_0, (BitAction)(1)); // Bit_RESET
//       Delay_ms(delay_time);
//   }
//}

// 常量定义
const int delay_time = 100; // 用来定义常量,有作用域的限制。具有类型检查。
                                                 // const 定义的常量在程序运行时会分配内存,并且具有类型信息不是简单的文本替换。
// 函数声明                                   
void liushui(void); 
void liushui1(void); 
void beep(void);
void led_1(void);

int main (void)
{
    // 开启时钟
    // 可以通过按位或,选择多个引脚。
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB,  ENABLE); // rcc外设时钟控制的APB2外设接口, 第一个参数选择点亮那个口,第二个参数使能或者失能
    // 初始化gpioA的PIN0口
    GPIO_InitTypeDef GPIO_InitStruct_A;
    GPIO_InitStruct_A.GPIO_Mode = GPIO_Mode_Out_PP; // 推挽输出
    //GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_OD ; // 开漏输出,高电平没有驱动能力
    GPIO_InitStruct_A.GPIO_Pin = GPIO_Pin_All;    // PIN0 ~ PIN15 选中所有引脚。
    GPIO_InitStruct_A.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStruct_A); // 用结构体初始化gpio口
    
    // 初始化gpioB的PIN口
    GPIO_InitTypeDef GPIO_InitStruct_B;
    GPIO_InitStruct_B.GPIO_Mode = GPIO_Mode_Out_PP; // 推挽输出
    GPIO_InitStruct_B.GPIO_Pin = GPIO_Pin_All;    // PIN0 ~ PIN15 选中所有引脚。
    GPIO_InitStruct_B.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB, &GPIO_InitStruct_B); // 用结构体初始化gpio口
    
//  GPIO_SetBits(GPIOA, GPIO_Pin_0);
//  GPIO_ResetBits(GPIOA, GPIO_Pin_0); // 可以同时设置多个引脚
//  GPIO_WriteBit(GPIOA, GPIO_Pin_0, Bit_SET); // Bit_RESET
//  GPIO_Write;

   while(1)
   {
       liushui();
       beep()  ;
       liushui1();
       beep()  ;
       led_1();
   }
}

void led_1()
{
    int led1 [4] = {0x0081, 0x0042, 0x0024, 0x0018};
    int i;
    for(i=0; i <=3 ; i++) 
    {
        GPIO_Write(GPIOA, ~led1[i]);
        Delay_ms(delay_time);
    }
    beep();
    for(i=3; i >= 0 ; i--) 
    {
        GPIO_Write(GPIOA, ~led1[i]);
        Delay_ms(delay_time);
    }
    beep();
}

void beep()
{
   GPIO_ResetBits(GPIOB, GPIO_Pin_12); // 响
   Delay_ms(delay_time);
   GPIO_SetBits(GPIOB, GPIO_Pin_12);
   Delay_ms(delay_time);
   GPIO_ResetBits(GPIOB, GPIO_Pin_12); // 响
   Delay_ms(delay_time);
   GPIO_SetBits(GPIOB, GPIO_Pin_12);
}

void liushui()
{
    GPIO_Write(GPIOA, ~0x0001);
    Delay_ms(delay_time);
    GPIO_Write(GPIOA, ~0x0002);
    Delay_ms(delay_time);
    GPIO_Write(GPIOA, ~0x0004);
    Delay_ms(delay_time);
    GPIO_Write(GPIOA, ~0x0008);
    Delay_ms(delay_time);
    GPIO_Write(GPIOA, ~0x0010);
    Delay_ms(delay_time);
    GPIO_Write(GPIOA, ~0x0020);
    Delay_ms(delay_time);
    GPIO_Write(GPIOA, ~0x0040);
    Delay_ms(delay_time);
    GPIO_Write(GPIOA, ~0x0080);
    Delay_ms(delay_time);
}
void liushui1()
{
    GPIO_Write(GPIOA, ~0x0080);
    Delay_ms(delay_time);
    GPIO_Write(GPIOA, ~0x0040);
    Delay_ms(delay_time);
    GPIO_Write(GPIOA, ~0x0020);
    Delay_ms(delay_time);
    GPIO_Write(GPIOA, ~0x0010);
    Delay_ms(delay_time);
    GPIO_Write(GPIOA, ~0x0008);
    Delay_ms(delay_time);
    GPIO_Write(GPIOA, ~0x0004);
    Delay_ms(delay_time);
    GPIO_Write(GPIOA, ~0x0002);
    Delay_ms(delay_time);
    GPIO_Write(GPIOA, ~0x0001);
    Delay_ms(delay_time);
}

Rebuild started: Project: key_led *** Using Compiler 'V5.06 update 6 (build 750)', folder: 'E:\ARM\ARMCC\Bin' Rebuild target 'cjb_key_led' FCARM uses the following Files via command file '.\Auto_FcArm_Cmd.inp': timer.c TO sch_key_led.hex NOPRINT custom translating @.\Auto_FcArm_Cmd.inp... Custom: E:\ARM\Bin\FCARM.exe @.\Auto_FcArm_Cmd.inp FCARM FILE CONVERTER V2.57 Completed 1 File(s), Ratio 100% compiling sch_key_led... Error: C4065E: type of input file 'sch_key_led.hex' unknown assembling startup_stm32f10x_hd.s... compiling sch_key_led.hex... Error: C4065E: type of input file 'sch_key_led.hex' unknown compiling core_cm3.c... compiling stm32f10x_it.c... compiling system_stm32f10x.c... compiling main.c... timer.h(7): warning: #1-D: last line of file ends without a newline #endif main.c(52): warning: #1-D: last line of file ends without a newline } main.c: 2 warnings, 0 errors compiling stm32f10x_bkp.c... compiling stm32f10x_cec.c... compiling stm32f10x_crc.c... compiling stm32f10x_dac.c... compiling misc.c... compiling stm32f10x_adc.c... compiling stm32f10x_can.c... compiling stm32f10x_dbgmcu.c... compiling stm32f10x_exti.c... compiling stm32f10x_dma.c... compiling stm32f10x_fsmc.c... compiling stm32f10x_flash.c... compiling stm32f10x_gpio.c... compiling stm32f10x_iwdg.c... compiling stm32f10x_pwr.c... compiling stm32f10x_i2c.c... compiling stm32f10x_rcc.c... compiling stm32f10x_rtc.c... compiling beep.c... compiling stm32f10x_sdio.c... compiling stm32f10x_wwdg.c... compiling stm32f10x_spi.c... compiling stm32f10x_usart.c... compiling stm32f10x_tim.c... compiling exti.c... compiling key.c... compiling led.c... compiling delay.c... compiling usart.c... compiling sys.c... "..\sch_key_led.hex" - 2 Error(s), 2 Warning(s). Target not created. Build Time Elapsed: 00:00:01
05-30
Rebuild started: Project: templet *** Using Compiler 'V5.06 update 6 (build 750)', folder: 'D:\dpj32\ARM\ARMCC\Bin' Rebuild target 'Target 1' assembling startup_stm32f10x_md.s... compiling beep.c... APP\beep\beep.c(4): warning: #2532-D: support for trigraphs is disabled #define BEEP_SHORT_DURATION 100 // ??????(??:ms) APP\beep\beep.c: 1 warning, 0 errors compiling stm32f10x_rcc.c... compiling core_cm3.c... compiling system_stm32f10x.c... compiling stm32f10x_it.c... compiling stick.c... compiling main.c... User\main.c(16): warning: #223-D: function "TIM_Init" declared implicitly TIM_Init(); User\main.c(23): warning: #223-D: function "STICK_IsPressed" declared implicitly if (STICK_IsPressed()) User\main.c(28): warning: #223-D: function "TIM_GetTime" declared implicitly press_start_time = TIM_GetTime(); User\main.c(36): warning: #223-D: function "TIM_GetTime" declared implicitly uint32_t press_duration = TIM_GetTime() - press_start_time; User\main.c(40): warning: #223-D: function "BEEP_ShortBeep" declared implicitly BEEP_ShortBeep(); User\main.c(45): warning: #223-D: function "BEEP_LongBeep" declared implicitly BEEP_LongBeep(); User\main.c: 6 warnings, 0 errors compiling systm.c... compiling stm32f10x_tim.c... compiling led.c... compiling misc.c... compiling time.c... compiling stm32f10x_gpio.c... compiling SysTick.c... linking... Program Size: Code=1632 RO-data=268 RW-data=4 ZI-data=1028 FromELF: creating hex file... ".\Obj\templet.axf" - 0 Error(s), 7 Warning(s). Build Time Elapsed: 00:00:01
12-01
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值