⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 分析了现代城市交通控制与管理问题的现状
💻 C
📖 第 1 页 / 共 2 页
字号:
//===============================================================
//
//        The information contained herein is the exclusive property of
//      Sunplus Technology Co. And shall not be distributed, reproduced,
//      or disclosed in whole in part without prior written permission.
//
//            (C) COPYRIGHT 2001   SUNPLUS TECHNOLOGY CO.
//                           ALL RIGHTS RESERVED
//
//    The entire notice above must be reproduced on all authorized copies.
//
//==============================================================

//============================================================
//  工程名称:   TrafficLight.spj
//  功能描述:    模拟交通灯控制实验
//  IDE环境:   SUNPLUS u'nSPTM  IDE 1.8.4(or later)
//
//  涉及的库:	(a) C-Lib:  (CMacroB100.lib);
//				(b) sacmv25.lib
//
//  组成文件:
//    			 main.c
//				 hardware.asm
//     			 sram.asm
//     			 sys.asm
//     			 spce061.inc
//     			 spce061v004.h
//     			 s480.inc
//     			 s480.h
//				 unSPMACRO.h
//				 TrafficLight.h
//
//	Note:
//			常量和变量的定义请参考 TrafficLight.h
//
/*
61板与模拟交通灯控制板的接线方法
	61板		交通灯板
	+5V			VCC
	GND			VSS
	
	IOA4		S_L
	IOA5		S_S
	IOA6		S_R
	IOA7		N_L
	IOA8		N_S
	IOA9		N_R
	IOA10		E_L
	IOA11		E_S
	IOA12		E_R
	IOA13		W_L
	IOA14		W_S
	IOA15		W_R

	IOB0		CS0
	IOB1		CS1
	IOB2		CS2
	IOB3		CS3
	IOB4		CS4
	IOB5		CS5
	
	IOB8		IO-0
	IOB9		IO-1
	IOB10		IO-2
	IOB11		IO-3
	IOB12		IO-4
	IOB13		IO-5
	IOB14		IO-6
	IOB15		IO-7
*/
//
//  日期:        2004-4-29
//===============================================
#include "SPCE061V004.h"
#include "unSPMACRO.h"
#include "TrafficLight.h"
void MyDelay(unsigned int Ret);

main()
{
	IO_Initial();

	uiStatus = C_Status_Normal;
	uiStatus_Normal = C_1_Green;
	
	uiShowCnt = ui_SN_GreenLight;
	uiShi=uiShowCnt/10;											// 取倒计时显示的十位
	uiGe=uiShowCnt%10;											// 取倒计时显示的个位
	while(1)
	{
		if(g_uiResetIO == 20)
		{
			IO_Initial();
			g_uiResetIO = 0;
		}
		
		uiKey = GetKeyValue();									// 取得61板的按键键值								

		switch(uiStatus)
		{
			case C_Status_Normal:								// 正常运行状态
				Run_Normal();
				break;
			case C_Status_Emergency:							// 紧急状态
				Run_Emergency();
				break;
			case C_Status_Setup:								// 设置状态
				Run_Setup();
				break;
			case C_Status_Query:								// 查询状态
				Run_Query();
				break;
			default:				
				break;
		}
		
	}
}
//========================================================================================================
//函数: MyDelay
//语法:void MyDelay(unsigned int Ret)
//描述:延时函数 
//参数:Ret,延时的计数值
//返回:无 
//========================================================================================================
void MyDelay(unsigned int Ret)
{
	while(Ret--);
}

//========================================================================================================
//函数: Run_Normal
//语法:void Run_Normal(void)
//描述:正常运行函数 
//参数:无
//返回:无 
//========================================================================================================
//在正常运行状态,按下Key1会进入紧急状态,按下Key2会进入设置状态,按下Key3会进入查询状态。
void Run_Normal(void)					// 正常运行
{
	//按键处理
	switch(uiKey)
	{
		case C_Key1:
			uiStatus = C_Status_Emergency;
			break;
		case C_Key2:
			uiStatus = C_Status_Setup;
			break;
		case C_Key3:
			uiStatus = C_Status_Query;
			break;	
		case C_S_L:
			uiSouthCnt_L++;			//来自南边左转车流量加一
			break;
		case C_S_S:
			uiSouthCnt_S++;			//来自南边直行车流量加一
			break;
		case C_S_R:
			uiSouthCnt_R++;			//来自南边右转车流量加一
			break;
		case C_W_L:
			uiWeastCnt_L++;			//来自西边左转车流量加一
			break;
		case C_W_S:
			uiWeastCnt_S++;			//来自西边直行车流量加一			
			break;
		case C_W_R:
			uiWeastCnt_R++;			//来自西边右转车流量加一
			break;
		case C_N_L:
			uiNorthCnt_L++;			//来自北边左转车流量加一
			break;
		case C_N_S:
			uiNorthCnt_S++;			//来自北边直行车流量加一
			break;
		case C_N_R:
			uiNorthCnt_R++;			//来自北边右转车流量加一
			break;
		case C_E_L:
			uiEastCnt_L++;			//来自东边左转车流量加一
			break;
		case C_E_S:
			uiEastCnt_S++;			//来自东边直行车流量加一				
			break;
		case C_E_R:
			uiEastCnt_R++;			//来自东边右转车流量加一
			break;
		default :
			break;
	}
				
	//显示控制
	if(uiQuarterSecondCnt>=2)
	{
		uiQuarterSecondCnt = 0;
		uiFlash_Flag ^= 0xffff;
		ShowControl();
	}			
}

