Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Help needed with Combining of programs...

Status
Not open for further replies.

huzeeigat

Member level 4
Joined
Mar 18, 2012
Messages
75
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,288
Location
Andheri
Activity points
1,776
Hello,
I have combined two programs of temperature sensor and interrupt together..and i executed it in a circuit...as shown in fig.
The temperature sensor part works fine..but the interrupt part does not give me desired output..

 

Attachments

  • tmpump.zip
    934 bytes · Views: 74

For which device and on which compiler do you have written the code???
 

the device is pic18f452..and the compiler is mplabc18
 

The interrupt is triggered. Problem is that, you're staying stuck in the interrupt for too long.

The 3 for loops are wasting too much time. And since the interrupt is triggered from the switch anyways, the delay isn't required. Remove those 3 for loops and observe. You'll see that the output toggles as required.

Code:
#include "p18f452.h"

#pragma config WDT=OFF //Turn watchdog timer off
#pragma config OSC=HS //For 4MHz oscillator
#define mybit PORTBbits.RB7
unsigned int temp1=102;
//25'C gives LM35 voltage of 0.25V, Reference voltage = 2.5V,
//    so 0.25V gives ADC reading of (0.25/2.5)*1023 = 102

unsigned int temp2=110;
//27'C gives LM35 voltage of 0.27V, Reference voltage = 2.5V,
//    so 0.25V gives ADC reading of (0.27/2.5)*1023 = 110

unsigned int itime,bin_temp,i,j,k;
unsigned char l_byte,h_byte;
void chk_isr(void);
void INT0_ISR(void);

void temperature(void);
void delay(void);
#pragma interrupt chk_isr
void chk_isr (void)
{
if (INTCONbits.INT0IF==1)	
INT0_ISR( );			
}

#pragma code My_HiPrio_Int=0x08

void My_HiPrio_Int (void)
{ 
  _asm
  GOTO chk_isr
  _endasm
}
#pragma code
void main()
{

	ADCON0=0X81;
	ADCON1=0XC5;
	
	TRISD=0;
	TRISBbits.TRISB7=0;
	TRISBbits.TRISB0=1;
	TRISAbits.TRISA0=1;
	TRISAbits.TRISA2=1;
	INTCONbits.INT0IF=0;
	INTCONbits.INT0IE=1;
	INTCONbits.GIE=1;
	

while(1){
	temperature();
	if (bin_temp < temp1){ //If temperature < 25'C
		PORTDbits.RD3 = 1;
	}
	else{
		PORTDbits.RD3 = 0;
	}

if (bin_temp > temp2){ //If temperature > 27'C
		PORTDbits.RD2 = 1;
	}
	else{
		PORTDbits.RD2 = 0;
	}
}

}

void INT0_ISR(void)
 {
  //for(k=0;k<30;k++)
  //for(i=0; i<500; i++)
   //for(j=0; j<165; j++)
  //{
  //} 
    mybit=~mybit;
    PORTDbits.RD1=~PORTDbits.RD1;
    INTCONbits.INT0IF=0;
	}

void temperature(void)
{

delay();

ADCON0bits.GO=1;
while(ADCON0bits.DONE==1);

l_byte=ADRESL;
h_byte=ADRESH;

bin_temp=(h_byte << 8)|l_byte;
//Results direct result of conversion without processing
}

void delay(void)
{
		int i,j;
		for(i=0;i<=250;i++)
		for(j=0;j<=135;j++)
         {
          }
}

Also, change the reset circuitry as mentioned in the other thread: https://www.edaboard.com/threads/246822/#post1057582

Hope this helps.
Tahmid.
 
@Tahmid: The for loops i have written because i want the bit to toggle after 15sec that is why i used them...
 

Instead of waiting in the interrupt, set a flag in the interrupt.

In the main program, check if that flag is raised. If the flag is raised, start a timer and set that timer for interrupt. In each of those timer interrupts, increment a counter, until total time elapsed is about 15seconds. Then toggle the bits required and clear the flag that had been set in the INT0 interrupt. Also, clear the counter.

Try to make the interrupt as small as possible, as it halts execution of the main code. Waiting 15 seconds in the interrupt as you had done, means that 15 seconds of main program execution is halted. Not a good idea.

Hope this helps.
Tahmid.
 
@Tahmid: Thanks..Can you give me the change in program for that if possible...?
 

Use something like this:

Code:
#include "p18f452.h"

#pragma config WDT=OFF //Turn watchdog timer off
#pragma config OSC=HS //For 4MHz oscillator
#define mybit PORTBbits.RB7
unsigned int temp1=102;
//25'C gives LM35 voltage of 0.25V, Reference voltage = 2.5V,
//    so 0.25V gives ADC reading of (0.25/2.5)*1023 = 102

unsigned int temp2=110;
//27'C gives LM35 voltage of 0.27V, Reference voltage = 2.5V,
//    so 0.25V gives ADC reading of (0.27/2.5)*1023 = 110

unsigned int itime,bin_temp,i,j,k;
unsigned char l_byte,h_byte;
unsigned char counter;
void chk_isr(void);
void INT0_ISR(void);
void T1_ISR(void);

void temperature(void);
void delay(void);
#pragma interrupt chk_isr
void chk_isr (void)
{
if (INTCONbits.INT0IF==1)	
INT0_ISR( );			

if (PIR1bits.TMR1IF==1)
T1_ISR();
}

#pragma code My_HiPrio_Int=0x08

void My_HiPrio_Int (void)
{ 
  _asm
  GOTO chk_isr
  _endasm
}
#pragma code
void main()
{

	ADCON0=0X81;
	ADCON1=0XC5;
	
	TRISD=0;
	TRISBbits.TRISB7=0;
	TRISBbits.TRISB0=1;
	TRISAbits.TRISA0=1;
	TRISAbits.TRISA2=1;
	INTCONbits.INT0IF=0;
	INTCONbits.INT0IE=1;
	PIE1bits.TMR1IE=1;
	INTCONbits.PEIE = 1;
	INTCONbits.GIE=1;

	T1CON = 0x30;
	

while(1){
	temperature();
	if (bin_temp < temp1){ //If temperature < 25'C
		PORTDbits.RD3 = 1;
	}
	else{
		PORTDbits.RD3 = 0;
	}

if (bin_temp > temp2){ //If temperature > 27'C
		PORTDbits.RD2 = 1;
	}
	else{
		PORTDbits.RD2 = 0;
	}
}

}

void INT0_ISR(void)
 {
  //for(k=0;k<30;k++)
  //for(i=0; i<500; i++)
   //for(j=0; j<165; j++)
  //{
  //} 
	TMR1H=0;TMR1L=0;
	T1CONbits.TMR1ON = 1; //Turn timer on
    INTCONbits.INT0IF=0;
	}

void T1_ISR(){
	counter++;
	if (counter == 29){
	    mybit=~mybit;
    	PORTDbits.RD1=~PORTDbits.RD1;
		counter=0;
		T1CONbits.TMR1ON = 0;
	}
	PIR1bits.TMR1IF = 0;
}

void temperature(void)
{

delay();

ADCON0bits.GO=1;
while(ADCON0bits.DONE==1);

l_byte=ADRESL;
h_byte=ADRESH;

bin_temp=(h_byte << 8)|l_byte;
//Results direct result of conversion without processing
}

void delay(void)
{
		int i,j;
		for(i=0;i<=250;i++)
		for(j=0;j<=135;j++)
         {
          }
}

I've used timer 1 here. Have you worked on timer 1 before? If not, you should go through the above code and the datasheet and write a few programs for timer 1 using interrupt and polling. Then, it'll be easier for you.

Hope this helps.
Tahmid.
 
@Tahmid: Thanks bro!! u have been of great help... The above program works only for about 8 sec right?
 

