#include <pic.h>
#include "stdio.h"
const unsigned char SET_KEY_PRESSED = 0x30;
const unsigned char UP_KEY_PRESSED = 0x24;
const unsigned char ENTER_KEY_PRESSED = 0x14;
const unsigned char seg_sel[8]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
const unsigned char seg[15]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x77,0x7C,0x39,0x5E,0x71};
const unsigned char set[8] ={0xFF,0xFF,0xFF,0xFF,0xFF,0x92,0x86,0x87};
const unsigned char rst[8] ={0xFF,0xFF,0xFF,0xFF,0xFF,0xAF,0x92,0x87};
const unsigned char out[8] ={0xFF,0xFF,0xFF,0xFF,0xFF,0xC0,0xC1,0x87};
const unsigned char yes[8] = {0xFF,0xFF,0xFF,0xFF,0xFF,0x91,0x86,0x92};
const unsigned char Noo[8] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xAB,0xC0};
const unsigned char test[8] = {0xFF,0xFF,0x92,0x86,0x87,0xFF,0xAB,0xC0};
unsigned int count=0;
unsigned char seg_counter;
unsigned char val[10];
unsigned char Key_Scan_Value=0,Key_Value=0;
unsigned char Key_Pressed=0;
unsigned int set_delay,delay_count=0,start_delay=0;
unsigned char Chk_All_Key_Open=0;
unsigned char Start_Set_Timer=0,Setting_mode=0;
unsigned int Set_Timer_Count=0,Set_timer_complete=0;
unsigned char display_string=0;
unsigned char Setting_Case=0;
unsigned char position_couter=0;
unsigned int Start_Blinking=0,Blink_Count=0,Blink_Value=0;
unsigned char Disp_Position=0;
unsigned long Set_Count_Value=0;
unsigned char temp1=0,temp2=0,temp3=0,temp4=0;
signed char index=0 ;
void delayms(unsigned int count);
unsigned char Check_Key_Pressed(void);
void timer0_init(void)
{
OPTION = 0x83;//option reg as 0x83
TMR0 = 83;
T0IE = 1;//timer 0 interrupt enable flag
GIE = 1;
}
//--------------------------------------------------------------------------------------------------------
void timer1_init(void)
{
T1CON = 0x01;
TMR1H = 0xF5;
TMR1L = 0x33;
TMR1IE = 1;
}
//--------------------------------------------------------------------------------------------------------
void delayms(unsigned int count)
{
delay_count = 0;
set_delay = count;
start_delay = 1;
while(start_delay == 1);
}
//---------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------
void display_number_upto_99(unsigned char value)
{
unsigned char k;
k = value;
val[0] = k/10;
k = k%10;
val[1] = k;
}
//--------------------------------------------------------------------------------------------------------
void display_set_on_display()
{
unsigned char i;
display_string = 1;
for(i=8;i>0;i--)
val[i-1] = set[8-i];
}
//--------------------------------------------------------------------------------------------------------
void display_test_on_display()
{
unsigned char i;
display_string = 1;
for(i=8;i>0;i--)
val[i-1] = test[8-i];
}
//--------------------------------------------------------------------------------------------------------
void display_rst_on_display()
{
unsigned char i;
display_string = 1;
for(i=8;i>0;i--)
val[i-1] = rst[8-i];
}
//--------------------------------------------------------------------------------------------------------
void display_out_on_display()
{
unsigned char i;
display_string = 1;
for(i=8;i>0;i--)
val[i-1] = out[8-i];
}
//--------------------------------------------------------------------------------------------------------------------------
void interrupt any_ISR(void)
{
//--------------------------------------------------------------------------------------------------------
if (T0IF==1)
{
seg_counter++;
if(seg_counter > 7)
seg_counter = 0;
PORTD = 0x00;
PORTD = seg_sel[seg_counter];
PORTB = 0xFF;
if(display_string == 0)
{
PORTB = seg[val[seg_counter]];
}
else
PORTB = val[seg_counter];
T0IF=0;
TMR0 = 83;
}
//--------------------------------------------------------------------------------------------------------
if(TMR1IF)
{
count++;
if(count > 1000)
{
count = 0;
RC3 ^=1;
}
if(Start_Set_Timer==1)
{
Set_Timer_Count++;
if(Set_Timer_Count > 1000)
{
Set_Timer_Count = 0;
Start_Set_Timer = 0;
Set_timer_complete = 1;
}
}
if(start_delay == 1)
delay_count++;
if(delay_count > set_delay)
{
set_delay = 0;
start_delay = 0;
}
TMR1IF = 0;
TMR1H = 0xF5;
TMR1L = 0x33;
}
}
//--------------------------------------------------------------------------------------------------------
void soft_delay(void)
{
unsigned int i;
for(i=0;i<1000;i++);
}
//-------------------------------------------------------------------------------------------------------------------------
unsigned char Check_Key_Pressed(void)
{
Set_Timer_Count = 0;
Start_Set_Timer = 0;
Key_Value = 0;
Key_Scan_Value = 0;
Key_Scan_Value = PORTA & 0x34;
if(Key_Scan_Value != 0x34)
{
Key_Scan_Value = PORTA & 0x34;
RC4 = 1;
soft_delay();//soft_delay();soft_delay();
RC4 = 0;
Key_Scan_Value = PORTA & 0x34;
if(Key_Scan_Value != 0x34)
{
Chk_All_Key_Open = 1;
Key_Value = Key_Scan_Value;
}
}
if(Chk_All_Key_Open == 1)
{
do
{
Key_Scan_Value = PORTA & 0x34;
}while(Key_Scan_Value != 0x34);
Chk_All_Key_Open = 0;
}
return(Key_Value);
}
//--------------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------
void main(void)
{
unsigned long temp_i=0;
unsigned char k=0;
TRISC = 0x00;
TRISD = 0x00;
TRISB = 0x00;
timer0_init();
timer1_init();
ADCON1 = 0x04;
TRISA = 0x3F;
delayms(250);
display_string = 0;
Setting_Case = 0;
position_couter = 0;
//-----------------------------------------------------------------------------------------------------
while(1)
{
Key_Pressed = Check_Key_Pressed();
if((Key_Pressed == SET_KEY_PRESSED) && (Setting_mode ==0))
{
Key_Pressed = 0;
Setting_mode = 1;
display_set_on_display();
}
if(Setting_mode == 1)
{
switch(Setting_Case)
{
case 0:
if(Key_Pressed == UP_KEY_PRESSED)
{
Key_Pressed = 0;
position_couter++;
if(position_couter > 2)
position_couter = 0;
if(position_couter == 0)
display_set_on_display();
else if(position_couter == 1)
display_rst_on_display();
else if(position_couter == 2)
display_out_on_display();
}
if(Key_Pressed == ENTER_KEY_PRESSED)
{
Key_Pressed = 0;
if(position_couter == 0)
{
Setting_Case = 1;
Blink_Value = 0;
Blink_Count = 0;
Start_Blinking = 1;
}
}
k=0;
break;
case 1:
display_string = 0;
if(k==0)
{
k=1;
Set_Count_Value = 12345678;
for ( index=0; Set_Count_Value>0; index++ )
{
temp_i = Set_Count_Value / 10 ;
val[index] = Set_Count_Value-10*temp_i ;
Set_Count_Value = temp_i ;
}
}
break;
}
}
Key_Pressed = 0;
}//while(1)
}//main()