微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > STM32简易交通灯设计(定时可调)

STM32简易交通灯设计(定时可调)

时间:11-30 来源:互联网 点击:

//按键key1 设置红灯时间
按键key2 设置绿灯时间
按键key3设置黄灯时间

#include

#include "sys.h"

#include "delay.h"

#include "usart.h"

#include "stdlib.h"

static int G=0,R=0,Y=0; //设置绿,红,黄灯时间变量

#include "lcd.h"

#include "key.h"

#include "time.h"

//----------- 按键函数----------

void scan_key1()

{

static u8 i,j;

if(key1==0)

{

if(i==0)

{

j++;

if(j>3)

{

i=1;j=0;

R++;

if(R>20)R=0;

printf("key1 is inputed ");

}

}

}

else

{

i=j=0;

}

}

void scan_key2()

{

static u8 i,j;

if(key2==0)

{

if(i==0)

{

j++;

if(j>3)

{

i=1;j=0;

G++;

if(G>20)G=0;

printf("key2 is inputed. ");

}

}

}

else

{

i=j=0;

}

}

void scan_key3()

{

static u8 i,j;

if(key3==1)

{

if(i==0)

{

j++;

if(j>3)

{

i=1;j=0;

Y++;

if(Y>20)Y=0;

printf("key3 is inputed. ");

}

}

}

else

{

i=j=0;

}

}

//各指示灯显示秒数函数

void LED_RED(u8 k)

{

LCD_ShowNum(120,55,k,2,16);

}

void LED_YELLOW(u8 k)

{

LCD_ShowNum(120,145,k,2,16);

}

void LED_GREEN(u8 k)

{

LCD_ShowNum(120,235,k,2,16);

}


//-----定时设置函数-------

void LED_SET_GREEN(u8 k)

{

LCD_ShowNum(160,235,k,2,16);

}

void LED_SET_YELLOW(u8 k)

{

LCD_ShowNum(160,145,k,2,16);

}

void LED_SET_RED(u8 k)

{

LCD_ShowNum(160,55,k,2,16);

}

//-----定时器中断函数---扫描按键和显示设定值用----

void TIM3_IRQHandler(void)

{

if(TIM3->SR&0x0001)

{

scan_key1();

scan_key2();

scan_key3();

LED_SET_RED(R);

LED_SET_GREEN(G);

LED_SET_YELLOW(Y);

}

TIM3->SR=~(1<0);

}

//------主函数-----------

int main(void)

{

u16 code; //TFT驱动芯片代号

int i;

Stm32_Clock_Init(9);

delay_init(72);

uart_init(72,9600);

printf("uart is ok. ");

Timer_init(300,7199); //定时器初始化 设定溢出时间是 30ms

printf("time_init is ok. ");

LCD_Init(); //TFT-LCD初始化

printf("lcd_init is ok.");

key_init(); //按键I/O口设置初始化

printf("key_init is ok.");

JTAG_Set(JTAG_SWD_DISABLE); //关闭JTAG调试器

code=LCD_ReadReg(0x0000);

delay_ms(500);

printf("deiver code is %ld",code); //读取驱动芯片型号是发送到串口

LCD_Fill(30,200,100,270,BLACK);

LCD_Fill(30,110,100,180,BLACK);

LCD_Fill(30,20,100,90,BLACK);

LCD_ShowString(110,15,"Show");

LCD_ShowString(110,30,"Time");

LCD_ShowString(160,15,"Set ");

LCD_ShowString(160,30,"Time");

LED_GREEN(0);

LED_YELLOW(0);

LED_RED(0);

while(1)

{

for(i=G;i>=0;i--) //绿灯控制

{

LED_GREEN(i);

LCD_Fill(30,200,100,270,GREEN);

delay_ms(1000);

LCD_Fill(30,200,100,270,BLACK);

delay_ms(1000);

}

for(i=Y;i>=0;i--) //黄灯控制

{

LED_YELLOW(i);

LCD_Fill(30,110,100,180,YELLOW);

delay_ms(1000);

LCD_Fill(30,110,100,180,BLACK);

delay_ms(1000);

}

for(i=R;i>=0;i--) //红灯控制

{

LED_RED(i);

LCD_Fill(30,20,100,90,RED);

delay_ms(1000);

LCD_Fill(30,20,100,90,BLACK);

delay_ms(1000);

}

}

}

//-------各类库函数省略---------

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top