十字路口路灯模拟
stm32单片机源程序如下:
- /* Includes ------------------------------------------------------------------*/
- #include "stm32f10x.h"
- GPIO_InitTypeDef GPIO_InitStructure;
- #define KEY_0 GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_0)
- #define KEY_1 GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1)
- #define KEY_2 GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_2)
- #define LED_EW_R_ON GPIO_SetBits(GPIOA, GPIO_Pin_3);
- #define LED_EW_R_OFF GPIO_ResetBits(GPIOA, GPIO_Pin_3);
- #define LED_EW_G_ON GPIO_SetBits(GPIOA, GPIO_Pin_4);
- #define LED_EW_G_OFF GPIO_ResetBits(GPIOA, GPIO_Pin_4);
- #define LED_EW_Y_ON GPIO_SetBits(GPIOA, GPIO_Pin_5);
- #define LED_EW_Y_OFF GPIO_ResetBits(GPIOA, GPIO_Pin_5);
- #define LED_NS_R_ON GPIO_SetBits(GPIOC, GPIO_Pin_6);
- #define LED_NS_R_OFF GPIO_ResetBits(GPIOC, GPIO_Pin_6);
- #define LED_NS_G_ON GPIO_SetBits(GPIOC, GPIO_Pin_7);
- #define LED_NS_G_OFF GPIO_ResetBits(GPIOC, GPIO_Pin_7);
- #define LED_NS_Y_ON GPIO_SetBits(GPIOC, GPIO_Pin_8);
- #define LED_NS_Y_OFF GPIO_ResetBits(GPIOC, GPIO_Pin_8);
- char number=0,type=1; //闪烁次数,操作类型变量
- void RCC_Configuration(void);
- void LED_Config(void);
- void Delay(int x);
- void Traffic(void);
- void fShowLight1(void);
- void fShowLight2(void);
- void fShowLight3(void);
- void offLights(void);
- /****************************************************************************
- * 名 称:void LED_Config(void)
- * 功 能:LED 控制初始化函数
- * 入口参数:无
- * 出口参数:无
- * 说 明:
- * 调用方法:无
- ****************************************************************************/
- void LED_Config(void){
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC , ENABLE);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_0|GPIO_Pin_2;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- }
- /****************************************************************************
- * 名 称:int main(void)
- * 功 能:主函数
- * 入口参数:无
- * 出口参数:无
- * 说 明:
- * 调用方法:无
- ****************************************************************************/
- int main(void)
- {
- RCC_Configuration(); //系统时钟配置
- LED_Config(); //LED控制配置
- while (1)
- {
- if(KEY_0&&!KEY_1&&!KEY_2){
- Traffic();
- }
- else if(KEY_1&&!KEY_0&&!KEY_2){
- fShowLight1();
- }
- else if(KEY_2&&!KEY_0&&!KEY_1){
- fShowLight3();
- }
- else{
- offLights();
- }
- }
- }
- /****************************************************************************
- * 名 称:void RCC_Configuration(void)
- * 功 能:系统时钟配置为72MHZ
- * 入口参数:无
- * 出口参数:无
- * 说 明:
- * 调用方法:无
- ****************************************************************************/
- void RCC_Configuration(void)
- {
- SystemInit();
- }
- /****************************************************************************
- * 名 称:void Delay(int x)
- * 功 能:延时函数
- * 入口参数:无
- * 出口参数:无
- * 说 明:
- * 调用方法:无
- ****************************************************************************/
- void Delay(int x)
- {
- char i;
- while(x--) for(i=0;i<120;i++);
- }
- /****************************************************************************
- * 名 称:void Traffic(void)
- * 功 能:交通灯切换函数
- * 入口参数:无
- * 出口参数:无
- * 说 明:
- * 调用方法:无
- ****************************************************************************/
- void Traffic(void)
- {
- switch(type)
- {
- case 1: //东西向绿灯与南北向红灯亮
- LED_EW_R_OFF;LED_EW_Y_OFF;LED_EW_G_ON;
- LED_NS_R_ON;LED_NS_Y_OFF;LED_NS_G_OFF;
- Delay(200);
- type=2;
- break;
- case 2: //东西向黄灯闪烁,绿灯关闭
- LED_EW_Y_ON;LED_EW_G_OFF;
- Delay(10);
- LED_EW_Y_OFF;LED_EW_G_OFF;
- Delay(10);
- if(++number!=3) return; //闪烁3次
- number=0;
- type=3;
- break;
- case 3: //东西向红灯,南北向绿灯亮
- LED_EW_R_ON;LED_EW_Y_OFF;LED_EW_G_OFF;
- LED_NS_R_OFF;LED_NS_Y_OFF;LED_NS_G_ON;
- Delay(200);
- type=4;
- break;
- case 4: //南北向黄灯闪烁3次
- LED_NS_Y_ON;LED_NS_G_OFF;
- Delay(10);
- LED_NS_Y_OFF;LED_NS_G_OFF;
- Delay(10);
- if(++number!=3) return;
- number=0;
- type=1;
- }
- }
- /****************************************************************************
- * 名 称:void fShowLight1(void)
- * 功 能:六灯走马效果
- * 入口参数:无
- * 出口参数:无
- * 说 明:
- * 调用方法:无
- ****************************************************************************/
- void fShowLight1(void)
- {
- LED_EW_R_ON;LED_EW_G_OFF;LED_EW_Y_OFF;
- LED_NS_R_OFF;LED_NS_G_OFF;LED_NS_Y_ON;
- Delay(200);
- LED_EW_R_ON;LED_EW_G_ON;LED_EW_Y_OFF;
- LED_NS_R_OFF;LED_NS_G_OFF;LED_NS_Y_OFF;
- Delay(200);
- LED_EW_R_OFF;LED_EW_G_ON;LED_EW_Y_ON;
- LED_NS_R_OFF;LED_NS_G_OFF;LED_NS_Y_OFF;
- Delay(200);
- LED_EW_R_OFF;LED_EW_G_OFF;LED_EW_Y_ON;
- LED_NS_R_ON;LED_NS_G_OFF;LED_NS_Y_OFF;
- Delay(200);
- LED_EW_R_OFF;LED_EW_G_OFF;LED_EW_Y_OFF;
- LED_NS_R_ON;LED_NS_G_ON;LED_NS_Y_OFF;
- Delay(200);
- LED_EW_R_OFF;LED_EW_G_OFF;LED_EW_Y_OFF;
- LED_NS_R_OFF;LED_NS_G_ON;LED_NS_Y_ON;
- Delay(200);
- }
- /****************************************************************************
- * 名 称:void fShowLight2(void)
- * 功 能:六灯闪烁效果
- * 入口参数:无
- * 出口参数:无
- * 说 明:
- * 调用方法:无
- ****************************************************************************/
- void fShowLight2(void)
- {
- LED_EW_R_ON;LED_EW_G_OFF;LED_EW_Y_OFF;
- LED_NS_R_ON;LED_NS_G_OFF;LED_NS_Y_OFF;
- Delay(200);
- LED_EW_R_OFF;LED_EW_G_ON;LED_EW_Y_OFF;
- LED_NS_R_OFF;LED_NS_G_ON;LED_NS_Y_OFF;
- Delay(200);
- LED_EW_R_OFF;LED_EW_G_OFF;LED_EW_Y_ON;
- LED_NS_R_OFF;LED_NS_G_OFF;LED_NS_Y_ON;
- Delay(200);
- }
- /****************************************************************************
- * 名 称:void fShowLight3(void)
- * 功 能:六灯反复流水效果
- * 入口参数:无
- * 出口参数:无
- * 说 明:
- * 调用方法:无
- ****************************************************************************/
- void fShowLight3(void)
- {
- LED_EW_R_ON;LED_EW_G_OFF;LED_EW_Y_OFF;
- LED_NS_R_OFF;LED_NS_G_OFF;LED_NS_Y_OFF;
- Delay(200);
- LED_EW_R_OFF;LED_EW_G_ON;LED_EW_Y_OFF;
- LED_NS_R_OFF;LED_NS_G_OFF;LED_NS_Y_OFF;
- Delay(200);
- LED_EW_R_OFF;LED_EW_G_OFF;LED_EW_Y_ON;
- LED_NS_R_OFF;LED_NS_G_OFF;LED_NS_Y_OFF;
- Delay(200);
- LED_EW_R_OFF;LED_EW_G_OFF;LED_EW_Y_OFF;
- LED_NS_R_ON;LED_NS_G_OFF;LED_NS_Y_OFF;
- Delay(200);
- LED_EW_R_OFF;LED_EW_G_OFF;LED_EW_Y_OFF;
- LED_NS_R_OFF;LED_NS_G_ON;LED_NS_Y_OFF;
- Delay(200);
- LED_EW_R_OFF;LED_EW_G_OFF;LED_EW_Y_OFF;
- LED_NS_R_OFF;LED_NS_G_OFF;LED_NS_Y_ON;
- Delay(200);
- LED_EW_R_OFF;LED_EW_G_OFF;LED_EW_Y_OFF;
- LED_NS_R_OFF;LED_NS_G_ON;LED_NS_Y_OFF;
- Delay(200);
- LED_EW_R_OFF;LED_EW_G_OFF;LED_EW_Y_OFF;
- LED_NS_R_ON;LED_NS_G_OFF;LED_NS_Y_OFF;
- Delay(200);
- LED_EW_R_OFF;LED_EW_G_OFF;LED_EW_Y_ON;
- LED_NS_R_OFF;LED_NS_G_OFF;LED_NS_Y_OFF;
- Delay(200);
- LED_EW_R_OFF;LED_EW_G_ON;LED_EW_Y_OFF;
- LED_NS_R_OFF;LED_NS_G_OFF;LED_NS_Y_OFF;
- Delay(200);
- }
- /****************************************************************************
- * 名 称:void offLights(void)
- * 功 能:关灯
- * 入口参数:无
- * 出口参数:无
- * 说 明:
- * 调用方法:无
- ****************************************************************************/
- void offLights(void){
- LED_EW_R_OFF;LED_EW_G_OFF;LED_EW_Y_OFF;
- LED_NS_R_OFF;LED_NS_G_OFF;LED_NS_Y_OFF;
- }
- /******************* (C) COPYRIGHT 2011 奋斗STM32 *****END OF FILE****/
复制代码
所有资料51hei提供下载:
GPIO2.rar
(278.27 KB, 下载次数: 8)
|