|
本帖最后由 ma19920829 于 2015-7-10 15:18 编辑
- #include "reg51.h"
- #define UINT unsigned int
- #define ULONG unsigned long
- #define UCHAR unsigned char
- /*
- 信号灯变量
- 南北方向绿灯
- sbit n_bike_g = P1^0; //自行车
- sbit n_right_g = P1^1; //右转
- sbit n_up_g = P1^2; //直行
- sbit n_left_g = P1^3; //左转 调头
- 南北方向红灯
- sbit n_bike_r = P1^4; //自行车
- sbit n_right_r = P1^5; //右转
- sbit n_up_r = P1^6; //直行
- sbit n_left_r = P1^7; //左转 调头
- 南北方向黄灯
- sbit n_bike_y = P3^0; //自行车
- sbit n_right_y = P3^1; //右转
- sbit n_up_y = P3^2; //直行
- sbit n_left_y = P3^3; //左转 调头
- 东西方向绿灯
- sbit e_bike_g = P2^0; //自行车
- sbit e_right_g = P2^1; //右转
- sbit e_up_g = P2^2; //直行
- 东西方向红灯
- sbit e_bike_r = P2^4; //自行车
- sbit e_right_r = P2^5; //右转
- sbit e_up_r = P2^6; //直行
- 东西方向黄灯
- sbit e_bike_y = P3^4; //自行车
- sbit e_right_y = P3^5; //右转
- sbit e_up_y = P3^6; //直行
- */
- //延时
- void delay(UINT t, UINT s)
- {
- while (t)
- {
- UINT i;
- for (i = 0; i < s; i++)
- {
- }
- t --;
- }
- }
- //信号灯状态
- void time_x(UCHAR P_P1, UCHAR P_P2, UCHAR P_P3)
- {
- P1 = P_P1;
- P2 = P_P2;
- P3 = P_P3;
- delay(150, 65535);
- }
- void time_s(UCHAR P_P1, UCHAR P_P2, UCHAR P_P3, UCHAR P_P11, UCHAR P_P22)
- {
- UINT i;
- for (i = 0; i < 3; i ++)
- {
- P1 = P_P1;
- P2 = P_P2;
- delay(5, 65535);
- P1 = P_P11;
- P2 = P_P22;
- delay(5, 65535);
- }
- P1 = P_P1;
- P2 = P_P2;
- P3 = P_P3;
- delay(10, 65535);
- }
- //主程序
- void main()
- {
- P1 = P2 = P3 = 0x0;
- while (1)
- {
- time_x(0xA5, 0x70, 0x0);
- time_s(0xA4, 0x70, 0x1, 0xA5, 0x70);
- time_x(0x96, 0x52, 0x0);
- time_s(0x92, 0x52, 0x4, 0x96, 0x52);
- time_x(0x5A, 0x52, 0x0);
- time_s(0x50, 0x50, 0x2A, 0x5A, 0x52);
- time_x(0xF0, 0x25, 0x0);
- time_s(0xF0, 0x24, 0x20, 0xF0, 0x25);
- time_x(0xD2, 0x16, 0x0);
- time_s(0xD0, 0x10, 0x62, 0xD2, 0x16);
- }
- }
复制代码
|
|