如图,笔者用TQFP(32-100PIN)0.55MM转直插的转接板焊了一个STM32F207VET6的板子。板上引出了SWD调试接口(仅占用PA13和PA14),USART1串口引脚,插了一个触摸传感器和蜂鸣器模块。
所要实现的功能是:用手触碰一下触摸传感器后,蜂鸣器响一声。
接触摸传感器模块输出信号接到PA0口上。在没有接电源电容之前,每一次单片机复位(无论是软件复位还是按下复位键复位),PA0上会自动产生一个异常的高电平,要等上将近10秒才会回到低电平,然后触摸传感器才能正常工作。每次复位的时候蜂鸣器都会响一下,10秒之内按触摸键都没有反应。
后来我接了一个4.7μF的电解电容器和两个100nF的无极性电容器,问题就解决了。单片机复位后蜂鸣器不会响,手按触摸键后马上就能响,不用再等10秒。
还有,不接电容器,串口下载以及SWD/JTAG下载有时也会受影响。特别是没有外接25MHz的HSE晶振的情况下,Flash Loader Demo(串口烧写STM32的工具)经常连不上芯片。
这说明,这些电容对保证单片机以及外围器件运行的可靠性非常重要。
【20-Pin的SWD调试接口连线】
【测试用的程序】
#include
#include
int fputc(int ch, FILE *fp)
{
if (fp == stdout)
{
if (ch == '
')
{
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, '
');
}
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, ch);
}
return ch;
}
void showclk(void)
{
RCC_ClocksTypeDef clocks;
RCC_GetClocksFreq(&clocks);
printf("USART1->BRR=%d
", USART1->BRR);
printf("SYSCLK=%dHz HCLK=%dHz PCLK1=%dHz PCLK2=%dHz
", clocks.SYSCLK_Frequency, clocks.HCLK_Frequency, clocks.PCLK1_Frequency, clocks.PCLK2_Frequency);
printf("HSERDY=%d, SYSCLK=%d
", RCC_GetFlagStatus(RCC_FLAG_HSERDY), RCC_GetSYSCLKSource());
}
int main(void)
{
EXTI_InitTypeDef exti;
GPIO_InitTypeDef gpio;
TIM_OCInitTypeDef oc;
TIM_TimeBaseInitTypeDef tim;
USART_InitTypeDef usart;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_TIM2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
PWR_BackupAccessCmd(ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_TIM2);
gpio.GPIO_Mode = GPIO_Mode_AF;
gpio.GPIO_OType = GPIO_OType_PP;
gpio.GPIO_Pin = GPIO_Pin_3;
gpio.GPIO_PuPd = GPIO_PuPd_NOPULL;
gpio.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOA, &gpio);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
gpio.GPIO_Pin = GPIO_Pin_9;
gpio.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &gpio);
USART_StructInit(&usart);
usart.USART_BaudRate = 115200;
USART_Init(USART1, &usart);
USART_Cmd(USART1, ENABLE);
showclk();
if (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == SET)
printf("LSE on!
");
else
{
RCC_ITConfig(RCC_IT_LSERDY, ENABLE);
NVIC_EnableIRQ(RCC_IRQn);
RCC_LSEConfig(RCC_LSE_ON);
}
TIM_UpdateRequestConfig(TIM2, TIM_UpdateSource_Regular);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
NVIC_EnableIRQ(TIM2_IRQn);
TIM_TimeBaseStructInit(&tim);
tim.TIM_Period = 9;
tim.TIM_Prescaler = 1699;
TIM_TimeBaseInit(TIM2, &tim);
oc.TIM_OCMode = TIM_OCMode_PWM2;
oc.TIM_OCPolarity = TIM_OCPolarity_High;
oc.TIM_OutputState = TIM_OutputState_Enable;
oc.TIM_Pulse = 4;
TIM_OC4Init(TIM2, &oc);
exti.EXTI_Line = EXTI_Line0;
exti.EXTI_LineCmd = ENABLE;
exti.EXTI_Mode = EXTI_Mode_Interrupt;
exti.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
EXTI_Init(&exti);
NVIC_EnableIRQ(EXTI0_IRQn);
while (1)
__WFI();
}
void EXTI0_IRQHandler(void)
{
EXTI_ClearITPendingBit(EXTI_Line0);
if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == Bit_SET)
{
TIM_Cmd(TIM2, ENABLE);
printf("Touch!
");
}
else
printf("Released!
");
}
void RCC_IRQHandler(void)
{
if (RCC_GetITStatus(RCC_IT_LSERDY) == SET)
{
RCC_ClearITPendingBit(RCC_IT_LSERDY);
printf("LSE ready!
");
}
}
void TIM2_IRQHandler(void)
{
static uint16_t counter = 0;
if (TIM_GetITStatus(TIM2, TIM_IT_Update) == SET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
counter++;
if (counter == 999)
TIM_SelectOnePulseMode(TIM2, TIM_OPMode_Single);
else if (counter == 1000)
{
counter = 0;
TIM_SelectOnePulseMode(TIM2, TIM_OPMode_Repetitive);
}
}
}