蓝桥杯嵌入式基础模板

首先用cubemx配置基础外设(STM32G431RBT6)

RCC:

GPIO:包括按键(PB0,PB1,PB2,PA0全部配置输入),LED(PC8~PC15和PD2全部配置输出)

把PC8~PC15设置输出高电平

UART:PA9和PA10

ADC:ADC1和ADC2用PB12  PB15

时钟修改一下

TIM:

先配基本定时器TIM6

再配TIM2(输入捕获)

一个上升沿一个下降沿

再配TIM3跟TIM17(PWM输出)

频率1KHz,占空比10%

输出频率和占空比可自行修改

用__HAL TIM SET COMPARE (&htim2,TIM_CHANNEL_1,500)

和__HAI TIM SET AUTORELOAD(&htim2,999)

最后配置TIM15

TIM全部搞定

RTC:

1.LED部分(点亮或者熄灭8个LED灯)

void LED_Disp(uint8_t ucLed)
{
    //**将所有的灯熄灭
    HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15|GPIO_PIN_8
                                                |GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12,               GPIO_PIN_SET);
    HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_SET);        
    HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_RESET);

    //根据ucLed的数值点亮相应的灯
    HAL_GPIO_WritePin(GPIOC, ucLed<<8, GPIO_PIN_RESET);
    HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_SET);        
    HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_RESET);    
    
}


 

2.KEY部分(四个按键)

uint8_t Key_Scan(void)
{
    uint8_t unKey_Val = 0;
    
    if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_0) == GPIO_PIN_RESET)
        unKey_Val = 1;

    if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_1) == GPIO_PIN_RESET)
        unKey_Val = 2;

    if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_2) == GPIO_PIN_RESET)
        unKey_Val = 3;
    
    if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == GPIO_PIN_RESET)
        unKey_Val = 4;    
    
    return unKey_Val;
}

3.LCD显示

    LCD_Init();//初始化LCD
    LCD_Clear(White);
    LCD_SetBackColor(White);
    LCD_SetTextColor(Blue);    

    sprintf((char *)Lcd_Disp_String, "xxx");//xxx为想显示的内容
    LCD_DisplayStringLine(Line0, Lcd_Disp_String);

4.USART串口使用

检测串口部分
void Usart_Proc(void)
{
    if((uwTick -  uwTick_Usart_Point)<1000)    return;//减速函数
    uwTick_Usart_Set_Point = uwTick;
    
    sprintf(str, "%04d:Hello,world.\r\n", counter);
    HAL_UART_Transmit(&huart1,(unsigned char *)str, strlen(str), 50);
    
    if(++counter == 10000)
        counter = 0;
}

5.ADC通道采集(R37跟R38)

uint16_t getADC1(void)
{
    uint16_t adc = 0;
    
    HAL_ADC_Start(&hadc1);
    adc = HAL_ADC_GetValue(&hadc1);
    
    return adc;
}


uint16_t getADC2(void)
{
    uint16_t adc = 0;
    
    HAL_ADC_Start(&hadc2);
    adc = HAL_ADC_GetValue(&hadc2);
    
    return adc;
}

6.PWM输入捕获输出比较(占空比问题)

//基本定时器6更新回调函数
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
    if(htim->Instance==TIM6)
  {
//        if(++counter == 10)
//        {
//            counter = 0;
//            sprintf(str, "Hello,world.\r\n");
//            HAL_UART_Transmit(&huart1,(unsigned char *)str, strlen(str), 50);    
//        }
    }
}


//输入捕获PWM中断回调
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
      if(htim->Instance==TIM2)
  {
                if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
            {
                PWM_T_Count =  HAL_TIM_ReadCapturedValue(htim,TIM_CHANNEL_1)+1;
                PWM_Duty = (float)PWM_D_Count/PWM_T_Count;
            }
                else if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2)
            {
                PWM_D_Count =  HAL_TIM_ReadCapturedValue(htim,TIM_CHANNEL_2)+1;
            }            
    }    
}


//方波输出回调函数
void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim)
{
  if(htim->Instance==TIM15)
  {
                if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
            {            
              __HAL_TIM_SET_COMPARE(htim,TIM_CHANNEL_1,(__HAL_TIM_GetCounter(htim)+500));//1Khz
            }
    }
}

7.E2PROM的读写(掉电保存数据)

void iic_24c02_write(uint8_t *pucBuf, uint8_t ucAddr, uint8_t ucNum)
{
    I2CStart();
    I2CSendByte(0xa0);
    I2CWaitAck();
    
    I2CSendByte(ucAddr);    
    I2CWaitAck();
    
    while(ucNum--)
    {
        I2CSendByte(*pucBuf++);
        I2CWaitAck();    
    }
    I2CStop();
    delay1(500);    
}


void iic_24c02_read(uint8_t *pucBuf, uint8_t ucAddr, uint8_t ucNum)
{
    I2CStart();
    I2CSendByte(0xa0);
    I2CWaitAck();
    
    I2CSendByte(ucAddr);    
    I2CWaitAck();
    
    I2CStart();
    I2CSendByte(0xa1);
    I2CWaitAck();
    
    while(ucNum--)
    {
        *pucBuf++ = I2CReceiveByte();
        if(ucNum)
            I2CSendAck();    
        else
            I2CSendNotAck();
    }
    I2CStop();    
}

8.RTC时钟

  

    HAL_RTC_GetTime(&hrtc, &H_M_S_Time, RTC_FORMAT_BIN);//读取日期和时间必须同时使用
    HAL_RTC_GetDate(&hrtc, &Y_M_D_Date, RTC_FORMAT_BIN);
    sprintf((char *)Lcd_Disp_String, "Time:%02d-%02d-%02d",(unsigned int)H_M_S_Time.Hours,      (unsigned int)H_M_S_Time.Minutes,(unsigned int)H_M_S_Time.Seconds);
    LCD_DisplayStringLine(Line4, Lcd_Disp_String);        

比赛官方资料:

【超级会员V5】通过百度网盘分享的文件:比赛当天资料
链接:https://pan.baidu.com/s/1QyKQomwl2xOOmWBF-ccnCw 
提取码:ud3d
复制这段内容打开「百度网盘APP 即可获取」

  • 5
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

热爱嵌入式的小佳同学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值