Osawa_Odessa
Banned
- Joined
- Dec 31, 2012
- Messages
- 168
- Helped
- 1
- Reputation
- 2
- Reaction score
- 1
- Trophy points
- 1,298
- Activity points
- 0
Hi, I need your help again! I am having a hard time with the code to measure temperature, humidity and control a fan.
The problem located in two bold parts in the code. In the timer0 interrupt , I can check and make sure that "enable_display" really has value 1 but the code in if in main doesn't happen.
When I remove enable_interrupts(GLOBAL) in timer0 interrupt, the if code can be done but I can remove " enable_interrupts(GLOBAL)".
Code:
#include <16f877a.h>
#device adc=10
#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES NOPUT //No Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD //No EE protection
#FUSES NOWRT //Program memory not write protected
#FUSES RESERVED //Used to set the reserved FUSE bits
#use fast_io(a)
#use fast_io(b)
#use fast_io(c)
#use fast_io(d)
#use fast_io(e)
#use i2c(Master,sda=PIN_C4,scl=PIN_C3)
#use delay(clock=20000000)
#include <lcd.c>
#bit set_ra5=0x85.5
#bit RC0=0x87.0
#bit RC3=0x87.3
#byte trisc=0x07
#byte trisa=0x85
#byte trisb=0x86
#byte trise=0x89
#byte porte=0x09
#byte portb=0x06
#byte portc=0x07
#byte porta=0x05
//khai bao bien cho quet led
int8 maled[10]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
//khai bao bien dem
int8 count1=0,count2, x;
//bien doc nhiet do
int16 t,t1;
// 1 so bien kiem tra dieu kien
int1 kt1=0,kt2=0;
//kt6 kiem tra nut ok dc nhan
/*kt1 la bien
kt2 la bien de chon led ben trai hay ben phai cua led doi sang
*/
unsigned int16 frequency=0;
unsigned int8 enable_display=0;
unsigned int8 count;
//cac ham dung trong chuong trin
unsigned int8 HS1101_GetHumi(unsigned int8 frequency);
const unsigned int16 HS1101_Table[101]={
8109,8090,8070,8051,8033,8015,7997,7979,7961,7944,
7927,7910,7894,7878,7862,7846,7830,7815,7799,7784,
7769,7755,7740,7726,7711,7697,7683,7669,7655,7641,
7628,7614,7600,7587,7574,7560,7547,7534,7521,7507,
7494,7481,7468,7455,7442,7429,7416,7403,7390,7377,
7364,7350,7337,7324,7311,7298,7284,7271,7257,7244,
7230,7216,7203,7189,7175,7161,7147,7132,7118,7103,
7089,7074,7059,7045,7029,7014,6999,6984,6968,6952,
6936,6920,6904,6888,6872,6855,6838,6821,6804,6787,
6770,6752,6735,6717,6699,6680,6662,6644,6625,6606,
6587};
unsigned int8 HS1101_GetHumi(unsigned int8 frequency)
{
unsigned int8 j;
for(j=0;j<101;j++)
{
if((frequency+25)>HS1101_Table[j])return j;
}
return 100;
}
#int_timer0
void ngat_timer0()
{
count1++; //bien dem cho update thoi gian,nhiet do
kt2++;
// QUET LED 7 THANH
if(kt2==1) //0.0131072s
{
//RC0=1; //
output_c(0x01);
portb=maled[t/10];
} //
else //
{
//RC1=1; //
output_c(0x08); //
portb=maled[t%10];
}
//update thoi gian,nhiet do
if(count1>=50) //0.66 giay update 1 lan
{
t=read_adc(); //update nhiet do
t=t*100/204;
t1=read_adc();
t1=t1*100/204;
count1=0;
kt1=1;
}
[B] disable_interrupts(GLOBAL);
set_timer0(5);
count++;
if(count==78)
{
frequency=get_timer1();
set_timer1(0);
count=0;
enable_display =1;
}
enable_interrupts(GLOBAL);
}[/B]
#int_timer2
void dk_dongco()
{
if((0<=t1)&&(t1<40)) set_pwm1_duty(0);
else if(t1>=60) set_pwm1_duty(624);
else if( (t1<60)&&(t1>=40)) set_pwm1_duty((t1-40)*15+312);
set_timer2(0);
}
void main()
{
unsigned int8 humi;
//khoi tao lcd
lcd_init();
//khoi tao cac port
set_ra5=1;
//trise=0b111;
set_tris_e(0b111);
//porte=0x00;
output_e(0);
//trisb=0b10000000;
set_tris_b(0b10000000);
//portb=0x00;
output_b(0);
//trisc=0b11100000;
set_tris_c(0b11100010);
//trisc=0b11100000;
//output_c(0);
//setup adc
setup_adc_ports(AN0);
setup_adc(adc_clock_internal);
set_adc_channel(PIN_A0);
delay_us(10);
//setup timer0
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256);
set_timer0(5);
enable_interrupts(int_timer0);
//setup timer1
// enable_interrupts(INT_TIMER1);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
set_timer1(0);
enable_interrupts(INT_TIMER1);
//setup timer2
//enable_interrupts(int_timer2);
setup_ccp1(ccp_pwm);
setup_timer_2(t2_div_by_16,155,1);
//dk_dongco();
set_timer2(0);//vi da set trong ham ngat rui.
enable_interrupts(int_timer2);
//ngat toan cuc
enable_interrupts(global);
while(true)
{
[B]
if(enable_display)
{
printf(lcd_putc,"%u",32);
humi= HS1101_GetHumi(frequency);
enable_display=0;
printf(lcd_putc,"%u",humi);
} [/B]
}
}
disable_interrupts(GLOBAL);
set_timer0(5);
count++;
if(count==78)
{
frequency=get_timer1();
set_timer1(0);
count=0;
enable_display =1;
}
enable_interrupts(GLOBAL);
}
Code:
if(enable_display)
{
printf(lcd_putc,"%u",32);
humi= HS1101_GetHumi(frequency);
enable_display=0;
printf(lcd_putc,"%u",humi);
}
Last edited: