蓝桥杯嵌入式基于HAL库竞赛基础(初学者必看!!!)

本文详细介绍了在嵌入式系统中如何配置和使用各种接口,包括LED控制、LCD显示、按键输入(KEY)、IIC通信、ADC转换、DAC输出、PWM生成与检测以及UART和RTC时钟。通过具体的代码示例展示了这些技术在实际应用中的实现方式。
摘要由CSDN通过智能技术生成

目录

LED

LCD

KEY

IIC

ADC

DAC

PWM

PWM输出

频率检测

PWM检测

UART

RTC


LED

设置:PC8-PC15,GPIO_OUT;PD2,GPIO_OUT。

void LED_Control(int LED1_flag,int LED2_flag)
{
    HAL_GPIO_WritePin(GPIOC,GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15,GPIO_PIN_SET);
    
    HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);
    
    if(LED1_flag == 1)
    {
        HAL_GPIO_WritePin(GPIOC,GPIO_PIN_8,GPIO_PIN_RESET);
    }
    if(LED2_flag == 1)
    {
        HAL_GPIO_WritePin(GPIOC,GPIO_PIN_9,GPIO_PIN_RESET);
    }
    
    HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
}
int main(void)
{
    led_control (led1_flag,led2_flag);
    while(1)
    {
        led_control (led1_flag,led2_flag);
    }
}

LCD

设置:lcd_hal.c/lcd_hal.h/fonts_hal.h分别放到文件夹中,main.c中#include "lcd_hal.h"

LCD屏规格:320✖240(10✖20),每个字符所占像素大小为16✖24

void PSD_UI(char PSD_B1_show,char PSD_B2_show,char PSD_B3_show)
{
    LCD_DisplayStringLine(Line1,(unsigned char *)"       PSD          ");
    
    memset(str,0,20);
    sprintf((char *)str,"    B1:%c            ",PSD_B1_show);
    LCD_DisplayStringLine(Line3,str);
    
    memset(str,0,20);
    sprintf((char *)str,"    B2:%c            ",PSD_B2_show);
    LCD_DisplayStringLine(Line4,str);
    
    memset(str,0,20);
    sprintf((char *)str,"    B3:%c            ",PSD_B3_show);
    LCD_DisplayStringLine(Line5,str);
}
​
void STA_UI(int fre,int duty)
{
    LCD_DisplayStringLine(Line1,(unsigned char *)"       STA          ");
    
    memset(str,0,20);
    sprintf((char *)str,"    F:%dHz        ",fre);
    LCD_DisplayStringLine(Line3,str);
    
    memset(str,0,20);
    sprintf((char *)str,"    D:%d%%           ",duty);
    LCD_DisplayStringLine(Line4,str);
    
    LCD_DisplayStringLine(Line5,(unsigned char *)"                    ");
}
int main(void)
{
    LCD_Init();
    LCD_Clear(Black);
    LCD_SetBackColor(Black);
    LCD_SetTextColor(White);
    
    while(1)
    {
        if(UI_flag == 0)
        {
            PSD_UI(PSD_B1_show,PSD_B2_show,PSD_B3_show);
        }
        else
        {
            STA_UI(fre,duty);
        }
    }
}

KEY

设置:PB0-PB2、PA0,GPIO_IN;Activated TIM16、Enabled NVIC、Parameter settings: Prescarler(计数值): 79 、Counter Period: 9999(每10ms触发一次)


