Autor Tema: Explicacion INT_AD y Touch Capacitivo por favor....  (Leído 21800 veces)

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

Desconectado rotting79

  • PIC12
  • **
  • Mensajes: 91
Re: Explicacion INT_AD y Touch Capacitivo por favor....
« Respuesta #45 en: 27 de Agosto de 2013, 23:44:47 »
hey there

for those who program with CCS, here is the same program as above, you will see about 300 words in this program, instead 1000 and something with XC8 (3 to 4 times bigger).

Hola

para aquellos que programa con CCS, aquí es el mismo programa que el anterior, verá a unos 300 palabras en este programa, en vez 1000 y algo con XC8 (3 a 4 veces más grande)


the rest of the files are pretty much as  i do my libraries, and i think can be done easily and intuitive to follow. (just read the datasheet)
el resto de los archivos son más o menos lo que yo hago mis bibliotecas, y creo que se puede hacer fácilmente e intuitivo a seguir.(simplemente leer la hoja de datos)
« Última modificación: 27 de Agosto de 2013, 23:46:58 por rotting79 »
...On November 2nd, a PIC10 became self-aware and decided our fate in __delay_us ( 1 ); ...

Desconectado MGLSOFT

  • Moderadores
  • DsPIC33
  • *****
  • Mensajes: 7912
Re: Explicacion INT_AD y Touch Capacitivo por favor....
« Respuesta #46 en: 28 de Agosto de 2013, 00:11:56 »
Queda mas grande el programa en XC8 que el mismo en CCS ??
Que nivel de optimizacion usas ??

It is the largest program XC8 the same in CCS??
What level of optimization you use??
Todos los dias aprendo algo nuevo, el ultimo día de mi vida aprenderé a morir....
Mi Abuelo.

Desconectado rotting79

  • PIC12
  • **
  • Mensajes: 91
Re: Explicacion INT_AD y Touch Capacitivo por favor....
« Respuesta #47 en: 28 de Agosto de 2013, 01:09:52 »
Sadly I'm poor, so I can't afford the PRO version, so I use the FREE one which spend more instructions (as attached image shows), and runs a little bit more slow, because the extra instructions, but instead the CCS does not do that, but has some lacks, like linkers, Data Memory write, etc.

so it's up to you

Por desgracia yo soy pobre, así que no puedo permitirme la versión PRO, así que uso el libre que pasar más instrucciones (como se muestra en la imagen adjunta), y corre un poco más lento, porque las instrucciones adicionales, pero en lugar el CCS no lo hace, pero tiene algunas carencias, como conectores, escribir en la memoria de datos, etc

por lo que depende de ti

« Última modificación: 28 de Agosto de 2013, 01:27:13 por rotting79 »
...On November 2nd, a PIC10 became self-aware and decided our fate in __delay_us ( 1 ); ...

Desconectado MGLSOFT

  • Moderadores
  • DsPIC33
  • *****
  • Mensajes: 7912
Re: Explicacion INT_AD y Touch Capacitivo por favor....
« Respuesta #48 en: 28 de Agosto de 2013, 08:36:27 »
No te preocupes, has entrado en el Foro correcto.
Aqui todos somos pobres !!  :D :D :D
Solamente que me llamo la atencion que el compilador de CCS optimizara mejor, ya que por defecto esta programado que usa el nivel medio de optimizacion.
Que version del compilador de CCS utilizas??

Por otro lado, en el programa que escribiste para CCS, configuras el reloj para 16 MHz, tu usas en esa aplicacion el reloj interno o un resonador o cristal externo?

Do not worry, you have entered the correct forum.
Here are all poor!  :D :D
Only to my attention that the CCS compiler optimizes better, because the default is programmed using the average level of optimization.
What version of the CCS compiler use??

On the other hand, in the program you wrote for CCS, you set the clock to 16 MHz, you use it in this application the internal clock or an external crystal or resonator?
Todos los dias aprendo algo nuevo, el ultimo día de mi vida aprenderé a morir....
Mi Abuelo.

Desconectado MGLSOFT

  • Moderadores
  • DsPIC33
  • *****
  • Mensajes: 7912
Re: Explicacion INT_AD y Touch Capacitivo por favor....
« Respuesta #49 en: 28 de Agosto de 2013, 09:24:02 »
Si intento compilar tu aplicacion tal cual esta,  me da una gran cantidad de errores, hay muchas librerias que no estan y hay muchos errores provocados por GETENV.
Podras subir la aplicacion completa??
Gracias.


If I try to compile your application as it is, gives me a lot of errors, there are many libraries that are not and there are many errors caused by GETENV.
You can upload the completed application?
Thank you.
Todos los dias aprendo algo nuevo, el ultimo día de mi vida aprenderé a morir....
Mi Abuelo.

Desconectado rotting79

  • PIC12
  • **
  • Mensajes: 91
Re: Explicacion INT_AD y Touch Capacitivo por favor....
« Respuesta #50 en: 28 de Agosto de 2013, 22:54:36 »
man, damn it!!!!!  i got to write it again, seems that waste much time...... shoot (didn't publish the first time)

OK here is again...

yes, i always use uC with internal Oscillators.
CCS v 4.12 (XC8 v1.2 if u want to know)


and for the rest, let me explain u some tricks over here, I use my own libraries for two reasons:
1) I really like to understand what I'm doing, so I spend some time reading datasheets (It's very important to REALLY understand whats going on on ur application, PCB and firmware) and there is no other way to spend some time to get it, I must confess I've invest more time trying to understand how the things work than making them work, but I like it, I really KNOW how they work. and of course, reading some others libraries (like Microchip's) and examples and over here, etc, believe me, I'm in the process of learning (still).
2)If u use someone else's libraries (like Microchip), most people don't bother read the whole thing, because it's already running the application, and because of that don't know how it works, Am I right¿ and sometimes, actually all the time, u can optimize the application because u do what u really need o want to do.


So after the Litany, here's a trick:

rather than use "getenv" u can use the registers' address. here is an example:

//#BYTE PORTA = getenv("SFR:PORTA")          //0xF92.
#BYTE PORTA = 0xF92

that's why I comment the address at the end, but not anymore, because if u use "getenv" u can get the address automatically.

for doing that u should look at the datasheet (again) and get the address manually, for the last example go to datasheet PIC18F26K80 page 111, and u will see all registers and get the actual address u need, the problem doing this way is that you need to change the address every time u change the pic, that's why I use getenv.... I still don't know why doesn't work with you!!!!


the other trick is doing your own "register map" or werever u want to call it, such as this one:



/* OSCCON: OSCILLATOR CONTROL REGISTER  pag 53*/
enum SystemClock            
{
   InternalOscillatorBlock   = 0x02,
   SecondaryOscillator      = 0x01,
   PrimaryClock            = 0x00
   };
enum InternalRCOscillatorFrequency            
{
   INTOSC16MHz   = 0x07,      // Values.
   INTOSC8MHz   = 0x06,   
   INTOSC4MHz   = 0x05,      
   INTOSC2MHz   = 0x04,      
   INTOSC1MHz   = 0x03,   
   INTOSC500kHz   = 0x02,
   INTOSC250kz   = 0x01,
   INTOSC31kHz   = 0x00
   };
      
struct
{
   SystemClock                     SCS : 2;      // 1:0 System Clock Select bit.
   int1   HFIOFS;                           // 2 HFINTOSC Frequency Stable bit.
   int1   OSTS;                              // 3 Oscillator Start-up Time-out Status bit.
   InternalRCOscillatorFrequency      IRCF : 3;   // 6:4 Internal RC Oscillator Frequency Select bits.
   int1   IDLEN;                              // 7 Idle Enable bit.
   } OSCCON;
//#BYTE OSCCON = getenv("SFR:OSCCON")    //0xFD3.
//or
#BYTE OSCCON = 0xFD3

IMPORTANT NOTE: Read the datasheet once again!!!!!!

then u can use them  as STRUCT in your own library!!!!!, just like this:

// Oscillator.h

#include "OscillatorRegisters.h"

   #define      OSC_64MHz   0x0A   // Any number.
   #define      OSC_16MHz      0x07   // 0bx111xxxxx.
   #define      OSC_8MHz      0x06   // 0bx110xxxxx.
   #define      OSC_4MHz      0x05   // 0bx101xxxx.
   #define      OSC_2MHz      0x04   // 0bx100xxxx.
   #define      OSC_1MHz      0x03   // 0bx011xxxx.
   #define      OSC_500kHz   0x02   // 0bx010xxxx.
   #define      OSC_250kHz   0x01   // 0bx001xxxx.
   #define      OSC_31kHz      0x00   // 0bx000xxxx.


