The nesting of timer 0 and external interrupt 0 realizes the running light and button respectively

Publisher:悦耳旋律Latest update time:2020-06-21 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

#include <STC12C5A60S2.h>

 

typedef unsigned char u8;

 

typedef enum

{

Tim0_L_Int0_H = 1,

Tim0_L_Int0_L,

Tim0_H_Int0_L,

}EnumCase;

 

 

sbit LED1 = P2 ^ 4;

sbit LED2 = P2 ^ 5;

sbit LED3 = P2 ^ 6;

sbit LED4 = P2 ^ 7;

 

sbit BEEP = P1 ^ 0;     

 

sbit KEY1 = P3 ^ 2;

sbit KEY2 = P3 ^ 4;

 

//---------------------------------------------------------------------

static u8 i_1sCnt = 0;

static u8 LEDStatus = 0;

//---------------------------------------------------------------------

 

void delay_2ms(unsigned long int time) //2ms�Ļ�

{

unsigned long int i;

for (i = 0; i < time * 100; i++);

}

 

 

void SetPriorty(EnumCase chose)

{

switch (chose)

{

case Tim0_L_Int0_H:

IP = 0x01;

break;

case Tim0_L_Int0_L:

IP = 0x00;

break;

case Tim0_H_Int0_L:

IP = 0x02;

break;

default:

break;

}

}

 

 

void Timer0_Init()

{

TH0 = 0; // Maximum delay, about 130ms

TL0 = 0;

TMOD = 0x01;     

TR0 = 1;

     

ET0 = 1;

  EA = 1;

}

 

void Timer0() interrupt 1

{

TH0 = 0;  

TL0 = 0;

 

i_1sCnt++;

if (i_1sCnt==15)

{

i_1sCnt = 0;

 

LEDStatus++;

switch (LEDStatus)

{

case 1:

LED1 = 0; LED2 = 1; LED3 = 1; LED4 = 1;

break;

case 2:

LED1 = 1; LED2 = 0; LED3 = 1; LED4 = 1;

break;

case 3:

LED1 = 1; LED2 = 1; LED3 = 0; LED4 = 1;

break;

case 4:

LED1 = 1; LED2 = 1; LED3 = 1; LED4 = 0;

LEDStatus = 0;

break;

default:

break;

}

 

}

}

 

 

void ISR_Init()

{

IT0 = 0;   

 

EX0 = 1;   

  EA = 1;    

}

 

void INT0_ISR() interrupt 0

{

EX0 = 0;  

 

BEEP = 1;

delay_2ms(125);

BEEP = 0;

delay_2ms(125);

BEEP = 1;

delay_2ms(125);

BEEP = 0;

delay_2ms(125);

 

EX0 = 1; 

}

 

//-------------------------------------------------------------------------

 

//Set the ones you want to view to 1, and the ones you don't want to view to 0. Only one is 1

#define TEST1  1

#define TEST2  0

#define TEST3  0

 

void main()

{

BEEP = 0;

 

#if TEST1

SetPriorty(Tim0_L_Int0_H);

#endif

#if TEST2

SetPriorty(Tim0_L_Int0_L);

#endif

 

#if TEST3

SetPriorty(Tim0_H_Int0_L);

#endif

 

Timer0_Init(); //

ISR_Init();

 

while (1);

}

Reference address:The nesting of timer 0 and external interrupt 0 realizes the running light and button respectively

Previous article:AT89C51 external interrupt experiment realizes 8 lights turning on and off at the same time (controlled by external switch)
Next article:External interrupt 0/1 to light up the LED code

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号