触发周期=(计数值+1)✖(分频+1)/时钟频率(80MHZ)(一定要记住!!!)
 

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
    if(htim -> Instance == TIM16 ) 
    {
        key_sta[1] = HAL_GPIO_ReadPin (GPIOB ,GPIO_PIN_0 );
        key_sta[2] = HAL_GPIO_ReadPin (GPIOB ,GPIO_PIN_1 );
        key_sta[3] = HAL_GPIO_ReadPin (GPIOB ,GPIO_PIN_2 );
        key_sta[4] = HAL_GPIO_ReadPin (GPIOA ,GPIO_PIN_0 );
        
        for (int i = 1; i < 5; i++ )
        {
            switch(key_flag[i])
            {
                case 0:
                    if(key_sta[i] == GPIO_PIN_RESET )
                    {
                        key_flag [i] = 1;
                    }
                    break;
                case 1:
                    if(key_sta [i] == GPIO_PIN_RESET )
                    {
                        key_flag [i] = 2;
                        
                    }
                    else
                        key_flag [i] = 0;
                    break ;
                case 2:
                {
                    if(key_sta [i] == GPIO_PIN_SET )
                    {
                        key_single [i] = 1;
                        key_flag [i] = 0;
                    }
                    else 
                    {
                        time_counter[i] +=10;
                    }
                    break ;
                }
            }
        }
    }
    
}
int main(void)
{
    HAL_TIM_Base_Start_IT(&htim16);
    
    while(1)
    {
        if(key_single[1] == 1)
        {
            if(time_counter[1]>500)
            {
                执行长按B1的操作;
            }
            else
            {
                执行短按B1的操作;
            }
​
            key_single[1] = 0;
        }
        
        if(key_single[2] == 1)
        {
            B2_function();
            key_single[2] = 0;
        }
        
        if(key_single[3] == 1)
        {
            B3_function();
            key_single[3] = 0;
        }
        
        if(key_single[4] == 1)
        {
            B4_function();
            key_single[4] = 0;
        }
    }
}
 

IIC

设置:PB6、PB7,GPIO_OUT;main.c中#include "i2c_hal.h"

void iic_write(unsigned char address ,unsigned char info)
{
    I2CStart();
    
    I2CSendByte(0Xa0);
    I2CWaitAck();
    I2CSendByte(address);
    I2CWaitAck();
    I2CSendByte(info);
    I2CWaitAck();
    
    I2CStop();
}
​
unsigned char iic_read(unsigned char address)
{
    unsigned char value;
    
    I2CStart();
    
    I2CSendByte(0Xa0);
    I2CWaitAck();
    I2CSendByte(address);
    I2CWaitAck();
    
    I2CStop();
    I2CStart();
    
    I2CSendByte(0Xa1);
    I2CWaitAck();
    value = I2CReceiveByte();
    
    I2CStop();
    
    return(value);
}
int main(void)
{
    I2CInit();
    
    #在0X01记录开机次数
    root_counter=iic_read(0X01);
    root_counter++;
    iic_write(0X01, root_counter);
    HAL_Delay (5);
    
}

ADC

设置:R37->PB15,R38->PB12,ADC-IN;single-ended simpleing time 最高

float ADC_R()
{
    float ADC_value = 0;
    
    HAL_ADC_Start(*hadc1);
    HAL_ADC_PollForConversion(*hadc1,10 );
    ADC_value = HAL_ADC_GetValue(*hadc1);
    HAL_ADC_Stop(*hadc1)
    
    return ADC_value*3.3/4096;
​
}

DAC

设置:R39->PB4,R40->PA15,DAC_IN;connected to external pin only

void DAC_R(float value)
{
    uint16_t temp = 0;
    temp = value*4096/3.3f
    HAL_DAC_SetValue(*hdac1, DAC_CHANNEL_1, DAC_ALIGN_12B_R, temp);
    
}
viod main()
{
    HAL_DAC_Start(*hdac1, DAC_CHANNEL_1);
​
}

PWM

PWM输出

设置:PA1-PA7,根据题目选择GPIO,TIM;clock source :Internal Clock;channel: PWM Generation CH; prescaler : 79 ; counter : 999; autu-reloaded : enabled; Pulse:500; out put:enabled; NVIC: enabled

void set_PWM(int period, int compare)
{
    htim2.Init.Period = period;
    HAL_TIM_Base_Init(&htim2);
    __HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_2 ,compare );
}
int main(void)
{
    HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_2 );
    __HAL_TIM_CLEAR_IT(&htim2,TIM_CHANNEL_2);
    
}

