基于protrus8设计电路原理图
先要思考明白十字路口红绿灯的逻辑
东西是一样的,南北是一样的。
东西红灯,南北就是绿灯,反之一样的,弄明白这些写代码就有思路了
中间的两个数码管是模拟真实红绿灯倒计时的用的,放置4个也行,但是为了方便观看就放一个了
下面放几张运行效果图,方便大家思考,我设计的是红绿灯都是倒计时30秒,黄灯倒计时3秒
代码
(注释在里面要好好思考,化为己用)
(这个小项目的代码稍微有点多,采用的是模块化编程,所以自己写程序的时侯要仔细,再认真理解,不要搞错了)
主函数 main.c- #include "stm32f10x.h"
- #include "smg.h"
- #include "Delay.h"
- #include "led.h"
- uint16_t table[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
- uint16_t disp[2];
- uint16_t temp,i;
- void zxm_Init(void) //字形码函数
- {
- SMG_Init(); //数码管初始化
- disp[1] = table[i/10]; //数码管显示十位数字的字形码
- disp[0] = table[i%10]; //数码管显示个位数字的字形码
- temp = (disp[1]<<8)|(disp[0]&0x0ff); //十位数的字形码左移8位,然后与个位数的字形码合并
- GPIO_Write(GPIOC,temp);
- Delay(100);
- }
- int main(void)
- {
- LED_Init(); //led初始化
- while(1)
- {
- for(i=30;i>0;i--) //数码管从30开始自减,到0结束
- {
- LED1_ON(); //东西红灯亮
- LED6_ON(); //南北绿灯亮
- zxm_Init();
- }
- for(i=3;i>0;i--) //上一个循环结束进入下一个,黄灯开始倒计时
- {
- LED1_OFF(); //东西红灯灭
- LED6_OFF(); //南北绿灯灭
- LED2_ON(); //东西黄灯亮
- LED5_ON(); //南北黄灯亮
- zxm_Init();
- }
- for(i=30;i>0;i--)
- {
- LED2_OFF(); //东西黄灯灭
- LED5_OFF(); //南北黄灯灭
- LED3_ON(); //东西绿灯亮
- LED4_ON(); //南北红灯亮;
- zxm_Init();
- }
- for(i=3;i>0;i--)
- {
- LED3_OFF(); //东西绿灯灭
- LED4_OFF(); //南北红灯灭
- LED2_ON(); //东西黄灯亮
- LED5_ON(); //南北黄灯亮
- zxm_Init();
- }
- LED2_OFF(); //东西黄灯灭
- LED5_OFF(); //南北黄灯灭
-
- //这样就结束了一个完整的循环,最后把黄灯灭掉,让程序一直在while里循环
- }
- }
复制代码
数码管函数
smg.c
- #include "stm32f10x.h"
- #include "smg.h"
- void SMG_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); //使能GPIOC时钟
- GPIO_InitStructure.GPIO_Pin = 0xffff; //引脚配置
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //配置引脚为推挽输出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //GPIOB速度为50MHz
- GPIO_Init(GPIOC, &GPIO_InitStructure); //初始化
-
- }
复制代码
smg.h- #ifndef __SMG_H
- #define __SMG_H
- void SMG_Init(void);
- #endif
复制代码
LED灯函数
led.c
- #include "stm32f10x.h"
- #include "led.h"
- void LED_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2
- |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(GPIOB, &GPIO_InitStructure);
-
- }
- void LED1_ON(void)
- {
- GPIO_SetBits(GPIOB,GPIO_Pin_0);
- }
- void LED1_OFF(void)
- {
- GPIO_ResetBits(GPIOB,GPIO_Pin_0);
- }
- void LED2_ON(void)
- {
- GPIO_SetBits(GPIOB,GPIO_Pin_1);
- }
- void LED2_OFF(void)
- {
- GPIO_ResetBits(GPIOB,GPIO_Pin_1);
- }
- void LED3_ON(void)
- {
- GPIO_SetBits(GPIOB,GPIO_Pin_2);
- }
- void LED3_OFF(void)
- {
- GPIO_ResetBits(GPIOB,GPIO_Pin_2);
- }
- void LED4_ON(void)
- {
- GPIO_SetBits(GPIOB,GPIO_Pin_3);
- }
- void LED4_OFF(void)
- {
- GPIO_ResetBits(GPIOB,GPIO_Pin_3);
- }
- void LED5_ON(void)
- {
- GPIO_SetBits(GPIOB,GPIO_Pin_4);
- }
- void LED5_OFF(void)
- {
- GPIO_ResetBits(GPIOB,GPIO_Pin_4);
- }
- void LED6_ON(void)
- {
- GPIO_SetBits(GPIOB,GPIO_Pin_5);
- }
- void LED6_OFF(void)
- {
- GPIO_ResetBits(GPIOB,GPIO_Pin_5);
- }
复制代码
led.h
- #ifndef __LED_H
- #define __LED_H
- void LED_Init(void);
- void LED1_ON(void);
- void LED1_OFF(void);
- void LED2_ON(void);
- void LED2_OFF(void);
- void LED3_ON(void);
- void LED3_OFF(void);
- void LED4_ON(void);
- void LED4_OFF(void);
- void LED5_ON(void);
- void LED5_OFF(void);
- void LED6_ON(void);
- void LED6_OFF(void);
- #endif
复制代码
延时函数
Delay.c
- #include "stm32f10x.h"
- void Delay(unsigned int count)
- {
- unsigned int i;
- for(;count!=0;count--)
- {
- i=5000;
- while(i--);
- }
- }
复制代码
Delay.h
- #ifndef __DELAY_H
- #define __DELAY_H
- void Delay(unsigned int count);
- #endif
复制代码
|