Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
#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++)
{
}
}
#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++)
{
}
}
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?
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.