0706_ARM8

练习1:

PWM控制蜂鸣器,风扇,震动马达

pwm.h

cpp 复制代码
#ifndef __UART4_H__
#define  __UART4_H__
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_tim.h"

//蜂鸣器 PB6 TIME4_CH1 初始化
void hal_beep_init();

//风扇
void hal_fan_init();

//马达
void hal_monitor_init();

#endif

pwm.c

cpp 复制代码
#include "pwm.h"

//蜂鸣器 PB6 TIM4_CH1
void hal_beep_init(){

    //rcc章节初始化
    //1.使能gpiob组控制器
    RCC->MP_AHB4ENSETR |= (0x1<<1);
    //2.使能tim4组控制器
    RCC->MP_APB1ENSETR |= (0x1<<2);

    //gpiob组控制器初始化
    //1.设置复用功能模式
    GPIOB->MODER &= (~(0x3<<12));
    GPIOB->MODER |= (0x2<<12);
    //2.设置复用功能为tim4_ch1
    GPIOB->AFRL &= (~(0xf<<24));
    GPIOB->AFRL |= (0x2<<24);

    //tim4章节初始化
    //产生方波 频率1000hz
    //系统提供时钟源位209mhz
    //1.设置预分频器
    TIM4->PSC = 208; //因为时钟频率=fck_psc/(PSC[15:0]+1),所以这里传输208
    //2.设置自动重载值
    TIM4->ARR = 1000; //1000
    //3.设置比较寄存器,控制占空比
    TIM4->CCR1 = 300;  //300
    //4.设置输出比较模式
    TIM4->CCMR1 &= (~(0x7<<4));
    TIM4->CCMR1 &= (~(0x1<<16));
    TIM4->CCMR1 |= (0x6<<4);
    //5.输出比较预加载使能
    TIM4->CCMR1 |= (0x1<<3);
    //6.设置通道1配置为输出
    TIM4->CCMR1 &= (~(0x3<<0));
    //8.设置起始时低电平
    TIM4->CCER |= (0x1<<1);
    //9.使能捕获
    TIM4->CCER |= (0x1<<0); 
    //10.设置自动重载预加载使能
    TIM4->CR1 |= (0x1<<7);
    //11.设置边沿对齐方式
    TIM4->CR1 &= (~(0x3<<5));
    //12.设置递减计数
    TIM4->CR1 |= (0x1<<4);
    //13.设置计数器使能
    TIM4->CR1 |= (0x1<<0);

}
//风扇
void hal_fan_init(){
    //rcc章节初始化
    //1.使能gpioe组控制器
    RCC->MP_AHB4ENSETR |= (0x1<<4);
    //2.使能tim1组控制器
    RCC->MP_APB2ENSETR |= (0x1<<0);

    //gpioe组控制器初始化
    //1.设置复用功能模式
    GPIOE->MODER &= (~(0x3<<18));
    GPIOE->MODER |= (0x2<<18);
    //2.设置复用功能为tim1_ch1
    GPIOE->AFRH &= (~(0xf<<4));
    GPIOE->AFRH |= (0x1<<4);

    //tim1章节初始化
    //产生方波 频率1000hz
    //系统提供时钟源位209mhz
    //1.设置预分频器
    TIM1->PSC = 208; //因为时钟频率=fck_psc/(PSC[15:0]+1),所以这里传输208
    //2.设置自动重载值
    TIM1->ARR = 1000; //1000
    //3.设置比较寄存器,控制占空比
    TIM1->CCR1 = 500;  //500
    //4.设置输出比较模式
    TIM1->CCMR1 &= (~(0x7<<4));
    TIM1->CCMR1 &= (~(0x1<<16));
    TIM1->CCMR1 |= (0x6<<4);
    //5.输出比较预加载使能
    TIM1->CCMR1 |= (0x1<<3);
    //6.设置通道1配置为输出
    TIM1->CCMR1 &= (~(0x3<<0));
    //14.设置主输出使能
    TIM1->BDTR |= (0x1<<15);

    //8.设置起始时低电平
    TIM1->CCER |= (0x1 << 3);
    TIM1->CCER |= (0x1<<1);
    //9.使能捕获
    TIM1->CCER |= (0x1<<0); 
    //10.设置自动重载预加载使能
    TIM1->CR1 |= (0x1<<7);
    //11.设置边沿对齐方式
    TIM1->CR1 &= (~(0x3<<5));
    //12.设置递减计数
    TIM1->CR1 |= (0x1<<4);
    //13.设置计数器使能
    TIM1->CR1 |= (0x1<<0);
    
}

