Está en la página 1de 57

“AÑO DE LA UNIDAD, LA PAZ Y EL DESARROLLO”

LABORATORIO 03 - ADC
INTEGRANTES:

Maza Torres, Diego Jonathan U18302032


Carranza Tello Moises Maykg Pavel U20202203
Johan Andre Lescano U21215658
Julio Antonio Velarde Lloclla U21218772
Antoni Maycol Rodríguez Pinedo U20229296
Cristhian soto Ortiz U19214971
Nallely Alvarado Purizaca
U20230040

(10312): MICROCONTROLADORES

DOCENTE: Carlos Alberto Olivera

10 de Julio 2023
PRIMER PROGRAMA:
En la primera pregunta deben elaborar un programa en C, el cual utilice el ADC para
leer un potenciómetro periódicamente, una LCD alfanumérica para mostrar el voltaje
medido y el Timer0 para sincronizar la lectura y escritura de datos

Descripción general del programa:


El funcionamiento del programa consiste en medir la variación de voltaje producida por una
señal analógica de entrada producida por un potenciómetro cuya variación se verá reflejada
como salida digital en un LCD 16X2.

LIBRERIAS:

*ADC_Libreria.c // (SOURCE FILES)

#include <xc.h>

void ADC_init(void){
ADCON1bits.PCFG = 0; // Todos los puertos Analogicos
ADCON1bits.VCFG = 0;
ADCON0 = 0;
ADCON2bits.ACQT = 3;
ADCON2bits.ADCS = 5;
ADCON2bits.ADFM = 1;
}

unsigned int ADC_read(unsigned char ch){


if(ch > 13)return 0;
ADCON0 = 0;
ADCON0 = (ch<<2);
ADCON0bits.ADON = 1;
ADCON0bits.GO_DONE = 1;
while(ADCON0bits.GO_DONE == 1);
ADCON0bits.ADON = 0;
return ADRES;
}

*LCD_CONFIG.c // (SOURCE FILES)

#include <xc.h>
// PIC18F4550 Configuration Bit Settings

// 'C' source line config statements

// CONFIG1L
#pragma config PLLDIV = 1 // PLL Prescaler Selection bits (No prescale (4 MHz
oscillator input drives PLL directly))

2
#pragma config CPUDIV = OSC1_PLL2// System Clock Postscaler Selection bits ([Primary
Oscillator Src: /1][96 MHz PLL Src: /2])
#pragma config USBDIV = 1 // USB Clock Selection bit (used in Full-Speed USB mode
only; UCFG:FSEN = 1) (USB clock source comes directly from the primary oscillator block
with no postscale)

// CONFIG1H
#pragma config FOSC = INTOSCIO_EC// Oscillator Selection bits (Internal oscillator, port
function on RA6, EC used by USB (INTIO))
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock
Monitor disabled)
#pragma config IESO = OFF // Internal/External Oscillator Switchover bit (Oscillator
Switchover mode disabled)

// CONFIG2L
#pragma config PWRT = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOR = OFF // Brown-out Reset Enable bits (Brown-out Reset enabled
in hardware only (SBOREN is disabled))
#pragma config BORV = 3 // Brown-out Reset Voltage bits (Minimum setting 2.05V)
#pragma config VREGEN = OFF // USB Voltage Regulator Enable bit (USB voltage
regulator disabled)

// CONFIG2H
#pragma config WDT = ON // Watchdog Timer Enable bit (WDT enabled)
#pragma config WDTPS = 32768 // Watchdog Timer Postscale Select bits (1:32768)

// CONFIG3H
#pragma config CCP2MX = ON // CCP2 MUX bit (CCP2 input/output is multiplexed
with RC1)
#pragma config PBADEN = ON // PORTB A/D Enable bit (PORTB<4:0> pins are
configured as analog input channels on Reset)
#pragma config LPT1OSC = OFF // Low-Power Timer 1 Oscillator Enable bit (Timer1
configured for higher power operation)
#pragma config MCLRE = ON // MCLR Pin Enable bit (MCLR pin enabled; RE3 input
pin disabled)

// CONFIG4L
#pragma config STVREN = ON // Stack Full/Underflow Reset Enable bit (Stack
full/underflow will cause Reset)
#pragma config LVP = OFF // Single-Supply ICSP Enable bit (Single-Supply ICSP
enabled)
#pragma config ICPRT = OFF // Dedicated In-Circuit Debug/Programming Port
(ICPORT) Enable bit (ICPORT disabled)
#pragma config XINST = OFF // Extended Instruction Set Enable bit (Instruction set
extension and Indexed Addressing mode disabled (Legacy mode))

// CONFIG5L
#pragma config CP0 = OFF // Code Protection bit (Block 0 (000800-001FFFh) is not
code-protected)

2
#pragma config CP1 = OFF // Code Protection bit (Block 1 (002000-003FFFh) is not
code-protected)
#pragma config CP2 = OFF // Code Protection bit (Block 2 (004000-005FFFh) is not
code-protected)
#pragma config CP3 = OFF // Code Protection bit (Block 3 (006000-007FFFh) is not
code-protected)

// CONFIG5H
#pragma config CPB = OFF // Boot Block Code Protection bit (Boot block (000000-
0007FFh) is not code-protected)
#pragma config CPD = OFF // Data EEPROM Code Protection bit (Data EEPROM is
not code-protected)

// CONFIG6L
#pragma config WRT0 = OFF // Write Protection bit (Block 0 (000800-001FFFh) is not
write-protected)
#pragma config WRT1 = OFF // Write Protection bit (Block 1 (002000-003FFFh) is not
write-protected)
#pragma config WRT2 = OFF // Write Protection bit (Block 2 (004000-005FFFh) is not
write-protected)
#pragma config WRT3 = OFF // Write Protection bit (Block 3 (006000-007FFFh) is not
write-protected)

// CONFIG6H
#pragma config WRTC = OFF // Configuration Register Write Protection bit
(Configuration registers (300000-3000FFh) are not write-protected)
#pragma config WRTB = OFF // Boot Block Write Protection bit (Boot block (000000-
0007FFh) is not write-protected)
#pragma config WRTD = OFF // Data EEPROM Write Protection bit (Data EEPROM is
not write-protected)

// CONFIG7L
#pragma config EBTR0 = OFF // Table Read Protection bit (Block 0 (000800-001FFFh)
is not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF // Table Read Protection bit (Block 1 (002000-003FFFh)
is not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF // Table Read Protection bit (Block 2 (004000-005FFFh)
is not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF // Table Read Protection bit (Block 3 (006000-007FFFh)
is not protected from table reads executed in other blocks)

// CONFIG7H
#pragma config EBTRB = OFF // Boot Block Table Read Protection bit (Boot block
(000000-0007FFh) is not protected from table reads executed in other blocks)

// #pragma config statements should precede project file includes.


// Use project enums instead of #define for ON and OFF.

2
*LCD_LIBRERIA.c // (SOURCE FILES)

#include <xc.h>
#include "LCD_LIBRERIA.h"
#include "LCD_CONFIG.h"
#include <stdint.h>

//18 Ciclcos de Instrucción


void DelayFor18TCY(void){
NOP();
NOP();
NOP();
NOP();
NOP();
NOP();
NOP();
NOP();
NOP();
NOP();
NOP();
NOP();
return;
}

//Mínimo 15ms
void DelayPORXLCD(void){
__delay_ms(15);
return;
}

//Mínimo 5ms
void DelayXLCD(void){
__delay_ms(5);
return;
}
//Posición de lo que deseo mostrar
void LCD_gotoXY(int x,int y)
{
if ( x>0 ){WriteCmdXLCD(LINEA2+y);}
else {WriteCmdXLCD(LINEA1+y);}
return;
}

//Escribir Caracteres
void LCD_WriteChr_CGRAM( const char *buffer, unsigned char Addres)
{ unsigned char i=0;

SetCGRamAddr(Addres*8);
for (i=0;i<8;i++) // Write data to LCD up to null

2
{ WriteDataXLCD(*buffer); // Write character to LCD
++buffer;
}
}

//Configuración del LCD e inicialización


void OpenXLCD(unsigned char lcdtype)
{
// The data bits must be either a 8-bit port or the upper or
// lower 4-bits of a port. These pins are made into inputs
#ifdef BIT8 // 8-bit mode, use whole port
DATA_PORT = 0;
TRIS_DATA_PORT = 0x00;
#else // 4-bit mode
#ifdef UPPER // Upper 4-bits of the port
DATA_PORT &= 0x0f;
TRIS_DATA_PORT &= 0x0F;
#else // Lower 4-bits of the port
DATA_PORT &= 0xf0;
TRIS_DATA_PORT &= 0xF0;
#endif
#endif
TRIS_RW = 0; // All control signals made outputs
TRIS_RS = 0;
TRIS_E = 0;
RW_PIN = 0; // R/W pin made low
RS_PIN = 0; // Register select pin made low
E_PIN = 0; // Clock pin made low

// Delay for 15ms to allow for LCD Power on reset


DelayPORXLCD();
//-------------------reset procedure through software----------------------
WriteCmdXLCD(0x30);
__delay_us(1);

WriteCmdXLCD(0x30);
__delay_us(1);

WriteCmdXLCD(0x32);
while( BusyXLCD() );
//------------------------------------------------------------------------------------------

// Set data interface width, # lines, font


while(BusyXLCD()); // Wait if LCD busy
WriteCmdXLCD(lcdtype); // Function set cmd

// Turn the display on then off


while(BusyXLCD()); // Wait if LCD busy

2
WriteCmdXLCD(DOFF&CURSOR_OFF&BLINK_OFF); // Display OFF/Blink
OFF
while(BusyXLCD()); // Wait if LCD busy
WriteCmdXLCD(DON&CURSOR_ON&BLINK_ON); // Display ON/Blink ON

// Clear display
while(BusyXLCD()); // Wait if LCD busy
WriteCmdXLCD(0x01); // Clear display

// Set entry mode inc, no shift


while(BusyXLCD()); // Wait if LCD busy
WriteCmdXLCD(SHIFT_CUR_LEFT); // Entry Mode

// Set DD Ram address to 0


while(BusyXLCD()); // Wait if LCD busy
SetDDRamAddr(0x80); // Set Display data ram address to 0

return;
}

