⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 STM8示例程序
💻 C
字号:
/**
  ******************************************************************************
  * @file WWDG_RearmReset\main.c
  * @brief This file contains the main function for wwdg Rearm Reset example.
  * @author STMicroelectronics - MCD Application Team
  * @version V1.0.1
  * @date 09/22/2008
  ******************************************************************************
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2008 STMicroelectronics</center></h2>
  * @image html logo.bmp
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "stm8s_lib.h"

/**
  * @addtogroup WWDG_RearmReset
  * @{
  */

/* Private typedef -----------------------------------------------------------*/

/* Private define ------------------------------------------------------------*/

/* Evalboard I/Os configuration */

#define LEDS_PORT 				(GPIOH)
#define LED1_PIN  				(GPIO_PIN_3)
#define LED2_PIN  				(GPIO_PIN_2)
#define LED3_PIN  				(GPIO_PIN_1)
#define LED4_PIN  				(GPIO_PIN_0)

#define JOYSTICK_SEL_PORT	(GPIOD)
#define JOYSTICK_SEL_PIN	(GPIO_PIN_7)

#define BUTTON_PORT 			(GPIOC)
#define BUTTON_PIN  			(GPIO_PIN_0)

#define Key_is_pressed ((GPIO_ReadInputData(BUTTON_PORT) & BUTTON_PIN) == (u8)0x00)
#define JoySelect_is_pressed ((GPIO_ReadInputData(JOYSTICK_SEL_PORT) & JOYSTICK_SEL_PIN) == (u8)0x00)

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
u8 window = 0x77;
u16 TIME = 0xFFFF;
u8 Counter;
FunctionalState Refresh;
FunctionalState NonAlowedRefresh;
/* Private function prototypes -----------------------------------------------*/
void Delay (u16 nCount);
void GPIO_Configuration(void);
void WWDG_Configuration(void);
void Test_WWDGReset(void);

/**
  * @brief Example firmware main entry point.
  * @par Parameters:
  * None
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Library functions called:
  * - WWDG_Init
  * - GPIO_Init
  * - GPIO_WriteLow	
  * - GPIO_WriteHigh
  * - GPIO_WriteReverse
  * - RST_GetFlagStatus
  * - RST_ClearFlag
  */
void main(void)
{

  /* Contains which LEDs to operate */
	u8 Leds; 

  /* GPIO Configuration */
  GPIO_Configuration();

  /*Test if Reset occured*/
  Test_WWDGReset();

  /* WWDG Configuration */
  WWDG_Configuration();
	  
	/* Enable general interrupts for Timer 4 and Push Button on eval board */
  enableInterrupts();
	
	
	/* All LEDs ON */
  GPIO_WriteHigh(LEDS_PORT, LED1_PIN | LED2_PIN | LED3_PIN | LED4_PIN );
	
	/* Enable WWDG safe Refresh */
  Refresh = ENABLE;
	NonAlowedRefresh = DISABLE;
	
  while (1)
  {
/* Refresh during the Non Alowed Window => WWDG RESET */
		if (Key_is_pressed)
		{
			/* ------------- Button is pressed ------------- */	
			/* Reverse LED1 and LED4 status */
			GPIO_WriteReverse(LEDS_PORT, (LED1_PIN | LED4_PIN));
			Delay(TIME);
			
			/* Authorize WWDG refresh on non alowed window */ 
			NonAlowedRefresh = ENABLE;
	  }
		
/* No Refresh => WWDG RESET */
		if (JoySelect_is_pressed)
		{
			/* ------------- JOYSTICK SELECT button is pressed ------------- */	
      
			/* Disable Refresh */
			Refresh = DISABLE;
			
			/* Reverse LED2 and LED3 status */
			GPIO_WriteReverse(LEDS_PORT, (LED2_PIN | LED3_PIN));
			Delay(TIME);
	  }
	
		if (Refresh != DISABLE)
		{
			if (NonAlowedRefresh != DISABLE)
			{
				Counter = (u8)(WWDG_GetCounter() & 0x7F);
				if(Counter > window)
				{
					WWDG_SetCounter(0xFF);
				}
			}
			else
			{
				Counter = (u8)(WWDG_GetCounter() & 0x7F);
				if(Counter < window)
				{
					WWDG_SetCounter(0xFF);
				}
			}
		}
 }
}
/* Private functions ---------------------------------------------------------*/
void Delay(u16 nCount) 
{
  /* Decrement nCount value */
  while (nCount != 0)
  {
    nCount--;
  }
}


/**
  * @brief Configures the WWDG to generate a Reset if the WWDG is not refreched during the correct window.
  * @par Parameters:
  * None
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Library functions called:
  * - WWDG_Init
  */

void WWDG_Configuration(void) 
{
    /* WWDG Configuration */
    /* Watchdog Window= 0x7F step to 0x3F step
    									= (0x7F-0x3F) * 1 step
    									= 64 * 1 step
    									= 64 * (12288/2Mhz)
    									= 393,216ms
    */
    /* Allowed Window = (0x7F-window) * 1 step
		                  = (0x7F-0x77) * 1 step
    									= 7 * 1 step 
    									= 7 * (12288/2Mhz) 
    									= 43.008ms 
    */
    /* So the non allowed window starts from 0.0ms to 43.008ms
    and the alowed window starts from 43.008ms to 393,216ms */
    WWDG_Init(0x7F,window);
}

/**
  * @brief Configures the Leds and the Buttons IO.
  * @par Parameters:
  * None
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Library functions called:
  * - GPIO_Init
  */

void GPIO_Configuration(void) 
{
  

  /* LEDS: Initialize I/Os in Output Mode */
  GPIO_Init(LEDS_PORT, (LED1_PIN | LED2_PIN | LED3_PIN | LED4_PIN), GPIO_MODE_OUT_PP_LOW_FAST);
	
	/* Button: Initialize I/O in Input Mode with No Interrupt */

  GPIO_Init(BUTTON_PORT, BUTTON_PIN, GPIO_MODE_IN_FL_NO_IT);
	
	/* JOYSTICK_SEL: Initialize I/O in Input Mode with No Interrupt */
  GPIO_Init(JOYSTICK_SEL_PORT, JOYSTICK_SEL_PIN, GPIO_MODE_IN_FL_NO_IT);
}

/**
  * @brief Tests if the Microcontroller has occured an WWDG Reset, If Yes, Leds are Toggled n times.
  * @par Parameters:
  * None
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Library functions called:
  * - GPIO_WriteLow	
  * - GPIO_WriteHigh
  * - RST_GetFlagStatus
  * - RST_ClearFlag
  */

void Test_WWDGReset(void)
{
  FlagStatus WwdgFlag;
	u8 i=0;
	u8 n=2;
  u8 Leds = (LED1_PIN | LED2_PIN | LED3_PIN | LED4_PIN);
  
  /*Get WWDG Reset Status */ 
	WwdgFlag = RST_GetFlagStatus(RST_FLAG_WWDGF);

  
  /* Test if a WWDG Reset has occured */
	if (WwdgFlag)
  {
		 /* Clear IWDGF Flag */
		RST_ClearFlag(RST_FLAG_WWDGF);
		
		for(i=0;i<n;i++)
		{
			/* ---------- An WWDG Reset has occured, Toggles LD1, LD2, LD3 and LD4 ------- */
			/* LEDs on */
			GPIO_WriteHigh(LEDS_PORT, Leds);
			/* delay */		
			Delay(TIME);
			Delay(TIME);
			/* LEDs off */
			GPIO_WriteLow(LEDS_PORT, Leds); 
			/* delay */		
			Delay(TIME);
			Delay(TIME);
		}
	}
}

/* Public functions ----------------------------------------------------------*/

/**
  * @brief Reports the name of the source file and the source line number where
  * the assert error has occurred.
  * User can add his own implementation to report the file name and line number.
  * ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line)
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Called functions:
  * None
  */
#ifdef FULL_ASSERT
void assert_failed(u8 *file, u16 line)
#else
void assert_failed(void)
#endif
{
  /* Add your own code to manage an assert error */
  /* Infinite loop */
  while (1)
  {
  }
}

/**
  * @}
  */

/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -