📄 main.c
字号:
//---------------------------------------------------------------------
//
// Name: CSR_7Button_Demo_v2.1
// - 4/1/06 - version 2.1 (XCH)
// - removed function call to output
// count values
// - added output of CSR_baSwOnMask[0]
// to LCD
// - 3/20/06 - version 2.0 (XCH)
// - added Calibration routine
// - adjusted settings
//
// For Board: CY3212-CapSense RevA
// Chip: CY8C21434-24LFXI
//
// Description:
// This code determines which switch is depressed among 7 switches
// and displays that switch number on the LCD display. This project uses
// Period method of measurement. The display on LCD (port 2 in the LCD UM
// parameters) will display the value of CSR_baSwOnMask[0]. This variable
// holds the on/off status of the 7 switches as a bitmask. Then once switch
// is pressed, the switch number is displayed on the LCD display. The
// switch numbers and port pins are assigned in UM wizard.
//
// Project Settings (in the Device Editor):
//
// Global Resources:
// Power Setting [Vcc/SysClk freq]: 5.0V / 24MHz
// CPU_Clock: SysClk/1
//
// User Module Parameters:
// Method: Period
// FingerThreshold: 100
// NoiseThreshold: 40
// BaselineUpdateRate: 40
// ESDDebounce: Disabled
//
// Switch to port settings in CSR wizard (for CY3212 demo board)
//
// SW0 -> P1[4]
// SW1 -> P1[6]
// SW2 -> P3[0]
// SW3 -> P3[2]
// SW4 -> P3[3]
// SW5 -> P3[1]
// SW6 -> P1[3]
//
// Notes:
// CSR_1_baSwOnMask -> is an array of BYTEs that contains the data
// that determines which button has been pressed. The first
// byte CSR_1_baSwOnMask[0] holds the on/off state of the first 8 switches
// in the system, CSR_1_baSwOnMask[1] would hold the on/off state of the
// next 8 switches in the system. Since there are only 7 switches in
// this particular system we only use CSR_1_baSwOnMask[0].
// Th LSb (bit 0) represents the state of switch0:
// 0 = off
// 1 = on
// Bit 1 represents switch1 and so on.
//------------------------------------------------------------------------
#include <m8c.h> // part specific constants and macros
#include "PSoCAPI.h" // PSoC API definitions for all User Modules
// bit masks defined for each button
#define BUTTON_0 0x01
#define BUTTON_1 0x02
#define BUTTON_2 0x04
#define BUTTON_3 0x08
#define BUTTON_4 0x10
#define BUTTON_5 0x20
#define BUTTON_6 0x40
// define the number of switches in this system
#define NUM_SWITCHES 7
// the followin defines the ranges for the raw counts
// during the calibration routines
#define DAC_MAX_RAW_COUNT 310
#define DAC_MIN_RAW_COUNT 290
// autocalibration routine
//-----------------------------
// will automatically determine the DAC settings for each switch in the system
void CalibrateSwitches(void);
// Array that holds the individual DAC settings for each switch
BYTE bDACcurrent[NUM_SWITCHES];
WORD i,x;
// start main
//------------------------------------------------------------------------------------------
void main()
{
CSR_1_Start(); // call CSR Start, does all the internal CSR connections
M8C_EnableGInt; // Enable global interrupts
LCD_1_Start(); // Initialize LCD
// call the calibrate switches function to initialize the parameter settings
CalibrateSwitches();
LCD_1_Start(); // Initialize LCD
LCD_1_Position(0,0);
LCD_1_PrCString("7-Button ");
LCD_1_Position(1,0);
LCD_1_PrCString("Demo v2.1 ");
// delay long enough for title and version to be displayed
for (i=0; i< 60000; i++)
{
for (x=0; x< 20; x++)
{}
}
LCD_1_Position(0,0);
LCD_1_PrCString(" ");
LCD_1_Position(1,0);
LCD_1_PrCString(" ");
while(1) // start the main loop
{
// scan each switch individually
for (i=0; i<NUM_SWITCHES; i++)
{
CSR_1_SetDacCurrent(bDACcurrent[i],0); // Sets DAC current
CSR_1_SetScanSpeed(3); // use a scanspeed of three (no overlay so buttons are very sensitive)
CSR_1_StartScan(i,1,0); // Scan one Switch (i)
while(!(CSR_1_GetScanStatus() & CSR_1_SCAN_SET_COMPLETE)); // wait until the scan is complete
}
// call the update baseline function which does several functions, the two main
// functions it provides are:
// 1) updates each switches baseline (if necessary and according to the rate
// set by the updateBaseline value
// 2) determine the button status (on/off) and places this value
// in CSR_1_baSwOnMask[]
// if any of the buttons are determined to be "ON" then the function will
// return a non-zero value
if(CSR_1_bUpdateBaseline(0))
{
// check to see if BUTTON 0 is on or off
if ((CSR_1_baSwOnMask[0]) & BUTTON_0)
{
// switch is ON
LCD_1_Position(0,0);
LCD_1_PrCString("0 ");
}else
{
// switch is OFF
LCD_1_Position(0,0);
LCD_1_PrCString(" ");
}
// check to see if BUTTON 1 is on or off
if ((CSR_1_baSwOnMask[0]) & BUTTON_1)
{
// switch is ON
LCD_1_Position(0,2);
LCD_1_PrCString("1 ");
}else
{
// switch is OFF
LCD_1_Position(0,2);
LCD_1_PrCString(" ");
}
// check to see if BUTTON 2 is on or off
if ((CSR_1_baSwOnMask[0]) & BUTTON_2)
{
// switch is ON
LCD_1_Position(0,4);
LCD_1_PrCString("2 ");
}else
{
// switch is OFF
LCD_1_Position(0,4);
LCD_1_PrCString(" ");
}
// check to see if BUTTON 3 is on or off
if ((CSR_1_baSwOnMask[0]) & BUTTON_3)
{
// switch is ON
LCD_1_Position(0,6);
LCD_1_PrCString("3 ");
}else
{
// switch is OFF
LCD_1_Position(0,6);
LCD_1_PrCString(" ");
}
// check to see if BUTTON 4 is on or off
if ((CSR_1_baSwOnMask[0]) & BUTTON_4)
{
// switch is ON
LCD_1_Position(0,8);
LCD_1_PrCString("4 ");
}else
{
// switch is OFF
LCD_1_Position(0,8);
LCD_1_PrCString(" ");
}
// check to see if BUTTON 5 is on or off
if ((CSR_1_baSwOnMask[0]) & BUTTON_5)
{
// switch is ON
LCD_1_Position(0,10);
LCD_1_PrCString("5 ");
}else
{
// switch is OFF
LCD_1_Position(0,10);
LCD_1_PrCString(" ");
}
// check to see if BUTTON 6 is on or off
if ((CSR_1_baSwOnMask[0]) & BUTTON_6)
{
// switch is ON
LCD_1_Position(0,12);
LCD_1_PrCString("6 ");
}else
{
// switch is OFF
LCD_1_Position(0,12);
LCD_1_PrCString(" ");
}
}
else // blank the top line of the LCD screen
{
LCD_1_Position(0,0);
LCD_1_PrCString(" ");
}
// output the value of CSR_baSwOnMask[0] to the LCD screen
LCD_1_Position(1,0);
LCD_1_PrCString("baSwOnMask[0]: ");
} // End of normal operation loop
} // End of main
// -------------------------------------------------------------
//
// Function: CalibrateSwitches()
//
// Description:
// This function will automatically determine the appropriate
// DAC values for each switch in the system. The values will be
// stored in the variable - bDACcurrent[]. The function will set
// the DAC value depending on the values set by the following lines:
//
// #define DAC_MAX_RAW_COUNT 310
// #define DAC_MIN_RAW_COUNT 290
//
// These lines are located at the top of the program and give a range
// for the raw CSR count values to be in to set the DAC value. This
// function will iterate through each switch. It will do a single
// scan and compare the raw count values with the range defined above.
// If the count values for that particular switch are outside of the
// range, the function will change the DAC value appropriately and do
// a rescan. This process is repeated until the raw counts for each
// particular switch are within the specified range.
//
void CalibrateSwitches(void)
{
BYTE bFlag = 1;
for(i=0; i<NUM_SWITCHES; i++) // iterate through all the switches in the system
{
// calibrate DAC setting for a switch
//------------------------------------------------------
bFlag = 1; // reset the flag to a true state
CSR_1_SetScanSpeed(3); // start with a scanspeed of three to get ONE oscillator cycle or period
bDACcurrent[i] = 20; // a DAC current of 20 will start us in range
// do one scan and compare the raw count value against
// the defined range.
// - If the counts are BELOW the set range
// DECREASE the DAC setting to INCREASE the counts
// - If the counts are ABOVE the set range
// INCREASE the DAC setting to DECREASE the counts
do
{
// set the DAC current
CSR_1_SetDacCurrent(bDACcurrent[i], 0);
// scan just the one button
CSR_1_StartScan(i,1,0);
while(!(CSR_1_GetScanStatus() & CSR_1_SCAN_SET_COMPLETE));
// check to see if the counts are in range
if (CSR_1_iaSwResult[i] < DAC_MIN_RAW_COUNT)
{
bDACcurrent[i]--; // counts are BELOW the range so DECREASE the DAC setting
}
else if (CSR_1_iaSwResult[i] > DAC_MAX_RAW_COUNT)
{
bDACcurrent[i]++; // counts are ABOVE the range so INCREASE the DAC setting
}
else
{
bFlag = 0; // the counts are in range so exit the do loop
}
}while (bFlag); //if the flag is still 1 then go back and rescan the switch
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -