//! Initialize the STM32F4xx ports and USB. void stm32f4xx_init() { int i=20; SystemInit(); ioinit(); usartinit(); while(i--) stmdelay(); return; }
void main(void) { UINT8 i=0,j=0,k,t;//togflg = 1; OSCCON = 0b01111000; OSCTUNE = 0b00000000; SWDTEN = 0b01; UINT8 tholdcount; ANSELA = 0; ANSELB = 0; TRISA = 0; TRISB = 0; TRISC = 0; WDTCONbits.WDTPS = 0b01010; for ( j = 0 ; j < 8 ; j++ ) { debounc[j] = 0; threshold[j] = 0; cur_read[j] = 0; average[j] = 0; keystat[j]= 0;keycnt[j]= 0; } TRISCbits.TRISC5 = 0; // RC6 = TX out KEYSTATPIN = 1; capsensinit(); //Delay(5); setscanchannel(); channelno = 0;avgflag = 0; count = 0; usartinit(); I2Cinit(); if(STATUSbits.nTO == 0){ USARTWriteString(" reset ");} //else {tholdflg = 0;} //tholdflg = 0; tholdcount=0; max = 500; pos = 9; // TRISAbits.TRISA2 = 0; // LATAbits.LATA2 = 1; /* while(1) { USARTWriteChar('U'); } */ while(1) { CLRWDT(); /* if(togflg) { LATAbits.LATA2 = 0;//togflg; togflg = 0; } else { LATAbits.LATA2 = 1;//togflg; togflg = 1; } */ if(avgflag) { for( i = 0 ; i < 8 ; i++ ) { average[i] /= capsense_avg_cnt; /*USARTWriteInt(average[i],5); USARTWriteChar('\t'); if(i == 7) { USARTWriteChar('\n');USARTWriteChar('\r');}*/ if(tholdflg == 0) { if(tholdcount == 8) threshold[i] = 0; threshold[i] += average[i]; if ( i == 7 ) tholdcount++ ; if( tholdcount == 16 ) { for ( j = 0 ; j < 8 ; j++ ) threshold[ j ] >>= 3 ; tholdflg = 1; } } else { olddiff[i] = curdiff[i]; curdiff[i] = threshold[i] - average[i]; if ( curdiff[i] > olddiff[i] ) diff[i]= curdiff[i] - olddiff[i]; else diff[i]= olddiff[i]- curdiff[i]; if( diff[i] > max ) { max = diff[i]; pos = i; } //if( curdiff[i] > max ){ max = curdiff[i]; pos = i;} //USARTWriteInt(diff[i],5); //USARTWriteChar('\t'); if(i == 7) { //USARTWriteInt(pos,2); //USARTWriteChar(' '); //USARTWriteInt(diff[pos],5); //USARTWriteChar('\n');USARTWriteChar('\r'); for(t = 0; t < 8 ; t++) { if(t != pos) {if(keycnt[t])keycnt[t]--;keystat[t] =0;} else if( max > 600 ) { keycnt[t]++; if( keycnt[t] > 7 ) { for(k = 0 ; k < 8 ; k++) keycnt[k] = 0; keystat[t] = 1; } } else{ if(keycnt[t])keycnt[t]-- ; keystat[t] = 0;} USARTWriteInt(keycnt[t],1); } if( pos != 9 && debounc[pos] == 0 ) { debounc[pos] = 400; USARTWriteChar('\n'); USARTWriteString("Key Detected = "); i2c_tx_buff[ 0 ] = pos; KEYSTATPIN = 0; } USARTWriteInt(pos,2); //i2c_tx_buff[ 0 ] = pos; USARTWriteChar('\n');USARTWriteChar('\r'); max = 500; pos = 9; } //USARTWriteInt(keycnt[i],1); //USARTWriteChar('\t'); //if(i == 7){USARTWriteChar('\n');USARTWriteChar('\r');} //pos = i; /*USARTWriteInt(curdiff[i],5); USARTWriteChar('\t'); if(i == 7) { USARTWriteChar('\n');USARTWriteChar('\r');} if(curdiff[i] > 800){keycnt[i]++;} else if(curdiff[i] < 800){ keycnt[i]=0; keystat[i] = 0;} if(keycnt[i] == 8) keystat[i] = 1;*/ /*USARTWriteInt(keystat[i],1); USARTWriteChar('\t'); if(i == 7) { USARTWriteChar('\n');USARTWriteChar('\r');}*/ } } avgflag = 0 ; } }