久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2621|回復(fù): 3
打印 上一主題 下一主題
收起左側(cè)

C8051F500單片機(jī)Watchdog程序

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:959278 發(fā)表于 2021-8-6 19:05 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
// Program Description:
//
// This program helps the user to learn about operating the Watch Dog Timer.
// The WDT is used to generate resets if the times between writes to the WDT
// update register (PCA0CPH5) exceed a specified limit. The WDT can be disabled
// and enabled in the software as needed. When enabled the PCA Module 5 acts as
// the WDT. This program resets the MCU when P1.4 switch is pressed. Also upon
// reset the LED blinks approximately five times faster when compared to
// before.The reset is caused due to a WDT overflow and can be confirmed by
// checking the value of the RSTSRC register where bit 3 is set to indicate a
// reset caused by WDT.
//
// Resources Used:
// * Internal Oscillator: MCU clock source
// * Timer 2: Blinks LED
// * PCA Module 5 : Watchdog Timer
//
// How to Test:
// 1) Compile and download code to a 'F500 device.
// 2) Make sure the LED and switch jumpers are placed on J19.
// 3) Run the code:
//        - The test will blink the LED at a rate of 10Hz until the switch SW1
//          (P1.4) is pressed.
//        - Once the the switch is pressed and held for a long enough time,
//          it will prevent the WDT from being touched and the WDT will
//          cause a reset.
//        - Upon reset the code checks for a WDT reset and blinks the LED five
//          times faster than before to indicate the same.
//
// Target:         C8051F500 (Side A of a C8051F500-TB)
// Tool chain:     Keil C51 8.0 / Keil EVAL C51
// Command Line:   None
//
// Release 1.1 / 11 JUN 2008 (ADT)
//    -Edited Formatting
//
// Release 1.0 / 03 MAR 2008 (GP)
//    -Initial Revision
//
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------

#include <compiler_defs.h>
#include <C8051F500_defs.h>            // SFR declarations

//-----------------------------------------------------------------------------
// Global CONSTANTS
//-----------------------------------------------------------------------------

#define SYSCLK       24000000 / 8      // SYSCLK frequency in Hz

SBIT (LED, SFR_P1, 3);                 // LED == 1 means ON
SBIT (SW1, SFR_P1, 4);                 // SW1 == 0 means switch depressed

//-----------------------------------------------------------------------------
// Function PROTOTYPES
//-----------------------------------------------------------------------------

void OSCILLATOR_Init (void);
void PORT_Init (void);
void PCA_Init (void);
void TIMER2_Init (U16 counts);

INTERRUPT_PROTO (TIMER2_ISR, INTERRUPT_TIMER2);

