void LED_Disp(unsigned char ds)
{
HAL_GPIO_WritePin( GPIOC,GPIO_PIN_All,GPIO_PIN_SET);
HAL_GPIO_WritePin( GPIOC,ds<<1,GPIO_PIN_RESET);
HAL_GPIO_WritePin (GPIOD,GPIO_PIN_2,GPIO_PIN_SET);
HAL_GPIO_WritePin (GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
}
注意在MX中设置为上拉模式(pull_up)。
在main.c中开启定时器
HAL_TIM_Base_Start_IT(&htim3 );
回调函数
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if( htim->Instance==TIM3 )
{
key[0].key_state=HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0 );
key[1].key_state=HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1 );
key[2].key_state=HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_2 );
key[3].key_state=HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0 );
int i;
for(i=0;i<=3;i++)
{
switch(key[i].judge_state)
{
case 0:
{
if (key[i].key_state==0)
{
key[i].judge_state=1;
}
}break;
case 1:
{
if (key[i].key_state==0)
{
key[i].judge_state=2;
}
else
key[i].judge_state=0;
}break;
case 2:
{
if (key[i].key_state==1)
{
key[i].judge_state=0;
if(key[i].time<70)
key[i].singflag=1;
key[i].time=0;
}
else
{
key[i].time++;
if(key[i].time>=70)
{
key[i].longflag=1;
}
}
}break;
}
}
}
}
LCD_Init();
LCD_Clear (White );
LCD_SetBackColor(White );
LCD_SetTextColor(Black );
sprintf(text," PA6:%d%% ",pa6_duty );
LCD_DisplayStringLine( Line2, (uint8_t *)text);
先把PB6,PB7引脚设置为 GPIO_OUT
eeprom读取函数
uint8_t eeprom_read(uint8_t addr)
{
I2CStart();
I2CSendByte(0xa0);
I2CWaitAck();
I2CSendByte(addr);
I2CWaitAck();
I2CStop();
I2CStart();
I2CSendByte(0xa1);
I2CWaitAck();
uint8_t a;
a=I2CReceiveByte();
I2CWaitAck();
I2CStop();
return a;
}
eeprom写入函数
void eeprom_writ(uint8_t addr,uint8_t dat)
{
I2CStart();
I2CSendByte(0xa0);
I2CWaitAck();
I2CSendByte(addr);
I2CWaitAck();
I2CSendByte(dat );
I2CWaitAck();
I2CStop();
}
MCP4017读取函数
uint8_t MCP4017_read(void)
{
unsigned char val;
I2CStart();
I2CSendByte(0x5f);
I2CWaitAck();
val = I2CReceiveByte();
I2CStop();
return(val);
}
MCP4017写入函数
void MCP4017_write(unsigned char info)
{
I2CStart(); /启动总线
I2CSendByte(0x5e); /
I2CWaitAck(); /等待响应函数
I2CSendByte(info); /
I2CWaitAck();
I2CStop(); /结束总线
}
MX中clock source设置为internal clock,设置频率,并打开中断
HAL_TIM_Base_Start_IT(&htim3 );
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{if( htim->Instance==TIM3 ) {} }
MX中PWM GenerationCH1,设置频率和 占空比(pulse)
HAL_TIM_PWM_Start(&htim16 ,TIM_CHANNEL_1 );
HAL_TIM_PWM_Start(&htim17 ,TIM_CHANNEL_1 );
__HAL_TIM_SetCompare(&htim16 ,TIM_CHANNEL_1,pa6_duty );
MX中设置555所接的引脚,clock source设置为internal clock, channel1:input capture direct mode, channel2:input capture indirect mode,设置频率(prescaler80-1,counter period设置为65535(避免,外部波源没用引起中断,内部引起) ),通道一设置为Rising edge,通道二设置为falling edge,并打开中断
HAL_TIM_IC_Start_IT(&htim2, TIM_CHANNEL_1);
HAL_TIM_IC_Start_IT(&htim2, TIM_CHANNEL_2);//
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance==TIM2)
{
if(htim->Channel==HAL_TIM_ACTIVE_CHANNEL_1)//中断消息来源 选择直接输入的通道
{
ccr1_val1a=HAL_TIM_ReadCapturedValue(htim,TIM_CHANNEL_1);//直接
ccr1_val1b=HAL_TIM_ReadCapturedValue(htim,TIM_CHANNEL_2);//间接
__HAL_TIM_SetCounter(htim,0);
frquent1=(80000000/80)/ccr1_val1a;
duty1=(ccr1_val1b/ccr1_val1a)*100;
HAL_TIM_IC_Start(htim,TIM_CHANNEL_1);
HAL_TIM_IC_Start(htim,TIM_CHANNEL_2);
}
}
}
}
在MX中mode设置为asynchronous ,引脚一般会自动设置错误,必须自己设置为PA9和PA10,并打开中断。
发送
char text[20];
sprintf (text,"Error");
HAL_UART_Transmit(&huart1 ,(uint8_t *)text ,strlen(text),70);
接收
HAL_UART_Receive_IT (&huart1 ,&rxdat,1);
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
rxdata[j++]=rxdat ;
HAL_UART_Receive_IT(&huart1,&rxdat,1);
}
在主函数while中
while (1)
{
//判断串口是否接收完和数据处理
if(j>0)
{
int m;
m=j;
HAL_Delay (2);
if(m==j)
{
if(j==22)
{
sscanf (rxdata ,"%4s:%4s:%12s",car.tingcheleixing ,car.cheliangleixing ,car.time );
}
else
{
char text[20];
sprintf (text,"Error");
HAL_UART_Transmit(&huart1 ,(uint8_t *)text ,strlen(text),70);
}
j=0;
memset (rxdata,'\0',30);
}
}
PB12和PB15设置为,ADC *_IN*
double getADC(ADC_HandleTypeDef *pin)
{
uint16_t adc;
HAL_ADC_Start (pin);
adc = HAL_ADC_GetValue(pin);
return adc*3.3/4096;
}
sprintf(text,"adc1:%.2f",getADC(&hadc1) );
LCD_DisplayStringLine( Line1, (uint8_t *)text );
RTC_TimeTypeDef Time;
RTC_DateTypeDef Date;
HAL_RTC_GetTime (&hrtc , &Time,RTC_FORMAT_BIN);//获取时间
HAL_RTC_GetDate (&hrtc , &Date,RTC_FORMAT_BIN);