Autor Tema: ¿ Alguien ha usado CTMU Capsense en CCS con un PIC24 ?  (Leído 1543 veces)

0 Usuarios y 1 Visitante están viendo este tema.

Desconectado sanpic

  • Colaborador
  • PIC18
  • *****
  • Mensajes: 430
¿ Alguien ha usado CTMU Capsense en CCS con un PIC24 ?
« en: 19 de Julio de 2012, 02:45:17 »
Amigos , estoy intentando manejar un teclado capacitivo mediante CTMU. El PIC en cuestión es el 24FV32KA302.
He estado recopilando info al respecto de un lado y de otro , y he podido acercarme a lo que necesito. El tema es que por más que pido que me lea el botón conectado en AN10 , lo único que hace es mostrarme , y de manera correcta el estado de pulsación de AN0.
Gracias por su colaboración.

Un abrazo a todos.


Código: C
  1. [code]  
  2.  
  3.    #include <main3.h>
  4.    #include <flex_lcd.c>
  5.    
  6.    
  7.    
  8.    
  9.    
  10.    #define GREEN_LED1   PIN_B8
  11.    #define COUNT 500 //@ 8MHz = 125uS.
  12.    #define DELAY for(i=0;i<COUNT;i++)    
  13.    #define OPENSW 1024 //Unpressed switch value    
  14.    #define TRIP 150 //Difference between pressed     //and unpressed switch    
  15.    #define HYST 65 //amount to change     //from pressed to unpressed    
  16.    #define PRESSED 1    
  17.    #define UNPRESSED 0
  18.    
  19.    #use rs232(baud=9600,BRGH1OK,UART1,parity=N,bits=8)
  20.    
  21.    
  22.    #word CTMUCON1  = 0x35A
  23.    #bit  CTMUEN_bit   = CTMUCON1.15
  24.    #bit  IDISSEN_bit  = CTMUCON1.9
  25.    #bit  TGEN_bit     = CTMUCON1.12
  26.    #bit  EDGEN_bit    = CTMUCON1.11
  27.    #bit  EDGSEQEN_bit   = CTMUCON1.10
  28.    #bit  CTTRIG_bit   = CTMUCON1.8
  29.    #bit  CTMUSIDL_bit = CTMUCON1.13
  30.    
  31.    
  32.    
  33.    #word CTMUCON2  = 0x35c
  34.    //#bit  EDG2SEL_bit = CTMUCON2.2
  35.    //#bit  EDG1SEL_bit = CTMUCON2.10
  36.    
  37.    #bit  EDG1SEL_3_bit = CTMUCON2.13
  38.    #bit  EDG1SEL_2_bit = CTMUCON2.12
  39.    #bit  EDG1SEL_1_bit = CTMUCON2.11
  40.    #bit  EDG1SEL_0_bit = CTMUCON2.10
  41.    
  42.    #bit  EDG2POL_bit = CTMUCON2.6
  43.    
  44.    #bit  EDG2SEL_3_bit = CTMUCON2.5
  45.    #bit  EDG2SEL_2_bit = CTMUCON2.4
  46.    #bit  EDG2SEL_1_bit = CTMUCON2.3
  47.    #bit  EDG2SEL_0_bit = CTMUCON2.2
  48.    
  49.    #bit  EDG1POL_bit = CTMUCON2.14
  50.    #bit  EDG1STAT_bit = CTMUCON2.8
  51.    #bit  EDG2STAT_bit = CTMUCON2.9
  52.    
  53.    
  54.    #word AD1CON1  = 0x340
  55.    #bit  SAMP_bit     = AD1CON1.1
  56.    #bit  DONE_bit     = AD1CON1.0
  57.    #bit  FORM_1_bit    = AD1CON1.9
  58.    #bit  FORM_0_bit    = AD1CON1.8
  59.    #bit  ADON_bit     = AD1CON1.15
  60.    
  61.    
  62.    #word AD1CON2  = 0x342
  63.    
  64.    
  65.    #word AD1CON3  = 0x344
  66.    
  67.    
  68.    #word AD1CSSL  = 0x350
  69.    
  70.    
  71.    #word ADC1BUF0 = 0x300
  72.    
  73.    
  74.    //#word AD1PCFG = 0x32C
  75.    
  76.    
  77.    #word AD1CHS = 0x328
  78.    
  79.    
  80.    #word IFSO     = 0x084
  81.    #bit  AD1IF_bit    = IFSO.13
  82.    
  83.    
  84.    
  85.    //#byte CTMUICON = 0x35E
  86.    
  87.    #word CTMUICON     = 0x35E
  88.    #bit  ITRIM_bit    = CTMUICON.10  
  89.    #bit  IRNG_0_bit    = CTMUICON.8  
  90.    #bit  IRNG_1_bit    = CTMUICON.9  
  91.    
  92.    
  93.    
  94.    #word ANSB        = 0x4E2
  95.    #bit  ANSB0_bit   = ANSB.0
  96.    #bit  ANSB1_bit   = ANSB.1
  97.    #bit  ANSB2_bit   = ANSB.2
  98.    #bit  ANSB3_bit   = ANSB.3
  99.    #bit  ANSB4_bit   = ANSB.4
  100.    #bit  ANSB5_bit   = ANSB.5
  101.    
  102.    #bit  ANSB9_bit   = ANSB.9
  103.    #bit  ANSB10_bit  = ANSB.10
  104.    #bit  ANSB11_bit  = ANSB.11
  105.    #bit  ANSB12_bit  = ANSB.12
  106.    #bit  ANSB13_bit  = ANSB.13
  107.    #bit  ANSB14_bit  = ANSB.14
  108.    #bit  ANSB15_bit  = ANSB.15
  109.    
  110.    
  111.    
  112.    #word TRISB         = 0x2C8
  113.    #bit  TRISB0_bit    = TRISB.0
  114.    #bit  TRISB1_bit    = TRISB.1
  115.    #bit  TRISB2_bit    = TRISB.2
  116.    #bit  TRISB3_bit    = TRISB.3
  117.    #bit  TRISB4_bit    = TRISB.4
  118.    #bit  TRISB5_bit    = TRISB.5
  119.    #bit  TRISB6_bit    = TRISB.6
  120.    #bit  TRISB7_bit    = TRISB.7
  121.    #bit  TRISB8_bit    = TRISB.8
  122.    #bit  TRISB9_bit    = TRISB.9
  123.    #bit  TRISB10_bit   = TRISB.10
  124.    #bit  TRISB11_bit   = TRISB.11
  125.    #bit  TRISB12_bit   = TRISB.12
  126.    #bit  TRISB13_bit   = TRISB.13
  127.    #bit  TRISB14_bit   = TRISB.14
  128.    #bit  TRISB15_bit   = TRISB.15
  129.    
  130.    
  131.    #word LATB         = 0x2CC
  132.    #bit  LATB0_bit    = LATB.0
  133.    #bit  LATB1_bit    = LATB.1
  134.    #bit  LATB2_bit    = LATB.2
  135.    #bit  LATB3_bit    = LATB.3
  136.    #bit  LATB4_bit    = LATB.4
  137.    #bit  LATB5_bit    = LATB.5
  138.    #bit  LATB6_bit    = LATB.6
  139.    #bit  LATB7_bit    = LATB.7
  140.    #bit  LATB8_bit    = LATB.8
  141.    #bit  LATB9_bit    = LATB.9
  142.    #bit  LATB10_bit   = LATB.10
  143.    #bit  LATB11_bit   = LATB.11
  144.    #bit  LATB12_bit   = LATB.12
  145.    #bit  LATB13_bit   = LATB.13
  146.    #bit  LATB14_bit   = LATB.14
  147.    #bit  LATB15_bit   = LATB.15
  148.    
  149.    
  150.    #word PORTB         = 0x2CA
  151.    #bit  PORTB0_bit    = PORTB.0
  152.    #bit  PORTB1_bit    = PORTB.1
  153.    #bit  PORTB2_bit    = PORTB.2
  154.    #bit  PORTB3_bit    = PORTB.3
  155.    #bit  PORTB4_bit    = PORTB.4
  156.    #bit  PORTB5_bit    = PORTB.5
  157.    #bit  PORTB6_bit    = PORTB.6
  158.    #bit  PORTB7_bit    = PORTB.7
  159.    #bit  PORTB8_bit    = PORTB.8
  160.    #bit  PORTB9_bit    = PORTB.9
  161.    #bit  PORTB10_bit   = PORTB.10
  162.    #bit  PORTB11_bit   = PORTB.11
  163.    #bit  PORTB12_bit   = PORTB.12
  164.    #bit  PORTB13_bit   = PORTB.13
  165.    #bit  PORTB14_bit   = PORTB.14
  166.    #bit  PORTB15_bit   = PORTB.15
  167.    
  168.    
  169.    
  170.    #word AD1CTMENL   = 0x362
  171.    #bit CTMEN10_bit = AD1CTMENL.10            
  172.                
  173.      
  174.    // variables de uso general
  175.    char c;
  176.    int inc,incG,vecesP,vecesG;
  177.    int unit=0;
  178.    int i;
  179.    
  180.    
  181.    
  182.    // Funciones
  183.    
  184.    
  185.    void sw_delay(unsigned long value)
  186.    {
  187.       while(value--);
  188.    }
  189.    
  190.    
  191.    
  192.    void InitCTMU(void)
  193.    {  
  194.      
  195.        TRISB14_bit=1;
  196.      
  197.       //setup CTMU
  198.       //CTMU CONFIGURATION
  199.       CTMUEN_bit = 0;                 //make sure CTMU is disabled
  200.       CTMUSIDL_bit = 0;               //CTMU continues to run in idle mode
  201.       TGEN_bit = 0;                   //disable edge delay generation mode of the CTMU
  202.       EDGEN_bit = 0;                  //edges are blocked
  203.       EDGSEQEN_bit = 0;               //edge sequence not needed
  204.       IDISSEN_bit = 0;                //Do not ground the current source
  205.       CTTRIG_bit = 0;                 //Trigger Output is disabled
  206.       EDG2POL_bit = 0;
  207.                      //CTMUCONbits.EDG2SEL = 0x3;              //Edge2 Src = OC1
  208.                      EDG2SEL_0_bit = 1;
  209.                      //EDG2SEL_1_bit = 1;
  210.       EDG1POL_bit = 0;
  211.                      //CTMUCONbits.EDG1SEL = 0x3;              //Edge1 Src = OC1
  212.                      EDG1SEL_0_bit = 1;
  213.                      //EDG1SEL_1_bit = 1;
  214.    
  215.        //CTMUICON
  216.                      IRNG_0_bit = 1;                           //0x02:-> 10 x 0.55uA. 0x03:-> 100 x 0.55uA.
  217.                      IRNG_1_bit = 1;
  218.    
  219.        //ADC CONFIGURATION  
  220.        //AD1PCFGL = 0x0000;
  221.        ANSB = 0x0000;
  222.        
  223.        AD1CON1 = 0x0000;
  224.        AD1CHS = 10;                        //select the analog channel 10      
  225.                      FORM_0_bit  = 0x0;                 //Unsigned int format
  226.                      FORM_1_bit  = 0x0;
  227.        AD1CON3 = 0x0000;                       //ADRC = Clock derived from system clock  //GC4
  228.        AD1CON2 = 0x0000;
  229.        ADON_bit = 1;
  230.        SAMP_bit = 1;
  231.                      CTMUEN_bit = 1;
  232.        IDISSEN_bit = 1;                // Drain any charge on the circuit
  233.        sw_delay(100);
  234.        IDISSEN_bit = 0;
  235.        sw_delay(100);
  236.        
  237.    }
  238.    
  239.    
  240.    
  241.    
  242.    unsigned int ReadCTMU(int Index)
  243.    {
  244.        unsigned long   total = 0;
  245.        int             chrd;
  246.        const int samples=16;
  247.        unsigned int immediateValue;
  248.        unsigned int switchState;
  249.    
  250.        // Get the raw sensor reading.
  251.        for(chrd = 0; chrd < samples; chrd++)
  252.        {
  253.    
  254.                         //AD1PCFGL = 0xFFFF;  // A/D mux must connect to channel for CTMU to drain charge
  255.                         ANSB = 0xFFFF;
  256.    
  257.    
  258.            TRISB14_bit=0;
  259.    
  260.            LATB14_bit=0;
  261.            
  262.            DELAY //wait for the capacitors to be discharged
  263.            DELAY
  264.    
  265.            TRISB14_bit=1;
  266.            
  267.            //////////////////////////////////////////////////////////////////////////////////////////////////////////
  268.            
  269.                               //AD1PCFGL = 0; //configure again as analog inputs
  270.                               //ANSB = 0x0000;
  271.            AD1CHS = Index;
  272.          
  273.            
  274.            
  275.            ITRIM_bit=0;
  276.                    
  277.            AD1IF_bit = 0;
  278.            SAMP_bit = 1;               // Manually start the conversion
  279.            EDG2STAT_bit = 0;           // Make sure edge2 is 0
  280.            EDG1STAT_bit = 1;           // Set edge1 - Start Charge
  281.                        
  282.            DELAY  // Delay for CTMU charge time
  283.            DELAY
  284.                
  285.          
  286.            EDG1STAT_bit = 0;           // Clear edge1 - Stop Charge
  287.            AD1IF_bit = 0;
  288.            SAMP_bit = 0;
  289.            while(!AD1IF_bit);
  290.    
  291.            // Wait for the A/D conversion to finish
  292.            immediateValue = ADC1BUF0;          // Read the value from the A/D conversion
  293.            SAMP_bit = 0;
  294.            AD1IF_bit = 0;
  295.            DONE_bit = 0;
  296.    
  297.            //End of CTMU read
  298.            total = total + immediateValue;
  299.            
  300.        }
  301.    
  302.           immediateValue = total / samples;
  303.        
  304.           if(immediateValue < OPENSW - TRIP)    
  305.               {     switchState = PRESSED;     }    
  306.           else if(immediateValue > OPENSW - TRIP + HYST)     {     switchState = UNPRESSED;     }    
  307.          
  308.           return switchState;
  309.    
  310.    }  
  311.    
  312.    
  313.    
  314.    
  315.    
  316.    // Función principal
  317.    
  318.    void main()
  319.    {
  320.    
  321.       //setup_adc_ports(sAN9|VSS_VDD);
  322.       setup_adc(ADC_CLOCK_DIV_32);
  323.       setup_adc(ADC_CLOCK_INTERNAL);
  324.       setup_oscillator(OSC_INTERNAL);
  325.    
  326.      
  327.       // TODO: USER CODE!!
  328.          
  329.      
  330.       Delay_ms(500);        //
  331.      
  332.       while(TRUE)
  333.       {
  334.    
  335.          while(1)
  336.          {
  337.             InitCTMU();
  338.             unsigned int key_value;
  339.             key_value= ReadCTMU(10); //read touch channel 10  
  340.             if(key_value)
  341.             {
  342.             printf(LCD_PUTC, "\f\ntecla pulsada");
  343.             }
  344.             delay_ms(200);
  345.             printf(LCD_PUTC, "\f");
  346.             delay_ms(5);
  347.          }  
  348.    
  349.       }
  350.    
  351.    }
[/code]
        
IF you_know = true THEN                                 
     share_it
ENDIF