lunes, 9 de junio de 2014

(MPLAB-IDE) Digital/Analog converter MCP3204 SPI

In this practice I am going to explain how to read an analog value from a knob and convert it to a digital signal to display these real value in a LCD screen using the SPI serial interface to comunicate the DAC with the PIC18F4550.

To perform this conversion, I will use a MCP3204 configured with SPI interface. This device needs a library (MCP3204.c) to be programmed more easily.


The first step is build a scheme in Proteus:













Program:


I started configurating the library with the pins I want to use:


#define MCP3204_CLK PIN_B1
#define MCP3204_DOUT PIN_B0
#define MCP3204_DIN PIN_C7
#define MCP3204_CS PIN_A0


And this is the code i used:


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

// CABECERA /////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
#include <18f4550.h>
#fuses XT,MCLR
#use delay(clock=20000000)
#include <LCD_flexible.c>
#include <C:\Users\Talavera\Desktop\Clase\New Folder\Luis\MPLAB\MCP_3204_SPI\mcp3204.c>
//////////////////////////////////////////////////////////////////////////
// VARIABLES GLOBALES ///////////////////////////
//////////////////////////////////////////////////////////////////////////
long int valor;
float voltaje;
//////////////////////////////////////////////////////////////////////////
// FUNCIONES ////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////

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

// PRINCIPAL //////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
void main()
{
lcd_init();
adc_init();
lcd_putc("CAD MCP 3204");
for(;;)
{
valor = read_analog(0);
voltaje = (valor*5.0)/4096;
LCD_GOTOXY(1,2);
printf(lcd_putc,"Voltaje = %f V", voltaje);
}

}
//////////////////////////////////////////////////////////////////////////

Is time to simulate our program in Proteus and see the results:














We can see in the LCD screen how this voltaje value matches with the voltimeter value we control with a knob.

No hay comentarios:

Publicar un comentario