Jp3 is connected to the PO port.
Jp1 is connected to P2 and j22 to p3.0 and p3.1.
#include<reg51.h>#define uint unsigned int#define uchar unsigned charuchar smg = 0;uchar code table[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90, 0x88, 0x83, 0xc6, 0xa1, 0x86, 0x8e};int flag_t0_green = 0;sbit led_frag_green = P3^1;sbit led_frag_red = P3^0;void init_t0();void init_traffic();void main(){init_t0();init_traffic();while(1){}}//¶¨Ê±Æ÷³õʼ»¯void init_t0(){TMOD = 0x01;TH0 = (65536-50000)/256;TL0 = (65536-50000)%256;ET0 = 1;EA = 1;TR0 = 1;}//³õʼ»¯½»Í¨µÆvoid init_traffic(){//Ö÷¸ÉµÀÂ̵ÆÁÁ£¬Ö§Â·ºìµÆÁÁ,Ö÷¸ÉµÀ·½ÏòÉϵÄÈËÐеÀ¿ÉÒÔͨÐÐP2 = 0xb3;led_frag_green = 1;led_frag_red = 0;P0 = table[0];}//ÖжϷþÎñ³ÌÐòvoid timer0() interrupt 1{TH0 = (65536-50000)/256;TL0 = (65536-50000)%256;flag_t0_green++;if(flag_t0_green%20 == 0){smg++;P0 = table[smg];}if(flag_t0_green == 200){P2 = 0xab;smg = 0;}if(flag_t0_green == 300){smg = 0;P2 = 0x5e;led_frag_green = 0;led_frag_red = 1;}if(flag_t0_green == 400){//½«Ö§Â·µÄ»ÆµÆµãÁÁP2 = 0x5d;smg = 0;}if(flag_t0_green == 500){init_traffic();flag_t0_green = 0;smg = 0;}}
MCU Lesson 16th: simple traffic light