PIC单片机(PIC16F877A)定时器

2019-04-15 11:52发布

#include #define uchar unsigned char #define uint unsigned int uchar i,temp; uchar count; void delay(uint x) //1ms { uint y,z; for(y=x;y>0;y--) for(z=25;z>0;z--); } void init(void) { T1CON=0x01;//使能定时器1,时钟为外部晶振四分频 PIE1|=0x01;//允许中断 INTCON|=0xc0;//开全局中断和第一外设中断 TMR1H=15536/256;//50ms初值 TMR1L=15536%256; } void interrupt timer1(void) { if(TMR1IE&&TMR1IF) { TMR1H=(65535-50000)/256; TMR1L=(65535-50000)%256; count++; TMR1IF=0;//中断标志位清零 if(count==20) { count=0; if(i<8) { PORTD=temp; temp<<=1; i++; } else { i=0; temp=0x01; } } } } void main(void) { TRISD=0x00; PORTD=0x00; temp=0x01; i=0; init(); while(1); }