2.png (135.38 KB, 下載次數: 69)
下載附件
2016-9-30 10:54 上傳
七步走,
第一步:給CS放電,S3開,
第二步:所有開關關閉,
第三步:S2開啟,給CX充電。
第四步:所有開關關閉
第五步:開啟S1給CS充電
第六步:所有開關關閉
第七步:S1和S3開啟,給CX和CS放電
重復上面的7個步驟,直到CS電壓到高電平(有觸摸時重復次數將減少)
在第六步后采集到I/0端口電平是否超過閥值。
2.png (126.19 KB, 下載次數: 77)
下載附件
2016-9-30 10:55 上傳
void Configure_TSC(void)
{
RCC->AHBENR |= RCC_AHBENR_TSEN;
TSC->CR = TSC_CR_PGPSC_2 | TSC_CR_PGPSC_0 // fPGCLK = fHCLK/32
| TSC_CR_CTPH_0 | TSC_CR_CTPL_0 // pulse high = 2xtPGCLK,Master
| TSC_CR_MCV_2 | TSC_CR_MCV_1 // Max count value = 16383 pulses
| TSC_CR_TSCE; // Enable TSC
TSC->IOHCR &= (uint32_t)(~(TSC_IOHCR_G2_IO4 | TSC_IOHCR_G2_IO3)); // 默認使能,去除不要的端口
TSC->IER = TSC_IER_EOAIE; // 轉換系列結束中斷
TSC->IOSCR = TSC_IOSCR_G2_IO4; // 采樣端口電容接在 G2IO4上
TSC->IOCCR = TSC_IOCCR_G2_IO3; // 觸摸按鍵設在G2IO3上
TSC->IOGCSR |= TSC_IOGCSR_G2E; // 使能組2
NVIC_SetPriority(TSC_IRQn, 0);
NVIC_EnableIRQ(TSC_IRQn);
}
void TSC_IRQHandler(void)
{
if((TSC->ISR & TSC_ISR_EOAF) == TSC_ISR_EOAF) //系列結束否
{
TSC->ICR = TSC_ICR_EOAIC; /* Clear flag */
AcquisitionValue = TSC->IOGXCR[1]; /* Get G2 counter value */
}
}
void Process(void)
{
static uint32 NumberOfCalibration=0;
uchar RefIndex=0;
static uchar RefIndexStatic=0;
static uint32 ReferenceTab[REFERENCE_TAB_SIZE];
if(AcquisitionValue) //有數據到來
{
if(CalibrationDone) //是否矯正完成
{
if((AcquisitionValue + THRESHOLD) < Reference) /* Touch detected */
{
Activities = 1;
}
else if(AcquisitionValue > (Reference + THRESHOLD))
{
Activities = 1;
CalibrationDone = 0; /* restart calibration */
Reference = 0; /* Reset reference */
}
else /* no touch detected */
{
if(ReferenceAdaptation)
{
ReferenceAdaptation=0;
RefIndexStatic%=REFERENCE_TAB_SIZE;
ReferenceTab[RefIndexStatic++] = AcquisitionValue;
for(RefIndex=0;RefIndex<REFERENCE_TAB_SIZE;RefIndex++)
{
Reference += ReferenceTab[RefIndex];
}
Reference /= (REFERENCE_TAB_SIZE + 1);
}
}
}
else /* Calibration */
{
if(NumberOfCalibration < NUMBER_OF_CALIBRATION)
{
Reference += AcquisitionValue;
NumberOfCalibration++;
}
else if(NumberOfCalibration == NUMBER_OF_CALIBRATION)
{
Reference += AcquisitionValue;
Reference /= (NUMBER_OF_CALIBRATION + 1); /* Compute reference */
NumberOfCalibration = 0; /* Reset number of calibration for nex time */
CalibrationDone = 1; /* Calibration Completed */
for(RefIndex=0;RefIndex<REFERENCE_TAB_SIZE;RefIndex++)
{
ReferenceTab[RefIndex] = Reference;
}
}
}
AcquisitionValue = 0; //復位count值
TSC->CR |= (1<<1) ; // 開始新的系列
}
}
void main()
{
Configure_TSC();
TSC->CR |= (1<<1); // 開始轉換
while(1)
{
Process();
}
}
|