交通灯控制系统程式

前一段时间做的一个课程设计,使用SST 89c54 MSC做中心控制器,本来要求使用asm来写程序,写了一半我觉得开发效率太低,还是c来的快,于是重新写了一下,现在无聊帖出来玩玩。IDE用的Keil。

#include <SST/SST89x5x4.h>

sbit NS_RED=P0^0;
sbit NS_YELLOW=P0^1;
sbit NS_GREEN=P0^2;
sbit EW_RED=P0^3;
sbit EW_YELLOW=P0^4;
sbit EW_GREEN=P0^5;

#define RED 0
#define YELLOW 1
#define GREEN 2

//KEY is controlled by the pedestrians
sbit  KEY=P2^0;
// NS_COLOR:
// 0 - RED / 1 - YELLOW / 2 - GREEN

// LED_POS: 0,1,2,3 - NS_H,NS_L,EW_H,EW_L
unsigned char NS_COLOR, EW_COLOR, NS_TIME, EW_TIME, LED_POS,debounce,counter;

//Var for numeric display LED(LED_POS)
#define NS_H 2
#define NS_L 4
#define EW_H 8
#define EW_L 1

bit pressed;

/*******************************
 * Key Pressed
 *******************************/

void keyPressed() {
 if ((NS_COLOR == RED) && (NS_TIME > 5)) { //the pedestrian wanna cross the NS way
  pressed = 1;
  NS_TIME = 5;
  EW_COLOR = YELLOW;
  EW_TIME = 5;
 }
}

/*****************************
 * Update per second
 *****************************/

void secondUp() {
 // make sure time is never negative
 NS_TIME--;
 EW_TIME--;

 if (NS_TIME == 0) {
  switch (NS_COLOR) {
   case RED://when NS is RED,if key is pressed,NS will be 20s of GREEN,or NS should be 18s of GREEN
    NS_COLOR = GREEN;
    if (pressed)
     {NS_TIME = 20;pressed = 0;}
    else
     NS_TIME = 18;
    break;
   case YELLOW:
    NS_COLOR = RED;
    NS_TIME = 20;
    break;
   case GREEN:
        NS_COLOR = YELLOW;
    NS_TIME = 2;
       break;
  }
 }

 if (EW_TIME == 0) {
  switch (EW_COLOR) {
   case RED:
    EW_COLOR = GREEN;
    EW_TIME = 18;
    break;
   case YELLOW://when EW is YELLOW,if key is pressed,EW will be 22s of RED,or will be 20s of RED
    EW_COLOR = RED;
   if (pressed)
     {EW_TIME = 22;pressed = 0;}
    else
     EW_TIME = 20;
    break;
   case GREEN:
        EW_COLOR = YELLOW;
    EW_TIME = 2;
       break;
  }
 }
}

void display() {
 unsigned char DISP,LED;
 NS_RED = (NS_COLOR != RED);
 NS_YELLOW = (NS_COLOR != YELLOW);
 NS_GREEN = (NS_COLOR != GREEN);
 EW_RED = (EW_COLOR != RED);
 EW_YELLOW = (EW_COLOR != YELLOW);
 EW_GREEN = (EW_COLOR != GREEN);
 switch (LED_POS) {
  case NS_H:
   LED_POS = NS_L;
   DISP = (int) (NS_TIME / 10);
   break;
  case NS_L:
   LED_POS = EW_H;
   DISP = (int) (NS_TIME % 10);
   break;
  case EW_H:
   LED_POS = EW_L;
   DISP = (int) (EW_TIME / 10);
   break;
  case EW_L:
   LED_POS = NS_H;
   DISP = (int) (EW_TIME % 10);
   break;
 }
 // LED defined as char (8 bits)(4 bits for the number,and 4 bits for the position)
 LED = (DISP << 4) | LED_POS;
 P1 = LED;//output the one bit number
}

