MCU Lesson 16th: simple traffic light

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.