//马达
void hal_monitor_init(){
     //rcc章节初始化
    //1.使能gpiof组控制器
    RCC->MP_AHB4ENSETR |= (0x1<<5);
    //2.使能tim16组控制器
    RCC->MP_APB2ENSETR |= (0x1<<3);

    //gpiof组控制器初始化
    //1.设置复用功能模式
    GPIOF->MODER &= (~(0x3<<12));
    GPIOF->MODER |= (0x2<<12);
    //2.设置复用功能为tim1_ch1
    GPIOF->AFRL &= (~(0xf<<24));
    GPIOF->AFRL |= (0x1<<24);

    //tim16章节初始化
    //产生方波 频率1000hz
    //系统提供时钟源位209mhz
    //1.设置预分频器
    TIM16->PSC = 208; //因为时钟频率=fck_psc/(PSC[15:0]+1),所以这里传输208
    //2.设置自动重载值
    TIM16->ARR = 1000; //1000
    //3.设置比较寄存器,控制占空比
    TIM16->CCR1 = 500;  //500
    //4.设置输出比较模式
    TIM16->CCMR1 &= (~(0x7<<4));
    TIM16->CCMR1 &= (~(0x1<<16));
    TIM16->CCMR1 |= (0x6<<4);
    //5.输出比较预加载使能
    TIM16->CCMR1 |= (0x1<<3);
    //6.设置通道1配置为输出
    TIM16->CCMR1 &= (~(0x3<<0));
    //7.设置主输出使能
    TIM16->BDTR |= (0x1<<15);
    //8.设置起始时低电平
    TIM16->CCER |= (0x1 << 3);
    TIM16->CCER |= (0x1<<1);
    //9.使能捕获
    TIM16->CCER |= (0x1<<0); 
    //10.设置自动重载预加载使能
    TIM16->CR1 |= (0x1<<7);
    //11.设置计数器使能
    TIM16->CR1 |= (0x1<<0);
}

main.c

cpp 复制代码
#include "pwm.h"

int main()
{
    hal_beep_init();
    hal_fan_init();
    hal_monitor_init();
    while(1){

    }
    return 0;
}
相关推荐
创龙科技-黄工11 小时前
【深度剖析】自主可控的全国产方案,基于龙芯LS2K1000LA-i!
arm·龙芯·工业核心板·国产处理器
シ風箏2 天前
Neo4j【环境部署 02】图形数据库Neo4j在Linux系统ARM架构下的安装使用
linux·数据库·arm·neo4j
徐某人..5 天前
ARM嵌入式学习--第八天(PWM)
arm开发·学习·arm
徐某人..5 天前
ARM嵌入式学习--第七天(GPT)
学习·arm
whik11949 天前
Keil-MDK开发环境编译后axf自动转换bin格式文件
stm32·arm·keil·axi·bin·mdk
编程零零七10 天前
【Python】tensorflow中的argmax()函数
开发语言·python·信息可视化·数据分析·arm·python学习·python数据可视化
嵌入式小能手13 天前
shell编程入门之提取字符并设置rtc时间
linux·单片机·嵌入式硬件·arm
爱喝纯牛奶的柠檬16 天前
基于stm23的智慧宿舍系统 (DAY10)_小程序
stm32·嵌入式硬件·物联网·阿里云·小程序·arm
嵌入式小能手17 天前
自动化点亮LED灯之shell点灯
linux·单片机·嵌入式硬件·arm
白书宇23 天前
28.100ASK_T113-PRO Linux+QT 显示一张照片
linux·开发语言·arm开发·嵌入式硬件·qt·arm·硬件工程