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

📄 f338_touchsense_switch.c

📁 芯科原厂所有c8051fxx程序的例子。
💻 C
📖 第 1 页 / 共 2 页
字号:
}

//-----------------------------------------------------------------------------
// COMPARATOR0_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// This function configures comparator0.
//-----------------------------------------------------------------------------
void COMPARATOR0_Init (void)
{
   unsigned int i = 0;
   CPT0CN    = 0x80;
   for (i = 0; i < 35; i++);           // Wait 10us for initialization
   CPT0CN    &= ~0x30;                 // Clear rising and falling edge flags
   CPT0MX    = 0x88;                   // CP0+ = P2.0;  CP0- = P2.1
}

//-----------------------------------------------------------------------------
// TIMER0_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// This function configures timer0.
//-----------------------------------------------------------------------------
void TIMER0_Init (void)
{
   TMOD     &= ~0x0F;                  // Clear Timer0 bits in the TMOD SFR
                                       // Sets Mode0, and no INT0 gating

   TMOD     |= 0x04;                   // Timer0 incremented by high-to-low 
                                       // transitions on ext. input T0 (P2.3)

   TH0       = 0x00;
   TL0       = 0x00;

   TR0       = 1;                      // Start Timer0
}

//-----------------------------------------------------------------------------
// TIMER2_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// This function configures timer2 in 16-bit auto-reload mode, clocked by
// SYSCLK/12; set to overflow at T2_OVERFLOW_RATE.
//-----------------------------------------------------------------------------
void TIMER2_Init (void)
{
   CKCON    &= ~0x30;                  // Timer2 clock is defined in TMR2CN
   TMR2CN    = 0x00;                   // Timer2 is clocked by SYSCLK/12
                                       // Operates in 16-bit auto-reload mode

   // Set to overflow at T2_OVERFLOW_RATE
   // (Negation sign in front because the timer counts "0xFFFF-TMR2RL" before
   //  an overflow)
   TMR2RL    = -(SYSCLK / 12 * T2_OVERFLOW_RATE / 1000);

   TMR2      = 0xFFFF;                 // Set to overflow immediately

   TR2       = 1;                      // Start Timer2
}

//-----------------------------------------------------------------------------
// INTERRUPTS_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// This function configures interrupts; Enables Timer2 interrupt.
//-----------------------------------------------------------------------------
void INTERRUPTS_Init (void)
{
   ET2 = 1;
}

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

//-----------------------------------------------------------------------------
// TIMER2_ISR
//-----------------------------------------------------------------------------
//
// This ISR is triggered whenever Timer2 overflows. It checks the switch status
// by comparing the current count of Timer0 against an established threshold.
//-----------------------------------------------------------------------------
INTERRUPT(TIMER2_ISR, INTERRUPT_TIMER2)
{
   TF2H = 0;                           // Clear Timer2 high-byte overflow flag

   TR0 = 0;                            // Stop Timer0
   
   // Save Timer0 count (SW3 relaxation oscillator transition count)
   SW3_T0_Count.U8[LSB] = TL0;         
   SW3_T0_Count.U8[MSB] = TH0;
   
   TL0 = 0;                            // Reset Timer0 count
   TH0 = 0;
   
   TR0 = 1;                            // Restart Timer0

   // If the count is greater than the cal value, it implies lower capacitance,
   // which means the switch is not being touched.
   // If the count is lower or equal to the cal value, it implies higher
   // capacitance that is added due to a finger on the switch.

   if (SW3_T0_Count.U16 > SW3_Cal_Value)
   {
      LED = 0;                         // Switch LED OFF
      SW3_Status = 1;                  // Update status of global variable
   }
   else
   {
      LED = 1;                         // Switch LED ON
      SW3_Status = 0;                  // Update status of global variable
   }
}

//-----------------------------------------------------------------------------
// Support Subroutines
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// Calibrate_SW3
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// This routine will store the touch value in non-volatile Flash memory as
// the calibration threshold for the switch SW3.
//
// 1. Place a finger on the touch switch SW3, and hold it there.
// 2. Press switch SW2 (P0.7) for >1 sec and release using another finger.
// 3. Remove finger from SW3.
//-----------------------------------------------------------------------------
void Calibrate_SW3 (void)
{
   U8 EA_Save;                    
        
   while (SW2);                        // Wait till SW2 is pressed
   
   EA_Save = IE;                       // Preserve EA
   EA = 0;                             // Disable interrupts

   // Add additional sensitivity value to calibration value
   SW3_T0_Count.U16 += SW3_SENSITIVITY;
   
   FLASH_ByteWrite ((U16)(&SW3_Cal_Value)+MSB, SW3_T0_Count.U8[MSB]);
   FLASH_ByteWrite ((U16)(&SW3_Cal_Value)+LSB, SW3_T0_Count.U8[LSB]);

   if ((EA_Save & 0x80) != 0)          // Restore EA
      EA = 1;
}

//-----------------------------------------------------------------------------
// FLASH_ByteWrite
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters:
//   1) U16 addr - Flash address to write
//         Range - 0x0000 to 0x3DFF (15.5k user accessible flash)
//   2) U8 byte  - Byte to write
//         Range - 0x00 to 0xFF
//
// This routine writes "byte" at flash address "addr"; Adapted from AN201.
//
//-----------------------------------------------------------------------------
void FLASH_ByteWrite (U16 addr, U8 byte)
{
   U8 EA_Save = IE;                    // Preserve EA
   SEGMENT_VARIABLE_SEGMENT_POINTER (pwrite, U8, xdata, data); 
   //unsigned char xdata * data pwrite;// FLASH write pointer

   EA = 0;                             // Disable interrupts

   VDM0CN = 0x80;                      // Enable VDD monitor


   RSTSRC = 0x02;                      // Enable VDD monitor as a reset source

   pwrite = (char xdata *) addr;

   FLKEY  = 0xA5;                      // Key Sequence 1
   FLKEY  = 0xF1;                      // Key Sequence 2
   PSCTL |= 0x01;                      // PSWE = 1


   VDM0CN = 0x80;                      // Enable VDD monitor


   RSTSRC = 0x02;                      // Enable VDD monitor as a reset source

   *pwrite = byte;                     // Write the byte

   PSCTL &= ~0x01;                     // PSWE = 0

   if ((EA_Save & 0x80) != 0)          // Restore EA
      EA = 1;
}

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

⌨️ 快捷键说明

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