timer0.c

来自「学习C51时写的定时器程序,提供给刚学单片机程序的朋友参考」· C语言 代码 · 共 35 行

C
35
字号
//This program is just for S51E&AVR board.
//If you want to see more informations
//please login our website: http//www.mcu2008.com 
//copy rights by NanJing Victor Electronic Co.,Ltd
//定时器中断实验一
//功能:利用定时器0产生的中断从而定时控制小灯的闪烁。
#include <reg51.h>
#define const_time 100;   //100x5ms=500ms=0.5s
unsigned char buff_time;  //buffer for storing the times
bit LED_Buffer;
sbit light0 = P1^0;

void main() 
{ TMOD=0x01;   //set the timer's mode
  TH0=(65536-5000)/256; //fill the timer0's higher register 
  TL0=(65536-5000)%256; //fill the timer0's lower register
  IE=0x82;   //EA=1,IT0=1
  LED_Buffer=0;
  light0=0;
  buff_time=const_time;
  TR0=1;     //start the timer!
  while(1){
  light0=LED_Buffer;
  }  
}

void T0Int() interrupt 1
{TH0=(65536-5000)/256; //fill the timer0's higher register 
 TL0=(65536-5000)%256; //fill the timer0's lower register 
 buff_time--;
 if (buff_time==0)
{buff_time=const_time; // refill the buffer
 LED_Buffer=!LED_Buffer;  // opposite the LED display
 }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?