That's a hardware delay using Timer 1.
If the speed is wrong it implies the value you load into TMR1 is wrong in the routine "preload_tmr1()" which you haven't posted. The correct value depends on the clock speed so I can't tell you what it should be without knowing it.
Brian.
Dear Brain, pls make a note on the program mentioned below. It will be very helpful if I receive any suggestions from your end.
#include<htc.h>
#define PB(p,b) ((unsigned)&(p)*8+(b))
#define gen_init() PORTA=0,PORTC=0,TRISA=0x0c,TRISC=0,CMCON=0x07,OSCCAL=0xff,OPTION=0X80,\
INTCON=0,IOCA=0,PIE1=0;T1CON=0x00,VRCON=0;
bit dew_input @ PB(PORTA,2); //
bit switch1 @ PB(PORTA,3); //
bit PIN2_led1 @ PB(PORTA,5); //
bit PIN3_led2 @ PB(PORTA,4); //
bit PIN5_led6 @ PB(PORTC,5);
bit PIN10_led9 @ PB(PORTC,0);
bit Dewpoint_Switch @ PB(PORTA,0); // This is actually pin 13
bit PIN12_led10 @ PB(PORTA,1); //
bit PIN6_40_percent_led3 @ PB(PORTC,4);
bit PIN7_60_percent_led4 @ PB(PORTC,3); //
bit PIN8_80_percent_led5 @ PB(PORTC,2); //
bit PIN9_100_percent_led7 @ PB(PORTC,1); //
#define preload_tmr1() TMR1H=0x15,TMR1L=0x50; //0.5 sec delay
#define init_tmr1() T1CON=0x00,PIE1=0x01,PEIE=1;
#define enable_tmr1 TMR1ON=1;
#define disable_tmr1 TMR1ON=0;
unsigned char sw_count,count,data,tensec_count,sec_count;
unsigned int min_count,min_5_count;
bit sec_7_flag,dew_flag,init_flag,dec_flag;
void eeprom_write1(void)
{
EECON1=0;
EEIF=0;
EEADR=0x05;
EEDATA=sw_count; //LS BYTES pulse_width DATA save
WREN=1;
EECON2=0x55;
EECON2=0xaa;
WR=1;
while(!EEIF);
asm("nop");
}
void eeprom_read1(void)
{
EEADR=0x05;
EEDATA=0;
EECON1=0;
RD=1;
while(RD);
data=EEDATA;
}
void led_set1(void)
{
PIN3_led2=0;
PIN2_led1=0;
PIN5_led6=0;
PIN10_led9=0;
Dewpoint_Switch=1;
PIN12_led10=0;
}
void led_set2(void)
{
PIN3_led2=0;
PIN2_led1=1;
PIN5_led6=0;
PIN10_led9=0;
Dewpoint_Switch=0;
PIN12_led10=1;
}
void led_set3(void)
{
PIN3_led2=1;
PIN2_led1=0;
PIN5_led6=0;
PIN10_led9=0;
Dewpoint_Switch=0;
PIN12_led10=1;
}
void led_set4(void)
{
PIN3_led2=0;
PIN2_led1=0;
PIN5_led6=1;
PIN10_led9=0;
Dewpoint_Switch=0;
PIN12_led10=0;
}
void led_set5(void)
{
PIN3_led2=0;
PIN2_led1=0;
PIN5_led6=0;
PIN10_led9=1;
Dewpoint_Switch=1;
PIN12_led10=0;
}
void interrupt isr(void)
{
if(TMR1IF)
{
TMR1IF=0;
preload_tmr1();
if(sec_count++>20) //20
{
sec_count=0;
if(!dew_flag) //If dew_flag is true it means dew point is good and timing will not be incremented. stretch
min_count++; // Dew point poor and timing incremented
if(min_count>=266)
min_count=0;
}
if(!switch1)
count++;
if(switch1)
count=0;
tensec_count--;
if(tensec_count==0)
{
sec_7_flag=0;
count=0;
}
if(count>=70)
{
sec_7_flag=1;
tensec_count=200;
count=0;
}
switch(sw_count)
{
case 1:
PIN6_40_percent_led3=1;
PIN9_100_percent_led7=0;
if(min_count<=2)
led_set5();
else if(min_count>2 && min_count<30)
led_set1();
else if(min_count==30)
{led_set1();
if(dew_input) {dew_flag=0;}
else {dew_flag=1;}
}
else if(min_count>=31 && min_count<=66)
led_set2();
else if(min_count>66 && min_count<150)
led_set3();
else if(min_count==150)
{
led_set3();
if(dew_input) {dew_flag=0;}
else {dew_flag=1;}
}
else if(min_count>150 && min_count<=186 )
led_set4();
else if (min_count<=240)
led_set1();
break;
case 2:
PIN7_60_percent_led4=1;
PIN6_40_percent_led3=0;
if(min_count<=2)
led_set5();
else if(min_count>2 && min_count<30)
led_set1();
else if(min_count==30)
{
led_set1();
if(dew_input) {dew_flag=0;}
else {dew_flag=1;}
}
else if(min_count>=31 && min_count<=84)
led_set2();
else if(min_count>84 && min_count<150)
led_set3();
else if(min_count==150)
{
led_set3();
if(dew_input) {dew_flag=0;}
else {dew_flag=1;}
}
else if(min_count>150 && min_count<=204 )
led_set4();
else if (min_count<=240)
led_set1();
break;
case 3:
PIN8_80_percent_led5=1;
PIN7_60_percent_led4=0;
if(min_count<=2)
led_set5();
else if(min_count>2 && min_count<30)
led_set1();
else if(min_count==30)
{
led_set1();
if(dew_input) {dew_flag=0;}
else {dew_flag=1;}
}
else if(min_count>=31 && min_count<=102)
led_set2();
else if(min_count>102 && min_count<150)
led_set3();
else if(min_count==150)
{
led_set3();
if(dew_input) {dew_flag=0;}
else {dew_flag=1;}
}
else if(min_count>150 && min_count<=222 )
led_set4();
else if (min_count<=240)
led_set1();
break;
case 4:
PIN9_100_percent_led7=1;
PIN8_80_percent_led5=0;
if(min_count<=2)
led_set5();
else if(min_count>2 && min_count<33)
led_set1();
else if(min_count==33)
{
led_set1();
if(dew_input) {dew_flag=0;}
else {dew_flag=1;}
}
else if(min_count>=34 && min_count<=133)
led_set2();
else if(min_count>134 && min_count<166)
led_set3();
else if(min_count==166)
{
led_set3();
if(dew_input) {dew_flag=0;}
else {dew_flag=1;}
}
else if(min_count>166 && min_count<=266 )
led_set4();
break;
} //end of switch
}
}
void main(void)
{
gen_init();
init_tmr1();
GIE=1;
PIN3_led2=0;
PIN2_led1=0;
PIN5_led6=0;
dew_flag=0;
sec_7_flag=0;
PIN10_led9=0;
//Dewpoint_Switch=0;
PIN12_led10=0;
PIN6_40_percent_led3=0;
PIN7_60_percent_led4=0;
PIN8_80_percent_led5=0;
PIN9_100_percent_led7=0;
preload_tmr1();
min_5_count=0;
sw_count=4;
init_flag=1;
min_5_count=0;
eeprom_read1();
if(data==0xff)
sw_count=4;
else
sw_count=data;
enable_tmr1;
while(1)
{
if(sec_7_flag||init_flag)
{
while(tensec_count||init_flag)
{
init_flag=0;
if(!switch1)
{
while(!switch1);
sw_count++;
if(sw_count>=5)
sw_count=1;
min_count=0;
eeprom_write1();
tensec_count=200;
}
}
}
}
}