#include   "Oscillator.h"

/*******************************************************************/
/*   -SetUpOscillator ( Frequency )
/*      -Frequency - at the uC will run.
/*   
/*   Note: This library just run for Internal Oscillator.
/******************************************************************/
void SetUpOscillator ( unsigned char Frequency )
{
   OSCCON = 0x00;            // Set to POR/BOR default values.

#if   defined ( _PIC18F26K80_H_ ) || defined ( _PIC18F45K22_H_ )
   if ( Frequency == OSC_64MHz )
   {
      OSCCON . IRCF = OSC_16MHz;
      OSCTUNE . PLLEN = 0x01;    // Frequency Multiplier PLL Enable.
      }   
   else
      OSCCON . IRCF = Frequency;   // Passing the Fequency value.

#elif   defined ( _PIC16F887_H_ ) || defined ( _PIC16F886_H_ ) || defined ( _PIC12F683_H_ )
   OSCCON . OSTS = 0x00;      // Internal oscillator.
   OSCCON . SCS = 0x01;      // Internal oscillator = system clock.
   OSCCON . HTS = 0x01;      // OSC stable.
         
   OSCCON |= Frequency;         // Passing the Fequency value.
#endif
   }

U got the main idea, I think it is better to show u how to do it (and WE can learn something from it) than give it, and once again, don't learn nothing..... Am I wrong¿¿¿

but it is OK, we are here to learn something, so if I can help u with anything, sure just ask please, because I'm pretty sure that if I'm stuck, u'd help, right¿¿


hombre , maldita sea ! ! ! tengo que escribirlo de nuevo, parece que he perder demasiado tiempo ...... disparar ( no publicó la primera vez)


Bien aquí es de nuevo ...

sí, yo siempre utilizo uC con osciladores internos.
CCS v 4.12 ( XC8 v1.2 si quieres saber)


y para el resto , permítanme explicar u algunos trucos de aquí , yo uso mis propias bibliotecas , por dos razones :
1 ) Me gusta entender lo que estoy haciendo , por lo que pasar algún tiempo leyendo las hojas de datos (es muy importante para entender realmente lo que pasa en la aplicación ur , PCB y firmware) y no hay otra manera de pasar algún tiempo para que , debo confesar que he invierto más tiempo tratando de entender cómo funcionan las cosas que hacer que funcionen, pero me gusta , lo que realmente saben cómo funcionan. y por supuesto , la lectura de algunas otras bibliotecas (como las de Microchip ) y ejemplos y por aquí, etc , créeme , estoy en el proceso de aprendizaje (aún) .
2 ) En caso de uso u otra persona bibliotecas (como Microchip ) , la mayoría de las personas no se molestan en leer toda la cosa, porque ya está ejecutando la aplicación , y debido a que no saben cómo funciona, ¿ Estoy en lo cierto y, a veces , En realidad todo el tiempo , u puede optimizar la aplicación, ya que u hacer lo que u realmente necesita o quiere hacer.


Así que después de la Letanía , aquí hay un truco :

en lugar de utilizar " getenv " u puede utilizar la dirección de los registros . Aquí está un ejemplo:

/ / # BYTE PORTA = getenv ( " SFR : PORTA ") / / 0xF92 .
# BYTE PORTA = 0xF92

es por eso que comento la dirección al final , pero ya no, porque si u utilizar " getenv " u puede obtener la dirección automáticamente.

para hacer que u debe mirar la hoja de datos ( de nuevo) y obtener la dirección manualmente , para el último ejemplo ir a la ficha técnica PIC18F26K80 página 111 , yu ver todos los registros y obtener la necesidad u dirección real , el problema haciendo de esta manera es que necesita cambiar la dirección cada vez u cambiar la imagen, es por eso que uso getenv .... Todavía no sé por qué no trabajar con usted! !


Por otro truco está haciendo su propia "hoja de registro " o werever u quiera llamarlo, como este :



/ * OSCCON : OSCILADOR DE REGISTRO DE CONTROL pag 53 * /
enumeración SystemClock
{
InternalOscillatorBlock = 0x02 ,
SecondaryOscillator = 0x01 ,
PrimaryClock = 0x00
} ;
enumeración InternalRCOscillatorFrequency
{
INTOSC16MHz = 0x07 , / / Valores .
INTOSC8MHz = 0x06 ,
INTOSC4MHz = 0x05 ,
INTOSC2MHz = 0x04 ,
INTOSC1MHz = 0x03 ,
INTOSC500kHz = 0x02 ,
INTOSC250kz = 0x01 ,
INTOSC31kHz = 0x00
} ;

struct
{
SystemClock SCS : 2 / / 01:00 reloj del sistema Seleccione bits .
INT1 HFIOFS ; / / 2 HFINTOSC Frecuencia de bits estable.
INT1 OSTS ; / / 3 Oscilador Start -up Time-out bits de estado.
InternalRCOscillatorFrequency IRCF : 3 / / 06:04 internos RC oscilador de frecuencia de bits de selección .
INT1 IDLEN / / 7 bit Enable Idle .
} OSCCON ;
/ / # BYTE OSCCON = getenv ( " SFR : OSCCON ") / / 0xFD3 .
/ / o
# BYTE OSCCON = 0xFD3

NOTA IMPORTANTE : [b ] Lea la hoja de datos , una vez más [/ b] ¡¡¡ ¡¡¡

entonces u puede utilizar como STRUCT en su propia biblioteca, al igual que este ! ! ! :

/ / Oscillator.h

# include " OscillatorRegisters.h "

# define OSC_64MHz 0x0A / / cualquier número.
# define OSC_16MHz 0x07 / / 0bx111xxxxx .
# define OSC_8MHz 0x06 / / 0bx110xxxxx .
# define OSC_4MHz 0x05 / / 0bx101xxxx .
# define OSC_2MHz 0x04 / / 0bx100xxxx .
# define OSC_1MHz 0x03 / / 0bx011xxxx .
# define OSC_500kHz 0x02 / / 0bx010xxxx .
# define OSC_250kHz 0x01 / / 0bx001xxxx .
# define OSC_31kHz 0x00 / / 0bx000xxxx .


# include " Oscillator.h "

/ ************************************************* ****************** /
/ * - SetUpOscillator (Frecuencia)
/ * -Frequency - a la UC se ejecutará.
/ *
/ * Nota : Esta biblioteca sólo funciona para el oscilador interno .
/ ************************************************* ***************** /
void SetUpOscillator ( Frecuencia unsigned char )
{
OSCCON = 0x00 ; / / Establecer los valores predeterminados POR / BOR .

# if defined ( _PIC18F26K80_H_ ) | | define ( _PIC18F45K22_H_ )
if ( Frecuencia == OSC_64MHz )
{
OSCCON . IRCF = OSC_16MHz ;
OSCTUNE . PLLEN = 0x01 ; / / Multiplicador de frecuencia PLL Enable .
}
más
OSCCON . IRCF = Frecuencia / / Pasar el valor fequency .

# elif definido ( _PIC16F887_H_ ) | | define ( _PIC16F886_H_ ) | | define ( _PIC12F683_H_ )
OSCCON . OSTS = 0x00 ; oscilador / / Interna .
OSCCON . SCS = 0x01 ; / / Interna oscilador = reloj del sistema.
OSCCON . HTS = 0x01 ; / / OSC estable.

OSCCON | = Frecuencia / / Pasar el valor fequency .
# endif
}

U consiguió la idea principal, creo que es mejor mostrar u cómo hacerlo ( y podemos aprender algo de ella ) que le dan , y una vez más , no aprenden nada ..... ¿Me equivoco ¿¿¿

pero está bien, estamos aquí para aprender algo, por lo que si te puedo ayudar u con cualquier cosa, que acaba de pedir por favor, porque estoy bastante seguro de que si estoy atascado, ayuda u'd, ¿no ¿¿


seem that when google translate it for me, in the library make some change, but the original still up there!!!!

hope u can enjoy it as I do ....
« Última modificación: 28 de Agosto de 2013, 22:58:54 por rotting79 »
...On November 2nd, a PIC10 became self-aware and decided our fate in __delay_us ( 1 ); ...

Desconectado MGLSOFT

  • Moderadores
  • DsPIC33
  • *****
  • Mensajes: 7912
Re: Explicacion INT_AD y Touch Capacitivo por favor....
« Respuesta #51 en: 28 de Agosto de 2013, 23:12:49 »
Usas CCS 4.012 o CCS 4.120 ??
Hay una gran diferencia entre esas versiones, por eso pregunto...

Respecto a GETENV, el compilador o protesta por el uso de la instruccion, sino por el nombre de la direccion de memoria que buscas con la instruccion Getenv.
Como dices bien, es facil de arreglar...

