自学内容网 自学内容网

蓝桥杯嵌入式之模块驱动

一、LED驱动

1.头文件

#ifndef     __LED_H__
#define    __LED_H__
#include "main.h"
void led_disp(u8 uled);
void Led_dispos(u8 pos);
void LED_Disp(u8 dsLED);
void TurnOn_LED(uint8_t _led);
void TurnOff_LED(uint8_t _led);
void Toogle_LED(uint8_t _led);

#endif   /*__LED_H__*/

2.源文件

#include "led.h"
  void led_disp(u8 uled)
  {
    
       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);
  HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
  
  HAL_GPIO_WritePin(GPIOC,uled<<8,GPIO_PIN_RESET);
  HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);
  HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
  
  
  }

  void Led_dispos(u8 pos)
  {
     switch(pos)
  {
 case 1:
led_disp(0x01); 
 break;
  case 2:
led_disp(0x02); 
 break;
   case 3:
led_disp(0x04); 
 break;
    case 4:
led_disp(0x08); 
 break;
 case 5:
led_disp(0x10); 
 break;
  case 6:
led_disp(0x20); 
 break;
   case 7:
led_disp(0x40); 
 break;
    case 8:
led_disp(0x80); 
 break;
default:
led_disp(0x00);

  }
  
  }
  
  
  u32 led = 0xff00;

/*
LED_Disp:全灭LED灯,然后点亮一个LED灯
dsLED:要点亮的LED灯,如dsLED==0x01,点亮LED1
*/
void LED_Disp(u8 dsLED)
{
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_All,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC,dsLED<<8,GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
}

/*
TurnOn_LED:点亮某个LED灯(使用寄存器)
参数:
@_led:要打开的LED灯序号(如第一个为“1”)
*/
void TurnOn_LED(uint8_t _led)
{
led &= ~(0x01<<(7+_led));
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);
GPIOC->ODR = (uint32_t)led;
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
}

/*
TurnOff_LED:熄灭某个LED灯(使用寄存器)
参数:
@_led:要熄灭的LED灯序号(如第一个为“1”)
*/
void TurnOff_LED(uint8_t _led)
{
led |= (0x01<<(7+_led));
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_SET);
GPIOC->ODR = (uint32_t)led;
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_RESET);
}

/*
Toogle_LED:反转某个LED灯(使用寄存器)
参数:
@_led:要反转的LED灯序号(如第一个为“1”)
*/
void Toogle_LED(uint8_t _led)
{
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_SET);
HAL_GPIO_TogglePin(GPIOC, _led << 8);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_RESET);
}

/*
LED_mie:LED全灭
*/
void LED_mie(void)
{
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_All,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
}

/*
LED_linag:LED全亮
*/
void LED_liang(void)
{
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_All,GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
}
  
  
  

二、key驱动

1.头文件

#ifndef   __KEY_H__
#define   __KEY_H__
#include "main.h "
#include "stdbool.h"


struct keys
{
unsigned char judge_sta; //按键状态 0 1 2
bool key_sta;    //1->未按下 0->按下
bool single_flag;

    unsigned int key_time;
bool long_flag;
};



void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim);

#endif   /* __KEY_H__*/

二、源文件

#include "key.h"
#include "lcd.h"
#include "tim.h"
struct keys  key[4]={0,0,0,0,0};

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance == TIM3)
{
key[0].key_sta = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_0);
key[1].key_sta = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_1);
key[2].key_sta = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_2);
key[3].key_sta = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0);
for(unsigned char i = 0; i < 4; i++)
{
switch(key[i].judge_sta)
{
case 0:
{
if(key[i].key_sta == 0) //按下为0
{
key[i].judge_sta = 1;  //状态为1
}
}
break;
case 1:
{
if(key[i].key_sta == 0)   //再次确认按下为0
{
key[i].judge_sta = 2;   //第二次确认按键按下
}
else
{
key[i].judge_sta = 0;   //没按下的状态为0
}
}
break;
case 2:
{
if(key[i].key_sta == 1)// 按键松开后进入case0
{
key[i].judge_sta = 0;  //恢复状态0

if(key[i].key_time < 100)
{
   key[i].single_flag = 1; //表示单击
    }
if(key[i].key_time > 100)
{
  key[i].long_flag = 1;
key[i].key_time =0;

}
}
else 
{
  key[i].key_time++;

}
}

break;
}
}
}
}