//Escribe una cadena desde la memoria del Programa


void putrsXLCD(const char *buffer)
{
while(*buffer) // Write data to LCD up to null
{
while(BusyXLCD()); // Wait while LCD is busy
WriteDataXLCD(*buffer); // Write character to LCD
buffer++; // Increment buffer
}
return;
}

//Escribe una cadena desde la memoria de datos


void putsXLCD(char *buffer)
{
while(*buffer) // Write data to LCD up to null
{
while(BusyXLCD()); // Wait while LCD is busy
WriteDataXLCD(*buffer); // Write character to LCD
buffer++; // Increment buffer
}
return;
}

//Envia comandos al LCD


void WriteCmdXLCD(unsigned char cmd)
{
#ifdef BIT8 // 8-bit interface
TRIS_DATA_PORT = 0; // Data port output
DATA_PORT = cmd; // Write command to data port

2
RW_PIN = 0; // Set the control signals
RS_PIN = 0; // for sending a command
DelayFor18TCY();
E_PIN = 1; // Clock the command in
DelayFor18TCY();
E_PIN = 0;
DelayFor18TCY();
TRIS_DATA_PORT = 0xff; // Data port input
#else // 4-bit interface
#ifdef UPPER // Upper nibble interface
TRIS_DATA_PORT &= 0x0f;
DATA_PORT &= 0x0f;
DATA_PORT |= cmd&0xf0;
#else // Lower nibble interface
TRIS_DATA_PORT &= 0xf0;
DATA_PORT &= 0xf0;
DATA_PORT |= (cmd>>4)&0x0f;
#endif
RW_PIN = 0; // Set control signals for command
RS_PIN = 0;
DelayFor18TCY();
E_PIN = 1; // Clock command in
DelayFor18TCY();
E_PIN = 0;
#ifdef UPPER // Upper nibble interface
DATA_PORT &= 0x0f;
DATA_PORT |= (cmd<<4)&0xf0;
#else // Lower nibble interface
DATA_PORT &= 0xf0;
DATA_PORT |= cmd&0x0f;
#endif
DelayFor18TCY();
E_PIN = 1; // Clock command in
DelayFor18TCY();
E_PIN = 0;
#ifdef UPPER // Make data nibble input
TRIS_DATA_PORT |= 0xf0;
#else
TRIS_DATA_PORT |= 0x0f;
#endif
#endif
return;
}

//Lee la dirección del byte desde el LCD


unsigned char ReadAddrXLCD(void)
{
char data; // Holds the data retrieved from the LCD

#ifdef BIT8 // 8-bit interface

2
RW_PIN = 1; // Set control bits for the read
RS_PIN = 0;
DelayFor18TCY();
E_PIN = 1; // Clock data out of the LCD controller
DelayFor18TCY();
data = DATA_PORT; // Save the data in the register
E_PIN = 0;
RW_PIN = 0; // Reset the control bits
#else // 4-bit interface
RW_PIN = 1; // Set control bits for the read
RS_PIN = 0;
DelayFor18TCY();
E_PIN = 1; // Clock data out of the LCD controller
DelayFor18TCY();
#ifdef UPPER // Upper nibble interface
data = DATA_PORT&0xf0; // Read the nibble into the upper nibble of data
#else // Lower nibble interface
data = (DATA_PORT<<4)&0xf0; // Read the nibble into the upper nibble of data
#endif
E_PIN = 0; // Reset the clock
DelayFor18TCY();
E_PIN = 1; // Clock out the lower nibble
DelayFor18TCY();
#ifdef UPPER // Upper nibble interface
data |= (DATA_PORT>>4)&0x0f; // Read the nibble into the lower nibble of data
#else // Lower nibble interface
data |= DATA_PORT&0x0f; // Read the nibble into the lower nibble of data
#endif
E_PIN = 0;
RW_PIN = 0; // Reset the control lines
#endif
return (data&0x7f); // Return the address, Mask off the busy bit
}

//Establece la dirección de los caracteres generados


void SetCGRamAddr(unsigned char CGaddr)
{
while(BusyXLCD());
#ifdef BIT8 // 8-bit interface
TRIS_DATA_PORT = 0; // Make data port ouput
DATA_PORT = CGaddr | 0b01000000; // Write cmd and address to port
RW_PIN = 0; // Set control signals
RS_PIN = 0;
DelayFor18TCY();
E_PIN = 1; // Clock cmd and address in
DelayFor18TCY();
E_PIN = 0;
DelayFor18TCY();
TRIS_DATA_PORT = 0xff; // Make data port inputs
#else // 4-bit interface

2
#ifdef UPPER // Upper nibble interface
TRIS_DATA_PORT &= 0x0f; // Make nibble input
DATA_PORT &= 0x0f; // and write upper nibble
DATA_PORT |= ((CGaddr | 0b01000000) & 0xf0);
#else // Lower nibble interface
TRIS_DATA_PORT &= 0xf0; // Make nibble input
DATA_PORT &= 0xf0; // and write upper nibble
DATA_PORT |= (((CGaddr |0b01000000)>>4) & 0x0f);
#endif
RW_PIN = 0; // Set control signals
RS_PIN = 0;
__delay_us(1);
E_PIN = 1; // Clock cmd and address in
__delay_us(1);
E_PIN = 0;
#ifdef UPPER // Upper nibble interface
DATA_PORT &= 0x0f; // Write lower nibble
DATA_PORT |= ((CGaddr<<4)&0xf0);
#else // Lower nibble interface
DATA_PORT &= 0xf0; // Write lower nibble
DATA_PORT |= (CGaddr&0x0f);
#endif
__delay_us(1);
E_PIN = 1; // Clock cmd and address in
__delay_us(1);
E_PIN = 0;
#ifdef UPPER // Upper nibble interface
TRIS_DATA_PORT |= 0xf0; // Make inputs
#else // Lower nibble interface
TRIS_DATA_PORT |= 0x0f; // Make inputs
#endif
#endif
return;
}

//Configuración de la dirección de los datos


void SetDDRamAddr(unsigned char DDaddr)
{
#ifdef BIT8 // 8-bit interface
TRIS_DATA_PORT = 0; // Make port output
DATA_PORT = DDaddr | 0b10000000; // Write cmd and address to port
RW_PIN = 0; // Set the control bits
RS_PIN = 0;
DelayFor18TCY();
E_PIN = 1; // Clock the cmd and address in
DelayFor18TCY();
E_PIN = 0;
DelayFor18TCY();
TRIS_DATA_PORT = 0xff; // Make port input
#else // 4-bit interface

2
#ifdef UPPER // Upper nibble interface
TRIS_DATA_PORT &= 0x0f; // Make port output
DATA_PORT &= 0x0f; // and write upper nibble
DATA_PORT |= ((DDaddr | 0b10000000) & 0xf0);
#else // Lower nibble interface
TRIS_DATA_PORT &= 0xf0; // Make port output
DATA_PORT &= 0xf0; // and write upper nibble
DATA_PORT |= (((DDaddr | 0b10000000)>>4) & 0x0f);
#endif
RW_PIN = 0; // Set control bits
RS_PIN = 0;
DelayFor18TCY();
E_PIN = 1; // Clock the cmd and address in
DelayFor18TCY();
E_PIN = 0;
#ifdef UPPER // Upper nibble interface
DATA_PORT &= 0x0f; // Write lower nibble
DATA_PORT |= ((DDaddr<<4)&0xf0);
#else // Lower nibble interface
DATA_PORT &= 0xf0; // Write lower nibble
DATA_PORT |= (DDaddr&0x0f);
#endif
DelayFor18TCY();
E_PIN = 1; // Clock the cmd and address in
DelayFor18TCY();
E_PIN = 0;
#ifdef UPPER // Upper nibble interface
TRIS_DATA_PORT |= 0xf0; // Make port input
#else // Lower nibble interface
TRIS_DATA_PORT |= 0x0f; // Make port input
#endif
#endif
return;
}

//Lee Datos del LDC


char ReadDataXLCD(void)
{
char data;

#ifdef BIT8 // 8-bit interface


RS_PIN = 1; // Set the control bits
RW_PIN = 1;
DelayFor18TCY();
E_PIN = 1; // Clock the data out of the LCD
DelayFor18TCY();
data = DATA_PORT; // Read the data
E_PIN = 0;
RS_PIN = 0; // Reset the control bits
RW_PIN = 0;

2
#else // 4-bit interface
RW_PIN = 1;
RS_PIN = 1;
DelayFor18TCY();
E_PIN = 1; // Clock the data out of the LCD
DelayFor18TCY();
#ifdef UPPER // Upper nibble interface
data = DATA_PORT&0xf0; // Read the upper nibble of data
#else // Lower nibble interface
data = (DATA_PORT<<4)&0xf0; // read the upper nibble of data
#endif
E_PIN = 0; // Reset the clock line
DelayFor18TCY();
E_PIN = 1; // Clock the next nibble out of the LCD
DelayFor18TCY();
#ifdef UPPER // Upper nibble interface
data |= (DATA_PORT>>4)&0x0f; // Read the lower nibble of data
#else // Lower nibble interface
data |= DATA_PORT&0x0f; // Read the lower nibble of data
#endif
E_PIN = 0;
RS_PIN = 0; // Reset the control bits
RW_PIN = 0;
#endif
return(data); // Return the data byte
}

//Escribe bytes en el LCD


