题目
配置
注意事项
复制LCD的工程,先配置资源 --- 勾选完选项一定要再看一眼,可能选择错误
ADC:配置ADC2_IN15,对应PB15引脚
EEROM,配置PB6和PB7
按键 输入模式PB0、PB1、PB2、PA0
LED 一定要使能PD2
PWM互补输出,用TIM15
TIM6 - 10ms基准定时器
代码 - 默写大师
先默写几个函数
EEPROM读写函数
随机地址读函数 - 参考手册的时序
uint8_t EEPROM_ReadByte(uint8_t address)
{
uint8_t data;
I2CStart();
I2CSendByte(0xA0); // address + write
I2CWaitAck();
I2CSendByte(address);
I2CWaitAck();
I2CStop();
I2CStart();
I2CSendByte(0xA1); // address + read
I2CWaitAck();
data = I2CReceiveByte();
I2CSendNotAck();
I2CStop();
return data;
}
void EEPROM_WriteByte(uint8_t address, uint8_t data)
{
I2CStart();
I2CSendByte(0xA0);
I2CWaitAck();
I2CSendByte(address);
I2CWaitAck();
I2CSendByte(data);
I2CWaitAck();
I2CStop();
}
定义几个结构体
//-----------------------
void power_init(void);
void LCD_Disp(void);
void Key_Proc(void);
void ADC_Proc(void);
void PWM_Proc(void);
//-----------------------
struct Tick{
uint32_t lcd;
uint32_t key;
uint32_t adc;
uint32_t pwm;
};
extern struct Tick tick;
struct Flag{
bool PWM_Mode;
uint8_t LCD_View;
};
extern struct Flag flag;
struct Param{
double ADC;
uint16_t PWM_Frq; //频率
uint8_t PWM_Duty_PA9;
uint8_t PWM_Duty_PA14;
};
extern struct Param param;
//定义变量
struct Tick tick;
struct Flag flag;
struct Param param;
struct Keys key;
LED驱动函数
void LED_Disp(uint8_t state)
{
HAL_GPIO_WritePin(GPIOC, 0xFF00, GPIO_PIN_SET); //0ff
HAL_GPIO_WritePin(GPIOC, state << 8, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_RESET);
}