本帖最后由 lkc8210 于 2023-6-14 09:46 编辑
- #include "reg51.h"
- typedef unsigned char u8;
- typedef unsigned int u16;
- //-----------------------------------------------
- /* define constants */
- #define FOSC 11059200L
- #define T1MS (65536-FOSC/12/1000) //1ms timer calculation method in 12T mode
- /* define SFR */
- sbit TEST_LED = P1^0; //work LED, flash once per second
- /* define variables */
- u16 count; //1000 times counter
- //-----------------------------------------------
- /* Timer0 interrupt routine */
- void tm0_isr() interrupt 1
- {
- TL0 = T1MS; //reload timer0 low u8
- TH0 = T1MS >> 8; //reload timer0 high u8
- if (count-- == 0) //1ms * 1000 -> 1s
- {
- count = 1000; //reset counter
- TEST_LED = ! TEST_LED; //work LED flash
- }
- }
- //-----------------------------------------------
- /* main program */
- void main()
- {
- TMOD = 0x01; //set timer0 as mode1 (16-bit)
- TL0 = T1MS; //initial timer0 low u8
- TH0 = T1MS >> 8; //initial timer0 high u8
- TR0 = 1; //timer0 start running
- ET0 = 1; //enable timer0 interrupt
- EA = 1; //open global interrupt switch
- count = 0; //initial counter
- while (1); //loop
- }
复制代码
|