void WriteDataXLCD(char data)
{
while(BusyXLCD());
#ifdef BIT8 // 8-bit interface
TRIS_DATA_PORT = 0; // Make port output
DATA_PORT = data; // Write data to port
RS_PIN = 1; // Set control bits
RW_PIN = 0;
DelayFor18TCY();
E_PIN = 1; // Clock data into LCD
DelayFor18TCY();
E_PIN = 0;
RS_PIN = 0; // Reset control bits
TRIS_DATA_PORT = 0xff; // Make port input
#else // 4-bit interface
#ifdef UPPER // Upper nibble interface
TRIS_DATA_PORT &= 0x0f;
DATA_PORT &= 0x0f;
DATA_PORT |= data&0xf0;
#else // Lower nibble interface
TRIS_DATA_PORT &= 0xf0;
DATA_PORT &= 0xf0;

2
DATA_PORT |= ((data>>4)&0x0f);
#endif
RS_PIN = 1; // Set control bits
RW_PIN = 0;
DelayFor18TCY();
E_PIN = 1; // Clock nibble into LCD
DelayFor18TCY();
E_PIN = 0;
#ifdef UPPER // Upper nibble interface
DATA_PORT &= 0x0f;
DATA_PORT |= ((data<<4)&0xf0);
#else // Lower nibble interface
DATA_PORT &= 0xf0;
DATA_PORT |= (data&0x0f);
#endif
DelayFor18TCY();
E_PIN = 1; // Clock nibble into LCD
DelayFor18TCY();
E_PIN = 0;
#ifdef UPPER // Upper nibble interface
TRIS_DATA_PORT |= 0xf0;
#else // Lower nibble interface
TRIS_DATA_PORT |= 0x0f;
#endif
#endif
return;
}

//Está el LCD ocupado?


unsigned char BusyXLCD(void)
{
RW_PIN = 1; // Set the control bits for read
RS_PIN = 0;
DelayFor18TCY();
E_PIN = 1; // Clock in the command
DelayFor18TCY();
#ifdef BIT8 // 8-bit interface
if(DATA_PORT&0x80) // Read bit 7 (busy bit)
{ // If high
E_PIN = 0; // Reset clock line
RW_PIN = 0; // Reset control line
return 1; // Return TRUE
}
else // Bit 7 low
{
E_PIN = 0; // Reset clock line
RW_PIN = 0; // Reset control line
return 0; // Return FALSE
}
#else // 4-bit interface

2
#ifdef UPPER // Upper nibble interface
if(DATA_PORT&0x80)
#else // Lower nibble interface
if(DATA_PORT&0x08)
#endif
{
E_PIN = 0; // Reset clock line
DelayFor18TCY();
E_PIN = 1; // Clock out other nibble
DelayFor18TCY();
E_PIN = 0;
RW_PIN = 0; // Reset control line
return 1; // Return TRUE
}
else // Busy bit is low
{
E_PIN = 0; // Reset clock line
DelayFor18TCY();
E_PIN = 1; // Clock out other nibble
DelayFor18TCY();
E_PIN = 0;
RW_PIN = 0; // Reset control line
return 0; // Return FALSE
}
#endif
}

*LCD_PRINCIPAL.c // (SOURCE FILES)

#include <xc.h>
#include <stdio.h>
#include <stdint.h>
#include "LCD_CONFIG.h"
#include "LCD_LIBRERIA.h"
#include "ADC_libreria.h"

// 10 bit -> 2^10= 1024, 12 bit 2^12, 8 bits 2^8

//Voltaje de referencia 5v -> adc = 5/ 1024 = 4.8mV 1bit -> 3.3V

//20Mhz -> 1/20MHz = 50ns : Tosc


// TAD > 0.7us
// 8Tosc -> 8*50ns = 0.4us
// 16Tosc -> 16*50ns = 0.8us : TAD
// 2.45us < xTAD
//TACQ = 6*TAD = 6*0.8us = 4.8us > 2.45us

//

2
void OSC_Init(void)
{
OSCCONbits.IRCF = 0b111; // Establecer la frecuencia del oscilador interno a 8MHz
OSCCONbits.SCS = 0b10; // Seleccionar el oscilador interno como fuente de reloj del
sistema
}
uint16_t valor;
float voltaje;
char datos[20];

void LCD_Init(void){
OpenXLCD(FOUR_BIT & LINES_5X7 );
while(BusyXLCD()); // LCD esta ocupado
WriteCmdXLCD(0x06); //Mover el cursor a la derecha
WriteCmdXLCD(0x0C); //Desactivar el cursor
}

void main(){
OSC_Init(); // Inicializar el oscilador interno
LCD_Init();
ADC_init();
TRISA = 1; // PORTA -> Entrada
LCD_gotoXY(0,0);
putrsXLCD("LAB 03 - P01");
__delay_ms(100);
LCD_gotoXY(1,0);
putrsXLCD(" DMT - 29 ");
__delay_ms(500);
WriteCmdXLCD(BORRAR_LCD);
while(1){
LCD_gotoXY(0,0);
putrsXLCD("EL VOLTAJE ES:");
__delay_ms(100);
valor = ADC_read(0);
voltaje = (valor*5.0)/1023;
LCD_gotoXY(1,12);
sprintf(datos,"%.2f",voltaje);
putrsXLCD(datos);
__delay_ms(500);
}
}

*ADC_Libreria.h //(HEADER FILES)

/*
* File: ADC_libreria.h
* Author: Wels
*
* Created on 19 de junio de 2018, 08:47 PM

2
*/

void ADC_init(void);
unsigned int ADC_read(unsigned char ch);

*LCD_CONFIG.h //(HEADER FILES)

#ifndef LCD_CONFIG_h
#define LCD_CONFIG_h
#define _XTAL_FREQ 8000000L

#endif /* XC_HEADER_TEMPLATE_H */

*LCD_LIBRERIA.h // (HEADER FILES)

#ifndef __XLCD_H
#define __XLCD_H
#include "p18cxxx.h"
#include <stdint.h>
/* PIC18 XLCD peripheral routines.
*
* Notes:
* - These libraries routines are written to support the
* Hitachi HD44780 LCD controller.
* - The user must define the following items:
* - The LCD interface type (4- or 8-bits)
* - If 4-bit mode
* - whether using the upper or lower nibble
* - The data port
* - The tris register for data port
* - The control signal ports and pins
* - The control signal port tris and pins
* - The user must provide three delay routines:
* - DelayFor18TCY() provides a 18 Tcy delay
* - DelayPORXLCD() provides at least 15ms delay
* - DelayXLCD() provides at least 5ms delay
*/

/* Interface type 8-bit or 4-bit


* For 8-bit operation uncomment the #define BIT8
*/
/* #define BIT8 */

/* When in 4-bit interface define if the data is in the upper


* or lower nibble. For lower nibble, comment the #define UPPER
*/
/* #define UPPER */

/* DATA_PORT defines the port to which the LCD data lines are connected */

2
#define DATA_PORT PORTD
#define TRIS_DATA_PORT TRISD

/* CTRL_PORT defines the port where the control lines are connected.
* These are just samples, change to match your application.
*/
#define RW_PIN LATDbits.LATD6 /* PORT for RW */
#define TRIS_RW TRISDbits.TRISD6 /* TRIS for RW */

#define RS_PIN LATDbits.LATD5 /* PORT for RS */


#define TRIS_RS TRISDbits.TRISD5 /* TRIS for RS */

#define E_PIN LATDbits.LATD4 /* PORT for D */


#define TRIS_E TRISDbits.TRISD4 /* TRIS for E */

/* Display ON/OFF Control defines */


#define DON 0b00001111 /* Display on */
#define DOFF 0b00001011 /* Display off */
#define CURSOR_ON 0b00001111 /* Cursor on */
#define CURSOR_OFF 0b00001101 /* Cursor off */
#define BLINK_ON 0b00001111 /* Cursor Blink */
#define BLINK_OFF 0b00001110 /* Cursor No Blink */

/* Comandos creados */
#define BORRAR_LCD 0x01 /* Clears the LCD */
#define CURSOR_INICIO 0x02 /* Returns the cursor to the HOME position */
#define LINEA1 0b10000000 /* 4-bit Interface */
#define LINEA2 0b11000000 /* 4-bit Interface */
#define CGRAM_ADDRESS 0b01000000 /* 4-bit Interface

/* Cursor or Display Shift defines */


#define SHIFT_CUR_LEFT 0b00000100 /* Cursor shifts to the left */
#define SHIFT_CUR_RIGHT 0b00000101 /* Cursor shifts to the right */
#define SHIFT_DISP_LEFT 0b00000110 /* Display shifts to the left */
#define SHIFT_DISP_RIGHT 0b00000111 /* Display shifts to the right */

/* Function Set defines */


#define FOUR_BIT 0b00101100 /* 4-bit Interface */
#define EIGHT_BIT 0b00111100 /* 8-bit Interface */
#define LINE_5X7 0b00110000 /* 5x7 characters, single line */
#define LINE_5X10 0b00110100 /* 5x10 characters */
#define LINES_5X7 0b00111000 /* 5x7 characters, multiple line */

#ifdef _OMNI_CODE_
#define PARAM_SCLASS
#else
#define PARAM_SCLASS auto
#endif

#ifndef MEM_MODEL

2
#ifdef _OMNI_CODE_
#define MEM_MODEL
#else
#define MEM_MODEL far /* Change this to near for small memory model */
#endif
#endif

/* OpenXLCD
* Configures I/O pins for external LCD
*/
void OpenXLCD(PARAM_SCLASS unsigned char);

/* SetCGRamAddr
* Sets the character generator address
*/
void SetCGRamAddr(PARAM_SCLASS unsigned char);

/* SetDDRamAddr
* Sets the display data address
*/
void SetDDRamAddr(PARAM_SCLASS unsigned char);

/* BusyXLCD
* Returns the busy status of the LCD
*/
unsigned char BusyXLCD(void);

/* ReadAddrXLCD
* Reads the current address
*/
unsigned char ReadAddrXLCD(void);

/* ReadDataXLCD
* Reads a byte of data
*/
char ReadDataXLCD(void);

/* WriteCmdXLCD
* Writes a command to the LCD
*/
void WriteCmdXLCD(PARAM_SCLASS unsigned char);

/* WriteDataXLCD
* Writes a data byte to the LCD
*/
void WriteDataXLCD(PARAM_SCLASS char);

/* putcXLCD
* A putc is a write
*/

2
#define putcXLCD WriteDataXLCD

/* putsXLCD
* Writes a string of characters to the LCD
*/
void putsXLCD(PARAM_SCLASS char *);

/* putrsXLCD
* Writes a string of characters in ROM to the LCD
*/
void putrsXLCD(const char *);

