#include"pic.h"
__CONFIG(0xe824);
void Init();
void dis(void);
void display();
void ep_write();
void delay(unsigned int ms);
unsigned int flag_254us,value;
unsigned int n;
const unsigned char number[10] ={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //0~9共阳数码管段码表
unsigned char led7;
unsigned char led8;
unsigned char ge,shi;
void delay(unsigned int ms)
{
unsigned int i,j;
for(i=0;i<ms;i++)
for(j=0;j<120;j++);
}
void Init()
{
TRISC = 0x00;
PORTC = 0x7f;//段选口
TRISA5 = 1;
ANSEL = 0x00;
TRISB = 0x01;
ANSELH = 0x00;//位选口
//AD转换
TRISA1 = 1;
ANS1 = 1;
ADCON0 = 0x44;//AN1 f8
ADCON1 = 0x80;//右对齐,VDD VSS
ADIE = 0;//中断关
T2CON = 0x01;//16分频
PR2 = 250;
TMR2 = 0;//1*16*250*500ns =254us
TMR2IE = 1;
TMR2IF = 0;
PEIE = 1;//外设中断
TMR2ON = 1;
}
unsigned int AD_convert()
{
value = 0;
ADON = 1;
GODONE = 1;
while(!GODONE);
NOP();
NOP();
value = ADRESH;
value = value << 8;
value += ADRESL;
return value;
}
void interrupt t0()
{
if(TMR2IF)
{
TMR2IF = 0;
flag_254us++;
if(flag_254us>1000)
{
flag_254us = 0;
n++;
}
}
}
//void ep_write()
//{
// shi = n/10;
// ge = n%10;
//
// EEPROM_WRITE(0x05,ge);//保存个位
// EEPROM_WRITE(0x06,shi);//保存十位
//}
//unsigned long ep_read()
//{
// ge = EEPROM_READ(0x05);
// shi = EEPROM_READ(0x06);
// n = ge + (shi*10) ;
// return n;
//}
void display(led8,led7)
{
RB3 = 1;
PORTC = number[led8];
delay(1);
RB3 = 0;
PORTC = 0x00;
RB2 = 1;
PORTC = number[led7];
delay(1);
RB2 = 0;
PORTC = 0x00;
}
void dis(void)
{
led8 = n/10;
led7 = n%10;
display(led8,led7);
}
void main()
{
GIE = 1;
TMR2IE = 1;
Init();
n=EEPROM_READ(0x05);
while(1)
{
if(!RA5)
{
n=10;
}
dis();
if(n==100) n=0;
if(AD_convert() < 400)
{
EEPROM_WRITE(0x05,n);
}
}
}
|