#include<p18cxxx.h>
#include<timers.h>
#include<delays.h>
#pragma config OSC=HS
#pragma config WDT=OFF
#pragma config LVP=OFF
#pragma config BOR=OFF
#pragma config MCLRE=OFF
void MyHighInt(void);
void MyLowInt(void);
void timeTemp(void);
int getTemp(void);
void clearDisp(void);
#pragma interrupt MyHighInt
#pragma code high_vector=0x08
void high_vector(void)
{
_asm GOTO MyHighInt _endasm
}
#pragma interruptlow MyLowInt
#pragma code low_vector=0x18
void low_vector(void)
{
_asm GOTO MyLowInt _endasm
}
///////////////////////////////////////////////////////
rom near char look7[]=
{
0x3f,
0x06,
0x5b,
0x4f,
0x66,
0x6d,
0x7d,
0x07,
0x7f,
0x6f
};
char count;
char seconds;
char tenths;
char minutes;
char hours;
char displayRAM[8];
char setTimeFlag;
char select;
char pointer;
///////////////////////////////////////////////////////
#pragma code
void timeTemp(void)
{
int temp;
if(setTimeFlag==0)
{
displayRAM[0]=look7[hours/10];
displayRAM[1]=look7[hours%10];
displayRAM[2]=0b01000000;
displayRAM[3]=look7[minutes/10];
displayRAM[4]=look7[minutes%10];
displayRAM[5]=0b01000000;
displayRAM[6]=look7[seconds/10];
displayRAM[7]=look7[seconds%10];
}
else if(setTimeFlag==1){
temp=getTemp();
if(temp<0)
{
temp=-temp;
displayRAM[3]=0x40;
}
else if(temp>0)
{
displayRAM[3]=0;
}
else if(temp>=100)
{
temp-=100;
displayRAM[3]=6;
}
displayRAM[5]=look7[temp%10];
displayRAM[4]=look7[temp/10];
displayRAM[6]=0b01100011;
displayRAM[7]=0b00111001;
}
}
////////////////////////////////////////////////////////
void sendClock(void)
{
PORTAbits.RA1=0;
PORTAbits.RA1=1;
}
///////////////////////////////////////////////////////
int getTemp(void)
{
int temp=0;
char a;
PORTAbits.RA3=0;
for(a=0;a<16;a++)
{
temp|=PORTAbits.RA2;
temp<<=1;
sendClock();
}
a=0;
if((temp&0x8000)==0x8000)
{
a=1;
temp=-temp;
}
temp>>=7;
if(STATUSbits.C=1)
temp++;
temp/=2;
if(a)
temp=-temp;
PORTAbits.RA3=1;
return temp;
}
////////////////////////////////////////////////
int Switch(void)
{
int delay=1500;
do
{
while((PORTA&1)!=1);
Delay1KTCYx(15);
}while((PORTA&1)!=1);
do
{
while((PORTA&1)==1)
{
Delay1KTCYx(2);
delay--;
if(delay==0)
return 1;
}
Delay1KTCYx(15);
}while((PORTA&1)==1);
return 0;
}
///////////////////////////////////////////////////////
void clearDisp(void)
{
char c;
PORTB=0;
for(c=0;c<8;c++)
displayRAM[c]=0;
}
void main(void)
{
ADCON1=0x0F;
TRISC=0;
TRISB=0;
TRISA=0x05;
PORTA=0x0A;
PORTB=0;
PORTC=0;
count=tenths=seconds=minutes=setTimeFlag=pointer=0;
select=254;
hours=24;
timeTemp();
INTCON2bits.TMR0IP=1;
IPR1bits.TMR1IP=0;
WriteTimer0(0);
OpenTimer0(TIMER_INT_ON&T0_8BIT&T0_SOURCE_INT&T0_PS_1_4);
WriteTimer1(53036);
OpenTimer1(TIMER_INT_ON&T1_8BIT_RW&T1_SOURCE_INT&T1_PS_1_8);
RCONbits.IPEN=1;
INTCONbits.GIEH=1;
INTCONbits.GIEL=1;
clearDisp();
for(;;)
{
timeTemp();
if(count==8){
clearDisp();
setTimeFlag^=1;
count=0;
}
/* while(Switch()==1);{
setTimeFlag=1;
displayRAM[4]=0x76;
timeTemp();}
while(Switch()==0)
{
hours++;
if(hours==25)
{
hours=1;
timeTemp();
}
}
displayRAM[4]=0x6d;
timeTemp();
while(Switch()==0)
{
minutes++;
if(minutes==60)
{
minutes=0;
timeTemp();
}
setTimeFlag=0;
timeTemp();
}*/
}
}
//////////////////////////////////////////////////
void MyHighInt(void)
{
INTCONbits.TMR0IF=0;
PORTC=0xFF;
PORTAbits.RA5=1;
PORTAbits.RA4=1;
PORTB=displayRAM[pointer++];
PORTC=select;
if(select==0xfe){
PORTAbits.RA5=0;
}
else if(select==0xfd){
PORTAbits.RA4=0;
}
select<<=1;
select|=1;
if(pointer==8)
{
pointer=0;
select=254;
PORTAbits.RA4=1;
PORTAbits.RA5=1;
}
}
///////////////////////////////////////////////////
void MyLowInt(void)
{
PIR1bits.TMR1IF=0;
WriteTimer1(53036);
tenths++;
if(tenths==10)
{
tenths=0;
seconds++;
count++;
if(seconds==60)
{
seconds=0;
minutes++;
if(minutes==60)
{
minutes=0;
hours++;
if(hours==24)
{
hours=1;
}
}
}
}
if((tenths|seconds)==0)
timeTemp();
}