void main(void){
//initialization
NS_COLOR=RED;
EW_COLOR=GREEN;
NS_TIME=20;
EW_TIME=18;
LED_POS=NS_H;
pressed=0;
debounce=0;
counter=0;
//Timer 2e16-5*10e-3/(11.0592e-6/12)=60536=0xec78
TMOD=0x01;
TR0=1;
for(;;){
 TH0 = 0xEC;
 TL0 = 0x78;
 do{}while(!TF0);
//DO IT!
// in main pressed = 0; initialize time to a positive value

if (!KEY){//the key control by the pedestrians
 debounce++;
}

if (debounce >= 20) {//pressed and holded the key over 5 x 20 = 100 ms
 debounce = 0;
 keyPressed(); 
}

counter++;

if (counter >= 200) {//Now one second just passed.
 counter = 0;
 secondUp();
}

display();//Display the right light with the current second time.


//END OF DOIT
TF0=0;//reset the timer overflow status bit
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
c语言实现单片机的键盘程序 #include "SST89x5x4.H" #include #define uchar unsigned char #define uint unsigned int #define _Nop() _nop_() unsigned char code Key_Value_Table[16]={0xff,0x00,0x01,0xff,0x02,0xff,0xff,0xff, 0x03,0xff,0xff,0xff,0xff,0xff,0xff,0xff}; void Key_Init(void); unsigned char GetScanKey(void); unsigned char GetKey(void); void LCD_Init(void); void LCD_Init2(void); //void WriteW(uint a); void CheckBF(void); unsigned char Key_ASC2(unsigned char); void WritD(unsigned char); void Delay_ns(int i); void Delay_ms(int ms); unsigned char key; unsigned char key_asc2; unsigned char bKeyUp_Flag; uchar xdata *ptr; //函数功能描述:键盘初始化,将标志位置1; void Key_Init(void) { bKeyUp_Flag=1;//标志(全局变量)位置1 } //函数功能描述:键盘扫描函数,得到键的行列位置; unsigned char GetScanKey(void) { unsigned char key, i, temp; unsigned char xdata * ptr; key=0xff; for (i=1; i<0x10; i<<=1) //i的低4位为行数位,行依次检测 循环4次 { ptr=0x8fff; //数码管位选地址 * ptr =i; temp = * ptr; //取键盘IO口的值 temp &= 0x0f; //屏蔽高四位 if (temp!=0x00) //是否有有效键值 { key = i<<4; //取行数位的值并将其放入返回值高4位 key|=temp; //列数位的值放入返回值低4位 break; } } return key; //返回行位(高四)和列位(低四) } /*函数功能描述:取键值,长按无效; unsigned char code Key_Value_Table[16]={0xff,0x00,0x01,0xff,0x02,0xff,0xff,0xff, 0x03,0xff,0xff,0xff,0xff,0xff,0xff,0xff}; 说明:计算因子,定义在函数外部。此数组在计算键值的中间过程起作用。比如右下方键按下(行列值为0x88),通过查找数组得到行对应的中间值0x03,列对应的中间值0x03。 */ unsigned char GetKey(void) { unsigned char key, temp; if (!bKeyUp_Flag) //判断标志,是0执行 /*按键程序执行一次后会将bKeyUp_Flag标志位清零,执行此段程序,长按键无效返回无效值,直至按键无效返回无效按键值,置"1"标志位。按键输入恢复有效。屏蔽这部分则长按键有效*/ { key=GetScanKey(); if (key==0xff) //没有按键,置标志位 bKeyUp_Flag=1; else //保持按键 return 0xff; //因为0xff大于15,故为无效键值,实现长按键无效 } key=GetScanKey(); if (key==0xff) //没有按键 return key; else //有按键有效 temp=key; //取键值 Delay_ms(20); //延时20ms 消抖 key=GetScanKey(); //键盘扫描 if(key!=temp) //判断两次键值是否相同,排除干扰信号影响 确认有效信号 { key=0xff; return key; }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值