/* User defines these routines according to the oscillator frequency */


extern void DelayFor18TCY(void);
extern void DelayPORXLCD(void);
extern void DelayXLCD(void);

void LCD_gotoXY(int x,int y);


void LCD_WriteChr_CGRAM( const char *buffer, unsigned char Addres);
#endif

COMPILACION: (FUNCIONÓ)

2
IMAGEN PROTEUS: (SIMULACIÓN)

2
SEGUNDO PROGRAMA:
En la segunda pregunta deben elaborar un programa en C, el cual utilice el ADC
para leer un potenciómetro periódicamente, utilice el Timer0 para generar una señal
PWM que controle la posición de uno o más servomotores, y utilice un timer
adicional para sincronizar la lectura de las entradas digitales

Descripción general del Programa:


El funcionamiento del programa consiste en leer la variación de voltaje de un potenciómetro,
el cual genera una señal PWM que controla la rotación del servomotor SG90 en 180 grados

LIBRERIAS:

*SERVO2_CONFIG.h (HEADER FILES)

// Definiciones de pines
#define _XTAL_FREQ 8000000 // Frecuencia de oscilación de 8Mhz
#define POT_CHANNEL 0 // Canal del ADC para el potenciómetro
#define PWM_PIN CCP2 // Pin para generar la señal PWM
#define DIGITAL_PIN PORTBbits.RB0 // Pin para la entrada digital

*SERVO2_CONFIG.c (SOURCE FILES)

#include <xc.h>
// PIC18F4550 Configuration Bit Settings

// 'C' source line config statements

// CONFIG1L
#pragma config PLLDIV = 1 // PLL Prescaler Selection bits (No prescale (4 MHz
oscillator input drives PLL directly))
#pragma config CPUDIV = OSC1_PLL2// System Clock Postscaler Selection bits ([Primary
Oscillator Src: /1][96 MHz PLL Src: /2])
#pragma config USBDIV = 1 // USB Clock Selection bit (used in Full-Speed USB mode
only; UCFG:FSEN = 1) (USB clock source comes directly from the primary oscillator block
with no postscale)

// CONFIG1H
#pragma config FOSC = INTOSCIO_EC// Oscillator Selection bits (Internal oscillator, port
function on RA6, EC used by USB (INTIO))
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock
Monitor disabled)
#pragma config IESO = OFF // Internal/External Oscillator Switchover bit (Oscillator
Switchover mode disabled)

// CONFIG2L
#pragma config PWRT = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOR = ON // Brown-out Reset Enable bits (Brown-out Reset enabled in
hardware only (SBOREN is disabled))
#pragma config BORV = 3 // Brown-out Reset Voltage bits (Minimum setting 2.05V)

2
#pragma config VREGEN = OFF // USB Voltage Regulator Enable bit (USB voltage
regulator disabled)

// CONFIG2H
#pragma config WDT = OFF // Watchdog Timer Enable bit (WDT enabled
#pragma config DEBUG = OFF
#pragma config WDTPS = 32768 // Watchdog Timer Postscale Select bits (1:32768)

// CONFIG3H
#pragma config CCP2MX = ON // CCP2 MUX bit (CCP2 input/output is multiplexed
with RC1)
#pragma config PBADEN = ON // PORTB A/D Enable bit (PORTB<4:0> pins are
configured as analog input channels on Reset)
#pragma config LPT1OSC = OFF // Low-Power Timer 1 Oscillator Enable bit (Timer1
configured for higher power operation)
#pragma config MCLRE = ON // MCLR Pin Enable bit (MCLR pin enabled; RE3 input
pin disabled)

// CONFIG4L
#pragma config STVREN = ON // Stack Full/Underflow Reset Enable bit (Stack
full/underflow will cause Reset)
#pragma config LVP = ON // Single-Supply ICSP Enable bit (Single-Supply ICSP
enabled)
#pragma config ICPRT = OFF // Dedicated In-Circuit Debug/Programming Port
(ICPORT) Enable bit (ICPORT disabled)
#pragma config XINST = OFF // Extended Instruction Set Enable bit (Instruction set
extension and Indexed Addressing mode disabled (Legacy mode))

// CONFIG5L
#pragma config CP0 = OFF // Code Protection bit (Block 0 (000800-001FFFh) is not
code-protected)
#pragma config CP1 = OFF // Code Protection bit (Block 1 (002000-003FFFh) is not
code-protected)
#pragma config CP2 = OFF // Code Protection bit (Block 2 (004000-005FFFh) is not
code-protected)
#pragma config CP3 = OFF // Code Protection bit (Block 3 (006000-007FFFh) is not
code-protected)

// CONFIG5H
#pragma config CPB = OFF // Boot Block Code Protection bit (Boot block (000000-
0007FFh) is not code-protected)
#pragma config CPD = OFF // Data EEPROM Code Protection bit (Data EEPROM is
not code-protected)

// CONFIG6L
#pragma config WRT0 = OFF // Write Protection bit (Block 0 (000800-001FFFh) is not
write-protected)
#pragma config WRT1 = OFF // Write Protection bit (Block 1 (002000-003FFFh) is not
write-protected)

2
#pragma config WRT2 = OFF // Write Protection bit (Block 2 (004000-005FFFh) is not
write-protected)
#pragma config WRT3 = OFF // Write Protection bit (Block 3 (006000-007FFFh) is not
write-protected)

// CONFIG6H
#pragma config WRTC = OFF // Configuration Register Write Protection bit
(Configuration registers (300000-3000FFh) are not write-protected)
#pragma config WRTB = OFF // Boot Block Write Protection bit (Boot block (000000-
0007FFh) is not write-protected)
#pragma config WRTD = OFF // Data EEPROM Write Protection bit (Data EEPROM is
not write-protected)

// CONFIG7L
#pragma config EBTR0 = OFF // Table Read Protection bit (Block 0 (000800-001FFFh)
is not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF // Table Read Protection bit (Block 1 (002000-003FFFh)
is not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF // Table Read Protection bit (Block 2 (004000-005FFFh)
is not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF // Table Read Protection bit (Block 3 (006000-007FFFh)
is not protected from table reads executed in other blocks)

// CONFIG7H
#pragma config EBTRB = OFF // Boot Block Table Read Protection bit (Boot block
(000000-0007FFh) is not protected from table reads executed in other blocks)

// #pragma config statements should precede project file includes.


// Use project enums instead of #define for ON and OFF.

*SERVO2_PRINCIPAL.c (SOURCE FILES)

#include <xc.h>
#include "SERVO2_CONFIG.h"

// Prototipos de funciones
void setup();
void adc_setup();
void pwm_setup();
void timer_setup();
void __interrupt() isr();

void OSC_Init(void)
{
OSCCONbits.IRCF = 0b111; // Establecer la frecuencia del oscilador interno a 8MHz
OSCCONbits.SCS = 0b10; // Seleccionar el oscilador interno como fuente de reloj del
sistema
}
// Variables globales

2
unsigned int adc_value = 0;

void main(){
OSC_Init(); // Inicializar el oscilador interno
setup();

while (1) {
// Leer el valor del potenciómetro
ADCON0bits.GO = 1;
while (ADCON0bits.GO); // Esperar a que se complete la conversión
adc_value = (ADRESH << 8) + ADRESL;

// Actualizar la posición del servomotor con PWM//


CCP2CONbits.DC2B1 = (adc_value >> 1) & 1;
CCP2CONbits.DC2B0 = adc_value & 1;
CCPR2L = adc_value >> 2;

__delay_ms(100); // Esperar 100 ms antes de leer nuevamente el potenciómetro


}
}

void setup() {
TRISA = 0xFF; // Puerto A como entradas (ADC)
TRISB = 0x00; // Puerto B como salidas (PWM y entrada digital)
PORTB = 0x00; // Inicializar el puerto B en cero

adc_setup();
pwm_setup();
timer_setup();

INTCONbits.GIE = 1; // Habilitar interrupciones globales


INTCONbits.PEIE = 1; // Habilitar interrupciones periféricas
}

void adc_setup() {
ADCON1 = 0x0B; // Referencia Vref+ y Vref-
ADCON2 = 0b10101010; // Justificación a la derecha, Fosc/64, Tad = 16 us
ADCON0 = 0x01; // Habilitar ADC, canal 0 (potenciómetro)
}

void pwm_setup() {
PR2 = 249; // Configurar el periodo del PWM (20 ms)
CCP2CONbits.CCP2M = 0b1100; // Modo PWM
T2CONbits.T2CKPS = 0b11; // Preescaler 1:16
T2CONbits.TMR2ON = 1; // Habilitar Timer2

TRISCbits.TRISC1 = 0; // CCP2 como salida


}

void timer_setup() {

2
// Configurar Timer0 como temporizador
T0CONbits.TMR0ON = 1; // Habilitar Timer0
T0CONbits.T08BIT = 0; // Modo de 16 bits
T0CONbits.T0CS = 0; // Fuente de reloj interna
T0CONbits.PSA = 0; // Habilitar preescaler
T0CONbits.T0PS = 0b111; // Preescaler 1:256
TMR0 = 0;

// Configurar interrupción por desbordamiento del Timer0


INTCONbits.TMR0IE = 1; // Habilitar interrupción por desbordamiento de Timer0
INTCON2bits.TMR0IP = 1; // Alta prioridad
INTCONbits.TMR0IF = 0; // Limpiar bandera de interrupción por Timer0
}

void __interrupt() isr() {


if (INTCONbits.TMR0IF) {
INTCONbits.TMR0IF = 0; // Limpiar bandera de interrupción por Timer0
DIGITAL_PIN = !DIGITAL_PIN; // Cambiar estado de la entrada digital
}
}

COMPILACION: (FUNCIONÓ)

2
IMAGEN PROTEUS: (SIMULACIÓN)

2
TERCER PROGRAMA:
Descripción General del programa:

En la tercera pregunta deben elaborar un programa en C, el cual utilice el módulo


ADC para leer 3 potenciómetros, utilice una LCD alfanumérica para visualizar el
valor de las lecturas adquiridas, y utilice el Timer0 para sincronizar las mediciones y
la visualización de datos.

LIBRERIAS:

