圣龙扬特-AVR电子

标题: STM8S的电容触摸按键实验例程(板自带例程) [打印本页]

作者: 铜河    时间: 2013-2-26 15:45
标题: STM8S的电容触摸按键实验例程(板自带例程)
本例程利用STM8S固件驱动库及STM8S RC触摸按键软件库为用户演示如何用触摸按键控制LED的功能。
说实话,这个例程笔者还没研究透。
/**
  ******************************************************************************
  * @file STM8_main.c
  * @brief RC Touch Sensing Library for STM8 CPU family.
  * Application example.
  * @author STMicroelectronics - MCD Application Team
  * @version V1.2.0
  * @date 24-MAR-2009
  ******************************************************************************
  *
  * 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 2009 STMicroelectronics</center></h2>
  * @image html logo.bmp
  ******************************************************************************
  */

#include "STM8_TSL_RC_API.h"

/* LD1 on STM8S EVALBOARD */
#define LED1_PIN_MASK  ((u8)0x08)
#define LED1_PORT_ODR  GPIOD->ODR
#define LED1_PORT_DDR  GPIOD->DDR
#define LED1_PORT_CR1  GPIOD->CR1

#define LED2_PIN_MASK  ((u8)0x04)
#define LED2_PORT_ODR  GPIOD->ODR
#define LED2_PORT_DDR  GPIOD->DDR
#define LED2_PORT_CR1  GPIOD->CR1

#define LED3_PIN_MASK  ((u8)0x01)
#define LED3_PORT_ODR  GPIOD->ODR
#define LED3_PORT_DDR  GPIOD->DDR
#define LED3_PORT_CR1  GPIOD->CR1

#define LED1_ON()  {LED1_PORT_ODR |= LED1_PIN_MASK; Led1State = ON;}
#define LED1_OFF() {LED1_PORT_ODR &= (u8)(~LED1_PIN_MASK); Led1State = OFF;}

#define LED2_ON()  {LED2_PORT_ODR |= LED2_PIN_MASK; Led2State = ON;}
#define LED2_OFF() {LED2_PORT_ODR &= (u8)(~LED2_PIN_MASK); Led2State = OFF;}

#define LED3_ON()  {LED3_PORT_ODR |= LED3_PIN_MASK; Led3State = ON;}
#define LED3_OFF() {LED3_PORT_ODR &= (u8)(~LED3_PIN_MASK); Led3State = OFF;}

typedef enum
{
  OFF = 0,
  ON = 1
} LedState_T;

LedState_T Led1State,Led2State,Led3State;


_Bool in_range;
_Bool p_valid;
char *ptr;

//--------    SECTION DEFINITION FOR THIS FILE   --------------
#if defined(__CSMC__) && defined(USE_PRAGMA_SECTION)
#pragma section [CONFIG_RAM]
#pragma section @tiny [CONFIG_RAM0]
#pragma section (CONFIG_CODE)
#pragma section const {CONFIG_CONST}
#endif

void ExtraCode_Init(void);
void ExtraCode_StateMachine(void);


/**
  ******************************************************************************
  * @brief Main function.
  * @par Parameters:
  * None
  * @retval void None
  * @par Required preconditions:
  * None
  ******************************************************************************
  */
void main(void)
{

/* Only if _stext routine is not used */
/*
#asm
                sim                // Disable interrupts               
        // To initialize the page 0
                clrw        x
loop0:
                clr        (x)
                incw        x
                cpw        x,#0x05FF
                jrne        loop0
#endasm
*/

  unsigned char value = 100;
  in_range = (value >= 10) && (value <= 20);
  p_valid = ptr;   /* p_valid is true if ptr not 0 */


        CLK->CKDIVR = 0x00; // Main freq divided by 1 = Full Freq

        TSL_Init();

  ExtraCode_Init();

  for (;;) {
   
                ExtraCode_StateMachine();
               
    TSL_Action();
   
        }
  
}


/**
  ******************************************************************************
  * @brief Initialize all the keys, I/Os for LED
  * @par Parameters:
  * None
  * @retval void None
  * @par Required preconditions:
  * None
  ******************************************************************************
  */
void ExtraCode_Init(void)
{

  u8 i;

  /* All keys are implemented and enabled */

  for (i = 0; i < NUMBER_OF_SINGLE_CHANNEL_KEYS; i++)
  {
    sSCKeyInfo[i].Setting.b.IMPLEMENTED = 1;
    sSCKeyInfo[i].Setting.b.ENABLED = 1;
    sSCKeyInfo[i].DESGroup = 0x01; /* Put 0x00 to disable the DES on these pins */
  }

#if NUMBER_OF_MULTI_CHANNEL_KEYS > 0
  for (i = 0; i < NUMBER_OF_MULTI_CHANNEL_KEYS; i++)
  {
    sMCKeyInfo[i].Setting.b.IMPLEMENTED = 1;
    sMCKeyInfo[i].Setting.b.ENABLED = 1;
    sMCKeyInfo[i].DESGroup = 0x01; /* Put 0x00 to disable the DES on these pins */
  }
#endif

  /* Init I/O in Output Push-Pull to drive the LED */
        LED1_PORT_ODR |= LED1_PIN_MASK; /* LED1 is ON by default */
  LED1_PORT_DDR |= LED1_PIN_MASK;
  LED1_PORT_CR1 |= LED1_PIN_MASK;
  
  Led1State = ON;
  
  LED2_PORT_ODR |= LED2_PIN_MASK; /* LED1 is ON by default */
  LED2_PORT_DDR |= LED2_PIN_MASK;
  LED2_PORT_CR1 |= LED2_PIN_MASK;
  Led2State = ON;
  
  LED3_PORT_ODR |= LED3_PIN_MASK; /* LED1 is ON by default */
  LED3_PORT_DDR |= LED3_PIN_MASK;
  LED3_PORT_CR1 |= LED3_PIN_MASK;
  Led3State = ON;
  
  enableInterrupts();

}


/**
  ******************************************************************************
  * @brief Example of LED switching using touch sensing keys
  * KEY1: LED1 ON
  * KEY2: LED1 OFF
  * KEY3: LED1 ON then OFF after 1s delay
  * KEY4: LED1 ON then OFF after 200ms delay
  * KEY5: LED1 ON/OFF
  * @par Parameters:
  * None
  * @retval void None
  * @par Required preconditions:
  * None
  ******************************************************************************
  */
void ExtraCode_StateMachine(void)        {

        if ((TSL_GlobalSetting.b.CHANGED) && (TSLState == TSL_IDLE_STATE))
  {
   
                TSL_GlobalSetting.b.CHANGED = 0;
   
    if (sSCKeyInfo[0].Setting.b.DETECTED) /* KEY 1 touched */
    {
      if (Led1State == OFF)
      {
        LED1_ON();
      }
      else
      {
        LED1_OFF();
      }
    }
   
    if (sSCKeyInfo[1].Setting.b.DETECTED) /* KEY 2 touched */
    {
      if (Led2State == OFF)
      {
        LED2_ON();
      }
      else
      {
        LED2_OFF();
      }
    }
   
    if (sSCKeyInfo[2].Setting.b.DETECTED) /* KEY 3 touched */
    {
      if (Led3State == OFF)
      {
        LED3_ON();
      }
      else
      {
        LED3_OFF();
      }
    }
  }
}

/****************** (c) 2009  STMicroelectronics ******************************/

作者: 铜河    时间: 2013-2-26 15:48
全部文件打包如下,请高手指点:

作者: ba1731    时间: 2013-2-28 14:17
表四鄙人纯属随机路过。。。顺带帮顶。。。




欢迎光临 圣龙扬特-AVR电子 (http://avr.cnta.net/) Powered by Discuz! X2.5