//========================================================================================================
//函数: Run_Emergency
//语法:void Run_Emergency(void)
//描述:紧急状态函数 
//参数:无
//返回:无 
//========================================================================================================
//在紧急状态下,四面都是红灯。按下Key3键返回正常运行状态。
unsigned int Cnt=0;
void Run_Emergency(void)			// 紧急状态
{
	Cnt++;	
	if(uiKey == C_Key3)	
	{
		uiStatus = C_Status_Normal;
		uiStatus_Normal = C_1_Green;
		uiShowCnt = ui_SN_GreenLight;
		uiShi=uiShowCnt/10;											// 取倒计时显示的十位
		uiGe=uiShowCnt%10;											// 取倒计时显示的个位
	}
	if(Cnt%6000==1)
		PlayS480(C_JINGDI);
	*P_IOB_Data = C_ALL_Red;
}

//========================================================================================================
//函数: Run_Setup
//语法:void Run_Setup(void)
//描述:设置状态函数 
//参数:无
//返回:无 
//========================================================================================================
//在设置状态下,按下Key1开始设置南北的红灯时间,按下Key2设置南北的绿灯时间,按下Key3返回正常运行状态。
void Run_Setup(void)				// 设置状态
{
	if(uiKey == C_Key1)	
	{
		ui_SN_RedLight++;
		if(ui_SN_RedLight>99)
			ui_SN_RedLight = 20;
		uiShowCnt = ui_SN_RedLight;
	}
	else if(uiKey == C_Key2)	
	{
		ui_SN_GreenLight++;
		if(ui_SN_GreenLight>99)
			ui_SN_GreenLight = 20;
		uiShowCnt = ui_SN_GreenLight;
	}
	else if(uiKey == C_Key3)
	{
		uiStatus = C_Status_Normal;
		uiStatus_Normal = C_1_Green;
	}

	uiShi=uiShowCnt/10;												// 取倒计时显示的十位
	uiGe=uiShowCnt%10;												// 取倒计时显示的个位	
}

//========================================================================================================
//函数: Run_Query
//语法:void Run_Query(void)
//描述:查询状态函数 
//参数:无
//返回:无 
//========================================================================================================
//在查询状态,按下Key1播报南北路口的车流量,按下Key2播报东西路口的车流量,按下Key3返回正常运行状态。
void Run_Query(void)				// 查询状态
{
	if(uiKey == C_Key1)
	{
		//南北车流量的计算
		uiSouthCnt = uiEastCnt_L + uiNorthCnt_S + uiWeastCnt_R;
		uiNorthCnt = uiWeastCnt_L + uiSouthCnt_S + uiEastCnt_R;

		//播放往南走的车流量
		Play2Number(uiSouthCnt,C_NAN);

		//播放往北走的车流量
		Play2Number(uiNorthCnt,C_BEI);
	}
	else if(uiKey == C_Key2)
	{
		//东西车流量的计算
		uiEastCnt = uiNorthCnt_L + uiWeastCnt_S + uiSouthCnt_R;
		uiWeastCnt = uiSouthCnt_L + uiEastCnt_S + uiNorthCnt_R;

		//播放往东走的车流量
		Play2Number(uiEastCnt,C_DONG);

		//播放往西走的车流量
		Play2Number(uiWeastCnt,C_XI);
	}
	else if(uiKey == C_Key3)
	{
		uiStatus = C_Status_Normal;
		uiStatus_Normal = C_1_Green;
	}
}



//========================================================================================================
//函数: PlayS480
//语法:void PlayS480(int Index)
//描述:播音函数 
//参数:Index,播音的索引地址
//返回:无 
//========================================================================================================
void PlayS480(int Index)
{
	SACM_S480_Initial(1);
	SACM_S480_Play(Index, 3, 3);
	while((SACM_S480_Status()&0x0001) != 0)
	{
		SACM_S480_ServiceLoop();
		*P_Watchdog_Clear = C_WDTCLR;
	}
	SACM_S480_Stop();
}

//========================================================================================================
//函数: Play2Number
//语法:void Play2Number(unsigned int uiNumber,unsigned int uiDirection)
//描述:播音函数 
//参数:uiNumber,要播报的两位数;uiDirection,播音的索引地址
//返回:无 
//========================================================================================================
void Play2Number(unsigned int uiNumber,unsigned int uiDirection)	
{
	unsigned int	uiPlayShi,uiPlayGe;
	PlayS480(C_ZHUYI);
	uiPlayShi = uiNumber/10;			//取十位
	uiPlayGe = uiNumber%10;				//取个位
	PlayS480(C_WANG);
	PlayS480(uiDirection);
	PlayS480(C_ZOU);
	if(uiPlayShi)
	{
		PlayS480(uiPlayShi);
		PlayS480(C_10);
		if(uiNumber%10)
			PlayS480(uiPlayGe);	
	}
	else
		PlayS480(uiPlayGe);
}


//========================================================================================================
//函数: ShowControl
//语法:void ShowControl(void)
//描述:显示函数 
//参数:无
//返回:无 
//========================================================================================================
void ShowControl(void)
{
	uiShowCntFlag++;
	switch(uiStatus_Normal)
	{
		case C_1_Green://
			//对于双色LED
			uiE_2ColorLED = C_1_E_Green;
			uiW_2ColorLED = C_1_W_Green;
			uiS_2ColorLED = C_1_S_Green;
			uiN_2ColorLED = C_1_N_Green;
			//对于数码管

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -