# 12 - Motor DC + Sensor IR Sharp GP2Y0A21

> **PIC C Compiler CCS**
>
> [**PIC16F887 / PIC16F877A**](https://docs.microside.com/practicas/pic-c-compiler-ccs/pic16f887-or-pic16f877a)

<figure><img src="https://177299348-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FbyV2zAlQAiqg46a3Lr8z%2Fuploads%2FLOz1ey3F3zvpBKagPidl%2F13%20-%20Motor%20DC%20%2B%20Sensor%20IR%20Sharp%20GP2Y0A2.png?alt=media&#x26;token=5079151f-5ba4-4f1d-a4e4-53d1b58f4d84" alt=""><figcaption></figcaption></figure>

## **Introducción**

En esta práctica se implementa el control de un motor de corriente directa, usando los módulos PWM y ADC a través de un sensor SHARP para variar la velocidad. La velocidad del motor, depende de la distancia que halla entre un objeto y el sensor infrarrojo.

## **Descripción**

En esta práctica se realiza el control de velocidad de un motor DC, usando los módulos internos ADC y PWM por medio de un sensor Sharp conectado a la tarjeta **X-TRAINER** con el **PIC16F887/PIC16F877A**.

**Materiales:**

1 x Tarjeta **X-TRAINER**. [**Manuales**](https://docs.microside.com/tarjetas-de-desarrollo/x-trainer) | [**Comprar**](https://www.microside.com/shop/category/entrenadores-pic-1)

1 x Sensor IR Sharp GP2Y0A21.

1 x Motor DC 5 a 12 volts.

1 x Transistor NPN 2N3904.

1 x Diodo 1N4001.

1 x Juego de cable Dupont.

1 x Protoboard de 830 puntos.

1 x Cable micro USB.

**Software:**

Windows 8.1 o posterior.

PIC C Compiler v5.069 o posterior. [**Descarga**](http://www.ccsinfo.com/ccsfreedemo.php)

X-TRAINER Suite v1.0 [**Descarga**](https://raw.githubusercontent.com/MICROSIDE-TECHNOLOGY/microside_download_archive/main/Tools/X-TRAINER%20Suite/X-TRAINER%20Suite%20Setup.zip)

## **Procedimiento**

**1.** En el apartado de “[**Diagrama Esquemático**](#diagrama-esquematico)” selecciona el modelo de tu tarjeta, realiza las conexiones que se muestran en la imagen y conéctala a la computadora.

{% hint style="info" %}
Las conexiones mostradas en los diagramas esquemáticos son las mismas ya sea que la tarjeta **X-TRAINER** posea el **PIC16F887** o el **PIC16F877A**.
{% endhint %}

**2.** Ejecuta **PIC C Compiler**, crea un nuevo proyecto con el código fuente que se encuentra en el apartado "[**Código**](#codigo)" y compila el programa.

{% hint style="success" %}
Si tienes dudas de cómo realizar los pasos anteriores puedes encontrar un manual de **PIC C Compiler** a través del siguiente [**link**](https://docs.microside.com/practicas/ides/pic-c-compiler-ccs).
{% endhint %}

**3.** Abre el **Software X-TRAINER Suite v1.0** en el apartado **PIC16F877A/887**, selecciona el archivo .hex generado al compilar el proyecto y programa la tarjeta.

{% hint style="success" %}
Si tienes dudas de cómo realizar los pasos anteriores puedes encontrar un manual del **Software X-TRAINER Suite v1.0**. a través del siguiente[ **link**](https://docs.microside.com/tarjetas-de-desarrollo/software-x-trainer-suite).
{% endhint %}

**4.** Al terminar de programar, la tarjeta **X-TRAINER** se reiniciará automáticamente y se ejecutará el programa de esta práctica.

## **Diagrama esquemático**

Selecciona el modelo de tu tarjeta **X-TRAINER.**

{% tabs %}
{% tab title="LITE M R2" %}

<figure><img src="https://raw.githubusercontent.com/MICROSIDE-TECHNOLOGY/12-DC-Infrarrojo-CCS-PIC16F887-877A/main/Diagrama%20de%20conexi%C3%B3n/X-TRAINER_Lite-M-r2_Practica-12_PIC16F_Microside_02.png" alt=""><figcaption></figcaption></figure>
{% endtab %}

{% tab title="LITE M R3" %}

<figure><img src="https://177299348-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FbyV2zAlQAiqg46a3Lr8z%2Fuploads%2F2UrSB6WmIGtSa9OKs45R%2FMotor%20DC%20%2B%20Sensor%20IR%20Sharp%20GP2Y0A21%20PIC16F877A%20%20o%20PIC16F887%20X-TRAINER%20LITE%20M%20MICROSIDE.webp?alt=media&#x26;token=655f1e4d-13a1-4b2e-bfcd-5b628aa78e6f" alt=""><figcaption></figcaption></figure>
{% endtab %}

{% tab title="LITE F" %}

<figure><img src="https://raw.githubusercontent.com/MICROSIDE-TECHNOLOGY/12-DC-Infrarrojo-CCS-PIC16F887-877A/main/Diagrama%20de%20conexi%C3%B3n/X-TRAINER_LITE-F_Practica-12_PIC16F_Microside_01.png" alt=""><figcaption></figcaption></figure>
{% endtab %}

{% tab title="PRO R3 " %}

<figure><img src="https://177299348-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FbyV2zAlQAiqg46a3Lr8z%2Fuploads%2FJLG54SUMbdCPAkzFralQ%2F12%20Motor%20DC%20%2B%20Sensor%20IR%20Sharp%20GP2Y0A21%20X%20Trainer%20Pro%2016.png?alt=media&#x26;token=317724ca-5a7a-42d2-b71b-9ad3ccb149f0" alt=""><figcaption></figcaption></figure>
{% endtab %}

{% tab title="PRO R4" %}

<figure><img src="https://177299348-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FbyV2zAlQAiqg46a3Lr8z%2Fuploads%2F7GHgn8JrBuTis0l6MfYo%2FMotor%20DC%20%2B%20Sensor%20IR%20Sharp%20GP2Y0A21%20PIC16F877A%20%20o%20PIC16F887%20X-TRAINER%20PRO%20R4%20MICROSIDE.webp?alt=media&#x26;token=a3bdab7d-37da-4641-abb3-7038a3caf2d7" alt=""><figcaption></figcaption></figure>
{% endtab %}
{% endtabs %}

## **Código**

{% tabs %}
{% tab title="PIC16F887" %}
{% embed url="<https://gist.github.com/microside-mx/5f571cf408c332abdfdab38445fd1852>" %}
{% endtab %}

{% tab title="PIC16F877A" %}
{% embed url="<https://gist.github.com/microside-mx/7872d6c1840cb4d465f51b8576b0d9dc>" %}
{% endtab %}
{% endtabs %}

## **Descargas**

<table data-card-size="large" data-view="cards"><thead><tr><th align="center"></th><th data-hidden data-card-target data-type="content-ref"></th></tr></thead><tbody><tr><td align="center"><mark style="color:green;"><strong>Software X-TRAINER Suite v1.0</strong></mark></td><td><a href="https://raw.githubusercontent.com/MICROSIDE-TECHNOLOGY/microside_download_archive/main/Tools/X-TRAINER%20Suite/X-TRAINER%20Suite%20Setup.zip">https://raw.githubusercontent.com/MICROSIDE-TECHNOLOGY/microside_download_archive/main/Tools/X-TRAINER%20Suite/X-TRAINER%20Suite%20Setup.zip</a></td></tr><tr><td align="center"><mark style="color:green;"><strong>Proyecto</strong></mark></td><td><a href="https://github.com/MICROSIDE-TECHNOLOGY/12-DC-Infrarrojo-CCS-PIC16F887-877A/archive/refs/heads/main.zip">https://github.com/MICROSIDE-TECHNOLOGY/12-DC-Infrarrojo-CCS-PIC16F887-877A/archive/refs/heads/main.zip</a></td></tr></tbody></table>
