基于STM32的简易交通灯设计

智能交通灯控制器的设计。设计十字路口红绿灯的控制电路,根据交通规则,控制4组红绿黄共12个LED指示灯的显示。

基于STM32的简易交通灯设计_第1张图片

 

    通过对车流量的捕获,来合理的安排各个方位的红绿黄灯的时间,从而达到更高效率的交通。采用了摄像头OV7670模块来获取车流量信息,进而通知CPU进行那种方案。

方案1::所有红绿黄灯时间分别是:红灯10S,绿灯5S,绿灯闪3S,黄灯2S。

方案2:南北红灯8S,南北绿灯7S,南北绿灯闪3S,黄灯2S。

        东西红灯12S,东西绿灯3S,东西绿灯闪3S,黄灯2S。

方案3:东西红灯8S,东西绿灯7S,东西绿灯闪3S,黄灯2S。

       南北红灯12S,南北绿灯3S,南北绿灯闪3S,黄灯2S。

通过以上方案可以合理的减缓交通堵塞的情况。

 

交通灯规则有一下四种状态:

1、东西绿灯闪、黄灯亮,南北红灯。

2、东西红灯,南北绿灯。

3、东西红灯,南北绿灯闪、黄灯亮。

4、东西绿灯,南北红灯。

通过该规则可以使车合理的行驶,规范交通规则。

 

基于STM32的简易交通灯设计_第2张图片

 

#ifndef __LED_H

#define __LED_H

#include "stm32f10x.h"

 

#define GPIOC_ODR_Addr  (GPIOC_BASE+0x0C)

#define PCout(n)                            *(unsigned int *)((GPIOC_ODR_Addr&0xF0000000)+0x02000000+((GPIOC_ODR_Addr&0x00FFFFFF)<<5)+(n<<2))

 

#define GPIOC_IDR_Addr   (GPIOC_BASE+0x08)

#define PCin(n)  *(unsigned int*)((GPIOC_IDR_Addr&0xF0000000)+0x02000000+((GPIOC_IDR_Addr&0x00FFFFFF)<<5)+(n<<2))

 

#define ON  0

#define OFF 1

 

//北

#define N_R       PCout(0)

#define N_G PCout(1)

#define N_Y PCout(2)

 

//南

#define S_R PCout(3)

#define S_G PCout(4)

#define S_Y PCout(5)

 

//东

#define E_R PCout(6)

#define E_G PCout(7)

#define E_Y PCout(8)

 

//西

#define W_R PCout(9)

#define W_G PCout(10)

#define W_Y PCout(11)

 

//全灭

#define LED_MIE GPIO_Write(GPIOC,0xffff);

void LED_Config(void);

void delay_ms(u32 i);

#endif

 

 

#include "led.h"

 

void LED_Config()

{

       GPIO_InitTypeDef GPIO_InitStructure;

       RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);

      

       GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;

       GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0 | 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_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;

      

       GPIO_Init(GPIOC,&GPIO_InitStructure);

      

       LED_MIE;//灯全灭

      

}

 

void delay_ms(u32 i)

{

       u32 temp;

       SysTick->LOAD=9000*i;         //设置重装数值, 72MHZ时

       SysTick->CTRL=0X01;           //使能,减到零是无动作,采用外部时钟源

       SysTick->VAL=0;                    //清零计数器

       do

       {

              temp=SysTick->CTRL;        //读取当前倒计数值

       }

       while((temp&0x01)&&(!(temp&(1<<16))));       //等待时间到达

       SysTick->CTRL=0;    //关闭计数器

       SysTick->VAL=0;             //清空计数器

}

 

 

#include "led.h"

 

int main()

{

 

       LED_Config();

       while(1)

       {

                     u8 i=0;

                     delay_ms(1000);

                     while(i<5)

                     {

                            E_Y=OFF;

                            W_Y=OFF;

                            S_R=OFF;

                            N_R=OFF;

                            N_Y=OFF;

                            S_Y=OFF;

                            E_R=ON;

                            W_R=ON;

                            S_G=ON;

                            N_G=ON;//东西红灯、南北绿灯

                            delay_ms(1000);

                            i++;

                     }

                     i=0;

                     while(i<3)

                     {

                            S_G=ON;

                            N_G=ON;

                            delay_ms(500);

                            S_G=OFF;

                            N_G=OFF;

                            delay_ms(500);//东西红灯,南北绿灯闪

                            i++;

                     }

                     i=0;

                     while(i<2)

                     {                  

                            S_G=OFF;

                            N_G=OFF;

                            N_Y=ON;

                            S_Y=ON;

                            delay_ms(1000);//东西红灯,南北黄灯亮

                            i++;

                     }

                     i=0;

                     while(i<5)

                     {

                            E_R=OFF;

                            W_R=OFF;

                            N_Y=OFF;

                            S_Y=OFF;

                            E_G=ON;

                            W_G=ON;

                            S_R=ON;

                            N_R=ON;

                            delay_ms(1000);//东西绿灯,南北红灯

                            i++;

                     }

                     i=0;

                     while(i<3)

                     {

                            E_G=ON;

                            W_G=ON;

                            delay_ms(500);

                            E_G=OFF;

                            W_G=OFF;

                            delay_ms(500);//东西绿灯闪,南北红灯

                            i++;

                     }

                     i=0;

                     while(i<2)

                     {

                            E_G=OFF;

                            W_G=OFF;

                            E_Y=ON;

                            W_Y=ON;

                            delay_ms(1000);//东西黄灯亮,南北红灯

                            i++;

                     }

       }

}

      

 

 

 

 

 

 

 

 

你可能感兴趣的:(STM32)