*ADC_Libreria.h (HERADERS FILES)

void ADC_init(void);
unsigned int ADC_read(unsigned char ch);

*LCD_CONFIG.h (HERADERS FILES)

#ifndef LCD_CONFIG_h
#define LCD_CONFIG_h
#define _XTAL_FREQ 8000000L

#endif /* XC_HEADER_TEMPLATE_H */

*LCD_LIBRERIA.h (HERADERS FILES)

#ifndef __XLCD_H
#define __XLCD_H
#include "p18cxxx.h"
#include <stdint.h>
/* PIC18 XLCD peripheral routines.
*
* Notes:
* - These libraries routines are written to support the
* Hitachi HD44780 LCD controller.
* - The user must define the following items:
* - The LCD interface type (4- or 8-bits)
* - If 4-bit mode
* - whether using the upper or lower nibble
* - The data port
* - The tris register for data port
* - The control signal ports and pins
* - The control signal port tris and pins
* - The user must provide three delay routines:
* - DelayFor18TCY() provides a 18 Tcy delay
* - DelayPORXLCD() provides at least 15ms delay
* - DelayXLCD() provides at least 5ms delay

2
*/

/* Interface type 8-bit or 4-bit


* For 8-bit operation uncomment the #define BIT8
*/
/* #define BIT8 */

/* When in 4-bit interface define if the data is in the upper


* or lower nibble. For lower nibble, comment the #define UPPER
*/
/* #define UPPER */

/* DATA_PORT defines the port to which the LCD data lines are connected */
#define DATA_PORT PORTD
#define TRIS_DATA_PORT TRISD

/* CTRL_PORT defines the port where the control lines are connected.
* These are just samples, change to match your application.
*/
#define RW_PIN LATDbits.LATD6 /* PORT for RW */
#define TRIS_RW TRISDbits.TRISD6 /* TRIS for RW */

#define RS_PIN LATDbits.LATD5 /* PORT for RS */


#define TRIS_RS TRISDbits.TRISD5 /* TRIS for RS */

#define E_PIN LATDbits.LATD4 /* PORT for D */


#define TRIS_E TRISDbits.TRISD4 /* TRIS for E */

/* Display ON/OFF Control defines */


#define DON 0b00001111 /* Display on */
#define DOFF 0b00001011 /* Display off */
#define CURSOR_ON 0b00001111 /* Cursor on */
#define CURSOR_OFF 0b00001101 /* Cursor off */
#define BLINK_ON 0b00001111 /* Cursor Blink */
#define BLINK_OFF 0b00001110 /* Cursor No Blink */

/* Comandos creados */
#define BORRAR_LCD 0x01 /* Clears the LCD */
#define CURSOR_INICIO 0x02 /* Returns the cursor to the HOME position */
#define LINEA1 0b10000000 /* 4-bit Interface */
#define LINEA2 0b11000000 /* 4-bit Interface */
#define CGRAM_ADDRESS 0b01000000 /* 4-bit Interface

/* Cursor or Display Shift defines */


#define SHIFT_CUR_LEFT 0b00000100 /* Cursor shifts to the left */
#define SHIFT_CUR_RIGHT 0b00000101 /* Cursor shifts to the right */
#define SHIFT_DISP_LEFT 0b00000110 /* Display shifts to the left */
#define SHIFT_DISP_RIGHT 0b00000111 /* Display shifts to the right */

/* Function Set defines */

2
#define FOUR_BIT 0b00101100 /* 4-bit Interface */
#define EIGHT_BIT 0b00111100 /* 8-bit Interface */
#define LINE_5X7 0b00110000 /* 5x7 characters, single line */
#define LINE_5X10 0b00110100 /* 5x10 characters */
#define LINES_5X7 0b00111000 /* 5x7 characters, multiple line */

#ifdef _OMNI_CODE_
#define PARAM_SCLASS
#else
#define PARAM_SCLASS auto
#endif

#ifndef MEM_MODEL
#ifdef _OMNI_CODE_
#define MEM_MODEL
#else
#define MEM_MODEL far /* Change this to near for small memory model */
#endif
#endif

/* OpenXLCD
* Configures I/O pins for external LCD
*/
void OpenXLCD(PARAM_SCLASS unsigned char);

/* SetCGRamAddr
* Sets the character generator address
*/
void SetCGRamAddr(PARAM_SCLASS unsigned char);

/* SetDDRamAddr
* Sets the display data address
*/
void SetDDRamAddr(PARAM_SCLASS unsigned char);

/* BusyXLCD
* Returns the busy status of the LCD
*/
unsigned char BusyXLCD(void);

/* ReadAddrXLCD
* Reads the current address
*/
unsigned char ReadAddrXLCD(void);

/* ReadDataXLCD
* Reads a byte of data
*/
char ReadDataXLCD(void);

2
/* WriteCmdXLCD
* Writes a command to the LCD
*/
void WriteCmdXLCD(PARAM_SCLASS unsigned char);

/* WriteDataXLCD
* Writes a data byte to the LCD
*/
void WriteDataXLCD(PARAM_SCLASS char);

/* putcXLCD
* A putc is a write
*/
#define putcXLCD WriteDataXLCD

/* putsXLCD
* Writes a string of characters to the LCD
*/
void putsXLCD(PARAM_SCLASS char *);

/* putrsXLCD
* Writes a string of characters in ROM to the LCD
*/
void putrsXLCD(const char *);

/* User defines these routines according to the oscillator frequency */


extern void DelayFor18TCY(void);
extern void DelayPORXLCD(void);
extern void DelayXLCD(void);

void LCD_gotoXY(int x,int y);


void LCD_WriteChr_CGRAM( const char *buffer, unsigned char Addres);
void LCD_Init(void);
#endif

*ADC_Libreria.c (SOURCE FILES)

#include <xc.h>

void ADC_init(void)
{
ADCON1bits.PCFG = 0b1100; // RA0 RA1 RA2
ADCON1bits.VCFG = 0;
ADCON0 = 0;
ADCON2bits.ACQT = 3;
ADCON2bits.ADCS = 5;
ADCON2bits.ADFM = 1;
}

unsigned int ADC_read(unsigned char ch)

2
{
if(ch > 13)return 0;
ADCON0 = 0;
ADCON0 = (ch<<2);
ADCON0bits.ADON = 1;
ADCON0bits.GO_DONE = 1;
while(ADCON0bits.GO_DONE == 1);
ADCON0bits.ADON = 0;
return ADRES;
}

*LCD_CONFIG.c (SOURCE FILES)

#include <xc.h>
// PIC18F4550 Configuration Bit Settings

// 'C' source line config statements

// CONFIG1L
#pragma config PLLDIV = 1 // PLL Prescaler Selection bits (No prescale (4 MHz
oscillator input drives PLL directly))
#pragma config CPUDIV = OSC1_PLL2// System Clock Postscaler Selection bits ([Primary
Oscillator Src: /1][96 MHz PLL Src: /2])
#pragma config USBDIV = 1 // USB Clock Selection bit (used in Full-Speed USB mode
only; UCFG:FSEN = 1) (USB clock source comes directly from the primary oscillator block
with no postscale)

// CONFIG1H
#pragma config FOSC = INTOSCIO_EC// Oscillator Selection bits (Internal oscillator, port
function on RA6, EC used by USB (INTIO))
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock
Monitor disabled)
#pragma config IESO = OFF // Internal/External Oscillator Switchover bit (Oscillator
Switchover mode disabled)

// CONFIG2L
#pragma config PWRT = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOR = OFF // Brown-out Reset Enable bits (Brown-out Reset enabled
in hardware only (SBOREN is disabled))
#pragma config BORV = 3 // Brown-out Reset Voltage bits (Minimum setting 2.05V)
#pragma config VREGEN = OFF // USB Voltage Regulator Enable bit (USB voltage
regulator disabled)

// CONFIG2H
#pragma config WDT = ON // Watchdog Timer Enable bit (WDT enabled)
#pragma config WDTPS = 32768 // Watchdog Timer Postscale Select bits (1:32768)

// CONFIG3H
#pragma config CCP2MX = ON // CCP2 MUX bit (CCP2 input/output is multiplexed
with RC1)

2
#pragma config PBADEN = ON // PORTB A/D Enable bit (PORTB<4:0> pins are
configured as analog input channels on Reset)
#pragma config LPT1OSC = OFF // Low-Power Timer 1 Oscillator Enable bit (Timer1
configured for higher power operation)
#pragma config MCLRE = ON // MCLR Pin Enable bit (MCLR pin enabled; RE3 input
pin disabled)

// CONFIG4L
#pragma config STVREN = ON // Stack Full/Underflow Reset Enable bit (Stack
full/underflow will cause Reset)
#pragma config LVP = OFF // Single-Supply ICSP Enable bit (Single-Supply ICSP
enabled)
#pragma config ICPRT = OFF // Dedicated In-Circuit Debug/Programming Port
(ICPORT) Enable bit (ICPORT disabled)
#pragma config XINST = OFF // Extended Instruction Set Enable bit (Instruction set
extension and Indexed Addressing mode disabled (Legacy mode))

// CONFIG5L
#pragma config CP0 = OFF // Code Protection bit (Block 0 (000800-001FFFh) is not
code-protected)
#pragma config CP1 = OFF // Code Protection bit (Block 1 (002000-003FFFh) is not
code-protected)
#pragma config CP2 = OFF // Code Protection bit (Block 2 (004000-005FFFh) is not
code-protected)
#pragma config CP3 = OFF // Code Protection bit (Block 3 (006000-007FFFh) is not
code-protected)

// CONFIG5H
#pragma config CPB = OFF // Boot Block Code Protection bit (Boot block (000000-
0007FFh) is not code-protected)
#pragma config CPD = OFF // Data EEPROM Code Protection bit (Data EEPROM is
not code-protected)

// CONFIG6L
#pragma config WRT0 = OFF // Write Protection bit (Block 0 (000800-001FFFh) is not
write-protected)
#pragma config WRT1 = OFF // Write Protection bit (Block 1 (002000-003FFFh) is not
write-protected)
#pragma config WRT2 = OFF // Write Protection bit (Block 2 (004000-005FFFh) is not
write-protected)
#pragma config WRT3 = OFF // Write Protection bit (Block 3 (006000-007FFFh) is not
write-protected)

// CONFIG6H
#pragma config WRTC = OFF // Configuration Register Write Protection bit
(Configuration registers (300000-3000FFh) are not write-protected)
#pragma config WRTB = OFF // Boot Block Write Protection bit (Boot block (000000-
0007FFh) is not write-protected)
#pragma config WRTD = OFF // Data EEPROM Write Protection bit (Data EEPROM is
not write-protected)

2
// CONFIG7L
#pragma config EBTR0 = OFF // Table Read Protection bit (Block 0 (000800-001FFFh)
is not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF // Table Read Protection bit (Block 1 (002000-003FFFh)
is not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF // Table Read Protection bit (Block 2 (004000-005FFFh)
is not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF // Table Read Protection bit (Block 3 (006000-007FFFh)
is not protected from table reads executed in other blocks)

// CONFIG7H
#pragma config EBTRB = OFF // Boot Block Table Read Protection bit (Boot block
(000000-0007FFh) is not protected from table reads executed in other blocks)

// #pragma config statements should precede project file includes.


// Use project enums instead of #define for ON and OFF.

*LCD_LIBRERIA.c (SOURCE FILES)

#include <xc.h>
#include "LCD_LIBRERIA.h"
#include "LCD_CONFIG.h"
#include <stdint.h>

void LCD_Init(void){
OpenXLCD(FOUR_BIT & LINES_5X7 );
while(BusyXLCD()); // LCD esta ocupado
WriteCmdXLCD(0x06); //Mover el cursor a la derecha
WriteCmdXLCD(0x0C); //Desactivar el cursor
}

//18 Ciclcos de Instrucción


void DelayFor18TCY(void){
NOP();
NOP();
NOP();
NOP();
NOP();
NOP();
NOP();
NOP();
NOP();
NOP();
NOP();
NOP();
return;
}

2
//Mínimo 15ms
void DelayPORXLCD(void){
__delay_ms(15);
return;
}

//Mínimo 5ms
void DelayXLCD(void){
__delay_ms(5);
return;
}
//Posición de lo que deseo mostrar
void LCD_gotoXY(int x,int y)
{
if ( x>0 ){WriteCmdXLCD(LINEA2+y);}
else {WriteCmdXLCD(LINEA1+y);}
return;
}

//Escribir Caracteres
void LCD_WriteChr_CGRAM( const char *buffer, unsigned char Addres)
{ unsigned char i=0;

SetCGRamAddr(Addres*8);
for (i=0;i<8;i++) // Write data to LCD up to null
{ WriteDataXLCD(*buffer); // Write character to LCD
++buffer;
}
}

//Configuración del LCD e inicialización


void OpenXLCD(unsigned char lcdtype)
{
// The data bits must be either a 8-bit port or the upper or
// lower 4-bits of a port. These pins are made into inputs
#ifdef BIT8 // 8-bit mode, use whole port
DATA_PORT = 0;
TRIS_DATA_PORT = 0x00;
#else // 4-bit mode
#ifdef UPPER // Upper 4-bits of the port
DATA_PORT &= 0x0f;
TRIS_DATA_PORT &= 0x0F;
#else // Lower 4-bits of the port
DATA_PORT &= 0xf0;
TRIS_DATA_PORT &= 0xF0;
#endif
#endif
TRIS_RW = 0; // All control signals made outputs
TRIS_RS = 0;

2
TRIS_E = 0;
RW_PIN = 0; // R/W pin made low
RS_PIN = 0; // Register select pin made low
E_PIN = 0; // Clock pin made low

// Delay for 15ms to allow for LCD Power on reset


DelayPORXLCD();
//-------------------reset procedure through software----------------------
WriteCmdXLCD(0x30);
__delay_us(1);

WriteCmdXLCD(0x30);
__delay_us(1);

WriteCmdXLCD(0x32);
while( BusyXLCD() );
//------------------------------------------------------------------------------------------

// Set data interface width, # lines, font


while(BusyXLCD()); // Wait if LCD busy
WriteCmdXLCD(lcdtype); // Function set cmd

// Turn the display on then off


while(BusyXLCD()); // Wait if LCD busy
WriteCmdXLCD(DOFF&CURSOR_OFF&BLINK_OFF); // Display OFF/Blink
OFF
while(BusyXLCD()); // Wait if LCD busy
WriteCmdXLCD(DON&CURSOR_ON&BLINK_ON); // Display ON/Blink ON

// Clear display
while(BusyXLCD()); // Wait if LCD busy
WriteCmdXLCD(0x01); // Clear display

// Set entry mode inc, no shift


while(BusyXLCD()); // Wait if LCD busy
WriteCmdXLCD(SHIFT_CUR_LEFT); // Entry Mode

// Set DD Ram address to 0


while(BusyXLCD()); // Wait if LCD busy
SetDDRamAddr(0x80); // Set Display data ram address to 0

return;
}

//Escribe una cadena desde la memoria del Programa


void putrsXLCD(const char *buffer)
{
while(*buffer) // Write data to LCD up to null

2
{
while(BusyXLCD()); // Wait while LCD is busy
WriteDataXLCD(*buffer); // Write character to LCD
buffer++; // Increment buffer
}
return;
}

//Escribe una cadena desde la memoria de datos


void putsXLCD(char *buffer)
{
while(*buffer) // Write data to LCD up to null
{
while(BusyXLCD()); // Wait while LCD is busy
WriteDataXLCD(*buffer); // Write character to LCD
buffer++; // Increment buffer
}
return;
}

//Envia comandos al LCD


void WriteCmdXLCD(unsigned char cmd)
{
#ifdef BIT8 // 8-bit interface
TRIS_DATA_PORT = 0; // Data port output
DATA_PORT = cmd; // Write command to data port
RW_PIN = 0; // Set the control signals
RS_PIN = 0; // for sending a command
DelayFor18TCY();
E_PIN = 1; // Clock the command in
DelayFor18TCY();
E_PIN = 0;
DelayFor18TCY();
TRIS_DATA_PORT = 0xff; // Data port input
#else // 4-bit interface
#ifdef UPPER // Upper nibble interface
TRIS_DATA_PORT &= 0x0f;
DATA_PORT &= 0x0f;
DATA_PORT |= cmd&0xf0;
#else // Lower nibble interface
TRIS_DATA_PORT &= 0xf0;
DATA_PORT &= 0xf0;
DATA_PORT |= (cmd>>4)&0x0f;
#endif
RW_PIN = 0; // Set control signals for command
RS_PIN = 0;
DelayFor18TCY();
E_PIN = 1; // Clock command in
DelayFor18TCY();
E_PIN = 0;

2
#ifdef UPPER // Upper nibble interface
DATA_PORT &= 0x0f;
DATA_PORT |= (cmd<<4)&0xf0;
#else // Lower nibble interface
DATA_PORT &= 0xf0;
DATA_PORT |= cmd&0x0f;
#endif
DelayFor18TCY();
E_PIN = 1; // Clock command in
DelayFor18TCY();
E_PIN = 0;
#ifdef UPPER // Make data nibble input
TRIS_DATA_PORT |= 0xf0;
#else
TRIS_DATA_PORT |= 0x0f;
#endif
#endif
return;
}

//Lee la dirección del byte desde el LCD


unsigned char ReadAddrXLCD(void)
{
char data; // Holds the data retrieved from the LCD

#ifdef BIT8 // 8-bit interface


RW_PIN = 1; // Set control bits for the read
RS_PIN = 0;
DelayFor18TCY();
E_PIN = 1; // Clock data out of the LCD controller
DelayFor18TCY();
data = DATA_PORT; // Save the data in the register
E_PIN = 0;
RW_PIN = 0; // Reset the control bits
#else // 4-bit interface
RW_PIN = 1; // Set control bits for the read
RS_PIN = 0;
DelayFor18TCY();
E_PIN = 1; // Clock data out of the LCD controller
DelayFor18TCY();
#ifdef UPPER // Upper nibble interface
data = DATA_PORT&0xf0; // Read the nibble into the upper nibble of data
#else // Lower nibble interface
data = (DATA_PORT<<4)&0xf0; // Read the nibble into the upper nibble of data
#endif
E_PIN = 0; // Reset the clock
DelayFor18TCY();
E_PIN = 1; // Clock out the lower nibble
DelayFor18TCY();
#ifdef UPPER // Upper nibble interface

2
data |= (DATA_PORT>>4)&0x0f; // Read the nibble into the lower nibble of data
#else // Lower nibble interface
data |= DATA_PORT&0x0f; // Read the nibble into the lower nibble of data
#endif
E_PIN = 0;
RW_PIN = 0; // Reset the control lines
#endif
return (data&0x7f); // Return the address, Mask off the busy bit
}

//Establece la dirección de los caracteres generados


void SetCGRamAddr(unsigned char CGaddr)
{
while(BusyXLCD());
#ifdef BIT8 // 8-bit interface
TRIS_DATA_PORT = 0; // Make data port ouput
DATA_PORT = CGaddr | 0b01000000; // Write cmd and address to port
RW_PIN = 0; // Set control signals
RS_PIN = 0;
DelayFor18TCY();
E_PIN = 1; // Clock cmd and address in
DelayFor18TCY();
E_PIN = 0;
DelayFor18TCY();
TRIS_DATA_PORT = 0xff; // Make data port inputs
#else // 4-bit interface
#ifdef UPPER // Upper nibble interface
TRIS_DATA_PORT &= 0x0f; // Make nibble input
DATA_PORT &= 0x0f; // and write upper nibble
DATA_PORT |= ((CGaddr | 0b01000000) & 0xf0);
#else // Lower nibble interface
TRIS_DATA_PORT &= 0xf0; // Make nibble input
DATA_PORT &= 0xf0; // and write upper nibble
DATA_PORT |= (((CGaddr |0b01000000)>>4) & 0x0f);
#endif
RW_PIN = 0; // Set control signals
RS_PIN = 0;
__delay_us(1);
E_PIN = 1; // Clock cmd and address in
__delay_us(1);
E_PIN = 0;
#ifdef UPPER // Upper nibble interface
DATA_PORT &= 0x0f; // Write lower nibble
DATA_PORT |= ((CGaddr<<4)&0xf0);
#else // Lower nibble interface
DATA_PORT &= 0xf0; // Write lower nibble
DATA_PORT |= (CGaddr&0x0f);
#endif
__delay_us(1);
E_PIN = 1; // Clock cmd and address in

2
__delay_us(1);
E_PIN = 0;
#ifdef UPPER // Upper nibble interface
TRIS_DATA_PORT |= 0xf0; // Make inputs
#else // Lower nibble interface
TRIS_DATA_PORT |= 0x0f; // Make inputs
#endif
#endif
return;
}

