#include <xc.h>
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
unsigned int i,ms;
const unsigned char table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //共阴极数码管显示编码
void display(unsigned int seg_num);
void delay(unsigned int x);
void time0_init();
void main()
{
PORTD = 0x00; //数码管位选
PORTA = 0x00; //数码管段选
TRISA = 0x00;
TRISD = 0x00;
time0_init();
while(1)
{
display(ms);
if(ms == 99) //毫秒加到100清0
{
ms = 0;
}
}
}
void interrupt time0(void) //中断函数
{
if(INTCONbits.T0IF == 1)
{
INTCONbits.T0IF = 0;
TMR0 = 0xD9; //定时10MS
ms++; //过10MS加1
}
}
void delay(unsigned int x) //延时函数
{
unsigned int a,b;
for(a=x;a>0;a--)
for(b=110;b>0;b--);
}
void display(unsigned int seg_num)
{
unsigned int ms_dec,ms_un;
ms_dec = seg_num/10;
ms_un = seg_num%10;
PORTAbits.RA0 = 1; //段选最低位数码管(个位)
PORTD = table[ms_un]; //毫秒个位显示
PORTAbits.RA0 = 0; //关段选
// delay(1);
PORTAbits.RA1 = 1; //段选第二位数码管(十位)
PORTD = table[ms_dec];
PORTAbits.RA1 = 0;
// delay(1);
}
void time0_init()
{
TMR0 = 0xD9; //定时器0的设置,采用64分频
INTCONbits.GIE = 1;
INTCONbits.T0IE = 1;
INTCONbits.T0IF = 0;
OPTION_REGbits.T0CS = 0;
OPTION_REGbits.PSA = 0;
OPTION_REGbits.PS0 = 1;
OPTION_REGbits.PS1 = 0;
OPTION_REGbits.PS2 = 1;
}
|