三、IIC驱动

1.头文件

#ifndef __I2C_HAL_H
#define __I2C_HAL_H

#include "stm32g4xx_hal.h"
#include "main.h"
void I2CStart(void);
void I2CStop(void);
unsigned char I2CWaitAck(void);
void I2CSendAck(void);
void I2CSendNotAck(void);
void I2CSendByte(unsigned char cSendByte);
unsigned char I2CReceiveByte(void);
void I2CInit(void);


void eeprom_write(u8 add,u8 dat);
u8 eeprom_read(u8 add);
#endif

2.源文件

/*
  ����˵��: CT117E-M4Ƕ��ʽ������GPIOģ��I2C������������
  ��������: MDK-ARM HAL��
  Ӳ������: CT117E-M4Ƕ��ʽ������
  ��    ��: 2020-3-1
*/

#include "i2c_hal.h"

#define DELAY_TIME20

/**
  * @brief SDA������ģʽ����
  * @param None
  * @retval None
  */
void SDA_Input_Mode()
{
    GPIO_InitTypeDef GPIO_InitStructure = {0};

    GPIO_InitStructure.Pin = GPIO_PIN_7;
    GPIO_InitStructure.Mode = GPIO_MODE_INPUT;
    GPIO_InitStructure.Pull = GPIO_PULLUP;
    GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
}

/**
  * @brief SDA�����ģʽ����
  * @param None
  * @retval None
  */
void SDA_Output_Mode()
{
    GPIO_InitTypeDef GPIO_InitStructure = {0};

    GPIO_InitStructure.Pin = GPIO_PIN_7;
    GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_OD;
    GPIO_InitStructure.Pull = GPIO_NOPULL;
    GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
}

/**
  * @brief SDA�����һ��λ
  * @param val ���������
  * @retval None
  */
void SDA_Output( uint16_t val )
{
    if ( val )
    {
        GPIOB->BSRR |= GPIO_PIN_7;
    }
    else
    {
        GPIOB->BRR |= GPIO_PIN_7;
    }
}

/**
  * @brief SCL�����һ��λ
  * @param val ���������
  * @retval None
  */
void SCL_Output( uint16_t val )
{
    if ( val )
    {
        GPIOB->BSRR |= GPIO_PIN_6;
    }
    else
    {
        GPIOB->BRR |= GPIO_PIN_6;
    }
}

/**
  * @brief SDA����һλ
  * @param None
  * @retval GPIO����һλ
  */
uint8_t SDA_Input(void)
{
if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_7) == GPIO_PIN_SET){
return 1;
}else{
return 0;
}
}


/**
  * @brief I2C�Ķ�����ʱ
  * @param None
  * @retval None
  */
static void delay1(unsigned int n)
{
    uint32_t i;
    for ( i = 0; i < n; ++i);
}

/**
  * @brief I2C��ʼ�ź�
  * @param None
  * @retval None
  */
void I2CStart(void)
{
    SDA_Output(1);
    delay1(DELAY_TIME);
    SCL_Output(1);
    delay1(DELAY_TIME);
    SDA_Output(0);
    delay1(DELAY_TIME);
    SCL_Output(0);
    delay1(DELAY_TIME);
}

/**
  * @brief I2C�����ź�
  * @param None
  * @retval None
  */
void I2CStop(void)
{
    SCL_Output(0);
    delay1(DELAY_TIME);
    SDA_Output(0);
    delay1(DELAY_TIME);
    SCL_Output(1);
    delay1(DELAY_TIME);
    SDA_Output(1);
    delay1(DELAY_TIME);

}

