sábado, 14 de enero de 2012

Potenciómetro SPI


Muy buenas a todos, en esta entrada de blog os voy a enseñar cómo crear un potenciómetro comunicado a través del SPI, el potenciómetro que usaremos concretamente será el MCP41010.
Lo que haremos será crear una comunicación entre el PIC y el MCP mediante el bus SPI.

Como en anteriores ocasiones, tanto el PIC, como la comunicación SPI, se encuentra reflejada en mi BIBLIOTECA, por otro lado he mencionado una cosa nueva, el MCP41010, por tanto aquí os dejo su respectivo datasheet, donde podréis informaros y echar un vistazo para entenderlo.


Lo que vamos a hacer es usar una resistencia variable, será lo que utilicemos para meter datos al pic, la cual en función de los voltios que nos saque a la salida, el Pic , al ser datos analógicos, los pasará a digitales, y una vez los tenga convertidos, será el momento de, mediante fórmulas, hallar cual es la resistencia correcta en función de los voltios metidos.

Entonces el PIC irá al MCP 41010, que para que nos entendamos, es una especie de resistencia variable pero en vez de controlarla de manera analógica, se maneja de manera digital, mediante el PIC. En definitiva, el pic recibirá un voltaje, y en función de ese voltaje, dirá al MCP "ponte a 5k". Por tanto, nos mostrará a qué resistencia se está poniendo nuestra resistencia variable.

Para controlar nuestro potenciómetro utilizaremos la librería del  MCP41010. Que está incluida en la gama de librerías de “c compiler”.

Puesto que vamos a utilizar el LCD para mostrar los valores, también vamos a necesitar una librería para controlarlo.

Por lo que aquí os adjunto las dos librerías, en primer lugar la del LCD. Muy importante, nos imagináis los problemas que me han dado las librerías del LCD, yo uso para manejar este LCD la librería LCD_SIS.c, que es la única que me funcionaba. Las demás solo me dieron problemas, intenté probar usando otra placa con diferente diseño, pero tenía muy mal los zócalos y me generaba interferencias.

Así que aquí os adjunto la del LCD.SIS.c:

// flex_lcd.c

// These pins are for the Microchip PicDem2-Plus board,
// which is what I used to test the driver.  Change these
// pins to fit your own board.

#define LCD_DB4   PIN_D4
#define LCD_DB5   PIN_D5
#define LCD_DB6   PIN_D6
#define LCD_DB7   PIN_D7
//
#define LCD_RS    PIN_D0
#define LCD_RW    PIN_D1
#define LCD_E     PIN_D2

// If you only want a 6-pin interface to your LCD, then
// connect the R/W pin on the LCD to ground, and comment
// out the following line.

#define USE_LCD_RW   1

//========================================

#define lcd_type 2        // 0=5x7, 1=5x10, 2=2 lines
#define lcd_line_two 0x40 // LCD RAM address for the 2nd line


int8 const LCD_INIT_STRING[4] =
{
 0x20 | (lcd_type << 2), // Func set: 4-bit, 2 lines, 5x8 dots
 0xc,                    // Display on
 1,                      // Clear display
 6                       // Increment cursor
 };


//-------------------------------------
void lcd_send_nibble(int8 nibble)
{

// Note:  !! converts an integer expression
// to a boolean (1 or 0).
 output_bit(LCD_DB4, !!(nibble & 1));
 output_bit(LCD_DB5, !!(nibble & 2));
 output_bit(LCD_DB6, !!(nibble & 4));
 output_bit(LCD_DB7, !!(nibble & 8));

 delay_cycles(1);
 output_high(LCD_E);
 delay_us(2);
 output_low(LCD_E);
}

//-----------------------------------
// This sub-routine is only called by lcd_read_byte().
// It's not a stand-alone routine.  For example, the
// R/W signal is set high by lcd_read_byte() before
// this routine is called.

#ifdef USE_LCD_RW
int8 lcd_read_nibble(void)
{
int8 retval;
// Create bit variables so that we can easily set
// individual bits in the retval variable.
#bit retval_0 = retval.0
#bit retval_1 = retval.1
#bit retval_2 = retval.2
#bit retval_3 = retval.3

retval = 0;

output_high(LCD_E);
delay_cycles(1);

retval_0 = input(LCD_DB4);
retval_1 = input(LCD_DB5);
retval_2 = input(LCD_DB6);
retval_3 = input(LCD_DB7);

output_low(LCD_E);

return(retval);
}
#endif

//---------------------------------------
// Read a byte from the LCD and return it.

#ifdef USE_LCD_RW
int8 lcd_read_byte(void)
{
int8 low;
int8 high;

output_high(LCD_RW);
delay_cycles(1);

high = lcd_read_nibble();

low = lcd_read_nibble();

return( (high<<4) | low);
}
#endif

//----------------------------------------
// Send a byte to the LCD.
void lcd_send_byte(int8 address, int8 n)
{
output_low(LCD_RS);

#ifdef USE_LCD_RW
while(bit_test(lcd_read_byte(),7)) ;
#else
delay_us(60);
#endif

if(address)
   output_high(LCD_RS);
else
   output_low(LCD_RS);

 delay_cycles(1);

#ifdef USE_LCD_RW
output_low(LCD_RW);
delay_cycles(1);
#endif

output_low(LCD_E);

lcd_send_nibble(n >> 4);
lcd_send_nibble(n & 0xf);
}

//----------------------------
void lcd_init(void)
{
int8 i;

output_low(LCD_RS);

#ifdef USE_LCD_RW
output_low(LCD_RW);
#endif

output_low(LCD_E);

delay_ms(15);

for(i=0 ;i < 3; i++)
   {
    lcd_send_nibble(0x03);
    delay_ms(5);
   }

lcd_send_nibble(0x02);

for(i=0; i < sizeof(LCD_INIT_STRING); i++)
   {
    lcd_send_byte(0, LCD_INIT_STRING[i]);

    // If the R/W signal is not used, then
    // the busy bit can't be polled.  One of
    // the init commands takes longer than
    // the hard-coded delay of 60 us, so in
    // that case, lets just do a 5 ms delay
    // after all four of them.
    #ifndef USE_LCD_RW
    delay_ms(5);
    #endif
   }

}

//----------------------------

void lcd_gotoxy(int8 x, int8 y)
{
int8 address;

if(y != 1)
   address = lcd_line_two;
else
   address=0;

address += x-1;
lcd_send_byte(0, 0x80 | address);
}

//-----------------------------
void lcd_putc(char c)
{
 switch(c)
   {
    case '\f':
      lcd_send_byte(0,1);
      delay_ms(2);
      break;

    case '\n':
       lcd_gotoxy(1,2);
       break;

    case '\b':
       lcd_send_byte(0,0x10);
       break;

    default:
       lcd_send_byte(1,c);
       break;
   }
}

//------------------------------
#ifdef USE_LCD_RW
char lcd_getc(int8 x, int8 y)
{
char value;

lcd_gotoxy(x,y);

// Wait until busy flag is low.
while(bit_test(lcd_read_byte(),7));

output_high(LCD_RS);
value = lcd_read_byte();
output_low(lcd_RS);

return(value);
}
#endif

void lcd_setcursor_vb(short visible, short blink) {
  lcd_send_byte(0, 0xC|(visible<<1)|blink);
}


Y aquí os pongo la librería del MCP41010.

///////////////////////////////////////////////////////////////////////////
////   Library for a mcp41010-i/p                                      ////
////                                                                   ////
////   set_pot (int data);  Sets pot to new_value                      ////
////                                                                   ////
////   shutdown_pot ();  shutdown pot to save power                    ////
////                                                                   ////
///////////////////////////////////////////////////////////////////////////
////        (C) Copyright 1996,2003 Custom Computer Services           ////
//// This source code may only be used by licensed users of the CCS C  ////
//// compiler.  This source code may only be distributed to other      ////
//// licensed users of the CCS C compiler.  No other use, reproduction ////
//// or distribution is permitted without written permission.          ////
//// Derivative programs created using this software in object code    ////
//// form are not restricted in any way.                               ////
///////////////////////////////////////////////////////////////////////////