频率检测

设置:PB4->R39,PA15-> R40, TIM;clock source :Internal Clock;channel:Input Capture direct; prescaler : 79 ; counter : 65535; autu-reloaded : enabled; NVIC: enabled

void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
    R39_counter = __HAL_TIM_GET_COUNTER(&htim3 );
    __HAL_TIM_SET_COUNTER(&htim3, 0);
    R39_freq = 1000000 / R39_counter;
}
int main(void)
{
    HAL_TIM_IC_Start_IT(&htim3,TIM_CHANNEL_1);
    __HAL_TIM_CLEAR_IT(&htim3,TIM_CHANNEL_1);
​
}

PWM检测

设置:clock source :Internal Clock;combined channel:PWM Input on CH1;prescaler : 79 ; counter : 65535; autu-reloaded : enabled; NVIC: enabled

void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
    volatile static uint32_t freq = 0, duty = 0;
        if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
        {
            freq = HAL_TIM_ReadCapturedValue (&htim4, TIM_CHANNEL_1);
            freq_value = 1000000/freq;
        }
        if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2)
        {
            duty  = HAL_TIM_ReadCapturedValue (&htim4, TIM_CHANNEL_2);
            duty_value  = duty*100/freq;
        }
}
int main(void)
{
    HAL_TIM_IC_Start_IT(&htim4,TIM_CHANNEL_1);
    HAL_TIM_IC_Start_IT(&htim4,TIM_CHANNEL_2);
    
}

UART

设置:PA9、PA10,RX TX,mode:Asynchronous,DMA:add RX,TX,NVIC:ENABLE,,parameter:根据题目设定

int main(void)
{
    HAL_UART_Receive_DMA (&huart1,(uint8_t *)RX,50);
    __HAL_UART_CLEAR_IDLEFLAG (&huart1 );
    __HAL_UART_ENABLE_IT (&huart1,UART_IT_IDLE);
​
}
if(__HAL_UART_GET_FLAG(&huart1 ,UART_FLAG_IDLE) != RESET)
    {
        __HAL_UART_CLEAR_IDLEFLAG (&huart1);
        HAL_UART_DMAStop (&huart1);
        HAL_UART_IdleCpltCallback();
        
    }
void HAL_UART_IdleCpltCallback()
{
    LCD_ClearLine (Line9);
    recount = 50 - __HAL_DMA_GET_COUNTER ( &hdma_usart1_rx);
    HAL_UART_Transmit_DMA (&huart1,RX,recount);
    LCD_DisplayStringLine(Line9,RX);
    memset(RX,0 ,50);
    recount = 0;
    
    
    HAL_UART_Receive_DMA (&huart1 ,(uint8_t *)RX,50);
}

RTC

✅activate clock source

✅activate calendar

data format:BIN

设置好默认时间and日期

unsigned char show_time[16] = "hh-ms-ss";
​
unsigned char show_data[16] = "yyyy-mm-dd";
static void RTC_show(unsigned char * show_time,unsigned char * show_data)
{
    RTC_DateTypeDef nowdata;
    RTC_TimeTypeDef nowtime;
    
    HAL_RTC_GetTime(&hrtc,&nowtime,RTC_FORMAT_BIN);
    HAL_RTC_GetDate(&hrtc,&nowdata,RTC_FORMAT_BIN);
    
    sprintf((char*)show_time,"%02d:%02d:%02d",nowtime.Hours,nowtime.Minutes,nowtime.Seconds);
    sprintf((char*)show_data,"%4d-%02d-%02d",nowdata.Year+2000,nowdata.Month,nowdata.Date);
    
    LCD_DisplayStringLine (Line1, show_time);
    LCD_DisplayStringLine(Line2,show_data);
}
while (1)
{
        RTC_show(show_time,show_data);
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值