//-----------------------------------------------------------------------------
// main() Routine
//-----------------------------------------------------------------------------
//
// The MAIN routine performs all the intialization, and then loops until the
// switch is pressed. When SW2 (P0.7) is pressed the code checks the RSTSRC
// register to make sure if the last reset is because of WDT.
//-----------------------------------------------------------------------------
void main (void)
{
   SFRPAGE = ACTIVE_PAGE;              // Set SFR Page for PCA0MD and RSTSRC

   PCA0MD &= ~0x40;                    // Disable the watchdog timer

   OSCILLATOR_Init ();                 // Initialize system clock to 24.5/8 MHz
   PCA_Init ();                         // Intialize the PCA
   PORT_Init ();                        // Initialize crossbar and GPIO

   if ((RSTSRC & 0x02) == 0x00)        // First check the PORSF bit. if PORSF
   {                                   // is set, all other RSTSRC flags are
                                       // invalid
      // Check if the last reset was due to the Watch Dog Timer
      if (RSTSRC == 0x08)
      {
         TIMER2_Init (SYSCLK / 12 / 50);    // Make LED blink at 50Hz
         EA = 1;                            // Enable global interrupts

         while (1);                          // Wait forever
      }
      else
      {
         // Init Timer2 to generate interrupts at a 10Hz rate.
         TIMER2_Init (SYSCLK / 12 / 10);
      }
   }

   // Calculate Watchdog Timer Timeout
   // Offset calculated in PCA clocks
   // Offset = ( 256 x PCA0CPL5 ) + 256 - PCA0L
   //        = ( 256 x 255(0xFF)) + 256 - 0
   // Time   = Offset * (12/SYSCLK)
   //        = 255 ms ( PCA uses SYSCLK/12 as its clock source)

   PCA0MD  &= ~0x40;                   // Disable watchdog timer
   PCA0L    = 0x00;                    // Set lower byte of PCA counter to 0
   PCA0H    = 0x00;                    // Set higher byte of PCA counter to 0
   PCA0CPL5 = 0xFF;                    // Write offset for the WDT
   PCA0MD  |= 0x40;                    // Enable the WDT

   EA = 1;                             // Enable global interrupts

   //--------------------------------------------------------------------------
   // Main Application Loop/Task Scheduler
   //--------------------------------------------------------------------------

   while (1)
   {
       //----------------------------------------------------------------------
       // Task #1 - Check Port I/O
       //----------------------------------------------------------------------
       while (!SW1);                 // Force the MCU to stay in this task as
                                    // long as SW1 is pressed. This task must
                                    // finish before the watchdog timer
                                    // timeout expires.

       //----------------------------------------------------------------------
       // Task #2 - Reset Watchdog Timer
       //----------------------------------------------------------------------
       PCA0CPH5 = 0x00;             // Write a 'dummy' value to the PCA0CPH5
                                    // register to reset the watchdog timer
                                    // timeout. If a delay longer than the
                                    // watchdog timer delay occurs between
                                    // successive writes to this register,
                                    // the device will be reset by the watch
                                    // dog timer.
    }
}

//-----------------------------------------------------------------------------
// Initialization Subroutines
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// OSCILLATOR_Init
//-----------------------------------------------------------------------------
// Return Value : None
// Parameters   : None
//
// This routine initializes the system clock to use the internal 24 MHz / 8
// oscillator as its clock source.  Also enables missing clock detector reset.
//-----------------------------------------------------------------------------
void OSCILLATOR_Init (void)
{
   U8 SFRPAGE_save = SFRPAGE;
   SFRPAGE = CONFIG_PAGE;

   OSCICN = 0x84;                      // Configure internal oscillator for
                                       // 24 MHz / 8

   SFRPAGE = ACTIVE_PAGE;

   RSTSRC = 0x04;                      // Enable missing clock detector

   SFRPAGE = SFRPAGE_save;
}

//-----------------------------------------------------------------------------
// PCA_Init
//-----------------------------------------------------------------------------
// Return Value : None
// Parameters   : None
//
// This routine initializes the PCA to use the SYSCLK / 12
// as its clock source.  It also sets the offset value by writing to PCA0CPL2.
//-----------------------------------------------------------------------------
void PCA_Init()
{
   U8 SFRPAGE_save = SFRPAGE;
   SFRPAGE = ACTIVE_PAGE;

   PCA0CN     =  0x40;                 // PCA counter enable
   PCA0MD    &= ~0x40 ;                // Watchdog timer disabled
   PCA0MD    &=  0xF1;                 // Timebase selected - System clock / 12
   PCA0CPL5   =  0xFF;                 // Offset value

   SFRPAGE = SFRPAGE_save;
}

//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
// Return Value : None
// Parameters   : None
//
// This function configures the Crossbar and GPIO ports.
//
// P1.3   digital   push-pull     LED
// P1.4   digital   open-drain    SW1
//-----------------------------------------------------------------------------
void PORT_Init (void)
{
   U8 SFRPAGE_save = SFRPAGE;
   SFRPAGE = CONFIG_PAGE;

   P1MDOUT |= 0x08;                    // Enable LED as a push-pull output
   XBR2     = 0x40;                    // Enable crossbar and weak pull-ups

   SFRPAGE = SFRPAGE_save;
}