#define CS PIN_B2//para dejar libre A0
#define SCLK PIN_B1
#define SI PIN_C7

set_pot (int data) {
   BYTE i;
   BYTE cmd[2];

   cmd[0] = data;
   cmd[1] = 0x11;

   output_low(SCLK);
   output_low(CS);

   for(i=1;i<=16;++i) {
      output_bit(SI, shift_left(cmd,2,0));

      output_high(SCLK);
      output_low(SCLK);
   }
   output_high(CS);
}

shutdown_pot () {
   BYTE i;
   BYTE cmd[2];

   cmd[0] = 0;
   cmd[1] = 0x21;

   output_low(SCLK);
   output_low(CS);

   for(i=1;i<=16;++i) {
      output_bit(SI, shift_left(cmd,2,0));

      output_high(SCLK);
      output_low(SCLK);
   }
   output_high(CS);
}




Y aquí estaría el programa principal:



////////////////////////////////////////////////////////////////////////////////////
//   AUTOR:Gabi Allende                                        NOVIEMBRE/2011
////////////////////////////////////////////////////////////////////////////////////
//   PROGRAMA:    Potenciómetro                         VERSIÓN:        1.0
//   DISPOSITIVO: PIC 18F4550                           COMPILADOR:     CCS vs4.109
//   Entorno IDE: MPLAB                                 SIMULADOR:      Proteus 7.7 sp2
//   TARJETA DE APLICACIÓN: ___                         DEBUGGER:       ICD3

/////////////////////////////////////////////////////////////////////////////////                          
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
//      
//      
// Utilizar el Potenciómetro MCP41010 por el Bus SPI con el PIC 18F4550     ///////
//      
//////////////////////////////////////////////////////////////////////////////////

#include
#fuses INTHS                 //Selecciona el oscilador interno
//#FUSES HS                   //External clock
#FUSES MCLR                   //Master Clear pin enabled
//#use delay(clock=4000000)
#use delay(internal=2Mhz)   // Selecciona la velocidad del oscilador interno
#include
#include  
//#byte ucfg= 0xf6f //Para poder utilizar RC4 y RC5 (en modo TTL, sólo pueden ser entradas)

////////////////////////////////////////////////////////////////////////////////////
// VARIABLES GLOBALES //////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////

float dato,hey;
int  entrada;
int reg;
float formula;
////////////////////////////////////////////////////////////////////////////////////
// PRINCIPAL ///////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////


void main()
{  
setup_adc_ports( AN0_TO_AN1 );//habilito la entrada analógica RA0 recoge la entrada analógica
setup_adc(ADC_CLOCK_INTERNAL );//utilizo el oscilador interno para controlar la entrada analógica
//reg=entrada+entrada/10;
lcd_init();
for(;;)
{
reg=entrada;
delay_ms(100);
set_adc_channel(0);//selecciono AN0
entrada=read_adc()/30;//Divido el valor de la entrada analógica para tener menos rango de valores
if(entrada<2)
{
entrada=0;
}
set_pot(dato);
if(reg>(entrada+(entrada/30)) || reg<(entrada-(entrada/30)))//estabilización de rango al 10% (si reg es mayor que entrada mas su 10% O reg es menor que entrada -10%)
//FORMULAS PARA CALCULAR LA RESISTENCIA Y MOSTRARLA EN EL LCD
formula=entrada/1000;
//delay_MS(50);
dato=(entrada*500.0/64);
dato=dato+(formula/10);
set_pot(dato);
//if(entrada>5)
//entrada=5;
//SE MUESTRA EN EL LCD
lcd_gotoxy(1,1);
hey=dato/27.29;
printf(lcd_putc,"%0.2f K   Ohm ",hey);
lcd_gotoxy(1,2);
printf(lcd_putc,"%d Valor rango ",entrada);
delay_ms(100);
}





Aquí os dejo el vídeo demostrativo:









Y aquí os dejo la descarga de programa

No hay comentarios:

Publicar un comentario