Yo tambien leo mucho de cada hoja de datos para sacar el mejor provecho de una aplicacion, pero aun asi, uso el CCS porque me ayuda mucho en el desarrollo.

Seguramente tu usas el CCS Compiler desde dentro del MPlab, y esa puede ser la razon por la cual si has compilado esa aplicacion, a ti te funcione y a mi aun no.

Puedes subir el archivo .hex ya compilado, para intentar probarlo en la practica ??

Desde ya te agradezco las molestias que has tomado para mostrarnos el uso de tu codigo.


You use CCS 4.012 or CCS 4.120??
There is a big difference between those versions, so I ask ...

Regarding GETENV, the compiler or protest the use of the instruction, but by the name of the memory address you want to getenv instruction.
As you say, it is easy to fix ...

I also read a lot of each sheet to make the most of an application, but still, use the CCS because it helps me a lot in the development.

Surely you are using the CCS Compiler from within MPLAB, and that may be the reason why if you compiled the application, you will work and I have not.

You can upload the file. Hex and compiled, to try to prove it in practice?

Of course I appreciate the trouble you have taken to show the use of your code.
Todos los dias aprendo algo nuevo, el ultimo día de mi vida aprenderé a morir....
Mi Abuelo.

Desconectado rotting79

  • PIC12
  • **
  • Mensajes: 91
Re: Explicacion INT_AD y Touch Capacitivo por favor....
« Respuesta #52 en: 28 de Agosto de 2013, 23:43:34 »
hey hey hey my man

of course i share my hex file, but remember, because i did it when i pass it to PIC18F45K22, didn't work because the threshold need another constant (i was using a protoboard), so please try wtih the MPlab, I'd help you to make it run....