//-----------------------------------------------------------------------------
// TIMER2_Init
//-----------------------------------------------------------------------------
// Return Value : None
// Parameters   :
//   1)  int counts - calculated Timer overflow rate
//                    range is positive range of integer: 0 to 32767
//
// Configure Timer2 to 16-bit auto-reload and generate an interrupt at
// interval specified by <counts> using SYSCLK/48 as its time base.
//-----------------------------------------------------------------------------
void TIMER2_Init (U16 counts)
{
   U8 SFRPAGE_save = SFRPAGE;
   SFRPAGE = ACTIVE_PAGE;

   TMR2CN  = 0x00;                     // Stop Timer2; Clear TF2;
                                       // use SYSCLK/12 as timebase
   CKCON  &= ~0x60;                    // Timer2 clocked based on T2XCLK;

   TMR2RL  = -counts;                  // Init reload values
   TMR2    = 0xFFFF;                   // Set to reload immediately
   ET2     = 1;                        // Enable Timer2 interrupts
   TR2     = 1;                        // Start Timer2

   SFRPAGE = SFRPAGE_save;
}

//-----------------------------------------------------------------------------
// Interrupt Service Routines
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// Timer2_ISR
//-----------------------------------------------------------------------------
//
// This routine changes the state of the LED whenever Timer2 overflows.
//-----------------------------------------------------------------------------
INTERRUPT (TIMER2_ISR, INTERRUPT_TIMER2)
{
   TF2H = 0;                           // Clear Timer2 interrupt flag
   LED  = !LED;                        // Change state of LED
}

//-----------------------------------------------------------------------------
// End Of File
//-----------------------------------------------

評(píng)分

參與人數(shù) 1黑幣 +20 收起 理由
admin + 20 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:64765 發(fā)表于 2021-12-20 10:20 | 只看該作者
正在學(xué)習(xí)C8051F單片機(jī),學(xué)習(xí)參考。謝謝分享。
回復(fù)

使用道具 舉報(bào)

板凳
ID:713888 發(fā)表于 2021-12-20 19:51 | 只看該作者
單片機(jī),學(xué)習(xí)參考。謝謝分享
回復(fù)

使用道具 舉報(bào)

地板
ID:64765 發(fā)表于 2022-2-6 13:20 | 只看該作者
正在學(xué)習(xí)C8081F系列單片機(jī),真是雪中送炭。謝謝分享。
回復(fù)

使用道具 舉報(bào)

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表
主站蜘蛛池模板: 男人天堂99| 波多野结衣先锋影音 | 精品国产视频 | 亚洲国产精品久久久久 | 国产欧美一区二区久久性色99 | 亚洲 欧美 另类 综合 偷拍 | 日韩欧美大片在线观看 | 日韩一区和二区 | 日韩免费| 日韩欧美国产一区二区 | 精品国产91乱码一区二区三区 | 中文字幕在线精品 | 国产亚洲精品综合一区 | 精品久久久久一区二区国产 | 午夜一级做a爰片久久毛片 精品综合 | 国产97碰免费视频 | 夜夜夜夜夜夜曰天天天 | av网址在线播放 | 精品一区二区久久久久久久网站 | 国产精品一二三区 | 亚洲狠狠爱 | 在线看h| 日韩成人精品视频 | 日韩精品亚洲专区在线观看 | 国产成人精品久久二区二区91 | 午夜电影福利 | 欧美精品网站 | av中文字幕在线播放 | 欧美一级免费看 | 在线不卡视频 | 国产欧美精品一区二区三区 | 国产成人免费视频网站视频社区 | 狠狠插天天干 | 视频精品一区二区三区 | av在线黄 | 色综合久久88色综合天天 | 麻豆视频在线免费看 | 久久久久久成人 | 日韩精品视频在线观看一区二区三区 | 日韩av成人在线 | 成人免费区一区二区三区 |