//Configuración de la dirección de los datos


void SetDDRamAddr(unsigned char DDaddr)
{
#ifdef BIT8 // 8-bit interface
TRIS_DATA_PORT = 0; // Make port output
DATA_PORT = DDaddr | 0b10000000; // Write cmd and address to port
RW_PIN = 0; // Set the control bits
RS_PIN = 0;
DelayFor18TCY();
E_PIN = 1; // Clock the cmd and address in
DelayFor18TCY();
E_PIN = 0;
DelayFor18TCY();
TRIS_DATA_PORT = 0xff; // Make port input
#else // 4-bit interface
#ifdef UPPER // Upper nibble interface
TRIS_DATA_PORT &= 0x0f; // Make port output
DATA_PORT &= 0x0f; // and write upper nibble
DATA_PORT |= ((DDaddr | 0b10000000) & 0xf0);
#else // Lower nibble interface
TRIS_DATA_PORT &= 0xf0; // Make port output
DATA_PORT &= 0xf0; // and write upper nibble
DATA_PORT |= (((DDaddr | 0b10000000)>>4) & 0x0f);
#endif
RW_PIN = 0; // Set control bits
RS_PIN = 0;
DelayFor18TCY();
E_PIN = 1; // Clock the cmd and address in
DelayFor18TCY();
E_PIN = 0;
#ifdef UPPER // Upper nibble interface
DATA_PORT &= 0x0f; // Write lower nibble
DATA_PORT |= ((DDaddr<<4)&0xf0);
#else // Lower nibble interface
DATA_PORT &= 0xf0; // Write lower nibble
DATA_PORT |= (DDaddr&0x0f);
#endif
DelayFor18TCY();
E_PIN = 1; // Clock the cmd and address in

2
DelayFor18TCY();
E_PIN = 0;
#ifdef UPPER // Upper nibble interface
TRIS_DATA_PORT |= 0xf0; // Make port input
#else // Lower nibble interface
TRIS_DATA_PORT |= 0x0f; // Make port input
#endif
#endif
return;
}

//Lee Datos del LDC


char ReadDataXLCD(void)
{
char data;

#ifdef BIT8 // 8-bit interface


RS_PIN = 1; // Set the control bits
RW_PIN = 1;
DelayFor18TCY();
E_PIN = 1; // Clock the data out of the LCD
DelayFor18TCY();
data = DATA_PORT; // Read the data
E_PIN = 0;
RS_PIN = 0; // Reset the control bits
RW_PIN = 0;
#else // 4-bit interface
RW_PIN = 1;
RS_PIN = 1;
DelayFor18TCY();
E_PIN = 1; // Clock the data out of the LCD
DelayFor18TCY();
#ifdef UPPER // Upper nibble interface
data = DATA_PORT&0xf0; // Read the upper nibble of data
#else // Lower nibble interface
data = (DATA_PORT<<4)&0xf0; // read the upper nibble of data
#endif
E_PIN = 0; // Reset the clock line
DelayFor18TCY();
E_PIN = 1; // Clock the next nibble out of the LCD
DelayFor18TCY();
#ifdef UPPER // Upper nibble interface
data |= (DATA_PORT>>4)&0x0f; // Read the lower nibble of data
#else // Lower nibble interface
data |= DATA_PORT&0x0f; // Read the lower nibble of data
#endif
E_PIN = 0;
RS_PIN = 0; // Reset the control bits
RW_PIN = 0;
#endif

2
return(data); // Return the data byte
}

//Escribe bytes en el LCD


void WriteDataXLCD(char data)
{
while(BusyXLCD());
#ifdef BIT8 // 8-bit interface
TRIS_DATA_PORT = 0; // Make port output
DATA_PORT = data; // Write data to port
RS_PIN = 1; // Set control bits
RW_PIN = 0;
DelayFor18TCY();
E_PIN = 1; // Clock data into LCD
DelayFor18TCY();
E_PIN = 0;
RS_PIN = 0; // Reset control bits
TRIS_DATA_PORT = 0xff; // Make port input
#else // 4-bit interface
#ifdef UPPER // Upper nibble interface
TRIS_DATA_PORT &= 0x0f;
DATA_PORT &= 0x0f;
DATA_PORT |= data&0xf0;
#else // Lower nibble interface
TRIS_DATA_PORT &= 0xf0;
DATA_PORT &= 0xf0;
DATA_PORT |= ((data>>4)&0x0f);
#endif
RS_PIN = 1; // Set control bits
RW_PIN = 0;
DelayFor18TCY();
E_PIN = 1; // Clock nibble into LCD
DelayFor18TCY();
E_PIN = 0;
#ifdef UPPER // Upper nibble interface
DATA_PORT &= 0x0f;
DATA_PORT |= ((data<<4)&0xf0);
#else // Lower nibble interface
DATA_PORT &= 0xf0;
DATA_PORT |= (data&0x0f);
#endif
DelayFor18TCY();
E_PIN = 1; // Clock nibble into LCD
DelayFor18TCY();
E_PIN = 0;
#ifdef UPPER // Upper nibble interface
TRIS_DATA_PORT |= 0xf0;
#else // Lower nibble interface
TRIS_DATA_PORT |= 0x0f;
#endif

2
#endif
return;
}

//Está el LCD ocupado?


unsigned char BusyXLCD(void)
{
RW_PIN = 1; // Set the control bits for read
RS_PIN = 0;
DelayFor18TCY();
E_PIN = 1; // Clock in the command
DelayFor18TCY();
#ifdef BIT8 // 8-bit interface
if(DATA_PORT&0x80) // Read bit 7 (busy bit)
{ // If high
E_PIN = 0; // Reset clock line
RW_PIN = 0; // Reset control line
return 1; // Return TRUE
}
else // Bit 7 low
{
E_PIN = 0; // Reset clock line
RW_PIN = 0; // Reset control line
return 0; // Return FALSE
}
#else // 4-bit interface
#ifdef UPPER // Upper nibble interface
if(DATA_PORT&0x80)
#else // Lower nibble interface
if(DATA_PORT&0x08)
#endif
{
E_PIN = 0; // Reset clock line
DelayFor18TCY();
E_PIN = 1; // Clock out other nibble
DelayFor18TCY();
E_PIN = 0;
RW_PIN = 0; // Reset control line
return 1; // Return TRUE
}
else // Busy bit is low
{
E_PIN = 0; // Reset clock line
DelayFor18TCY();
E_PIN = 1; // Clock out other nibble
DelayFor18TCY();
E_PIN = 0;
RW_PIN = 0; // Reset control line
return 0; // Return FALSE
}

2
#endif
}

* 3_LCD_PRINCIPAL.c (SOURCE FILES)

#include <xc.h>
#include <stdio.h>
#include "LCD_CONFIG.h"
#include "LCD_LIBRERIA.h"
#include "ADC_LIBRERIA.h"

void OSC_Init(void)
{
OSCCONbits.IRCF = 0b111; // Establecer la frecuencia del oscilador interno a 8MHz
OSCCONbits.SCS = 0b10; // Seleccionar el oscilador interno como fuente de reloj del
sistema
}

uint16_t valor;
float voltaje;
char datos[20];

void PORT_Init(){
TRISBbits.RB0 = 1;
TRISBbits.RB1 = 1;
TRISBbits.RB2 = 1;
INTCON2bits.RBPU = 0;
TRISA = 1;
}

void main(){
OSC_Init(); // Inicializar el oscilador interno
LCD_Init();
ADC_init();
PORT_Init();
LCD_gotoXY(0,0);
putrsXLCD(" LAB03 - P03");
__delay_ms(1000);
LCD_gotoXY(1,0);
putrsXLCD(" ESO TILIN!! =D ");
__delay_ms(2000);
WriteCmdXLCD(BORRAR_LCD);
while(1){
LCD_gotoXY(0,0);
putrsXLCD("ADC 1 = PUL1");
__delay_ms(500);
LCD_gotoXY(1,0);
putrsXLCD("ADC 2 = PUL2");

2
__delay_ms(500);
WriteCmdXLCD(BORRAR_LCD);
LCD_gotoXY(0,0);
putrsXLCD("ADC 3 = PUL3");
__delay_ms(500);
if (PORTBbits.RB0 == 0){
while(1){
LCD_gotoXY(0,0);
putrsXLCD(" SELECTED ADC 1 ");
__delay_ms(100);
LCD_gotoXY(1,0);
putrsXLCD("TENSION ES:");
valor = ADC_read(0);
voltaje = (valor*5.0)/1023;
LCD_gotoXY(1,12);
sprintf(datos,"%.2f",voltaje);
putrsXLCD(datos);
__delay_ms(100);
if(PORTBbits.RB0 == 1)break;
}
}
else if(PORTBbits.RB1 == 0)
{
while(1){
LCD_gotoXY(0,0);
putrsXLCD(" SELECTED ADC 2 ");
__delay_ms(100);
LCD_gotoXY(1,0);
putrsXLCD("TENSION ES:");
valor = ADC_read(1);
voltaje = (valor*5.0)/1023;
LCD_gotoXY(1,12);
sprintf(datos,"%.2f",voltaje);
putrsXLCD(datos);
__delay_ms(100);
if(PORTBbits.RB1 == 1)break;
}
}
else if(PORTBbits.RB2 == 0)
{
while(1){
LCD_gotoXY(0,0);
putrsXLCD(" SELECTED ADC 3 " );
__delay_ms(100);
LCD_gotoXY(1,0);
putrsXLCD("TENSION ES:");
valor = ADC_read(2);
voltaje = (valor*5.0)/1023;
LCD_gotoXY(1,12);
sprintf(datos,"%.2f",voltaje);

2
putrsXLCD(datos);
__delay_ms(100);
if(PORTBbits.RB2 == 1)break;
}
}
}
}

