如圖,筆者用TQFP(32-100PIN)0.55MM轉(zhuǎn)直插的轉(zhuǎn)接板焊了一個STM32F207VET6的板子。板上引出了SWD調(diào)試接口(僅占用PA13和PA14),USART1串口引腳,插了一個觸摸傳感器和蜂鳴器模塊。
所要實(shí)現(xiàn)的功能是:用手觸碰一下觸摸傳感器后,蜂鳴器響一聲。
接觸摸傳感器模塊輸出信號接到PA0口上。在沒有接電源電容之前,每一次單片機(jī)復(fù)位(無論是軟件復(fù)位還是按下復(fù)位鍵復(fù)位),PA0上會自動產(chǎn)生一個異常的高電平,要等上將近10秒才會回到低電平,然后觸摸傳感器才能正常工作。每次復(fù)位的時候蜂鳴器都會響一下,10秒之內(nèi)按觸摸鍵都沒有反應(yīng)。
后來我接了一個4.7μF的電解電容器和兩個100nF的無極性電容器,問題就解決了。單片機(jī)復(fù)位后蜂鳴器不會響,手按觸摸鍵后馬上就能響,不用再等10秒。
還有,不接電容器,串口下載以及SWD/JTAG下載有時也會受影響。特別是沒有外接25MHz的HSE晶振的情況下,F(xiàn)lash Loader Demo(串口燒寫STM32的工具)經(jīng)常連不上芯片。
這說明,這些電容對保證單片機(jī)以及外圍器件運(yùn)行的可靠性非常重要。
【20-Pin的SWD調(diào)試接口連線】
【測試用的程序】
#include
#include
int fputc(int ch, FILE *fp)
{
if (fp == stdout)
{
if (ch == '\n')
{
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, '\r');
}
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\n", USART1->BRR);
printf("SYSCLK=%dHz HCLK=%dHz PCLK1=%dHz PCLK2=%dHz\n", clocks.SYSCLK_Frequency, clocks.HCLK_Frequency, clocks.PCLK1_Frequency, clocks.PCLK2_Frequency);
printf("HSERDY=%d, SYSCLK=%d\n", 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!\n");
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!\n");
}
else
printf("Released!\n");
}
void RCC_IRQHandler(void)
{
if (RCC_GetITStatus(RCC_IT_LSERDY) == SET)
{
RCC_ClearITPendingBit(RCC_IT_LSERDY);
printf("LSE ready!\n");
}
}
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);
}
}
}
上海意泓電子科技有限責(zé)任公司 版權(quán)所有 未經(jīng)授權(quán)禁止復(fù)制或鏡像
CopyRight 2020-2025 www.jlodreman.com All rights reserved 滬ICP備2021005866號