This page has been translated from English

Una guía para principiantes de programación microcontrolador PIC de Microchip

Tags: , ,

Microchip Microcontrolador PIC MCU de 8 bits Este tutorial te guiará en el proceso de crear su primer circuito de microcontroladores PIC de Microchip base.

Se cubrirá todo lo que hay que hacer para escribir un programa simple que parpadea un LED, en un paso a paso "hands-on" método. Todo el software utilizado en este tutorial es totalmente gratuito. Todo lo que necesitas es un simple programador JDM (siga leyendo para detials), y que son buenos para ir!



He aquí una lista de lo que debe conseguir antes de empezar:

  1. A PIC16F micro-controlador. usted puede comprar uno en tiendas de electrónica o en la web.
    En esta guía voy a utilizar un PIC16F684 porque es barato, pequeño y fácil de usar.
  2. el desarrollo de placa
  3. PC con sistema operativo Windows XP/2000. su PC debe tener una salida COM serie.
  4. Mujer sencilla de cable macho de cable serie 9 (DB9 PLUG)
  5. Una lista de algunos de los componentes.
  6. PIC Programmer

    usted puede comprar todo lo anterior aquí

Bedore de empezar, he aquí una breve explicación del proceso de desarrollo:

En primer lugar, vamos a escribir nuestro código en MPLAB - Entorno de desarrollo, lo que compilar y generar el código y obtener un archivo HEX.
con el fin de transferir el archivo hex al microcontrolador wi'll utilizar un programador PIC, un simple circuito electrónico que puede obtener el código de nuestro PC a través de
el puerto serie y el programa a la memoria Flash PIC programa.

Vamos a empezar, si usted tiene alguna pregunta por favor use nuestro sysyem foro.

  1. Descargar e instalar el MPLAB IDE de la página web de Microchip.
    En el proceso de instalación se le pedirá para instalar un compilador PICC desarrollado por la compañía PIC HI-TECH.
    Instalarlo!, Es un compilador de C que nos permiten wite nuestro código en C insted de ensamblador.
  2. Para programar el código para el microcontrolador debemos construir un programador. hay un montón de diseños para los programadores de la web.
    Yo recomiendo a la siguiente.
  3. Descargar WinPic800. es un buen programa que controla el programador. Soporta la mayoría de los microcontroladores de Microchip.
  4. Construir el siguiente circuito. se trata de un circuito simple que la tensión de 5V suply a la PIC y se conecta a la programación
    líneas a las piernas apropiate del microcontrolador.

Breve explicación sobre el circuito:

J1 - Conector de alimentación.
SW1 - interruptor de alimentación principal
D1 -

U1-5V regulador de voltaje.
C1, C2 -

D2 - LED
SW2 - usuario restablecer

matrix1

matrix2

matrix3

compruebe las conexiones con cuidado!

Nuestro circuito necesita fuente de alimentación. Un generador de energía es buena recomanded, pero si usted no tiene una y no quiere comprar uno, puede utilizar un transformador sencillo
que puede suministrar por lo menos 500 mA @ 9V. Es mejor que sea fusionado, porque usted no quiere quemar su transformador si por error se conecta a + y -.

transfformator

Conecte el transformador a su circuito y encienda el interruptor de encendido. el led verde se encenderá. Conecte un cable serial de que el puerto COM para el programador de PIC, y luego conectar el PIC programador ICSP cabecera de la placa con el conector de la derecha.

Si todo está conectado derecho que debería tener este aspecto:

matrixpgm

Escribir el programa PIC primero

  • Iniciar el programa MPLAB
  • Crear un nuevo proyecto (Proyectos-> Nuevo Proyecto)
  • Elija su microcontrolador de la lista (en nuestro caso, PIC16F684)

mplab-hitechpic

  • A continuación, seleccione el HI-TECH Univeral Toolsuite, y el compilador HI-TECH C (ver imagen abajo)
  • En la siguiente pantalla, el nombre de su proyecto y haga clic en "Siguiente".
  • Haga clic en "Siguiente" dos veces más, y eso es todo, su nuevo proyecto ya está creado!
  • Ahora vamos a añadir un nuevo archivo al proyecto que acabamos de crear. Haga clic en Archivo-> Nuevo.

En primer lugar PIC código de programa

Escriba el siguiente código en el archivo que acaba de crear:

include <pic.h>

void main(void)
{
	unsigned int i;
	TRISC=0x00;

	while(1)
	{
		for (i=0; i<0xFFFF; i++)
			PORTC=0xFF;

		for (i=0; i<0xFFFF; i++)
			PORTC=0x00;

	}

}
Explanation of the above code

include <pic.h>

This command instructs the compiler to include relevant code about the PIC we’re using.