COMPILACION: (FUNCIONÓ)

2
IMAGEN PROTEUS: (SIMULACIÓN)

2
2
2
2
CUARTO PROGRAMA:
En la cuarta pregunta deben elaborar un programa en C, el cual se utilice el módulo
ADC para leer 2 sensores de temperatura, compare el valor de las mediciones con
umbrales indicados, y aplique el método de histéresis para activar alarmas.

Descripción General del programa:


Se tienen dos sensores digitales de temperatura DHT11 cuyo intervalo de medida es de 0 a
50 grados Celsius, los cuales tomaran los valores de temperatura exterior, aplicando el
método de histéresis la alarma o señal de aviso(será representada por un led) se activará
cuando se tenga una temperatura mayor a 35 grados Celsius cualquiera de los dos sensores,
esta misma será desactivada cuando en los dos sensores digitales se tengo un valor de
temperatura menor a 20 grados Celsius, esto sincronizara los sensores y se evitaran las falsas
alarmas.
LIBRERIAS:

*TEMP_CONFIG.h (HEADER FILES)

#define UMBRAL_SUPERIOR 35 // Umbral superior de temperatura


#define UMBRAL_INFERIOR 20 // Umbral inferior de temperatura
#define _XTAL_FREQ 8000000L

*TEMP_CONFIGT.c (SOURCE FILES)

/*
* File: TEMP_CONFIG.c
* Author: U18302032
*
* Created on 9 de julio de 2023, 01:08 PM
*/

#include <xc.h>
// PIC18F4550 Configuration Bit Settings

// 'C' source line config statements

// CONFIG1L
#pragma config PLLDIV = 1 // PLL Prescaler Selection bits (No prescale (4 MHz
oscillator input drives PLL directly))
#pragma config CPUDIV = OSC1_PLL2// System Clock Postscaler Selection bits ([Primary
Oscillator Src: /1][96 MHz PLL Src: /2])
#pragma config USBDIV = 1 // USB Clock Selection bit (used in Full-Speed USB mode
only; UCFG:FSEN = 1) (USB clock source comes directly from the primary oscillator block
with no postscale)

// CONFIG1H

2
#pragma config FOSC = INTOSCIO_EC// Oscillator Selection bits (Internal oscillator, port
function on RA6, EC used by USB (INTIO))
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock
Monitor disabled)
#pragma config IESO = OFF // Internal/External Oscillator Switchover bit (Oscillator
Switchover mode disabled)

// CONFIG2L
#pragma config PWRT = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOR = OFF // Brown-out Reset Enable bits (Brown-out Reset disabled
in hardware and software)
#pragma config BORV = 3 // Brown-out Reset Voltage bits (Minimum setting 2.05V)
#pragma config VREGEN = OFF // USB Voltage Regulator Enable bit (USB voltage
regulator disabled)

// CONFIG2H
#pragma config WDT = OFF // Watchdog Timer Enable bit (WDT disabled (control is
placed on the SWDTEN bit))
#pragma config WDTPS = 32768 // Watchdog Timer Postscale Select bits (1:32768)

// CONFIG3H
#pragma config CCP2MX = ON // CCP2 MUX bit (CCP2 input/output is multiplexed
with RC1)
#pragma config PBADEN = ON // PORTB A/D Enable bit (PORTB<4:0> pins are
configured as analog input channels on Reset)
#pragma config LPT1OSC = OFF // Low-Power Timer 1 Oscillator Enable bit (Timer1
configured for higher power operation)
#pragma config MCLRE = ON // MCLR Pin Enable bit (MCLR pin enabled; RE3 input
pin disabled)

// CONFIG4L
#pragma config STVREN = ON // Stack Full/Underflow Reset Enable bit (Stack
full/underflow will cause Reset)
#pragma config LVP = OFF // Single-Supply ICSP Enable bit (Single-Supply ICSP
disabled)
#pragma config ICPRT = OFF // Dedicated In-Circuit Debug/Programming Port
(ICPORT) Enable bit (ICPORT disabled)
#pragma config XINST = OFF // Extended Instruction Set Enable bit (Instruction set
extension and Indexed Addressing mode disabled (Legacy mode))

// CONFIG5L
#pragma config CP0 = OFF // Code Protection bit (Block 0 (000800-001FFFh) is not
code-protected)
#pragma config CP1 = OFF // Code Protection bit (Block 1 (002000-003FFFh) is not
code-protected)
#pragma config CP2 = OFF // Code Protection bit (Block 2 (004000-005FFFh) is not
code-protected)
#pragma config CP3 = OFF // Code Protection bit (Block 3 (006000-007FFFh) is not
code-protected)

2
// CONFIG5H
#pragma config CPB = OFF // Boot Block Code Protection bit (Boot block (000000-
0007FFh) is not code-protected)
#pragma config CPD = OFF // Data EEPROM Code Protection bit (Data EEPROM is
not code-protected)

// CONFIG6L
#pragma config WRT0 = OFF // Write Protection bit (Block 0 (000800-001FFFh) is not
write-protected)
#pragma config WRT1 = OFF // Write Protection bit (Block 1 (002000-003FFFh) is not
write-protected)
#pragma config WRT2 = OFF // Write Protection bit (Block 2 (004000-005FFFh) is not
write-protected)
#pragma config WRT3 = OFF // Write Protection bit (Block 3 (006000-007FFFh) is not
write-protected)

// CONFIG6H
#pragma config WRTC = OFF // Configuration Register Write Protection bit
(Configuration registers (300000-3000FFh) are not write-protected)
#pragma config WRTB = OFF // Boot Block Write Protection bit (Boot block (000000-
0007FFh) is not write-protected)
#pragma config WRTD = OFF // Data EEPROM Write Protection bit (Data EEPROM is
not write-protected)

// CONFIG7L
#pragma config EBTR0 = OFF // Table Read Protection bit (Block 0 (000800-001FFFh)
is not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF // Table Read Protection bit (Block 1 (002000-003FFFh)
is not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF // Table Read Protection bit (Block 2 (004000-005FFFh)
is not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF // Table Read Protection bit (Block 3 (006000-007FFFh)
is not protected from table reads executed in other blocks)

// CONFIG7H
#pragma config EBTRB = OFF // Boot Block Table Read Protection bit (Boot block
(000000-0007FFh) is not protected from table reads executed in other blocks)

// #pragma config statements should precede project file includes.


// Use project enums instead of #define for ON and OFF.

*TEMP_PRINCIPAL.c (SOURCE FILES)

/*
* File: TEMP_CONFIG.c
* Author: U18302032
*
* Created on 9 de julio de 2023, 01:08 PM

2
*/
//OSCILADOR INTERNO//

#include <xc.h>
#include "TEMP_CONFIG.h"
void OSC_Init(void)
{
OSCCONbits.IRCF = 0b111; // Establecer la frecuencia del oscilador interno a 8MHz
OSCCONbits.SCS = 0b10; // Seleccionar el oscilador interno como fuente de reloj del
sistema
}
// Función de inicialización del ADC
void ADC_Init()
{
ADCON1bits.PCFG3 = 1; // Configura el pin AN0 como entrada analógica
ADCON1bits.PCFG0 = 0;
ADCON2bits.ADFM = 1; // Justificación derecha
ADCON2bits.ACQT = 4; // Tiempo de adquisición = 4 TAD
ADCON2bits.ADCS = 7; // Frecuencia de reloj = FOSC/2
ADCON0bits.ADON = 1; // Habilita el ADC
}

// Función para leer el valor del ADC


unsigned int ADC_Read(unsigned char channel)
{
ADCON0bits.CHS = channel; // Selecciona el canal de entrada
ADCON0bits.ADON = 1; // Habilita el módulo ADC
ADCON0bits.GO_DONE = 1; // Inicia la conversión

while (ADCON0bits.GO_DONE); // Espera a que se complete la conversión

return ((unsigned int)ADRESH << 8) + ADRESL; // Retorna el resultado de la


conversión
}

// Función principal
void main()
{
OSC_Init(); // Inicializar el oscilador interno
unsigned int sensor1, sensor2;

TRISA = 0xFF; // Configura todos los pines de PORTA como entradas


TRISB = 0; // Configura todos los pines de PORTB como salidas

ADC_Init(); // Inicializa el ADC

while (1)
{
// Lectura de los sensores
sensor1 = ADC_Read(0); // Lee el sensor 1 en el canal 0

2
sensor2 = ADC_Read(1); // Lee el sensor 2 en el canal 1

// Compara los valores con los umbrales


if (sensor1 > UMBRAL_SUPERIOR || sensor2 > UMBRAL_SUPERIOR)
{
PORTBbits.RB0 = 1; // Activa la alarma
}
else if (sensor1 < UMBRAL_INFERIOR && sensor2 < UMBRAL_INFERIOR)
{
PORTBbits.RB0 = 0; // Desactiva la alarma
}
}
}

COMPILACION: (FUNCIONÓ)

2
IMAGEN PROTEUS: (SIMULACIÓN)

2
2
Bibliografia
chrome-extension://efaidnbmnnnibpcajpcglclefindmkaj/http://www.ee.ic.ac.uk/pcheung/
teaching/DE1_EE/stores/sg90_datasheet.pdf
chrome-extension://efaidnbmnnnibpcajpcglclefindmkaj/https://www.mouser.com/datasheet/
2/758/DHT11-Technical-Data-Sheet-Translated-Version-1143054.pdf
chrome-extension://efaidnbmnnnibpcajpcglclefindmkaj/https://www.endrich.com/sixcms/
media.php/2/%20EDS.pdf
DATASHEET 18F4550
https://youtu.be/xskA1Mshxmo
https://youtu.be/7mGYV8kBsYg
https://youtu.be/OwOnSXHZYtQ

También podría gustarte