(I think the google translation is messing up what i meen, so don't get me wrong.. and i hope u can understand what i want to say)

hey hey hey my man

Por supuesto que comparto mi archivo hex, pero recuerda, ya lo hice cuando me pase a PIC18F45K22, no funcionó debido a que el umbral necesita otra constante (yo estaba usando un protoboard), así que por favor intente con el MPLAB, yo había ayudará a hacer que funcione ....

(Creo que la traducción de Google está arruinando lo que quiero decir, así que no me malinterpreten .. y espero u puede entender lo que quiero decir)
and i use CCS4.120  (sorry, my bad)

the last two files are in CCS, first one PIC18F45K22 and the other one is for the PIC18F26K80


« Última modificación: 28 de Agosto de 2013, 23:49:03 por rotting79 »
...On November 2nd, a PIC10 became self-aware and decided our fate in __delay_us ( 1 ); ...

Desconectado rotting79

  • PIC12
  • **
  • Mensajes: 91
Re: Explicacion INT_AD y Touch Capacitivo por favor....
« Respuesta #53 en: 03 de Septiembre de 2013, 00:26:44 »
hey there

Just testing the CVD Library.

The library come from the AN1298 Application Note, from Microchip.

Even when I try to make some tendence from Proteus' UART, getting done frome the very first time, I didn't know I can do that, with a *.csv file at 0:56, even two touch sensors (no capacitive pads).

At 1:14 testing with a  PIC18F45K22, at 1:20 testing with PIC16F887, at 1:25 testing with a PIC16F886, at 1:31 testing with a PIC18F26K80, and finally at 1:37 testing with a PIC12F683.

So anyway, just as the Application Note said, get it from a ADC channel to get a (int) value as a touch sensor.

I'll upload the library shortly...

Hola

Sólo probar la Biblioteca ECV.

La biblioteca proviene de la aplicación AN1298 Note, de Microchip.

Incluso cuando trato de hacer un poco de Tendence de Proteus 'UART, conseguir que se hagan desde el primer momento, yo no sabía que yo puedo hacer eso con un archivo csv *. A doce y cincuenta y seis, incluso los dos sensores de contacto (sin pastillas capacitivos ).

En las pruebas de 1:14 con una PIC18F45K22, a las 1:20 de pruebas con PIC16F887, en las pruebas de 01:25 con un PIC16F886, en las pruebas de 01:31 con una PIC18F26K80, y, finalmente, en las pruebas de una y treinta y siete con un PIC12F683.

En fin, como dijo el Nota de aplicación, obtener de un canal ADC para obtener un valor (int) como un sensor de contacto.

Voy a subir a la biblioteca en breve ...
...On November 2nd, a PIC10 became self-aware and decided our fate in __delay_us ( 1 ); ...

Desconectado rotting79

  • PIC12
  • **
  • Mensajes: 91
Re: Explicacion INT_AD y Touch Capacitivo por favor....
« Respuesta #54 en: 03 de Septiembre de 2013, 01:11:04 »
here is the XC8 Library...

CVD.c
Código: C
  1. /************ CVD.c *********/
  2.  
  3. #include "CVD.h"
  4. #include "stdio.h"
  5.  
  6. /** struct where the values for every cvd touch sensor saves its own values **/
  7. typedef struct _CVD_SENSOR
  8. {
  9.         unsigned int            Threshold;              // Un-pressed Sensor value.
  10.         unsigned char   Trip;                   // Difference between pressed and un-pressed switch.
  11.         unsigned char   Hysteresis;             // Amount to change.
  12.         unsigned char   PinRef;                 // Charging Pin to Reference.
  13.         unsigned char   Tris;                   // SFR Tris Register.
  14.         unsigned char   Port;                   // SFR Port Register.
  15.         unsigned char   SensorState;    // Actual Sensor State.
  16.         unsigned int            SensorRead;             // ADC value.
  17.         } CVD_TOUCH_SENSOR;
  18.  
  19. volatile CVD_TOUCH_SENSOR cvd_sensors [ NUMBER_OF_CVD_SENSORS ];
  20.  
  21.  
  22. /*******************************************************************/
  23. /*      -SetUpCVDTouch ( ReferencePin )
  24. /*              -ReferencePin
  25. /*              *configures the Pin which is a reference for touch sensors.
  26. /*              should be high (VDD) to charge and pass voltage to the sensors
  27. /*              internally.    
  28. /*
  29. /*      -SetUpCVDTouchSensor ( CVDSensorNumber, Threshold
  30. /*                                                      Trip, Hysteresis,
  31. /*                                                      ChargingPin, Tris, Port )
  32. /*              -TouchSensorNumber
  33. /*              -Threshold
  34. /*              -Trip
  35. /*              -Hysteresis
  36. /*              -ChargingPin    (reference pin)
  37. /*              -Tris
  38. /*              -Port
  39. /*              *sets the Touch Sensor Channel to be read.
  40. /*
  41. /*      -CVDTouchSensorNumber ( CVDTouchSensorNumber )
  42. /*              -CVDTouchSensorNumber
  43. /*              *Returns the value stored in ptrSensor -> SensorState
  44. /*               if pressed = 1; else = 0.
  45. /*
  46. /*      -DebbugCVDTouchSensor ( TouchSensorNumber, TouchSensorNumber )
  47. /*              -TouchSensorNumber
  48. /*              -TouchSensorNumber
  49. /*              *Prints out the value of any two touch sensors, int (16-bit).
  50. /*
  51. /******************************************************************/
  52.  
  53.  
  54. /********* Configure the CVD Touch Pin Reference ********************/
  55. void SetUpCVDTouch ( unsigned char ReferencePin )
  56. {
  57.         switch ( ReferencePin )
  58.         {
  59.                 case CVD_CH_0:
  60.                         REFERENCE_TRIS_PIN_0 = AS_OUTPUT;       // Chargin pin as output
  61.                         REFERENCE_PIN_0 = TO_VDD;                                       // and logic high.
  62.                         break;
  63.                 case CVD_CH_1:
  64.                         REFERENCE_TRIS_PIN_1 = AS_OUTPUT;       // Chargin pin as output
  65.                         REFERENCE_PIN_1 = TO_VDD;                                       // and logic high.
  66.                         break;
  67.                 case CVD_CH_2:
  68.                         REFERENCE_TRIS_PIN_2 = AS_OUTPUT;       // Chargin pin as output
  69.                         REFERENCE_PIN_2 = TO_VDD;                                       // and logic high.
  70.                         break;
  71.                 case CVD_CH_3:
  72.                         REFERENCE_TRIS_PIN_3 = AS_OUTPUT;       // Chargin pin as output
  73.                         REFERENCE_PIN_3 = TO_VDD;                                       // and logic high.
  74.                         break;
  75. //              case CVD_CH_4:
  76. //                      REFERENCE_TRIS_PIN_4 = AS_OUTPUT;       // Chargin pin as output
  77. //                      REFERENCE_PIN_4 = TO_VDD;                                       // and logic high.
  78. //                      break;
  79.                 case CVD_CH_5:
  80.                         REFERENCE_TRIS_PIN_5 = AS_OUTPUT;       // Chargin pin as output
  81.                         REFERENCE_PIN_5 = TO_VDD;                                       // and logic high.
  82.                         break;
  83.                        
  84.                 default:
  85.                         break;
  86.                 }      
  87.         }      
  88.  
  89.        
  90. /********* Configure the CVD Touch Sensor to use ********************/
  91. void SetUpCVDTouchSensor ( unsigned char CVDSensorNumber, unsigned int Threshold,
  92.                                                         unsigned char Trip, unsigned char Hysteresis,
  93.                                                         unsigned char ChargingPin, unsigned char Tris, unsigned char Port )
  94. {
  95.         CVD_TOUCH_SENSOR* ptrSensor;
  96.         ptrSensor = ( CVD_TOUCH_SENSOR* ) cvd_sensors + CVDSensorNumber;        // Pointing to the Touch Sensor
  97.                                                                                                                                         // to be Set it.
  98.         ptrSensor -> Threshold = Threshold;                                                             // Pasing the values to
  99.         ptrSensor -> Trip = Trip;                                                                                       // the Sensors
  100.         ptrSensor -> Hysteresis = Hysteresis;
  101.         ptrSensor -> PinRef = ChargingPin;
  102.         ptrSensor -> Tris = Tris;
  103.         ptrSensor -> Port = Port;
  104.         }      
  105.  
  106. /********* Decode the Touch Sensor Selsected; verified if it is Pressed or not ********************/
  107. int DecodeCVDTouchSensor ( unsigned char CVDTouchSensorNumber )
  108. {
  109.         CVD_TOUCH_SENSOR* ptrSensor;
  110.         ptrSensor = ( CVD_TOUCH_SENSOR* ) cvd_sensors +         // Pointing to the Touch Sensor
  111.                                 CVDTouchSensorNumber;                                           // to be Decode it.
  112.                                                                                                                                                        
  113.  
  114. #if     defined ( _PIC18F26K80_H_ ) || defined ( _PIC18F45K22_H_ )
  115.         ADCON0 &= 0b10000011;                                           // Cleaning up the previous channel.
  116. #elif   defined ( _PIC16F887_H_ ) || defined ( _PIC16F886_H_ )
  117.         ADCON0 &= 0b11000011;                                           // Cleaning up the previous channel.
  118. #elif defined   ( _PIC12F683_H_ )
  119.         ADCON0 &= 0b11110011;                                           // Cleaning up the previous channel.
  120. #endif
  121.         ADCON0 |= ptrSensor -> PinRef << 2 ;                    // Setting the AD channel to read
  122.                                                                                                 // without changing the ADCON0 value.
  123.         __delay_us ( 20 );
  124.  
  125.         switch ( CVDTouchSensorNumber )
  126.         {
  127.                 case CVD_CH_0:
  128.                         CVD_SENSOR_TRIS_0 = AS_OUTPUT;          // Set Sensing pin as output
  129.                         CVD_SENSOR_PORT_0 = TO_GND;                     // and logic low (discharging it).
  130.                
  131.                         CVD_SENSOR_TRIS_0 = AS_INPUT;           // Sensing pin as input.
  132.                         break;
  133.                 case CVD_CH_1:
  134.                         CVD_SENSOR_TRIS_1 = AS_OUTPUT;          // Set Sensing pin as output
  135.                         CVD_SENSOR_PORT_1 = TO_GND;                     // and logic low (discharging it).
  136.                
  137.                         CVD_SENSOR_TRIS_1 = AS_INPUT;           // Sensing pin as input.
  138.                         break;
  139.                 case CVD_CH_2:
  140.                         CVD_SENSOR_TRIS_2 = AS_OUTPUT;          // Set Sensing pin as output
  141.                         CVD_SENSOR_PORT_2 = TO_GND;                     // and logic low (discharging it).
  142.                
  143.                         CVD_SENSOR_TRIS_2 = AS_INPUT;           // Sensing pin as input.
  144.                         break;
  145.                 case CVD_CH_3:
  146.                         CVD_SENSOR_TRIS_3 = AS_OUTPUT;          // Set Sensing pin as output
  147.                         CVD_SENSOR_PORT_3 = TO_GND;                     // and logic low (discharging it).
  148.                
  149.                         CVD_SENSOR_TRIS_3 = AS_INPUT;           // Sensing pin as input.
  150.                         break;
  151. //              case CVD_CH_4:
  152. //                      CVD_SENSOR_TRIS_4 = AS_OUTPUT;          // Set Sensing pin as output
  153. //                      CVD_SENSOR_PORT_4 = TO_GND;                     // and logic low (discharging it).
  154. //             
  155. //                      CVD_SENSOR_TRIS_4 = AS_INPUT;           // Sensing pin as input.
  156. //                      break;
  157.                 case CVD_CH_5:
  158.                         CVD_SENSOR_TRIS_5 = AS_OUTPUT;          // Set Sensing pin as output
  159.                         CVD_SENSOR_PORT_5 = TO_GND;                     // and logic low (discharging it).
  160.                
  161.                         CVD_SENSOR_TRIS_5 = AS_INPUT;           // Sensing pin as input.
  162.                         break;
  163.                        
  164.                 default:
  165.                         break;
  166.                 }      
  167.  
  168. //      ADCON0 = 0b00000101;                                                    // imediatly pointing ADC to Sense pin.
  169. //      SetADCChannel ( CH_AN2 );
  170. #if     defined ( _PIC18F26K80_H_ ) || defined ( _PIC18F45K22_H_ )
  171.         ADCON0 &= 0b10000011;                           // Cleaning up the previous channel.
  172. #elif   defined ( _PIC16F887_H_ ) || defined ( _PIC16F886_H_ )
  173.         ADCON0 &= 0b11000011;                           // Cleaning up the previous channel.
  174. #elif defined   ( _PIC12F683_H_ )
  175.         ADCON0 &= 0b11110011;                           // Cleaning up the previous channel.
  176. #endif
  177.         ADCON0 |= CVDTouchSensorNumber << 2;    // Setting the AD channel to read
  178.                                                                                 // without changing the ADCON0 value.
  179.  
  180.         ADCON0bits . GO = 0x01;                                                                         // Starts the reading of the capacitive sensor.                        
  181.         while ( ADCON0bits . GO );                                                                              // Wait until convertion is done.
  182.         ptrSensor -> SensorRead = ( ( ( unsigned int ) ADRESH ) << 8 ) |        
  183.                         ( ADRESL );                                                                                             // Return the value from two Bytes.
  184.  
  185.         if ( ptrSensor -> SensorRead < DEFAULT_CVD_THRESHOLD - DEFAULT_CVD_TRIP )
  186.         {
  187.                 ptrSensor -> SensorState = PRESSED;
  188.                 }      
  189.  
  190.         else if ( ptrSensor -> SensorRead > DEFAULT_CVD_THRESHOLD - DEFAULT_CVD_TRIP + \
  191.                         DEFAULT_CVD_HYST )
  192.         {
  193.                 ptrSensor -> SensorState = UNPRESSED;
  194.                 }      
  195.  
  196.         return ptrSensor -> SensorState;
  197.         }
  198.        
  199. /********* Debug the CVD touch Sensor; debug propouse ********************/
  200. void DebbugCVDTouchSensor ( unsigned char TouchSensorNumber0, unsigned char TouchSensorNumber1 )
  201. {
  202.         CVD_TOUCH_SENSOR* ptrSensor0;
  203.         ptrSensor0 = ( CVD_TOUCH_SENSOR* ) cvd_sensors + TouchSensorNumber0;            // Pointing to the Touch Sensor
  204.                                                                                                                                                         // to be Decode it.
  205.         CVD_TOUCH_SENSOR* ptrSensor1;
  206.         ptrSensor1 = ( CVD_TOUCH_SENSOR* ) cvd_sensors + TouchSensorNumber1;                    // Pointing to the Touch Sensor
  207.                                                                                                                                                         // to be Decode it.
  208.  
  209.         printf ( "%d,%d\n", ptrSensor0 -> SensorRead    , ptrSensor1 -> SensorRead );   // Printing out the actual value
  210.                                                                                                                                                         // on the sensor selected.
  211.         }

CVD.h
Código: C
  1. /************ CVD.h *********/
  2.  
  3. /********* A/D Port Configuration Control bits ********/
  4. #define CVD_CH_0        0x00            // The same as ADC channels.
  5. #define CVD_CH_1        0x01
  6. #define CVD_CH_2        0x02
  7. #define CVD_CH_3        0x03
  8. #define CVD_CH_4        0x04
  9. #define CVD_CH_5        0x05
  10. #define CVD_CH_6        0x06
  11. #define CVD_CH_7        0x07
  12. #define CVD_CH_8        0x08
  13.  
  14.  
  15. /********* TRISx Port Configuration Control bits ********/
  16. /*********                                                      ADC pins only ********/
  17. /***** Reference Pin, got to be a ADC Pin ******************/
  18. #define REFERENCE_TRIS_PIN_0    TRISAbits . TRISA0
  19. #define REFERENCE_TRIS_PIN_1    TRISAbits . TRISA1
  20. #define REFERENCE_TRIS_PIN_2    TRISAbits . TRISA2
  21. #define REFERENCE_TRIS_PIN_3    TRISAbits . TRISA3
  22. #define REFERENCE_TRIS_PIN_4    TRISAbits . TRISA4
  23. #define REFERENCE_TRIS_PIN_5    TRISAbits . TRISA5
  24.  
  25. #define REFERENCE_PIN_0                 PORTAbits . RA0
  26. #define REFERENCE_PIN_1                 PORTAbits . RA1
  27. #define REFERENCE_PIN_2                 PORTAbits . RA2
  28. #define REFERENCE_PIN_3                 PORTAbits . RA3
  29. #define REFERENCE_PIN_4                 PORTAbits . RA4
  30. #define REFERENCE_PIN_5                 PORTAbits . RA5
  31.  
  32.  
  33. /********* TRISx Port Configuration Control bits ********/
  34. /*********                                                      ADC pins only ********/
  35. /***** Sensors Pin, got to be a ADC Pin ********************/
  36. #define CVD_SENSOR_TRIS_0               TRISAbits . TRISA0
  37. #define CVD_SENSOR_TRIS_1               TRISAbits . TRISA1
  38. #define CVD_SENSOR_TRIS_2               TRISAbits . TRISA2
  39. #define CVD_SENSOR_TRIS_3               TRISAbits . TRISA3
  40. #define CVD_SENSOR_TRIS_4               TRISAbits . TRISA4
  41. #define CVD_SENSOR_TRIS_5               TRISAbits . TRISA5
  42.  
  43. #define CVD_SENSOR_PORT_0               PORTAbits . RA0
  44. #define CVD_SENSOR_PORT_1               PORTAbits . RA1
  45. #define CVD_SENSOR_PORT_2               PORTAbits . RA2
  46. #define CVD_SENSOR_PORT_3               PORTAbits . RA3
  47. #define CVD_SENSOR_PORT_4               PORTAbits . RA4
  48. #define CVD_SENSOR_PORT_5               PORTAbits . RA5
  49.  
  50.  
  51. /********* General Registers ********/
  52. #define PRESSED         0x01
  53. #define UNPRESSED       0x00
  54.  
  55. #define AS_INPUT                0x01
  56. #define AS_OUTPUT       0x00
  57.  
  58. #define TO_VDD          0x01
  59. #define TO_GND          0x00
  60.  
  61.  
  62. /********* Values to Set The Touch Sensors ********/
  63. #define DEFAULT_CVD_THRESHOLD           300             // 3000; Un-pressed switch value.
  64. #define DEFAULT_CVD_TRIP                                70              // 200; Difference between pressed and un-pressed switch.
  65.                                                                                                
  66. #define DEFAULT_CVD_HYST                                5               // 65; Amount to change from pressed to un-pressed.
  67.  
  68.  
  69. #define NUMBER_OF_CVD_SENSORS   0x05            // Number of Sensors to be read in the uC.
...On November 2nd, a PIC10 became self-aware and decided our fate in __delay_us ( 1 ); ...

Desconectado rotting79

  • PIC12
  • **
  • Mensajes: 91
Re: Explicacion INT_AD y Touch Capacitivo por favor....
« Respuesta #55 en: 03 de Septiembre de 2013, 01:15:38 »
And right here is a example.... with PIC18F26K80

but it is already tested with PIC18F45K22, PIC18F44K22, PIC16F887, PIC16F886, PIC12F683

main.c
Código: C
  1. #include        <xc.h>
  2.  
  3. #define _XTAL_FREQ      FREQ_16MHz
  4.  
  5. #include "SetUp.c"
  6.  
  7. #define RESET_STATE     0x00
  8.  
  9. void main ( void )       //@0x100
  10. {
  11.         SetUp ( );
  12.  
  13.         LED1_OFF;
  14.         LED2_OFF;
  15.  
  16.         printf ( "Application Ready...\n" );   
  17.         printf ( "CVD:\n" );   
  18.  
  19.         while ( TRUE )
  20.         {
  21.                 __delay_ms ( 15 );
  22.                 TOUCH_SENSORS . Button0 = DecodeCVDTouchSensor ( CVD_CH_1 );
  23.                 TOUCH_SENSORS . Button1 = DecodeCVDTouchSensor ( CVD_CH_2 );
  24.                        
  25.                 LED1_OFF;
  26.                 LED2_OFF;
  27.  
  28.                 if ( TOUCH_SENSORS . Button0 )
  29.                 {
  30.                         LED1_ON;
  31.                         }      
  32.                 if ( TOUCH_SENSORS . Button1 )
  33.                 {
  34.                         LED2_ON;
  35.                         }
  36.  
  37.                 if ( !PORTBbits . RB7 )                                 // For debbuging propouses.
  38.                         DebbugCVDTouchSensor ( CVD_CH_1, CVD_CH_2 );
  39.                 }      
  40.         }

SetUp.c
Código: C
  1. /* CONFIG1L ************************/
  2. #pragma config  XINST = OFF                     // Extended Instruction Set.
  3. #pragma config  SOSCSEL = DIG                   // SOSC Power Selection and Mode Configuration.
  4. #pragma config  INTOSCSEL = HIGH        // LF-INTOSC Low-power.
  5. #pragma config  RETEN = OFF                     // VREG Sleep.
  6.  
  7. /* CONFIG1H ************************/
  8. #pragma config  IESO = OFF                              // Internal/External Oscillator Switchover.
  9. #pragma config  FCMEN = OFF                     // Fail-Safe Clock Monitor.
  10. #pragma config  PLLCFG = OFF                    // 4X PLL.
  11. #pragma config  FOSC = INTIO2                   // Oscillator.
  12.  
  13. /* CONFIG2L ************************/
  14. #pragma config  BORPWR = ZPBORMV        // BORMV Power-Level.
  15. #pragma config  BORV = 0x03                     // Brown-out Reset Voltage.
  16. #pragma config  BOREN = SBORDIS         // Brown-out Reset Enable.
  17. #pragma config  PWRTEN = ON                     // Power-up Timer.
  18.  
  19. /* CONFIG2H ************************/
  20. #pragma config  WDTPS = 0x01                    // Watchdog Timer Postscale.
  21. #pragma config  WDTEN = OFF                     // Watchdog Timer Enable.
  22.  
  23. /* CONFIG3H ************************/
  24. #pragma config  MCLRE = ON                      // MCLR Pin Enable.
  25. #pragma config  MSSPMSK = MSK7          // MSSP V3 7-Bit Address Masking Mode.
  26. #pragma config  CANMX = PORTB           // ECAN MUX.
  27.  
  28. /* CONFIG4L ************************/
  29. #pragma config  BBSIZ = BB1K                    // Boot Block Size.
  30. #pragma config  STVREN = ON                     // Stack Full/Underflow Reset.
  31.  
  32. /* CONFIG5L ************************/
  33. #pragma config  CP3 = OFF                               // Code Protection.
  34. #pragma config  CP2 = OFF                               // Code Protection.
  35. #pragma config  CP1 = OFF                               // Code Protection.
  36. #pragma config  CP0 = OFF                               // Code Protection.
  37.  
  38. /* CONFIG5H ************************/
  39. #pragma config  CPD = OFF                               // Data EEPROM Code Protection.
  40. #pragma config  CPB = OFF                               // Boot Block Code Protection.
  41.  
  42. /* CONFIG6L ************************/
  43. #pragma config  WRT3 = OFF                              // Write Protection.
  44. #pragma config  WRT2 = OFF                              // Write Protection.
  45. #pragma config  WRT1 = OFF                                      // Write Protection.
  46. #pragma config  WRT0 = OFF                              // Write Protection.
  47.  
  48. /* CONFIG6H ************************/
  49. #pragma config  WRTD = OFF                              // Data EEPROM Write.
  50. #pragma config  WRTB = OFF                              // Boot Block Write Protection.
  51. #pragma config  WRTC = OFF                              // Configuration Register Write.
  52.  
  53. /* CONFIG7L ************************/
  54. #pragma config  EBTR3 = OFF                             // Table Read Protection.
  55. #pragma config  EBTR2 = OFF                             // Table Read Protection.
  56. #pragma config  EBTR1 = OFF                             // Table Read Protection.
  57. #pragma config  EBTR0 = OFF                             // Table Read Protection.
  58.  
  59. /* CONFIG7H ************************/
  60. #pragma config  EBTRB = OFF                             // Boot Block Table Read Protection.
  61.  
  62.  
  63. #if     defined ( _PIC18F26K80_H_ )
  64.         #define FREQ_64MHz      64000000
  65.         #define FREQ_16MHz      16000000
  66.         #define FREQ_8MHz       8000000
  67.         #define FREQ_4MHz       4000000
  68.         #define FREQ_1MHz               1000000
  69. #endif
  70.  
  71. #define TRUE            0x01
  72. #define FALSE           0x00
  73.  
  74. #include "Oscillator.c"
  75. #include "UART.c"
  76. #include "ADC.c"
  77. #include "PWMs.c"
  78. #include "CVD.c"
  79. #include "PullUps.c"
  80. #include "stdio.h"
  81.  
  82.  
  83. #define LED1_ON         PORTCbits . RC2 = 0x01;
  84. #define LED2_ON         PORTCbits . RC3 = 0x01;
  85.  
  86. #define LED1_OFF                PORTCbits . RC2 = 0x00;
  87. #define LED2_OFF                PORTCbits . RC3 = 0x00;
  88.  
  89. //typedef enum _BOOL { FALSE = 0, TRUE } BOOL;
  90.  
  91. struct  _TOUCH_SENSORS
  92. {
  93.         unsigned char   Button0;
  94.         unsigned        char    Button1;
  95.         unsigned int            Slider0;
  96.         } TOUCH_SENSORS;
  97.  
  98.  
  99. void SetUp ( void )
  100. {
  101.         SetUpOscillator ( OSC_16MHz );
  102.  
  103.         TRISC = 0x00;
  104.  
  105.         TRISB = 0x08;
  106.         EnablePullUps ( WPU7 );
  107.  
  108.         SetUpUART ( BAUD_RATE_57600 );
  109.        
  110.         SetUpADC ( sAN1 | sAN2, RIGHT_JUSTIFIED, VDD_VSS, FOSC_32 );
  111.  
  112.         SetUpCVDTouch ( CVD_CH_3 );
  113.  
  114.         SetUpCVDTouchSensor ( CVD_CH_1, DEFAULT_CVD_THRESHOLD,
  115.                                                         DEFAULT_CVD_TRIP, DEFAULT_CVD_HYST,
  116.                                                         CVD_CH_3, CVD_SENSOR_TRIS_1, CVD_SENSOR_PORT_1 );
  117.         SetUpCVDTouchSensor ( CVD_CH_2, DEFAULT_CVD_THRESHOLD,
  118.                                                         DEFAULT_CVD_TRIP, DEFAULT_CVD_HYST,
  119.                                                         CVD_CH_3, CVD_SENSOR_TRIS_2, CVD_SENSOR_PORT_2 );
  120.                                
  121.         }
...On November 2nd, a PIC10 became self-aware and decided our fate in __delay_us ( 1 ); ...

Desconectado rotting79

  • PIC12
  • **
  • Mensajes: 91
Re: Explicacion INT_AD y Touch Capacitivo por favor....
« Respuesta #56 en: 04 de Septiembre de 2013, 00:15:37 »
Here is the code for CVD Touch Sensig

Once again, for those who  program in CCS, here are the libraries and example....

main.c
Código: C
  1. #include "18F26K80.h"
  2. #define _PIC18F26K80_H_
  3.  
  4. #FUSES          INTRC_IO, NOWDT, NOPROTECT, BROWNOUT, PUT, MCLR
  5.  
  6. #USE                    DELAY ( int = 16MHz )
  7.  
  8. #USE                    RS232 ( Baud = 57600, Xmit = PIN_C6, Rcv = PIN_C7 )
  9.  
  10. #include "SetUp.c"
  11.  
  12.  
  13. void main ( )
  14. {
  15.         SetUp ( );
  16.  
  17.         LED1_OFF;
  18.         LED2_OFF;
  19.  
  20.         printf ( "Application Ready...\n" );   
  21.         printf ( "CVD:\n" );   
  22.        
  23.         while ( TRUE )
  24.         {
  25.                 delay_ms ( 15 );
  26.                 TOUCH_SENSORS . Button0 = DecodeCVDTouchSensor ( CVD_CH_1 );
  27.                 TOUCH_SENSORS . Button1 = DecodeCVDTouchSensor ( CVD_CH_2 );
  28.                        
  29.                 LED1_OFF;
  30.                 LED2_OFF;
  31.  
  32.                 if ( TOUCH_SENSORS . Button0 )
  33.                 {
  34.                         LED1_ON;
  35.                         }      
  36.                 if ( TOUCH_SENSORS . Button1 )
  37.                 {
  38.                         LED2_ON;
  39.                         }
  40.  
  41.                 if ( !PORTB . RB7 )                                     // For debbuging propouses.
  42.                         DebbugCVDTouchSensor ( CVD_CH_1, CVD_CH_2 );
  43.                 }
  44. }

SetUp.c
Código: C
  1. #include "CVD.c"
  2. #include "ECANRegisters.h"
  3. #include "InterruptsRegisters.h"
  4. #include "Oscillator.c"
  5. #include "PullUps.c"
  6.  
  7.  
  8. #define LED1_ON         PORTC . RC2 = 0x01;
  9. #define LED2_ON         PORTC . RC3 = 0x01;
  10.  
  11. #define LED1_OFF                PORTC . RC2 = 0x00;
  12. #define LED2_OFF                PORTC . RC3 = 0x00;
  13.  
  14.  
  15. struct  _TOUCH_SENSORS
  16. {
  17.         unsigned char   Button0;
  18.         unsigned        char    Button1;
  19.         unsigned int            Slider0;
  20.         } TOUCH_SENSORS;
  21.  
  22.  
  23.  
  24. void SetUp ( void )
  25. {
  26.         SetUpOscillator ( OSC_16MHz );
  27.  
  28.         TRISC = 0x00;
  29.  
  30.         TRISB = 0x08;
  31.         EnablePullUps ( ALL_WPU );
  32.  
  33.         CANCON = 0;
  34.         CANSTAT = 0;
  35.         ECANCON = 0b10000000;
  36.  
  37.         SetUpADC ( sAN1 | sAN2, RIGHT_JUSTIFIED, VDD_VSS, FOSC_32 );
  38. //      setup_adc(ADC_CLOCK_INTERNAL );
  39. //      setup_adc_ports( sAN1 | sAN2 );
  40.  
  41.         SetUpCVDTouch ( CVD_CH_3 );
  42.  
  43.         SetUpCVDTouchSensor ( CVD_CH_1, DEFAULT_CVD_THRESHOLD,
  44.                                                         DEFAULT_CVD_TRIP, DEFAULT_CVD_HYST,
  45.                                                         CVD_CH_3, CVD_SENSOR_TRIS_1, CVD_SENSOR_PORT_1 );
  46.         SetUpCVDTouchSensor ( CVD_CH_2, DEFAULT_CVD_THRESHOLD,
  47.                                                         DEFAULT_CVD_TRIP, DEFAULT_CVD_HYST,
  48.                                                         CVD_CH_3, CVD_SENSOR_TRIS_2, CVD_SENSOR_PORT_2 );
  49.         }

CVD.c
Código: C
  1. /************ CVD.c *********/
  2.  
  3. #include "CVD.h"
  4. #include "ADC.c"
  5. #include "stdio.h"
  6.  
  7. /** struct where the values for every cvd touch sensor saves its own values **/
  8. typedef struct _CVD_SENSOR
  9. {
  10.         unsigned int16  Threshold;              // Un-pressed Sensor value.
  11.         unsigned char   Trip;                   // Difference between pressed and un-pressed switch.
  12.         unsigned char   Hysteresis;             // Amount to change.
  13.         unsigned char   PinRef;                 // Charging Pin to Reference.
  14.         unsigned char   Tris;                   // SFR Tris Register.
  15.         unsigned char   Port;                   // SFR Port Register.
  16.         unsigned char   SensorState;    // Actual Sensor State.
  17.         unsigned int16  SensorRead;             // ADC value.
  18.         } CVD_TOUCH_SENSOR;
  19.  
  20. volatile CVD_TOUCH_SENSOR cvd_sensors [ NUMBER_OF_CVD_SENSORS ];
  21.  
  22.  
  23. /*******************************************************************/
  24. /*      -SetUpCVDTouch ( ReferencePin )
  25. /*              -ReferencePin
  26. /*              *configures the Pin which is a reference for touch sensors.
  27. /*              should be high (VDD) to charge and pass voltage to the sensors
  28. /*              internally.    
  29. /*
  30. /*      -SetUpCVDTouchSensor ( CVDSensorNumber, Threshold
  31. /*                                                      Trip, Hysteresis,
  32. /*                                                      ChargingPin, Tris, Port )
  33. /*              -TouchSensorNumber
  34. /*              -Threshold
  35. /*              -Trip
  36. /*              -Hysteresis
  37. /*              -ChargingPin    (reference pin)
  38. /*              -Tris
  39. /*              -Port
  40. /*              *sets the Touch Sensor Channel to be read.
  41. /*
  42. /*      -CVDTouchSensorNumber ( CVDTouchSensorNumber )
  43. /*              -CVDTouchSensorNumber
  44. /*              *Returns the value stored in ptrSensor -> SensorState
  45. /*               if pressed = 1; else = 0.
  46. /*
  47. /*      -DebbugCVDTouchSensor ( TouchSensorNumber, TouchSensorNumber )
  48. /*              -TouchSensorNumber
  49. /*              -TouchSensorNumber
  50. /*              *Prints out the value of any two touch sensors, int (16-bit).
  51. /*
  52. /******************************************************************/
  53.  
  54.  
  55. /********* Configure the CVD Touch Pin Reference ********************/
  56. void SetUpCVDTouch ( unsigned char ReferencePin )
  57. {
  58.         switch ( ReferencePin )
  59.         {
  60.                 case CVD_CH_0:
  61.                         REFERENCE_TRIS_PIN_0 = AS_OUTPUT;       // Chargin pin as output
  62.                         REFERENCE_PIN_0 = TO_VDD;                                       // and logic high.
  63.                         break;
  64.                 case CVD_CH_1:
  65.                         REFERENCE_TRIS_PIN_1 = AS_OUTPUT;       // Chargin pin as output
  66.                         REFERENCE_PIN_1 = TO_VDD;                                       // and logic high.
  67.                         break;
  68.                 case CVD_CH_2:
  69.                         REFERENCE_TRIS_PIN_2 = AS_OUTPUT;       // Chargin pin as output
  70.                         REFERENCE_PIN_2 = TO_VDD;                                       // and logic high.
  71.                         break;
  72.                 case CVD_CH_3:
  73.                         REFERENCE_TRIS_PIN_3 = AS_OUTPUT;       // Chargin pin as output
  74.                         REFERENCE_PIN_3 = TO_VDD;                                       // and logic high.
  75.                         break;
  76. //              case CVD_CH_4:
  77. //                      REFERENCE_TRIS_PIN_4 = AS_OUTPUT;       // Chargin pin as output
  78. //                      REFERENCE_PIN_4 = TO_VDD;                                       // and logic high.
  79. //                      break;
  80.                 case CVD_CH_5:
  81.                         REFERENCE_TRIS_PIN_5 = AS_OUTPUT;       // Chargin pin as output
  82.                         REFERENCE_PIN_5 = TO_VDD;                                       // and logic high.
  83.                         break;
  84.                        
  85.                 default:
  86.                         break;
  87.                 }      
  88.         }      
  89.  
  90.        
  91. /********* Configure the CVD Touch Sensor to use ********************/
  92. void SetUpCVDTouchSensor ( unsigned char CVDSensorNumber, unsigned int16 Threshold,
  93.                                                         unsigned char Trip, unsigned char Hysteresis,
  94.                                                         unsigned char ChargingPin, unsigned char Tris, unsigned char Port )
  95. {
  96.         CVD_TOUCH_SENSOR* ptrSensor;
  97.         ptrSensor = ( CVD_TOUCH_SENSOR* ) cvd_sensors + CVDSensorNumber;        // Pointing to the Touch Sensor
  98.                                                                                                                                         // to be Set it.
  99.         ptrSensor -> Threshold = Threshold;                                                             // Pasing the values to
  100.         ptrSensor -> Trip = Trip;                                                                                       // the Sensors
  101.         ptrSensor -> Hysteresis = Hysteresis;
  102.         ptrSensor -> PinRef = ChargingPin;
  103.         ptrSensor -> Tris = Tris;
  104.         ptrSensor -> Port = Port;
  105.         }      
  106.  
  107. /********* Decode the Touch Sensor Selsected; verified if it is Pressed or not ********************/
  108. int DecodeCVDTouchSensor ( unsigned char CVDTouchSensorNumber )
  109. {
  110.         unsigned char Temp;
  111.  
  112.         CVD_TOUCH_SENSOR* ptrSensor;
  113.         ptrSensor = ( CVD_TOUCH_SENSOR* ) cvd_sensors +         // Pointing to the Touch Sensor
  114.                                 CVDTouchSensorNumber;                                           // to be Decode it.
  115.                                                                                                                                                        
  116.  
  117. #if     defined ( _PIC18F26K80_H_ ) || defined ( _PIC18F45K22_H_ )
  118.         Temp = ADCON0;
  119.         Temp &= 0b10000011;                                                     // Cleaning up the previous channel.
  120.         ADCON0 = Temp;
  121. #elif   defined ( _PIC16F887_H_ ) || defined ( _PIC16F886_H_ )
  122.         ADCON0 &= 0b11000011;                                           // Cleaning up the previous channel.
  123. #elif defined   ( _PIC12F683_H_ )
  124.         ADCON0 &= 0b11110011;                                           // Cleaning up the previous channel.
  125. #endif
  126.         Temp = ADCON0;
  127.         Temp |= ptrSensor -> PinRef << 2;                               // Setting the AD channel to read
  128.         ADCON0 = Temp;                                                  // without changing the ADCON0 value.
  129.  
  130.         delay_us ( 20 );
  131.  
  132.         switch ( CVDTouchSensorNumber )
  133.         {
  134.                 case CVD_CH_0:
  135.                         CVD_SENSOR_TRIS_0 = AS_OUTPUT;          // Set Sensing pin as output
  136.                         CVD_SENSOR_PORT_0 = TO_GND;                     // and logic low (discharging it).
  137.                
  138.                         CVD_SENSOR_TRIS_0 = AS_INPUT;           // Sensing pin as input.
  139.                         break;
  140.                 case CVD_CH_1:
  141.                         CVD_SENSOR_TRIS_1 = AS_OUTPUT;          // Set Sensing pin as output
  142.                         CVD_SENSOR_PORT_1 = TO_GND;                     // and logic low (discharging it).
  143.                
  144.                         CVD_SENSOR_TRIS_1 = AS_INPUT;           // Sensing pin as input.
  145.                         break;
  146.                 case CVD_CH_2:
  147.                         CVD_SENSOR_TRIS_2 = AS_OUTPUT;          // Set Sensing pin as output
  148.                         CVD_SENSOR_PORT_2 = TO_GND;                     // and logic low (discharging it).
  149.                
  150.                         CVD_SENSOR_TRIS_2 = AS_INPUT;           // Sensing pin as input.
  151.                         break;
  152.                 case CVD_CH_3:
  153.                         CVD_SENSOR_TRIS_3 = AS_OUTPUT;          // Set Sensing pin as output
  154.                         CVD_SENSOR_PORT_3 = TO_GND;                     // and logic low (discharging it).
  155.                
  156.                         CVD_SENSOR_TRIS_3 = AS_INPUT;           // Sensing pin as input.
  157.                         break;
  158. //              case CVD_CH_4:
  159. //                      CVD_SENSOR_TRIS_4 = AS_OUTPUT;          // Set Sensing pin as output
  160. //                      CVD_SENSOR_PORT_4 = TO_GND;                     // and logic low (discharging it).
  161. //             
  162. //                      CVD_SENSOR_TRIS_4 = AS_INPUT;           // Sensing pin as input.
  163. //                      break;
  164.                 case CVD_CH_5:
  165.                         CVD_SENSOR_TRIS_5 = AS_OUTPUT;          // Set Sensing pin as output
  166.                         CVD_SENSOR_PORT_5 = TO_GND;                     // and logic low (discharging it).
  167.                
  168.                         CVD_SENSOR_TRIS_5 = AS_INPUT;           // Sensing pin as input.
  169.                         break;
  170.                        
  171.                 default:
  172.                         break;
  173.                 }      
  174.  
  175. #if     defined ( _PIC18F26K80_H_ ) || defined ( _PIC18F45K22_H_ )
  176.         Temp = ADCON0;
  177.         Temp &= 0b10000011;                                                     // Cleaning up the previous channel.
  178.         ADCON0 = Temp;
  179. #elif   defined ( _PIC16F887_H_ ) || defined ( _PIC16F886_H_ )
  180.         ADCON0 &= 0b11000011;                           // Cleaning up the previous channel.
  181. #elif defined   ( _PIC12F683_H_ )
  182.         ADCON0 &= 0b11110011;                           // Cleaning up the previous channel.
  183. #endif
  184.         Temp = ADCON0;
  185.         Temp |= CVDTouchSensorNumber << 2;      // Setting the AD channel to read
  186.         ADCON0 = Temp;                                  // without changing the ADCON0 value.
  187.  
  188.         ADCON0 . GO = 0x01;                                                                             // Starts the reading of the capacitive sensor.                        
  189.         while ( ADCON0 . GO );                                                                          // Wait until convertion is done.
  190.         ptrSensor -> SensorRead = ( ( ( unsigned int16 ) ADRESH ) << 8 ) |      
  191.                         ( ADRESL );                                                                                             // Return the value from two Bytes.
  192.  
  193.         if ( ptrSensor -> SensorRead < DEFAULT_CVD_THRESHOLD - DEFAULT_CVD_TRIP )
  194.         {
  195.                 ptrSensor -> SensorState = PRESSED;
  196.                 }      
  197.  
  198.         else if ( ptrSensor -> SensorRead > DEFAULT_CVD_THRESHOLD - DEFAULT_CVD_TRIP + \
  199.                         DEFAULT_CVD_HYST )
  200.         {
  201.                 ptrSensor -> SensorState = UNPRESSED;
  202.                 }      
  203.  
  204.         return ptrSensor -> SensorState;
  205.         }
  206.        
  207. /********* Debug the CVD touch Sensor; debug propouse ********************/
  208. void DebbugCVDTouchSensor ( unsigned char TouchSensorNumber0, unsigned char TouchSensorNumber1 )
  209. {
  210.         CVD_TOUCH_SENSOR* ptrSensor0;
  211.         ptrSensor0 = ( CVD_TOUCH_SENSOR* ) cvd_sensors + TouchSensorNumber0;            // Pointing to the Touch Sensor
  212.                                                                                                                                                         // to be Decode it.
  213.         CVD_TOUCH_SENSOR* ptrSensor1;
  214.         ptrSensor1 = ( CVD_TOUCH_SENSOR* ) cvd_sensors + TouchSensorNumber1;                    // Pointing to the Touch Sensor
  215.                                                                                                                                                         // to be Decode it.
  216.  
  217.         printf ( "\n%ld,%ld", ptrSensor0 -> SensorRead  , ptrSensor1 -> SensorRead );   // Printing out the actual value
  218.                                                                                                                                                         // on the sensor selected.
  219.         }

CVD.h
Código: C
  1. /************ CVD.h *********/
  2.  
  3. #include "PORTsRegisters.h"
  4.  
  5. /********* A/D Port Configuration Control bits ********/
  6. #define CVD_CH_0        0x00            // The same as ADC channels.
  7. #define CVD_CH_1        0x01
  8. #define CVD_CH_2        0x02
  9. #define CVD_CH_3        0x03
  10. #define CVD_CH_4        0x04
  11. #define CVD_CH_5        0x05
  12. #define CVD_CH_6        0x06
  13. #define CVD_CH_7        0x07
  14. #define CVD_CH_8        0x08
  15.  
  16.  
  17. /********* TRISx Port Configuration Control bits ********/
  18. /*********                                                      ADC pins only ********/
  19. /***** Reference Pin, got to be a ADC Pin ******************/
  20. #define REFERENCE_TRIS_PIN_0    TRISA . TRISA0
  21. #define REFERENCE_TRIS_PIN_1    TRISA  . TRISA1
  22. #define REFERENCE_TRIS_PIN_2    TRISA  . TRISA2
  23. #define REFERENCE_TRIS_PIN_3    TRISA  . TRISA3
  24. #define REFERENCE_TRIS_PIN_4    TRISA  . TRISA4
  25. #define REFERENCE_TRIS_PIN_5    TRISA  . TRISA5
  26.  
  27. #define REFERENCE_PIN_0                 PORTA  . RA0
  28. #define REFERENCE_PIN_1                 PORTA  . RA1
  29. #define REFERENCE_PIN_2                 PORTA  . RA2
  30. #define REFERENCE_PIN_3                 PORTA  . RA3
  31. #define REFERENCE_PIN_4                 PORTA  . RA4
  32. #define REFERENCE_PIN_5                 PORTA  . RA5
  33.  
  34.  
  35. /********* TRISx Port Configuration Control bits ********/
  36. /*********                                                      ADC pins only ********/
  37. /***** Sensors Pin, got to be a ADC Pin ********************/
  38. #define CVD_SENSOR_TRIS_0               TRISA  . TRISA0
  39. #define CVD_SENSOR_TRIS_1               TRISA  . TRISA1
  40. #define CVD_SENSOR_TRIS_2               TRISA  . TRISA2
  41. #define CVD_SENSOR_TRIS_3               TRISA  . TRISA3
  42. #define CVD_SENSOR_TRIS_4               TRISA  . TRISA4
  43. #define CVD_SENSOR_TRIS_5               TRISA  . TRISA5
  44.  
  45. #define CVD_SENSOR_PORT_0               PORTA  . RA0
  46. #define CVD_SENSOR_PORT_1               PORTA  . RA1
  47. #define CVD_SENSOR_PORT_2               PORTA  . RA2
  48. #define CVD_SENSOR_PORT_3               PORTA  . RA3
  49. #define CVD_SENSOR_PORT_4               PORTA  . RA4
  50. #define CVD_SENSOR_PORT_5               PORTA  . RA5
  51.  
  52.  
  53. /********* General Registers ********/
  54. #define PRESSED         0x01
  55. #define UNPRESSED       0x00
  56.  
  57. #define AS_INPUT                0x01
  58. #define AS_OUTPUT       0x00
  59.  
  60. #define TO_VDD          0x01
  61. #define TO_GND          0x00
  62.  
  63.  
  64. /********* Values to Set The Touch Sensors ********/
  65. #define DEFAULT_CVD_THRESHOLD           3000            // 3000; Un-pressed switch value.
  66. #define DEFAULT_CVD_TRIP                                200             // 200; Difference between pressed and un-pressed switch.
  67.                                                                                                
  68. #define DEFAULT_CVD_HYST                                65              // 65; Amount to change from pressed to un-pressed.
  69.  
  70.  
  71. #define NUMBER_OF_CVD_SENSORS   0x05            // Number of Sensors to be read in the uC.

« Última modificación: 04 de Septiembre de 2013, 00:26:43 por rotting79 »
...On November 2nd, a PIC10 became self-aware and decided our fate in __delay_us ( 1 ); ...

Desconectado rotting79

  • PIC12
  • **
  • Mensajes: 91
Re: Explicacion INT_AD y Touch Capacitivo por favor....
« Respuesta #57 en: 24 de Octubre de 2013, 01:09:16 »
here is a training board for the PIC18F45K22 Training Board, which has ready to use three 10-bits analog inpits with 0.01uF on it, five PWM outputs, the whole PORTD, five CTMU Touch Buttons, four LEDs, RS232 Serial Comminication, 32k Bytes (less the bootloader firmware at the very end of the momory), seven Timers (three 8-bits, four 16-bits), SRLatch,2CCP, 2ECCP, 1ECCP (Full Bridge), Internal FVR and DAC, 1536 Bytes of SRAM, 256 Bytes EEPROM, two SPI and two I2C, two Comparators, 16MHz internal Oscillator, 16-bit instruction Word in a 2"x4" Board.


...On November 2nd, a PIC10 became self-aware and decided our fate in __delay_us ( 1 ); ...

Desconectado delirio

  • PIC12
  • **
  • Mensajes: 64
Re: Explicacion INT_AD y Touch Capacitivo por favor....
« Respuesta #58 en: 18 de Mayo de 2015, 09:46:57 »
Buenos días amigos, los sigo muy de cerca cada vez que puedo poner manos a la obra en algún proyecto, lo mio es solo hobby y últimamente he tenido poco tiempo.
Con respecto a este tema me surgen algunas dudas...

* a que le llaman canal secundario??
* es una entrada del pic que solo se usa para este fin?
* puntualmente en la porción de código que postea Nocturno, en RA0 y RA1 estará mi sensor o sector de cobre del PCB... y en RA2 que hay??

Gracias de antemano

- Fernando


 

anything