接線圖
SWD方式下載程序,4線,VCC,GND。
SWDIO:Serial Wire Data Input Output,串行數(shù)據(jù)輸入輸出引腳,作為仿真信號(hào)的雙向數(shù)據(jù)信號(hào)線,建議上拉。
SWCLK:Serial Wire Clock,串行線時(shí)鐘引腳,作為仿真信號(hào)的時(shí)鐘信號(hào)線,建議下拉;
蜂鳴器的IO口接在了最小系統(tǒng)板的PB12引腳上。
蜂鳴器的操作方法和LED方式一樣。 注:上圖蜂鳴器是低電平有效。
一、配置RCC時(shí)鐘
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
STM32任何外設(shè)**第一步都是先配置時(shí)鐘。**GPIO都是掛載在APB2總線上的。
二、配置GPIO
1.引入庫
推挽輸出,PB12引腳,引腳速度50MHZ,因?yàn)楸境绦蛴脕韺W(xué)習(xí),沒有考慮低功耗
最后一步就是調(diào)用STM32庫函數(shù),初始化GPIOB。這5句代碼非常常用。一般都是這個(gè)套路。
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
while循環(huán)
代碼如下(示例):
while (1)
{
GPIO_ResetBits(GPIOB, GPIO_Pin_12);
Delay_ms(100);
GPIO_SetBits(GPIOB, GPIO_Pin_12);
Delay_ms(100);
GPIO_ResetBits(GPIOB, GPIO_Pin_12);
Delay_ms(100);
GPIO_SetBits(GPIOB, GPIO_Pin_12);
Delay_ms(700);
}
這段程序的意思是
在while循環(huán)中,清除PB12引腳的數(shù)據(jù)(設(shè)置為低電平),延時(shí)100ms, PB12引腳置為1,延時(shí)100Ms,就是響,不響,響,不響,這樣一直循環(huán)。
下面是兩個(gè)函數(shù)的具體含義。
```c
/**
* @brief Clears the selected data port bits.
* @param GPIOx: where x can be (A..G) to select the GPIO peripheral.
* @param GPIO_Pin: specifies the port bits to be written.
* This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
* @retval None
*/
void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
{
/* Check the parameters */
assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
assert_param(IS_GPIO_PIN(GPIO_Pin));
GPIOx->BRR = GPIO_Pin;
}
翻譯:
- @brief清除選中的數(shù)據(jù)端口位。
-
- @param GPIOx:其中x可以是(A…G),用來選擇GPIO外設(shè)。
-
- @param GPIO_Pin:要寫的端口位。
- *該參數(shù)可以是GPIO_Pin_x的任意組合,其中x可以是(0…15)。
-
- @retval無
/**
* @brief Sets the selected data port bits.
* @param GPIOx: where x can be (A..G) to select the GPIO peripheral.
* @param GPIO_Pin: specifies the port bits to be written.
* This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
* @retval None
*/
void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
{
/* Check the parameters */
assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
assert_param(IS_GPIO_PIN(GPIO_Pin));
GPIOx->BSRR = GPIO_Pin;
}
翻譯:文章來源:http://www.zghlxwxcb.cn/news/detail-430239.html
* @brief設(shè)置所選數(shù)據(jù)端口位。
* * @param GPIOx:其中x可以是(A..G),用來選擇GPIO外設(shè)。
* * @param GPIO_Pin:要寫的端口位。
* *該參數(shù)可以是GPIO_Pin_x的任意組合,其中x可以是(0..15)。
* * @retval無
總結(jié)
資料來源:
1.STM32固件庫
2.某站自化協(xié)教程。文章來源地址http://www.zghlxwxcb.cn/news/detail-430239.html
到了這里,關(guān)于STM32F103C8T6最小系統(tǒng)板實(shí)現(xiàn)蜂鳴器報(bào)警的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!