STC12-タイマー0時間1秒LED0が1回フリップ

1. main.c

#include <reg52.h>  
#include <intrins.h>
#include <Timer0_Init.h>
#define uchar unsigned char
#define uint unsigned int

sbit LED0 = P2 ^ 0 ;
 void main() { uchar count = 0; // Timer0オーバーフローの数を確認するために使用されます
Timer0_Init(); while(1 ){if(TF0 == 1 ){TF0 = 0 ; TH0 =(65535-10000 + 1)/ 256; //初期値カウントTL0 =(65535-10000 + 1)%を再割り当て256 ; count ++ ; if(count> = 100)// 100オーバーフローは100 * 10ms = 1s {count = 0 ; LED0 = 〜LED0;}}}}

2.タイマー0の初期化 

(1)Timer0_Init.c

#include <Timer0_Init.h>
#include <reg52.h>
void Timer0_Init()
{
    TMOD = 0x01 ;
    TH0 =(65535-10000 + 1)/ 256; //数式:初期カウント値=(2 ^ 16カウントするカウント値)/ 2 ^ 8; 10000は10000マシンサイクルを意味します。12MHzのマシンサイクルは1usなので、 10000は10ms
    TL0 =(65535-10000 + 1)%256; // 2 ^ 16はタイマーが16ビットであることを意味し、2 ^ 8は上位8ビットがTH0に入れられ、%は下位8ビットであることを意味します    
    TR0 = 1; // = 1-> Timer0を開きます
}

(2)Timer0_Init.h

#ifndef __Timer0_Init_H
#define __Timer0_Init_H      
#include "reg52.h" 
void Timer0_Init(); #endif

PS:魔法の杖のc51列に、参照されている.hファイルのPATHアドレスを追加することを忘れないでください

おすすめ

転載: www.cnblogs.com/oneme1world/p/12735297.html