/**
  * @brief I2C�ȴ�ȷ���ź�
  * @param None
  * @retval None
  */
unsigned char I2CWaitAck(void)
{
    unsigned short cErrTime = 5;
    SDA_Input_Mode();
    delay1(DELAY_TIME);
    SCL_Output(1);
    delay1(DELAY_TIME);
    while(SDA_Input())
    {
        cErrTime--;
        delay1(DELAY_TIME);
        if (0 == cErrTime)
        {
            SDA_Output_Mode();
            I2CStop();
            return ERROR;
        }
    }
    SCL_Output(0);
    SDA_Output_Mode();
    delay1(DELAY_TIME);
    return SUCCESS;
}

/**
  * @brief I2C����ȷ���ź�
  * @param None
  * @retval None
  */
void I2CSendAck(void)
{
    SDA_Output(0);
    delay1(DELAY_TIME);
    delay1(DELAY_TIME);
    SCL_Output(1);
    delay1(DELAY_TIME);
    SCL_Output(0);
    delay1(DELAY_TIME);

}

/**
  * @brief I2C���ͷ�ȷ���ź�
  * @param None
  * @retval None
  */
void I2CSendNotAck(void)
{
    SDA_Output(1);
    delay1(DELAY_TIME);
    delay1(DELAY_TIME);
    SCL_Output(1);
    delay1(DELAY_TIME);
    SCL_Output(0);
    delay1(DELAY_TIME);

}

/**
  * @brief I2C����һ���ֽ�
  * @param cSendByte ��Ҫ���͵��ֽ�
  * @retval None
  */
void I2CSendByte(unsigned char cSendByte)
{
    unsigned char  i = 8;
    while (i--)
    {
        SCL_Output(0);
        delay1(DELAY_TIME);
        SDA_Output(cSendByte & 0x80);
        delay1(DELAY_TIME);
        cSendByte += cSendByte;
        delay1(DELAY_TIME);
        SCL_Output(1);
        delay1(DELAY_TIME);
    }
    SCL_Output(0);
    delay1(DELAY_TIME);
}

/**
  * @brief I2C����һ���ֽ�
  * @param None
  * @retval ���յ����ֽ�
  */
unsigned char I2CReceiveByte(void)
{
    unsigned char i = 8;
    unsigned char cR_Byte = 0;
    SDA_Input_Mode();
    while (i--)
    {
        cR_Byte += cR_Byte;
        SCL_Output(0);
        delay1(DELAY_TIME);
        delay1(DELAY_TIME);
        SCL_Output(1);
        delay1(DELAY_TIME);
        cR_Byte |=  SDA_Input();
    }
    SCL_Output(0);
    delay1(DELAY_TIME);
    SDA_Output_Mode();
    return cR_Byte;
}

//
void I2CInit(void)
{
    GPIO_InitTypeDef GPIO_InitStructure = {0};

    GPIO_InitStructure.Pin = GPIO_PIN_7 | GPIO_PIN_6;
    GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStructure.Pull = GPIO_PULLUP;
    GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
}

u8 eeprom_read(u8 addr)
{
   u8 data;
I2CStart();
I2CSendByte(0xA0);
I2CWaitAck();
I2CSendByte(addr);
I2CWaitAck();

I2CStart();
I2CSendByte(0xa1);
I2CWaitAck();
data =I2CReceiveByte();
    I2CSendNotAck();
I2CStop();
return data;

}

void eeprom_write(u8 addr,u8 data)
{
     I2CStart();
I2CSendByte(0xa0);
I2CWaitAck();
I2CSendByte(addr);
I2CWaitAck();
I2CSendByte(data);
I2CWaitAck();
I2CStop();

}


原文地址:https://blog.csdn.net/qq_50284870/article/details/137431267

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!