7-Potenciómetro SPI

En esta práctica vamos a utilizar el potenciómetro MCP41010 con el PIC 18F4550 mediante el Bus SPI .
Introduciremos valores por la entrada analógica del PIC para variar la resistencia del potenciómetro .

Veremos en un LCD la resistencia que existe entre PW0 y PB0 .



El bus interfaz con periféricos serie (SPI) fue desarrollado originalmente por Motorola a finales de los 80 en los microcontroladores serie 68000. Hoy en  día se puede encontrar en teléfonos móviles, PDA, etc,  para comunicar la CPU con los distintos periféricos.

El Bus SPI es un bus de tres líneas sobre el que se transmiten paquetes de información de 8bits. Cada dispositivo conectado al bus puede funcionar como transmisor y receptor al mismo tiempo, este tipo de comunicación se denomina full dúplex.

Hay dispositivos que solo son transmisores y otros solo receptores. Algunos, como las memorias EEPROM, son transmisores y receptores.

Los dispositivos conectados se pueden dividir en maestros y esclavos.

Los maestros son los que inician la transferencia de información y generan las señales de reloj y control.

Los esclavos son los dispositivos controlados por el maestro.

El bus utiliza las siguientes líneas:

Master Out - Slave In (MOSI): Conexión para la entrada de datos serie.

Master in- Slave out (MISO): Conexión para la salida de datos.

Clock (SCLK): Señal de reloj.

Slave select (SS): Seleccion.



Con respecto al I2C. el SPI alcanza velocidades muy superiors al utilizer lineas independientes de transmission y recepción.

En los PIC18 el SPI puede alcanzar frecuencias de 12 Mhz frente a 1Mhz del I2C.

La ventaja del I2C es que solo utiliza dos líneas, la de datos y la de reloj ( Sin contar GND) independientemente de los esclavos que tengas conectados dado que se seleccionan por software, por el contrario el SPI utiliza tres líneas para comunicarse y otra más para seleccionar.


Utilizaremos la librería del MCP41010 que viene con PIC C Compiler en “C:\Program Files (x86)\PICC\Drivers“ y así nos facilitará mucho las cosas para comunicarnos con el dispositivo.

He cambiado los pines para dejar libre RA0 que es desde donde voy a introducir los valores para variar el potenciómetro.

La librería es la siguiente:

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


///////////////////////////////////////////////////////////////////////////
////   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);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

También es necesaria la librería del LCD.

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


// 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 finalmente el programa principal.

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////////
//   AUTOR: José Antonio Pelayo                                        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 <c:\h\18F4550.h>
#fuses INTHS                 //Selecciona el oscilador interno
//#FUSES HS                   //External clock
#FUSES MCLR                   //Master Clear pin enabled
//#use delay(clock=4000000)
#use delay(internal=4000000hz)   // Selecciona la velocidad del oscilador interno
#include <LCD_flexible_1.c>
#include <mcp41010_pinescambiados.c>
//#byte ucfg= 0xf6f //Para poder utilizar RC4 y RC5 (en modo TTL, sólo pueden ser entradas)

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

float dato;
long int  entrada;
float formula;
////////////////////////////////////////////////////////////////////////////////////
// PRINCIPAL ///////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////


void main()
{    
setup_adc_ports( AN0_TO_AN1 );//habilito la entrada analógica
setup_adc(ADC_CLOCK_INTERNAL );//utilizo el oscilador interno para controlar la entrada analógica
lcd_init();
for(;;)
{
set_adc_channel(1);//selecciono AN0
entrada=read_adc()/2;//Divido el valor de la entrada analógica para tener menos rango de valores
set_pot(entrada);
//FORMULAS PARA CALCULAR LA RESISTENCIA Y MOSTRARLA EN EL LCD
formula=entrada/100;
dato=(entrada*10000.0/256);
dato=dato+(formula/10);
//SE MUESTRA EN EL LCD
lcd_gotoxy(1,1);
printf(lcd_putc,"%0.3f   Ohm ",dato);
lcd_gotoxy(1,2);
printf(lcd_putc,"%ld Valor adc port",entrada);
delay_ms(100);
}
}



////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Simulación en proteus:




Programado en tarjeta:








                                        DESCARGA PROGRAMA