//针对于4MHz的PIC单片机
#include<pic.h>
#define uchar unsigned char
#define uint unsigned int
__CONFIG(0x3B31);
void init();
void delay(uint x);
void display(uchar a,uchar b);
const uchar table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
uchar intnum;
void main()
{
uchar num0,num1;
init();
while(1)
{
num0=intnum/10;
num1=intnum%10;
display(num0,num1);
}
}
void init()
{
TRISB=0;
PORTB=0;
TRISC=0;
PORTC=0;
OPTION=0x07;//即PSA=0表示被分配的TIMER0 rate,否则为WDT rate。PS2:PS0=111(即1:256分频)
INTCON=0xa0;//开总中断和T0IE
TMR0=256-195;//给timer0装的初值。256*(256-195)=50ms
}
void display(uchar a,uchar b)
{
PORTB=~table[a];
PORTC=0x01;
delay(1);
PORTC=0x00;//消隐
PORTB=~table[b];
PORTC=0x02;
delay(1);
PORTC=0x00;//消隐
}
void delay(uint x)
{
uint a,b;
for(a=x;a>0;a--)
for(b=110;b>0;b--);
}
void interrupt time0()
{
uchar k;
T0IF=0;
TMR0=256-195;//256*(256-195)=50ms
k++;
if(k==20)//50ms*20=1s
{
k=0;
intnum++;
if(intnum==99)
{
intnum=0;
}
}
}