void
main( void ) {

Definition of the main function of the code.

unsigned int i;

creation of an integer variable, which will be used later in a delay loop.

TRISC=0×00;

Set the TRISC register. The TRISC register is an 8 bit register which sets the states of the PORTC pins of the PIC. A pin state can either be 0, which means an output, or 1, which means an input. We define all the port pins as outputs, and hence the 0×00 value.

while (1){

Since we want the LED to blink endlessly, we create an endless loop (1 states ‘true’ in programming, so the following line reads as “while (true)” which is computerish for ‘always’ :) .

for (i=0; i<0xFFFF; i++)

The purpose of this loop is to create a delay between the time the LED turns on, and when it turns off. This is not an optimal way to create a delay, but it will suffice for now.

PORTC=0xFF;

The PORTC register contains the values of it’s input\output legs. Since we initialized the port to be an output port, this command will set the value of every leg to 1 (remember that 0xFF is 11111111 in binary). In other words the MCU will rise the voltage in those legs to VCC, or 5v in our case, and hence cause the led to turn on.

for(i=0; i<0xFFFF; i++)

PORTC=0×00;

The purpose of the above lines is the same as the last ones – to create a delay. But this time we change the state of the PORTC register to low (0×00) which will bring the voltage of the PORTC legs to ground, and hentch turn off the led.

Having these ‘turn off’ and ‘turn on’ commands repeated infinitely by the while command will cause the led to blink.


  • Press File->Save and save the file as src1.c
  • Press View-Project and you will the a window containing the directory tree of the project we have created.

mp3

  • Right click on the directory and choose Add Files, and then Source File
  • Now the project is ready for compilation. Compile it by clicking on the Project->Compile  menu
  • If the compilation process finished without errors, you should have a HEX file created in the project directory. This file contains the binary form of your code – and burning it to the PIC will make the PIC run your code.
  • To burn the PIC, load the winpic800 program.
  • Now, we will configure the program for the first use.

winpichardware

  • Press on Settings->Hardware and choose the comport to which you connected your JDM Programmer .

winpicsoftware

  • Set the rest of the settings as shown in the image about.
  • In the top right of the program, choose the PIC model you are using (in the image PIC 16F684 is used)
  • Now we will check that the program can successfully connect to the PIC Programmer we are using.

winpictest

  • Click on the button marked Escriba el siguiente código en el archivo que acaba de crear:
    include <pic.h>
    
    void main(void)
    {
    	unsigned int i;
    	TRISC=0x00;
    
    	while(1)
    	{
    		for (i=0; i<0xFFFF; i++)
    			PORTC=0xFF;
    
    		for (i=0; i<0xFFFF; i++)
    			PORTC=0x00;
    
    	}
    
    }
    
    Explanation of the above code

    include <pic.h>

    This command instructs the compiler to include relevant code about the PIC we’re using.

    void
    main( void ) {

    Definition of the main function of the code.

    unsigned int i;

    creation of an integer variable, which will be used later in a delay loop.

    TRISC=0×00;

    Set the TRISC register. The TRISC register is an 8 bit register which sets the states of the PORTC pins of the PIC. A pin state can either be 0, which means an output, or 1, which means an input. We define all the port pins as outputs, and hence the 0×00 value.

    while (1){

    Since we want the LED to blink endlessly, we create an endless loop (1 states ‘true’ in programming, so the following line reads as “while (true)” which is computerish for ‘always’ :) .

    for (i=0; i<0xFFFF; i++)

    The purpose of this loop is to create a delay between the time the LED turns on, and when it turns off. This is not an optimal way to create a delay, but it will suffice for now.

    PORTC=0xFF;

    The PORTC register contains the values of it’s input\output legs. Since we initialized the port to be an output port, this command will set the value of every leg to 1 (remember that 0xFF is 11111111 in binary). In other words the MCU will rise the voltage in those legs to VCC, or 5v in our case, and hence cause the led to turn on.

    for(i=0; i<0xFFFF; i++)

    PORTC=0×00;

    The purpose of the above lines is the same as the last ones – to create a delay. But this time we change the state of the PORTC register to low (0×00) which will bring the voltage of the PORTC legs to ground, and hentch turn off the led.

    Having these ‘turn off’ and ‘turn on’ commands repeated infinitely by the while command will cause the led to blink.


    • Press File->Save and save the file as src1.c
    • Press View-Project and you will the a window containing the directory tree of the project we have created.

    mp3

    • Right click on the directory and choose Add Files, and then Source File
    • Now the project is ready for compilation. Compile it by clicking on the Project->Compile  menu
    • If the compilation process finished without errors, you should have a HEX file created in the project directory. This file contains the binary form of your code – and burning it to the PIC will make the PIC run your code.
    • To burn the PIC, load the winpic800 program.
    • Now, we will configure the program for the first use.

    winpichardware

    • Press on Settings->Hardware and choose the comport to which you connected your JDM Programmer .

    winpicsoftware

    • Set the rest of the settings as shown in the image about.
    • In the top right of the program, choose the PIC model you are using (in the image PIC 16F684 is used)
    • Now we will check that the program can successfully connect to the PIC Programmer we are using.

    winpictest

    • Click on the button marked in the image above.
    • If you get the same message as in the picture above, everything is connected properly and the hardware is recognized. Otherwise, check your connections and COM port settings.
    • Connect the PIC programmer to the breadboard again, and turn on the circuit. The led should turn on again.

    winpic detect

    • Now we will check that the software successfully recognizes the PIC MCU we are using. Click on the icon marked in the image above, and the right PIC model should be detected.
    • Now choose the file we want to program the PIC with (it’s the same HEX file that was created by compiling our code in the previous level)

    winpic set

    • Now press on the Settings button (marked in the image above).  This dialog allows us to configure several run-parameters of the PIC. For example – the internal clock speed, whether to use an internal or external clock, etc.
    • Choose INTOSCIO – Use the internal PIC clock.
    • Remove the marking of the WDTE.

    winpic programming

    • Now everything is ready to programming your PIC with the code you have written and compiled!
    • Press on the red button, marked on the picture above. The program will be written to the PIC, and if everything is correct, upon turning on the circuit the LED will begin to blink!

    Subscribe now via RSS and never miss a post!

Leave a Comment