PIC Vietnam

Go Back   PIC Vietnam > Microchip PIC > Cơ bản về vi điều khiển và PIC

Tài trợ cho PIC Vietnam
Trang chủ Đăng Kí Hỏi/Ðáp Thành Viên Lịch Bài Trong Ngày Vi điều khiển

Cơ bản về vi điều khiển và PIC Những bài hướng dẫn cơ bản nhất để làm quen với vi điều khiển PIC

 
 
Ðiều Chỉnh Xếp Bài
Prev Previous Post   Next Post Next
Old 29-08-2013, 05:08 PM   #1
huyle_khongminh
Nhập môn đệ tử
 
Tham gia ngày: Aug 2005
Bài gửi: 2
:
Help! WatchDog Không hoạt động

Em đang làm BTL về điều khiển độ ẩm nhưng chương trình của em hay bị treo và e có sử dụng watchdog để tự khởi động lại nhưng không hiểu sao chương trình của em vẫn bị treo. Mong các bác chỉ giáo
PIC: 16F873
chương trình chạy theo chu trình 1s đọc kết quả nhiệt độ rồi hiển thị LCD, và so sánh nếu nhiệt độ trong dải cho phép rồi thì tắt quạt ngược lại thì bật quạt

Code:
#include "main.h"
#include "LCD.C"
#include "sht75.c"

#define Led1 PIN_B7
#define Rst PIN_B6

#define Role1 PIN_B1
#define Role2 PIN_C6

#define RoleOn 0
#define RoleOff 1
#define constTNghi 1200
#define constTOn 11000
#define constTOff 1800

//#define constTNghi 400
//#define constTOn 600
//#define constTOff 600

#rom 0x2100={0x00}

char Mode=0;
int16 Time=0;
char iEr = 0, iOff = 0, iReset = 0;
float restemp=0,truehumid=0;
//Mode = 0: LOC OFF
//Mode = 1: HUT AM
//Mode = 2: Nghi

void StatusRole(char status){
output_bit(Role1, status);
output_bit(Role2, status);
output_bit(Led1, status);
}

void main()
{
//Init System
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_CLOCK_DIV_2);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);setup_wdt( WDT_2304MS);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_ccp1(CCP_OFF);

//Init Pro
StatusRole(RoleOff);
output_bit(Led1, 0);
sht_init();
lcd_init();

Mode=2;
Time= constTNghi + read_EEPROM(0x00)*2; //1200

//Mode Test
if(!input(Rst)){
delay_ms(100);
if(!input(Rst)){
Mode=1;
Time=0;
printf(lcd_putc, "\fChe do Kiem Tra");
while(!input(Rst));
}
}

sht_rd (restemp, truehumid);
delay_ms(1000);
output_bit(Led1, 1);

//restart_cause
if(restart_cause() != NORMAL_POWER_UP){
printf(lcd_putc,"\f%i",restart_cause());
while(true) restart_wdt();
}


//MAIN
while(true){
//Mode = 0: LOC OFF
//Mode = 1: HUT AM
//Mode = 2: Nghi

//output_bit(PIN_B5, 1);
//delay_ms(2);
//output_bit(PIN_B5, 0);

restart_wdt();
sht_rd(restemp, truehumid);
delay_ms(100);

Time++;
if((Time % 30)==2) lcd_init();
//restemp = restemp - read_EEPROM(0x2100);
//truehumid = truehumid - read_EEPROM(0x2101);
lcd_gotoxy(1,1); printf(lcd_putc, "t:%0.0f%cC ", restemp, 223);
lcd_gotoxy(9,1); printf(lcd_putc, "RH:%0.1f%% \n",truehumid);

//Reset
if(!input(Rst)){
delay_ms(100);
if(!input(Rst)) iReset++;
if(iReset>=2) reset_cpu();
}else iReset = 0;

//Check Loi CB
if(restemp<-20){
if(iEr>3){
lcd_gotoxy(1,2);
printf(lcd_putc, "LOI CAM BIEN ");
StatusRole(RoleOff);
}else{
iEr++;
}
Time--;
delay_ms(1000);
continue;
}else iEr=0;


//Chuyen Mode
if(truehumid>=55 && Mode==0) {
Mode = 1; //RH>55 dua ve Loc On o all cac Mode
Time = 0;
write_eeprom(0x01,Mode);

}else if(truehumid<=40 && Mode==1 && Time>99){ //Loc Off khi dang LOC on & RH<40
Mode=0;
Time=0;
write_eeprom(0x01,Mode);
}

if((Time>constTOn) && (Mode == 1)){
Mode=2;
Time=0;
write_eeprom(0x01,Mode);

}else if((Time>constTOff) && (Mode == 2)){
Mode=1; //Ve Mode 1 chay them 100s
Time=0;
write_eeprom(0x01,Mode);
}

//Thuc hien Mode
if(Mode==0){
if(Time>300) Time=0;
printf(lcd_putc, "OFF ");
for(iOff=0; iOff<=Time%5; iOff++) printf(lcd_putc, ".");
StatusRole(RoleOff);

}else if(Mode==1){
printf(lcd_putc, "HUT AM: %Lu",Time);
if(Time<100 ) printf(lcd_putc, "/100");
StatusRole(RoleOn);

}else if(Mode==2){
printf(lcd_putc, "NGHI: %Lu",Time);
StatusRole(RoleOff);
}

printf(lcd_putc, " ");

delay_ms(570);
}
}

Mong các bác chỉ giáo
File Kèm Theo
File Type: rar HutAm_v2.rar (109.1 KB, 1 lần tải)
huyle_khongminh vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
 


Quyền Sử Dụng Ở Diễn Ðàn
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is Mở
Smilies đang Mở
[IMG] đang Mở
HTML đang Tắt

Chuyển đến


Múi giờ GMT. Hiện tại là 07:18 PM.


Được sáng lập bởi Đoàn Hiệp
Powered by vBulletin®
Page copy protected against web site content infringement by Copyscape
Copyright © PIC Vietnam