#include "touchpic.h" //-------------------------------------- //core speed 16Mhz interni... //tmr1 drived by fosc //tmr0 drived by sense module cps //intterrupr at owerflow of tmro e in case of error from tmr1 //ra5 led port //------------------------------------------------------- void InitializeSystem() { //Set up registers //bit_clear(ANSELE, LED_E0|LED_E1); // LED ports set to digital ***** capacitive_sensor_settings(); //ledblink(2); } void main() { char i; // CSO active channel osccon=0b01111010;// 16mhz internal clock fuses trisa=0; trisc=0; //lata=0; ansela=0; ansela.B4=1; Sound_Init(&PORTa, 2); InitializeSystem(); disable_interrupts(); // The delay_ms will cause timer IF delay_ms(300); //ledblink(2); reset_system(); timer_presets(); initial_scan_channels(); // set 'off' CSO count for each channel while(1) { //ledblink(2); i =0; // repeatedly configure port until timer overflows, then handle it // then increase the channelNumber. reset_system(); // scan through each touch channel. while (i <= 0){ configurePort();// repeated until timer overflows if (interrupt_alarm) { // s/w interrupt alarm bit set by ISR alarmInterrupt(); // handle interrupt flag //uart1_write(channelNumber); reset_system(); // Start again with a clean slate i++ ; } // end if } // end while disable_interrupts(); // in mTouch library, updates channel status register and writes changed // channel status to UART. update_channel_status(); //noChange is a flag for when channel status is changed. /*if (noChange == false) { // changed i/p, so change LEDs mcp_activechannels(); // work out which LEDs to light on board mcp_update(LEDboard, MCPportA, MCPportB);// activate LEDs on board with status noChange = true; // reset flag } if (noChange == true){ // do nothing if no change to status //uart1_write(0x99); //TEST alive marker for debugging } // end if ********/ } // end while(1) }
/* library for mTouch touch For the PIC16F****,max of 16 channel capacitive touch "just sends button status and not the CSO count for each channel" */ #ifndef _MTOUCH_5_H__ #define _MTOUCH_5_H__ #include "notes.h" #define bit(num) (1 << num) // creates a bit mask #define bit_set(v, m) ((v) |=(m)) // Sets the bit // e.g. bit_set (PORTD, bit(0) | bit(1)); #define bit_clear(v, m) ((v) &= ~(m)) // Clears the bit #define bit_toggle(v, m) ((v) ^= (m)) // toggle the bit #define bit_read(v, m) ((v) & (m)) // read a bit and see if it is set #define bit_test(v,m) ((v) && (m)) #define true 1 #define false 0 #define LED_E0 bit(0) #define LED_E1 bit(1) #define LED_E0_on() bit_clear(PORTE,LED_E0) #define LED_E0_off() bit_set(PORTE,LED_E0) #define LED_E1_on() bit_clear(PORTE,LED_E1) #define LED_E1_off() bit_set(PORTE,LED_E1) //#define t0Interrupt bit(0) //#define t1Interrupt bit(1) #define rcieInterrupt bit(2) #define startChannel 3// h/w error on ch0 on '726 board #define lastChannel 3 //***** ho modificato il numero di canali #define numberChannels lastChannel-startChannel+1 #define PORTACapchannels 0b00110000 #define PORTBCapchannels 0b00111111 #define PORTDCapchannles #define threshold 0x04 // threshold to trigger change on a CSO channel #define timer1Off() (T1CON.TMR1ON = 0) //------------------------------------------------------------------------------------------------------------ // TRIS values for all of the CPS pins to turn only the current ChannelNumber CPS and leave all others as ouputs //------------------------------------------------------------------------------------------------------------ // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F const char TRISACapOscOn[] = {0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00010000,0b00100000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000}; const char TRISBCapOscOn[] = {0b00000001,0b00000010,0b00000100,0b00001000,0b00010000,0b00100000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000}; const char TRISDCapOscOn[] = {0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000001,0b00000010,0b00000100,0b00001000,0b00010000,0b00100000,0b01000000,0b10000000}; int activeChannels = 0x0000; // initialise to all off, 16 bits for 16 channels! int initialTriggerCount; // Counter keeps track of how many samples we have taken since the last integration of the released average int initialCapCount; // Counter keeps track of how many samples we have taken since the last integration of the released average int capCount; // Store MSB of timer1 for each channel int oldcapCount[numberChannels]; // Store old capCount char noChange = true; // flag for changed sensor status char interrupt_alarm = 0x00; // s/w alarm register, bit(0)=tmr0 IF, bit(1)=tmr1 IF, bit(2) = rcie IF char t0_preload = 0x06; // preset = 0 char t0Interrupt =0; char t1Interrupt =0; char ison=0; //char touch=0; //------------------------------------------------------------------------------------- //*******************************aggiunto da NOTES.H********************************** //------------------------------------------------------------------------------------- int pauseBetweenNotes; int noteduration; int typesound=0; //int threshold = 150; //unsigned int pressCount = 0; int thisnote; int coin_sound_num_notes = 2; int coin_sound_notes[] = {NOTE_D6 , NOTE_G6}; int coin_sound_note_durations[] = {16, 2}; int oneup_sound_num_notes = 6; int oneup_sound_notes[] = {NOTE_E6, NOTE_G6, NOTE_E7, NOTE_C7, NOTE_D7, NOTE_G7}; int oneup_sound_note_durations[] = {8, 8, 8, 8, 8, 8}; /* CapSense cs_4_2 = CapSense(CAP_SENSOR_SEND_PIN, CAP_SENSOR_RECEIVE_PIN); */ /* void setup() { pinMode(10, INPUT); pinMode(LIGHT_PIN, OUTPUT); digitalWrite(LIGHT_PIN, HIGH); delay(1000); digitalWrite(LIGHT_PIN, LOW); // tone(SPEAKER_PIN, NOTE_G7); } */ void playSound(int num_notes, int notes[], int durations[]) { // iterate over the notes of the melody: for ( thisNote = 0; thisNote < num_notes; thisNote++) { // to calculate the note duration, take one second // divided by the note type. //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc. noteDuration = 1000/durations[thisNote]; Sound_Play(notes[thisNote], noteDuration); // to distinguish the notes, set a minimum time between them. // the note's duration + 30% seems to work well: pauseBetweenNotes = noteDuration * 0.30; Vdelay_ms(pauseBetweenNotes); // stop the tone playing: //noTone(speaker_pin); } } //------------------------------------------------------------------------------------------- //------------------------------------------------------------------------------------------- //------------------------------------------------------------------------------------------- /* // Port expander module connections sbit SPExpanderRST at RC0_bit; sbit SPExpanderCS at RC1_bit; sbit SPExpanderRST_Direction at TRISC0_bit; sbit SPExpanderCS_Direction at TRISC1_bit; // End port expander module connections */ /* void fast_flash_E0(void) { // flash LED E0 bit_clear(TRISE, LED_E0); LED_E0_off(); Delay_ms(200); LED_E0_on(); Delay_ms(300); LED_E0_off(); } void fast_flash_E1(void) { // flash LED E1 bit_clear(TRISE, LED_E1); LED_E1_off(); Delay_ms(200); LED_E1_on(); Delay_ms(300); LED_E1_off(); } */ void ledblink(int num){ int a; for (a=0;adefines active pin //UART_out("tmr1 IF\n"); //delay_ms(300); //UART_out(t1InterruptTxt); //uart_out("\n"); // new line //delay_ms(500); flag_clear; //bit_clear(interrupt_alarm,t1Interrupt); // clear alarm flag for tmr1 interrupt }// end if /* if bit_read(interrupt_alarm, rcieInterrupt) { // uart rx flag] char rcieInterruptTxt[7]; // disable_interrupts(); IntToStr(RCREG, rcieInterruptTxt); //uart rx text //UART_out("rcie IF\n"); //uart_out(rcieInterruptTxt); // write back received text //uart_out("\n"); // new line if (RCREG == 4) { uartTrigger(); } // end if bit_clear(interrupt_alarm,rcieInterrupt); // clear alarm flag } // end if */ } // end alarmInterrupt /* void uart_write_short(short var) { uart1_write(var); } */ // Configure tris & CPSCON1 settings for channel void configurePort() { CPSCON1.CPSCH0= 1; // Set CSO to active channel CPSCON1.CPSCH1= 1; // Set CSO to active channel //ansela.B4=1; trisa.B4=1; //uart_write_short(CPSCON1); // debug info /* if (channel < 6) { TRISB = TRISBCapOscOn[channel]; // Channels 0-5 on portB } else if (channel < 8){ TRISA = TRISACapOscOn[channel]; // Channels 6-7 on portA } else { TRISD = TRISDCapOscOn[channel]; // Channels 8-15 on portD } */ } /* void scan_channels(){ // get CSO count for each channel char channel=0; while (channel <= 0){ configurePort();// repeated until timer overflows if (interrupt_alarm) { // s/w interrupt alarm bit set by ISR alarmInterrupt(); // handle interrupt flag reset_system(); // Start again with a clean slate channel++ ; } // end if } // end while }// end scan_channels */ void initial_scan_channels(){ // set initial CSO count threshold for each channel //char channel = 0; char find=0; //while (channel <= 0){ while (find<=0) { if (interrupt_alarm) { // s/w interrupt alarm bit set by ISR alarmInterrupt(); // handle interrupt flag initialTriggerCount = capCount+threshold; initialCapCount = capCount+(threshold-1); reset_system(); // Start again with a clean slate //channel++ ; find++; } // end if } // end while }// end scan_channels /******************** // error - tmr1 overflowed before tmr0 void tmr1_overflow_debug(short var){ char errorTxt[7]; disable_interrupts(); uart_out("Timer1 IF"); delay_ms(500); IntToStr(var, errorTxt); uart_out(errorTxt); delay_ms(500); } // end tmr1_overflow_debug */ /*void debug(int var) { // writes debug info to LCD char debugText[7]; disable_interrupts(); IntToStr(var, debugText); // SPI_LCD_write(1,1,"debug text"); uart_out("debug text"); delay_ms(500); // SPI_LCD_write(2,1,debugText); uart_out(debugText); delay_ms(500); } // end debug */ /* // send info of a channel turning ON to uart void uart_channel_on(char channel) { // set bit 6 high to avoid any 0x00 being sent as data bit_set(channel, bit(6)); // set bit 5 high to indicate channel turned ON bit_set(channel, bit(5)); uart1_write(channel); //uart1_write(0xAA); // for testing } // end uart_channel_on void uart_channel_off(char channel) { // set bit 6 high to avoid any 0x00 being sent as data bit_set(channel, bit(6)); // set bit 5 low to indicate channel turned OFF, should be anyway bit_clear(channel, bit(5)); uart1_write(channel); //uart1_write(0xEE); // testing } // end uart_channel_off */ // update activeChannels register with on/off info // send changed channels to uart int update_channel_status(int touch) { // update activeChannels with on/off info //unsigned int tempactiveChannels; // used for debugging char channel = 0; // channel counter //portc=capcount; while(channel <= 0) // scroll through each channel { // channel CSO count increased - channel turned ON if (capCount > initialTriggerCount){/* && !bit_read(activeChannels, bit(channel))******/ //portc=capcount; //noChange = false; // there's been a change //bit_set(activeChannels, bit(channel)); // flag this channel as on //disable_interrupts(); //ledblink(2); //porta.b5=1; if (!(touch==1)){ touch=1; if(ison==0){ ison=1; porta.b5=1; }//endif }//endif else { ison=0; porta.b5=0; }//endelse } // end if } // channel CSO count decreased - channel turned OFF if ((capCount < initialcapCount)&(touch==1)&(!(capcount>initialTriggerCount))){/* && bit_read(activeChannels, bit(channel))*****/ //noChange = false; // there's been a change //bit_clear(activeChannels, bit(channel)); // flag this channel as off //portc=capcount; //disable_interrupts(); touch=0; //porta.b5=0; //ledblink(1); // uart changed channel //uart_channel_off(channel); // send channel off status to uart *** } // end if //UART1_Write(capCount); capCount=0; channel++; //delay_ms(250); } // end while /* for debugging, writes out the activated channels if (noChange == false) { // for debugging, write both bytes of activeChannels to UART tempactiveChannels = activeChannels; uart1_write(tempactiveChannels);// write 8 LSB tempactiveChannels = tempactiveChannels >> 8; // shift MSB to LSB location uart1_write(tempactiveChannels); // write 8 MSB } // end if */ return(touch); } // end update_channel_status #endif