AVR Microcontroller


Atmega128 Watchdog Timer

จากที่ได้ค้นหาความรู้จากแหล่งหลายๆที่นั้น มีบางเว็บอะธิบายได้อย่างเห็นภาพกันเลยทีเดียว “Watchdog” หากแปลจากหลังมาหน้าก็จะได้เป็นคำตรงๆนี้เลย หมาดู หมาเฝ้า ฮาๆตลกดี ในการทำงานโดยใช้ cpu หรือ mcu เบอร์ใดๆก็ตาม จำต้องมีวงจรหนึ่งซึ่งทำหน้าที่เฝ้ามองการทำงานของ mcu

หลักการณ์ทำงานก็คือ ต้องกำหนดเวลาให้ Watchdog ทำงาน (การทำงานคือเริ่มต้นระบบใหม่หรือรีสตาทระบบใหม่อีกรอบ) ตามแต่ระบบนั้นๆ และอีกจุดคือคำสั่งสำหรับเซ็ตให้ Watchdog เริ่มต้นนับเวลาใหม่อีกรอบเพื่อไม่ให้ mcu เริ่มต้นทำงานใหม่อีกรอบนั่นเอง โค้ดต่อไปนี้ ของท่าน firmware.c จาก http://www.electoday.com/bbs/viewthread.php?tid=3498&extra=page%3D1 ที่โพสตอบผม ขอบคุณครับ

#include 
 
union  {
   struct {
       unsigned char flag1 :1;
       unsigned char flag2 :1;
       unsigned char flag3 :1;
       unsigned char flag4 :1;
       unsigned char undefine :4;
   }bit;
   unsigned char byte;
} health_flag;
 
int main (void)
{
    wdt_enable(WDTO_2S);
 
     while(1){
           process1();
           process2();
           process3();
           process4();
 
           if( check_health() ){
 
               wdt_reset(); // ให้ watchdog เริ่มต้นนับเวลาใหม่
               health_flag.byte = 0;  /* clear all health flags*/
           }
    }
}
 
void process1(void)
{
     health_flag.bit.flag1 = 1;
}
 
void process2(void)
{
     health_flag.bit.flag2 = 1;
}
 
void process3(void)
{
     health_flag.bit.flag3 = 1;
}
 
void process4(void)
{
     health_flag.bit.flag4 = 1;
}
 
unsigned char check_health (void)
{
     if(health_flag.byte == 0x0F){
        return 1; /* all process was service */
     }
 
     return 0;     /* some thing abnormal */
}

ท่านสามารถหาข้อมูลหรือโพสถามได้ที่ www.electoday.com

Convert UTC to local time

#include
typedef struct _DATE_TIME_STRUCT_
{
        unsigned char sec;
        unsigned char minute;
        unsigned char hour;
        unsigned char date;
        unsigned char month;
        unsigned short year;
}DATETIME;
 
void UTC2Localtime(DATETIME *dt, signed char gmt)
{
        unsigned char monthlimit[] = {31, 28, 31, 30, 31, 30,
                                                31, 31, 30, 31, 30, 31};
        signed char tmp;
 
        if ((dt->year % 4) == 0)
                monthlimit[1] = 29;
        if ((dt->year % 100) == 0)
                monthlimit[1] = 28;
        if ((dt->year % 400) == 0)                // for 4-digits year system
                monthlimit[1] = 29;
 
        tmp = dt->hour;
        tmp += gmt;
        if (tmp >= 24)
        {
                dt->hour = tmp - 24;
                dt->date++;
                if (dt->date > monthlimit[dt->month - 1])
                {
                        dt->date -= monthlimit[dt->month - 1];
                        dt->month++;
                        if (dt->month > 12)
                        {
                                dt->month -= 12;
                                dt->year++;
                        }
                }
        }
        else if (tmp < 0)
        {
                dt->hour = (24 + tmp);
                dt->date--;
                if (dt->date == 0)
                {
                        dt->month--;
                        if (dt->month == 0)
                        {
                                dt->year--;
                                dt->month = 12;
                        }
                        dt->date = monthlimit[dt->month - 1];
                }
        }
        else
        {
                dt->hour = tmp;
        }
}
 
int main(int argc, char *argv[])
{
	DATETIME d1;
 
	d1.date  = 26;
	d1.month = 12;
	d1.year  = 2009;
	//053326
	d1.sec    = 1;
	d1.minute = 23;
	d1.hour   = 19;
 
	UTC2Localtime(&d1,+7);
 
	printf("%d/%d/%d %d:%d:%d",d1.date,d1.month,d1.year,d1.hour,d1.minute,d1.sec);
 
	return 0;
}

Code by tuinui98 From http://www.electoday.com/bbs/viewthread.php?tid=2451&extra=&highlight=GMT&page=2

Post Footer automatically generated by Add Post Footer Plugin for wordpress.

แสดงความคิดเห็น

เขียนความคิดของคุณลงในช่องด้านล่างนี้ครับ!...
และถ้าคุณต้องการแสดงรูปภาพเมื่อแสดงความคิดเห็น สมัครใช้บริการที่นี่ >> gravatar!

*