主函数 main
#include <REGX52.H> #include "Delay.h" #include "Timer0.h" unsigned char count = 0; unsigned char pwm; void Timer0() interrupt 1 { // 每隔0.1ms=100us进入 TH0 = 65435 / 256; //设置定时初始值 TL0 = 65435 % 256; //设置定时初始值 if (count <= pwm) //5=0.5ms=500us 顺时针 //15=1.5ms=1500us 逆时针 {P0_0 = 1;} //高电平 else {P0_0 = 0;} //低电平 count ++; if (count >= 200) {count = 0;} } void main() { Timer_Init(); while(1) { pwm = 5; Delay(2000); count = 0; pwm = 15; Delay(2000); count = 0; } }
Delay.h
#ifndef __Delay_H__ #define __Delay_H__ void Delay(unsigned int xms); #endif
Delay.c
#include <REGX52.H> #include <intrins.h> // 100us // 1ms = 1000us = Delay(10) // 20ms = 20000us = Delay(200) void Delay(unsigned int xms) //@11.0592MHz { unsigned char i; while (xms--) { _nop_(); i = 43; while (--i); } }
Timer0.h
#ifndef __Timer0_H__ #define __Timer0_H__ void Timer_Init(); //1毫秒@11.0592MHz #endif
Timer.c
#include <REGX52.H> void Timer_Init() //@11.0592MHz { //设置定时器模式 TMOD &= 0xF0; TMOD |= 0x01; TF0 = 0; //清除TF0标志 TR0 = 1; //定时器0开始计时 // 65535us // 100us = 0.1ms 65435 TH0=0xff=65435/256,TL0=0x9b=65435%256 // 1000us = 1ms 64535 // 20000us = 20ms 45535 TH0 = 65435 / 256; //设置定时初始值 TL0 = 65435 % 256; //设置定时初始值 ET0 = 1; // 中断 EA = 1; // 中断 PT0 = 0; //中断优先级 } /* // 定时器中断函数 void Timer0() interrupt 1 { static unsigned int count; //static 静态变量,函数结束不清零 TL0 = 0xff; //设置定时初始值 TH0 = 0x9b; //设置定时初始值 count ++; if (count >= 100) { count = 0; P2_1 =~ P2_1; } } */
标签:count,__,void,初始值,51,Delay,单片机,Timer0,90g360 From: https://www.cnblogs.com/lld76/p/17346778.html