|
#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共陽數(shù)碼管段碼表
unsigned char led7;
unsigned char led8;
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轉(zhuǎn)換
TRISA1 = 1;
ANS1 = 1;
ADCON0 = 0x44;//AN1 f8
ADCON1 = 0x80;//右對齊,VDD VSS
ADIE = 0;//中斷關(guān)
T2CON = 0x01;//16分頻
PR2 = 250;
TMR2 = 0;//1*16*250*500ns =254us
TMR2IE = 1;
TMR2IF = 0;
PEIE = 1;//外設(shè)中斷
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++;
if(n==100) n=0;
}
}
}
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(AD_convert() > 400)
{
dis();
}
else//低于4V
{
RB2 = 0;
RB3 = 0;//關(guān)數(shù)碼管
GIE = 0;//關(guān)中斷
EEPROM_WRITE(0x05,n);//保存數(shù)據(jù)
}
}
// This header file should not be included directly
// Inclusion of this file is provided indirectly by including htc.h
/***********************************************************************/
/****** EEPROM memory read/write macros and function definitions *******/
/***********************************************************************/
/* NOTE WELL:
The macro EEPROM_READ() is NOT safe to use immediately after any
write to EEPROM, as it does NOT wait for WR to clear. This is by
design, to allow minimal code size if a sequence of reads is
desired. To guarantee uncorrupted writes, use the function
eeprom_read() or insert
while(WR)continue;
before calling EEPROM_READ().
*/
#if EEPROM_SIZE > 0
#ifdef __FLASHTYPE
// macro versions of EEPROM write and read
#define EEPROM_WRITE(addr, value) \
do{ \
while(WR)continue;EEADRL=(addr);EEDATA=(value); \
EECON1&=0x3F;CARRY=0;if(GIE)CARRY=1;GIE=0; \
WREN=1;EECON2=0x55;EECON2=0xAA;WR=1;WREN=0; \
if(CARRY)GIE=1; \
}while(0)
#define EEPROM_READ(addr) ((EEADRL=(addr)),(EECON1&=0x3F),(RD=1),EEDATA)
#else // else doesn't write flash
#define EEPROM_WRITE(addr, value) \
do{ \
while(WR)continue;EEADRL=(addr);EEDATA=(value); \
CARRY=0;if(GIE)CARRY=1;GIE=0; \
WREN=1;EECON2=0x55;EECON2=0xAA;WR=1;WREN=0; \
if(CARRY)GIE=1; \
}while(0)
#define EEPROM_READ(addr) ((EEADRL=(addr)),(RD=1),EEDATA)
#endif
/* library function versions */
extern void eeprom_write(unsigned char addr, unsigned char value);
extern unsigned char eeprom_read(unsigned char addr);
#endif // end EEPROM routines
|
|