因为自己是跟着蚂蚁工厂来学习的,感觉他们教的方法很赞,但是,我也有一点自己的小看法在排版上面。
用CUBE MX生成代码之后,可以创建一个中间层BSP。
然后把BSP这一层移到这个位置
就可以添加.c文件啦。。
在基础板上可以添加4个.c文件,当然是按自己习惯就好了。
led_key.c
#include "led_key.h"
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);
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);
}
uint8_t Key_Scans(void)
{
uint8_t ucKey_val = 0;
if(key1 == Key_Down)
ucKey_val = 1;
if(key2 == Key_Down)
ucKey_val = 2;
if(key3 == Key_Down)
ucKey_val = 3;
if(key4 == Key_Down)
ucKey_val = 4;
return ucKey_val;
}
led_key.h
#ifndef _LED_KEY
#define _LED_KEY
#include "main.h"
#define key1 HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0)
#define key2 HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1)
#define key3 HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_2)
#define key4 HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0)
#define Key_Down GPIO_PIN_RESET
void Led_Disp(uint8_t ucled);
uint8_t Key_Scans(void);
#endif
bsp_adc.c
#include "bsp_adc.h"
/* 采用查询方式进行ADC采样*/
uint32_t Get_Adc2_IN15(void) /* PB15 R37*/
{
uint32_t Adc2_IN15_value = 0;
HAL_ADC_Start(&hadc2);
if(HAL_ADC_PollForConversion(&hadc2,50) == HAL_OK)
{
Adc2_IN15_value = HAL_ADC_GetValue(&hadc2);
}
return Adc2_IN15_value;
}
/* 采用中断方式获取ADC1通道11的值*/
uint32_t Adc1_IN11_value;
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)
{
if(hadc->Instance == ADC1)
{
Adc1_IN11_value = HAL_ADC_GetValue(&hadc1);
}
HAL_ADC_Start_IT(&hadc1);
}
bsp_adc.h
#ifndef _BSP_ADC
#define _BSP_ADC
#include "main.h"
#include "adc.h"
extern uint32_t Adc1_IN11_value;
uint32_t Get_Adc2_IN15(void);
#endif
led.c和i2c.c已经提供给咱们了。
iic和mcp4017读写代码
/* EEPROM相关代码*/
void Write_AT24C02(uint8_t *buf,uint8_t addr,uint8_t len)
{
I2CStart();
I2CSendByte(0xa0);
I2CWaitAck();
I2CSendByte(addr);
I2CWaitAck();
while(len--)
{
I2CSendByte(*buf++);
I2CWaitAck();
}
I2CStop();
delay1(500);
}
void Read_AT24C02(uint8_t *buf,uint8_t addr,uint8_t len)
{
I2CStart();
I2CSendByte(0xa0);
I2CWaitAck();
I2CSendByte(addr);
I2CWaitAck();
I2CStart();
I2CSendByte(0xa1);
I2CWaitAck();
while(len--)
{
*buf++ = I2CReceiveByte();
if(len)
I2CSendAck();
else
I2CSendNotAck();
}
I2CStop();
delay1(500);
}
/* MCP4017*/
void Write_MCP4017(uint8_t value)
{
I2CStart();
I2CSendByte(0x5e);
I2CSendAck();
I2CSendByte(value);
I2CSendAck();
I2CStop();
}
uint8_t Read_MCP4017(void)
{
uint8_t value;
I2CStart();
I2CSendByte(0x5f);
I2CSendAck();
value = I2CReceiveByte();
I2CSendNotAck();
I2CStop();
return value;
}
然后其余的功能在main.c里面实现就好了。
按键处理函数
void Key_Proc(void)
{
if(uwTick - key_uwTick < 50)
return;
key_uwTick = uwTick;
//uint8_t ucKey_Val,ucKey_Down,ucKey_Up,ucKey_Old;
ucKey_Val = Key_Scans();
ucKey_Down = ucKey_Val & (ucKey_Old ^ ucKey_Val);
ucKey_Up = ~ucKey_Val & (ucKey_Old ^ ucKey_Val);
ucKey_Old = ucKey_Val;
switch(ucKey_Down)
{
case 1:
ucled |= 0x88;
count = 1;
break;
case 2:
ucled &= (~0x88);
count = 2;
break;
case 3:
ucled |= 0x55;
count = 3;
break;
case 4:
ucled &= (~0x55);
count = 4;
break;
}
}
led处理函数
void Led_Proc(void)
{
if(uwTick - led_uwTick < 200)
return;
led_uwTick = uwTick;
Led_Disp(ucled);
}
lcd处理函数
void Lcd_Proc(void)
{
if(uwTick - lcd_uwTick < 200)
return;
lcd_uwTick = uwTick;
HAL_RTC_GetTime(&hrtc,&H_M_S,RTC_FORMAT_BIN);
HAL_RTC_GetDate(&hrtc,&Y_M_D,RTC_FORMAT_BIN);
sprintf((char *)lcd_disp_string,"test:%02d i:%d",count,i);
LCD_DisplayStringLine(Line9,lcd_disp_string);
sprintf((char *)lcd_disp_string,"R37_Value:%.2fV",Get_Adc2_IN15()*3.3/4096);
LCD_DisplayStringLine(Line0,lcd_disp_string);
sprintf((char *)lcd_disp_string,"R38_Value:%.2fV",Adc1_IN11_value*3.3/4096);
LCD_DisplayStringLine(Line1,lcd_disp_string);
sprintf((char *)lcd_disp_string,"sbuf2:%d,%d,%d,%d,%d",sbuf2[0],sbuf2[1],sbuf2[2],sbuf2[3],sbuf2[4]);
LCD_DisplayStringLine(Line2,lcd_disp_string);
sprintf((char *)lcd_disp_string,"MCP_4017:%X",MCP_4017);
LCD_DisplayStringLine(Line3,lcd_disp_string);
sprintf((char *)lcd_disp_string,"pwm_duty1:%.2f",pwm_duty1);
LCD_DisplayStringLine(Line4,lcd_disp_string);
sprintf((char *)lcd_disp_string,"pwm_duty2:%.2f",pwm_duty2);
LCD_DisplayStringLine(Line5,lcd_disp_string);
sprintf((char *)lcd_disp_string,"Time:%02d-%02d-%02d",Y_M_D.Year,Y_M_D.Month,Y_M_D.Date);
LCD_DisplayStringLine(Line6,lcd_disp_string);
sprintf((char *)lcd_disp_string,"Time:%02d-%02d-%02d",H_M_S.Hours,H_M_S.Minutes,H_M_S.Seconds);
LCD_DisplayStringLine(Line7,lcd_disp_string);
}
串口相关函数
void Usart_Proc(void)
{
if(uwTick - usart_uwTick < 1000)
return;
usart_uwTick = uwTick;
HAL_UART_Transmit(&huart1,tx_dat,sizeof(tx_dat),50);
}
基本定时器实现基本定时函数
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance == TIM6)
{
if(++t == 1000)
{
ucled ^= 0x1;
t = 0;
i++;
if(i == 1000)
i = 0;
}
}
}
pwm脉冲输入
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance == TIM2) //PA15 R40
{
if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
{
pwm_count1 = HAL_TIM_ReadCapturedValue(&htim2,TIM_CHANNEL_1)+1;
pwm_duty1 = (float)pwm_high1/pwm_count1;
}
if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2)
{
pwm_high1 = HAL_TIM_ReadCapturedValue(&htim2,TIM_CHANNEL_2)+1;
}
}
if(htim->Instance == TIM3) //PB4 R39
{
if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
{
pwm_count2 = HAL_TIM_ReadCapturedValue(&htim3,TIM_CHANNEL_1)+1;
pwm_duty2 = (float)pwm_high2/pwm_count2;
}
if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2)
{
pwm_high2 = HAL_TIM_ReadCapturedValue(&htim3,TIM_CHANNEL_2)+1;
}
}
}
方波输出
void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance == TIM4)
{
if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
{
CCR_Value1 = __HAL_TIM_GetCounter(htim) + 100;
__HAL_TIM_SET_COMPARE(htim,TIM_CHANNEL_1,CCR_Value1);
}
if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2)
{
CCR_Value2 = __HAL_TIM_GetCounter(htim) + 400;
__HAL_TIM_SET_COMPARE(htim,TIM_CHANNEL_1,CCR_Value2);
}
}
}
main函数里面的内容
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_ADC1_Init();
MX_ADC2_Init();
MX_USART1_UART_Init();
MX_TIM6_Init();
MX_TIM2_Init();
MX_TIM3_Init();
MX_RTC_Init();
MX_TIM4_Init();
MX_TIM15_Init();
/* USER CODE BEGIN 2 */
/* LCD初始化*/
LCD_Init();
LCD_Clear(White);
LCD_SetBackColor(White);
LCD_SetTextColor(Black);
/* 启动ADC1通道采样*/
HAL_ADC_Start_IT(&hadc1);
/* 读写EEPROM*/
I2CInit();
Write_AT24C02(sbuf1,0,sizeof(sbuf1));
HAL_Delay(10);
Read_AT24C02(sbuf2,0,sizeof(sbuf2));
/* MCP4017*/
Write_MCP4017(0x45);
MCP_4017 = Read_MCP4017();
/* 打开串口接收中断*/
HAL_UART_Receive_IT(&huart1,&rx_dat,1);
/* 打开基本定时器中断*/
HAL_TIM_Base_Start_IT(&htim6);
/* 打开PWM输入中断*/
HAL_TIM_IC_Start_IT(&htim2,TIM_CHANNEL_1);
HAL_TIM_IC_Start_IT(&htim2,TIM_CHANNEL_2);
HAL_TIM_IC_Start_IT(&htim3,TIM_CHANNEL_1);
HAL_TIM_IC_Start_IT(&htim3,TIM_CHANNEL_2);
/* 方波输出*/
HAL_TIM_OC_Start_IT(&htim4,TIM_CHANNEL_1);
HAL_TIM_OC_Start_IT(&htim4,TIM_CHANNEL_2);
/* PWM输出*/
HAL_TIM_PWM_Start(&htim15,TIM_CHANNEL_1);//占空比30%
HAL_TIM_PWM_Start(&htim15,TIM_CHANNEL_2);//占空比70%
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
Key_Proc();
Led_Proc();
Lcd_Proc();
// Usart_Proc();
}
/* USER CODE END 3 */
}
相关变量定义
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* 按键变量*/
__IO uint32_t key_uwTick;
uint8_t ucKey_Val,ucKey_Down,ucKey_Up,ucKey_Old;
uint32_t count;
/* LED变量*/
__IO uint32_t led_uwTick;
uint8_t ucled;
/* LCD变量*/
__IO uint32_t lcd_uwTick;
uint8_t lcd_disp_string[21];
/* EEPROM相关变量*/
uint8_t sbuf1[5] = {13,45,24,56,67};
uint8_t sbuf2[5] = {0};
/* MCP4017变量*/
uint8_t MCP_4017;
/* 串口相关变量*/
__IO uint32_t usart_uwTick;
uint8_t tx_dat[20] = {"hello world!\r\n"};
/* 基本定时器变量*/
uint32_t t,i;
/* PWM输入变量*/
uint32_t pwm_count1,pwm_count2;
uint32_t pwm_high1,pwm_high2;
float pwm_duty1,pwm_duty2;
/* rtc相关变量*/
RTC_TimeTypeDef H_M_S;
RTC_DateTypeDef Y_M_D;
/* 方波输出相关变量*/
uint32_t CCR_Value1,CCR_Value2;
/* USER CODE END PV */
之前写代码的时候感觉很乱,但是之后做了一套省赛题目之后,就感觉突然知道自己该怎么写了,这仅代表个人想法。