With 4MHz clock, ~15 seconds. With 8MHz, ~8 seconds.

Timer 1 is set to interrupt every overflow. The prescaler is set to 1:8.

With 4MHz clock, instruction cycle is 1us.
So, timer 1 overflows every (1us * 65536)*8 = ~524.3us.

The counter is incremented every ~524us. Toggle is done after 29 increments. That is, ~15.2 seconds.

Hope this helps.
Tahmid.
 

With 4MHz clock, ~15 seconds. With 8MHz, ~8 seconds.

Timer 1 is set to interrupt every overflow. The prescaler is set to 1:8.

With 4MHz clock, instruction cycle is 1us.
So, timer 1 overflows every (1us * 65536)*8 = ~524.3us.

The counter is incremented every ~524us. Toggle is done after 29 increments. That is, ~15.2 seconds.

Hope this helps.
Tahmid.

Hey i did not get the calculations for
With 4MHz clock, instruction cycle is 1us.
So, timer 1 overflows every (1us * 65536)*8 = ~524.3us.

and if i am using a 10mhz crystal..how will i calculate the time when timer1 overflows..and why have u stored 0x00H value in timer1?
 

I implemented this circuit in hardware..the temp sensor part is working fine...but when i give the interrupt..the relay RL3 doesnt switch after 8 sec...can you tell me what is the problem....
 

Hey i did not get the calculations for
With 4MHz clock, instruction cycle is 1us.
So, timer 1 overflows every (1us * 65536)*8 = ~524.3us.

and if i am using a 10mhz crystal..how will i calculate the time when timer1 overflows..and why have u stored 0x00H value in timer1?

The oscillator frequency is divided by 4 by the PIC. 4MHz frequency means 0.25us time period. Since frequency is divided to 1MHz, time period becomes 1us.
The prescaler is set to 1:8. The timer increments every 8us. After 65536 increments, the timer will roll over to 0 and the interrupt will be generated.
So, as each increment takes 8us, 65536 increments takes approximately 524ms.

If you're using 10MHz crystal, the frequency becomes 2.5MHz and the time period becomes 0.4us.

Hope this helps.
Tahmid.

---------- Post added at 23:17 ---------- Previous post was at 23:17 ----------

With 4MHz clock, ~15 seconds. With 8MHz, ~8 seconds.

Timer 1 is set to interrupt every overflow. The prescaler is set to 1:8.

With 4MHz clock, instruction cycle is 1us.
So, timer 1 overflows every (1us * 65536)*8 = ~524.3us.

The counter is incremented every ~524us. Toggle is done after 29 increments. That is, ~15.2 seconds.

Hope this helps.
Tahmid.

There are 2 typos there. It's supposed to be 524ms, not 524us.

---------- Post added at 23:21 ---------- Previous post was at 23:17 ----------

You might have burnt the ULN2003. Connect a 1N4007 diode across each relay. The cathode of the diode goes to +12V, the anode goes to the other end connected to ULN2003.
If your circuit doesn't work, try with another ULN2003. Also, make sure everything else is fine, such as power supply and oscillator connections, etc.
 

m uploading a image...please see if it is correct connection for diode..?

---------- Post added at 23:47 ---------- Previous post was at 23:46 ----------

 

Yes, it's okay. Connect one across RL1 as well. One thing I noticed is that you did not connect pin 8 of ULN2003 to ground.
 

@Tahmid:pin 8 is not shown in proteus...
I still get the same error even after changing the uln and connecting the diode...the interrupt part does not get executed whereas the temperature sensor part works fine... i checkd all the connections on the board there is no problem...i am working on a bakelite general purpose board...what might be the possible problem please help!!
 

In the simulation, are the state changes for pins 20, 21 and 22 correct? Are there proper outputs on RD1, RD2 and RD3?
 

Yes in the proteus simulation the state changes are correct..the outputs at RD1,RD2 and RD3 are proper.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top