清单1 罗列了系统固件。与设立电容式触摸感应系统相关的大部分工作都已被编码为一组由C 程序来调用的标准CSR 例行程序。例如:CSR_1_Start()负责配置PSoC 的内部布线,以使电流源DAC 与模拟多路复用器相连,而比较器与经过正确初始化的PWM和16 位计数器相连。
清单1:用于电容式触摸感应系统的固件
//-----------------------------start of listing--------------------------------------------------------
//----------------------------------------------------------------------------
// main.c, a CapSense program in C
// A demonstration of Capacitive Sensing with PSoC
// with a 10mm glass overlay
//----------------------------------------------------------------------------
#include // part specific constants and macros
#include "PSoCAPI.h" // PSoC API definitions for all User Modules
void main()
{
//a flag that is set when a finger is on any buttons
int bBaselineButtonFlag;
CSR_1_Start(); //initialize CapSense user module
TX8_1_Start(TX8_1_PARITY_NONE); //initialize TX8 module
M8C_EnableGInt; //enable global interrupts
CSR_1_SetDacCurrent(200,0); //set current source to 200 out of 255
//use low range of current source
CSR_1_SetScanSpeed(255); //set number of osc cycles to 255-2=253
while(1)
{
CSR_1_StartScan(1,1,0); //scan one button only, button 1 on P2[3]
//wait for scanning of button to complete
while (!(CSR_1_GetScanStatus() & CSR_1_SCAN_SET_COMPLETE));
//update baseline if required, set flag if any button pressed
bBaselineButtonFlag = CSR_1_bUpdateBaseline(0);
//data log the raw counts on button 1
TX8_1_PutSHexInt(CSR_1_iaSwResult[1]);
TX8_1_PutChar(',');
//data log switch mask... which switch is on?
TX8_1_PutSHexInt(CSR_1_baSwOnMask[0]);
TX8_1_CPutString(",");
//data log switch difference = raw counts - baseline
TX8_1_PutSHexInt(CSR_1_iaSwDiff[1]);
TX8_1_PutChar(',');
//data log update timer as a teaching aid
TX8_1_PutSHexInt(CSR_1_bBaselineUpdateTimer);
TX8_1_PutChar(',');
//data log the baseline counts for button 1
TX8_1_PutSHexInt(CSR_1_iaSwBaseline[1]/4);
TX8_1_PutCRLF();
}
}
//-------------------------------end of listing--------------------------------------------------------