红×4、绿灯×4、黄灯×4、蜂鸣器×4、使用stm32f10c8t6芯片、
绿灯通行时,蜂鸣器响起提醒,延时函数简单编写不能精确的延时
keil5代码
main函数代码
#include “stm32f10x.h”
#include “led.h”
#include “beep.h”
void delay(int len)
{
volatile unsigned int i = 6*1000*len;
while(i--);
}
int main(void)
{
LED_Cfg();
BEEP_Cfg();
while(1){
LED_RED1_OFF; //红灯1和红灯3关闭
LED_RED3_OFF;
LED_GREEN1_ON; //绿灯1和绿灯3打开
LED_GREEN3_ON;
BEEP0_ON; //蜂鸣器0和蜂鸣器2打开
BEEP2_ON;
LED_RED2_ON; //红灯2和红灯4打开
LED_RED4_ON;
delay(200); //1和3马路通行,2和4马路不通行
LED_GREEN1_OFF; //绿灯1和绿灯3关闭
LED_GREEN3_OFF;
BEEP0_OFF; //蜂鸣器0和蜂鸣器2关闭
BEEP2_OFF;
LED_YELLOW1_ON; //黄灯1和黄灯3打开
LED_YELLOW3_ON;
delay(100);
LED_YELLOW1_OFF; //黄灯1和黄灯3关闭
LED_YELLOW3_OFF;
LED_RED1_ON; //红灯1和红灯3打开
LED_RED3_ON;
delay(50);
LED_RED2_OFF; //红灯2和红灯4关闭
LED_RED4_OFF;
LED_GREEN2_ON; //绿灯2和绿灯4打开
LED_GREEN4_ON;
BEEP1_ON; //蜂鸣器1和蜂鸣器3打开
BEEP3_ON;
delay(200); //2和4马路通行,1和3马路不通行
LED_GREEN2_OFF; //绿灯2和绿灯4关闭
LED_GREEN4_OFF;
BEEP1_OFF; //蜂鸣器1和蜂鸣器3关闭
BEEP3_OFF;
LED_YELLOW2_ON; //黄灯2和黄灯4打开
LED_YELLOW4_ON;
delay(100);
LED_YELLOW2_OFF; //黄灯2和黄灯4关闭
LED_YELLOW4_OFF;
LED_RED2_ON; //红灯2和红灯4打开
LED_RED4_ON;
delay(50);
}
}
led.c
/**
******************************************************************************
* @file led.c
* @author 陈
* @version Vx.x
* @date 2021.4.13
* @brief xxx.
******************************************************************************
* @attention
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include “led.h”
#include “stm32f10x.h”
void LED_Cfg(void)
{
GPIO_InitTypeDef led_gpio;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); // 使能端口B的时钟
/* LED I/O配置 */
led_gpio.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|
GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12; //1-12引脚
led_gpio.GPIO_Mode = GPIO_Mode_Out_PP; // 通用推挽输出
led_gpio.GPIO_Speed = GPIO_Speed_2MHz; // 2MHz
GPIO_Init(GPIOA, &led_gpio);
/* 配置完成后关闭所有LED */
LED_GREEN1_OFF;
LED_YELLOW1_OFF;
LED_RED1_OFF;
LED_GREEN2_OFF;
LED_YELLOW2_OFF;
LED_RED2_OFF;
LED_GREEN3_OFF;
LED_YELLOW3_OFF;
LED_RED3_OFF;
LED_GREEN4_OFF;
LED_YELLOW4_OFF;
LED_RED4_OFF;
}
led.h
/**
******************************************************************************
* @file led.h
* @author 陈
* @version Vx.x
* @date 2021.4.13
* @brief xxx.
******************************************************************************
* @attention
*
******************************************************************************
*/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __LED_H
#define __LED_H
/* Includes ------------------------------------------------------------------*/
#include “stm32f10x.h”
/* Exported types ------------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/
/* Exported macro ------------------------------------------------------------*/
/* 控制LED1(直接操作寄存器) */
//#define LED1_TOGGLE GPIOA-》ODR ^= GPIO_Pin_13 // PA0输出相反状态 - LED1状态反转
//#define LED1_OFF GPIOA-》BSRR = GPIO_Pin_13 // PA0输出高电平 - LED1关
//#define LED1_ON GPIOA-》BRR = GPIO_Pin_13 // PA0输出低电平 - LED1开
/* 控制LED1(调用库函数) */
#define LED_GREEN1_OFF GPIO_SetBits(GPIOA, GPIO_Pin_1) // PB1输出高电平 - LED1关
#define LED_GREEN1_ON GPIO_ResetBits(GPIOA, GPIO_Pin_1) // PB1输出低电平 - LED1开
#define LED_YELLOW1_OFF GPIO_SetBits(GPIOA, GPIO_Pin_2) // PB2输出高电平 - LED2关
#define LED_YELLOW1_ON GPIO_ResetBits(GPIOA, GPIO_Pin_2) // PB2输出低电平 - LED2开
#define LED_RED1_OFF GPIO_SetBits(GPIOA, GPIO_Pin_3) // PB3输出高电平 - LED3关
#define LED_RED1_ON GPIO_ResetBits(GPIOA, GPIO_Pin_3) // PB3输出低电平 - LED3开
#define LED_GREEN2_OFF GPIO_SetBits(GPIOA, GPIO_Pin_4) // PB4输出高电平 - LED4关
#define LED_GREEN2_ON GPIO_ResetBits(GPIOA, GPIO_Pin_4) // PB4输出低电平 - LED4开
#define LED_YELLOW2_OFF GPIO_SetBits(GPIOA, GPIO_Pin_5) // PB5输出高电平 - LED5关
#define LED_YELLOW2_ON GPIO_ResetBits(GPIOA, GPIO_Pin_5) // PB5输出低电平 - LED5开
#define LED_RED2_OFF GPIO_SetBits(GPIOA, GPIO_Pin_6) // PB6输出高电平 - LED6关
#define LED_RED2_ON GPIO_ResetBits(GPIOA, GPIO_Pin_6) // PB6输出低电平 - LED6开
#define LED_GREEN3_OFF GPIO_SetBits(GPIOA, GPIO_Pin_7) // PB7输出高电平 - LED7关
#define LED_GREEN3_ON GPIO_ResetBits(GPIOA, GPIO_Pin_7) // PB7输出低电平 - LED7开
#define LED_YELLOW3_OFF GPIO_SetBits(GPIOA, GPIO_Pin_8) // PB8输出高电平 - LED8关
#define LED_YELLOW3_ON GPIO_ResetBits(GPIOA, GPIO_Pin_8) // PB8输出低电平 - LED8开
#define LED_RED3_OFF GPIO_SetBits(GPIOA, GPIO_Pin_9) // PB9输出高电平 - LED9关
#define LED_RED3_ON GPIO_ResetBits(GPIOA, GPIO_Pin_9) // PB9输出低电平 - LED9开
#define LED_GREEN4_OFF GPIO_SetBits(GPIOA, GPIO_Pin_10) // PB10输出高电平 - LED10关
#define LED_GREEN4_ON GPIO_ResetBits(GPIOA, GPIO_Pin_10) // PB10输出低电平 - LED10开
#define LED_YELLOW4_OFF GPIO_SetBits(GPIOA, GPIO_Pin_11) // PB11输出高电平 - LED11关
#define LED_YELLOW4_ON GPIO_ResetBits(GPIOA, GPIO_Pin_11) // PB11输出低电平 - LED11开
#define LED_RED4_OFF GPIO_SetBits(GPIOA, GPIO_Pin_12) // PB12输出高电平 - LED12关
#define LED_RED4_ON GPIO_ResetBits(GPIOA, GPIO_Pin_12) // PB12输出低电平 - LED12开
/* Exported functions ------------------------------------------------------- */
void LED_Cfg(void);
#endif /* __LED_H */
beep.c
/**
******************************************************************************
* @file beep.c
* @author 陈
* @version Vx.x
* @date 2021.4.24
* @brief xxx.
******************************************************************************
* @attention
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include “beep.h”
#include “stm32f10x.h”
void BEEP_Cfg(void)
{
GPIO_InitTypeDef beep_gpio;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); // 使能端口B的时钟
/* LED I/O配置 */
beep_gpio.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3; //14引脚
beep_gpio.GPIO_Mode = GPIO_Mode_Out_PP; // 通用推挽输出
beep_gpio.GPIO_Speed = GPIO_Speed_2MHz; // 2MHz
GPIO_Init(GPIOB, &beep_gpio);
/* 配置完成后关闭所有BEEP */
BEEP0_OFF;
BEEP1_OFF;
BEEP2_OFF;
BEEP3_OFF;
}
beep.h
/**
******************************************************************************
* @file beep.h
* @author 陈
* @version Vx.x
* @date 2021.4.24
* @brief xxx.
******************************************************************************
* @attention
*
******************************************************************************
*/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __BEEP_H
#define __BEEP_H
/* Includes ------------------------------------------------------------------*/
#include “stm32f10x.h”
/* Exported types ------------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/
/* Exported macro ------------------------------------------------------------*/
/* 控制BEEP(调用库函数) */
#define BEEP0_OFF GPIO_SetBits(GPIOB, GPIO_Pin_0) // PB13输出高电平 - BEEP0关
#define BEEP0_ON GPIO_ResetBits(GPIOB, GPIO_Pin_0) // PB13输出低电平 - BEEP0开
#define BEEP1_OFF GPIO_SetBits(GPIOB, GPIO_Pin_1) // PB13输出高电平 - BEEP1关
#define BEEP1_ON GPIO_ResetBits(GPIOB, GPIO_Pin_1) // PB13输出低电平 - BEEP1开
#define BEEP2_OFF GPIO_SetBits(GPIOB, GPIO_Pin_2) // PB13输出高电平 - BEEP2关
#define BEEP2_ON GPIO_ResetBits(GPIOB, GPIO_Pin_2) // PB13输出低电平 - BEEP2开
#define BEEP3_OFF GPIO_SetBits(GPIOB, GPIO_Pin_3) // PB13输出高电平 - BEEP3关
#define BEEP3_ON GPIO_ResetBits(GPIOB, GPIO_Pin_3) // PB13输出低电平 - BEEP3开
/* Exported functions ------------------------------------------------------- */
void BEEP_Cfg(void);
#endif /* __BEEP_H */
proteus中仿真电路图
红×4、绿灯×4、黄灯×4、蜂鸣器×4、使用stm32f10c8t6芯片、
绿灯通行时,蜂鸣器响起提醒,延时函数简单编写不能精确的延时
keil5代码
main函数代码
#include “stm32f10x.h”
#include “led.h”
#include “beep.h”
void delay(int len)
{
volatile unsigned int i = 6*1000*len;
while(i--);
}
int main(void)
{
LED_Cfg();
BEEP_Cfg();
while(1){
LED_RED1_OFF; //红灯1和红灯3关闭
LED_RED3_OFF;
LED_GREEN1_ON; //绿灯1和绿灯3打开
LED_GREEN3_ON;
BEEP0_ON; //蜂鸣器0和蜂鸣器2打开
BEEP2_ON;
LED_RED2_ON; //红灯2和红灯4打开
LED_RED4_ON;
delay(200); //1和3马路通行,2和4马路不通行
LED_GREEN1_OFF; //绿灯1和绿灯3关闭
LED_GREEN3_OFF;
BEEP0_OFF; //蜂鸣器0和蜂鸣器2关闭
BEEP2_OFF;
LED_YELLOW1_ON; //黄灯1和黄灯3打开
LED_YELLOW3_ON;
delay(100);
LED_YELLOW1_OFF; //黄灯1和黄灯3关闭
LED_YELLOW3_OFF;
LED_RED1_ON; //红灯1和红灯3打开
LED_RED3_ON;
delay(50);
LED_RED2_OFF; //红灯2和红灯4关闭
LED_RED4_OFF;
LED_GREEN2_ON; //绿灯2和绿灯4打开
LED_GREEN4_ON;
BEEP1_ON; //蜂鸣器1和蜂鸣器3打开
BEEP3_ON;
delay(200); //2和4马路通行,1和3马路不通行
LED_GREEN2_OFF; //绿灯2和绿灯4关闭
LED_GREEN4_OFF;
BEEP1_OFF; //蜂鸣器1和蜂鸣器3关闭
BEEP3_OFF;
LED_YELLOW2_ON; //黄灯2和黄灯4打开
LED_YELLOW4_ON;
delay(100);
LED_YELLOW2_OFF; //黄灯2和黄灯4关闭
LED_YELLOW4_OFF;
LED_RED2_ON; //红灯2和红灯4打开
LED_RED4_ON;
delay(50);
}
}
led.c
/**
******************************************************************************
* @file led.c
* @author 陈
* @version Vx.x
* @date 2021.4.13
* @brief xxx.
******************************************************************************
* @attention
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include “led.h”
#include “stm32f10x.h”
void LED_Cfg(void)
{
GPIO_InitTypeDef led_gpio;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); // 使能端口B的时钟
/* LED I/O配置 */
led_gpio.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|
GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12; //1-12引脚
led_gpio.GPIO_Mode = GPIO_Mode_Out_PP; // 通用推挽输出
led_gpio.GPIO_Speed = GPIO_Speed_2MHz; // 2MHz
GPIO_Init(GPIOA, &led_gpio);
/* 配置完成后关闭所有LED */
LED_GREEN1_OFF;
LED_YELLOW1_OFF;
LED_RED1_OFF;
LED_GREEN2_OFF;
LED_YELLOW2_OFF;
LED_RED2_OFF;
LED_GREEN3_OFF;
LED_YELLOW3_OFF;
LED_RED3_OFF;
LED_GREEN4_OFF;
LED_YELLOW4_OFF;
LED_RED4_OFF;
}
led.h
/**
******************************************************************************
* @file led.h
* @author 陈
* @version Vx.x
* @date 2021.4.13
* @brief xxx.
******************************************************************************
* @attention
*
******************************************************************************
*/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __LED_H
#define __LED_H
/* Includes ------------------------------------------------------------------*/
#include “stm32f10x.h”
/* Exported types ------------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/
/* Exported macro ------------------------------------------------------------*/
/* 控制LED1(直接操作寄存器) */
//#define LED1_TOGGLE GPIOA-》ODR ^= GPIO_Pin_13 // PA0输出相反状态 - LED1状态反转
//#define LED1_OFF GPIOA-》BSRR = GPIO_Pin_13 // PA0输出高电平 - LED1关
//#define LED1_ON GPIOA-》BRR = GPIO_Pin_13 // PA0输出低电平 - LED1开
/* 控制LED1(调用库函数) */
#define LED_GREEN1_OFF GPIO_SetBits(GPIOA, GPIO_Pin_1) // PB1输出高电平 - LED1关
#define LED_GREEN1_ON GPIO_ResetBits(GPIOA, GPIO_Pin_1) // PB1输出低电平 - LED1开
#define LED_YELLOW1_OFF GPIO_SetBits(GPIOA, GPIO_Pin_2) // PB2输出高电平 - LED2关
#define LED_YELLOW1_ON GPIO_ResetBits(GPIOA, GPIO_Pin_2) // PB2输出低电平 - LED2开
#define LED_RED1_OFF GPIO_SetBits(GPIOA, GPIO_Pin_3) // PB3输出高电平 - LED3关
#define LED_RED1_ON GPIO_ResetBits(GPIOA, GPIO_Pin_3) // PB3输出低电平 - LED3开
#define LED_GREEN2_OFF GPIO_SetBits(GPIOA, GPIO_Pin_4) // PB4输出高电平 - LED4关
#define LED_GREEN2_ON GPIO_ResetBits(GPIOA, GPIO_Pin_4) // PB4输出低电平 - LED4开
#define LED_YELLOW2_OFF GPIO_SetBits(GPIOA, GPIO_Pin_5) // PB5输出高电平 - LED5关
#define LED_YELLOW2_ON GPIO_ResetBits(GPIOA, GPIO_Pin_5) // PB5输出低电平 - LED5开
#define LED_RED2_OFF GPIO_SetBits(GPIOA, GPIO_Pin_6) // PB6输出高电平 - LED6关
#define LED_RED2_ON GPIO_ResetBits(GPIOA, GPIO_Pin_6) // PB6输出低电平 - LED6开
#define LED_GREEN3_OFF GPIO_SetBits(GPIOA, GPIO_Pin_7) // PB7输出高电平 - LED7关
#define LED_GREEN3_ON GPIO_ResetBits(GPIOA, GPIO_Pin_7) // PB7输出低电平 - LED7开
#define LED_YELLOW3_OFF GPIO_SetBits(GPIOA, GPIO_Pin_8) // PB8输出高电平 - LED8关
#define LED_YELLOW3_ON GPIO_ResetBits(GPIOA, GPIO_Pin_8) // PB8输出低电平 - LED8开
#define LED_RED3_OFF GPIO_SetBits(GPIOA, GPIO_Pin_9) // PB9输出高电平 - LED9关
#define LED_RED3_ON GPIO_ResetBits(GPIOA, GPIO_Pin_9) // PB9输出低电平 - LED9开
#define LED_GREEN4_OFF GPIO_SetBits(GPIOA, GPIO_Pin_10) // PB10输出高电平 - LED10关
#define LED_GREEN4_ON GPIO_ResetBits(GPIOA, GPIO_Pin_10) // PB10输出低电平 - LED10开
#define LED_YELLOW4_OFF GPIO_SetBits(GPIOA, GPIO_Pin_11) // PB11输出高电平 - LED11关
#define LED_YELLOW4_ON GPIO_ResetBits(GPIOA, GPIO_Pin_11) // PB11输出低电平 - LED11开
#define LED_RED4_OFF GPIO_SetBits(GPIOA, GPIO_Pin_12) // PB12输出高电平 - LED12关
#define LED_RED4_ON GPIO_ResetBits(GPIOA, GPIO_Pin_12) // PB12输出低电平 - LED12开
/* Exported functions ------------------------------------------------------- */
void LED_Cfg(void);
#endif /* __LED_H */
beep.c
/**
******************************************************************************
* @file beep.c
* @author 陈
* @version Vx.x
* @date 2021.4.24
* @brief xxx.
******************************************************************************
* @attention
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include “beep.h”
#include “stm32f10x.h”
void BEEP_Cfg(void)
{
GPIO_InitTypeDef beep_gpio;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); // 使能端口B的时钟
/* LED I/O配置 */
beep_gpio.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3; //14引脚
beep_gpio.GPIO_Mode = GPIO_Mode_Out_PP; // 通用推挽输出
beep_gpio.GPIO_Speed = GPIO_Speed_2MHz; // 2MHz
GPIO_Init(GPIOB, &beep_gpio);
/* 配置完成后关闭所有BEEP */
BEEP0_OFF;
BEEP1_OFF;
BEEP2_OFF;
BEEP3_OFF;
}
beep.h
/**
******************************************************************************
* @file beep.h
* @author 陈
* @version Vx.x
* @date 2021.4.24
* @brief xxx.
******************************************************************************
* @attention
*
******************************************************************************
*/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __BEEP_H
#define __BEEP_H
/* Includes ------------------------------------------------------------------*/
#include “stm32f10x.h”
/* Exported types ------------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/
/* Exported macro ------------------------------------------------------------*/
/* 控制BEEP(调用库函数) */
#define BEEP0_OFF GPIO_SetBits(GPIOB, GPIO_Pin_0) // PB13输出高电平 - BEEP0关
#define BEEP0_ON GPIO_ResetBits(GPIOB, GPIO_Pin_0) // PB13输出低电平 - BEEP0开
#define BEEP1_OFF GPIO_SetBits(GPIOB, GPIO_Pin_1) // PB13输出高电平 - BEEP1关
#define BEEP1_ON GPIO_ResetBits(GPIOB, GPIO_Pin_1) // PB13输出低电平 - BEEP1开
#define BEEP2_OFF GPIO_SetBits(GPIOB, GPIO_Pin_2) // PB13输出高电平 - BEEP2关
#define BEEP2_ON GPIO_ResetBits(GPIOB, GPIO_Pin_2) // PB13输出低电平 - BEEP2开
#define BEEP3_OFF GPIO_SetBits(GPIOB, GPIO_Pin_3) // PB13输出高电平 - BEEP3关
#define BEEP3_ON GPIO_ResetBits(GPIOB, GPIO_Pin_3) // PB13输出低电平 - BEEP3开
/* Exported functions ------------------------------------------------------- */
void BEEP_Cfg(void);
#endif /* __BEEP_H */
proteus中仿真电路图
举报