Documentos de Académico
Documentos de Profesional
Documentos de Cultura
LABORATORIO 03 - ADC
INTEGRANTES:
(10312): MICROCONTROLADORES
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
LIBRERIAS:
#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;
}
#include <xc.h>
// PIC18F4550 Configuration Bit Settings
// 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)
2
*LCD_LIBRERIA.c // (SOURCE FILES)
#include <xc.h>
#include "LCD_LIBRERIA.h"
#include "LCD_CONFIG.h"
#include <stdint.h>
//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;
}
}
WriteCmdXLCD(0x30);
__delay_us(1);
WriteCmdXLCD(0x32);
while( BusyXLCD() );
//------------------------------------------------------------------------------------------
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
return;
}
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;
}
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
}
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;
}
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;
}
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
}
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;
}
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
}
#include <xc.h>
#include <stdio.h>
#include <stdint.h>
#include "LCD_CONFIG.h"
#include "LCD_LIBRERIA.h"
#include "ADC_libreria.h"
//
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);
}
}
/*
* 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);
#ifndef LCD_CONFIG_h
#define LCD_CONFIG_h
#define _XTAL_FREQ 8000000L
#endif /* XC_HEADER_TEMPLATE_H */
#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
*/
/* 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 */
/* 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
#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 *);
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
LIBRERIAS:
// 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
#include <xc.h>
// PIC18F4550 Configuration Bit Settings
// 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)
#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;
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();
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
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;
COMPILACION: (FUNCIONÓ)
2
IMAGEN PROTEUS: (SIMULACIÓN)
2
TERCER PROGRAMA:
Descripción General del programa:
LIBRERIAS:
void ADC_init(void);
unsigned int ADC_read(unsigned char ch);
#ifndef LCD_CONFIG_h
#define LCD_CONFIG_h
#define _XTAL_FREQ 8000000L
#endif /* XC_HEADER_TEMPLATE_H */
#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
*/
/* 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 */
/* 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
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 *);
#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;
}
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;
}
#include <xc.h>
// PIC18F4550 Configuration Bit Settings
// 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)
#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
}
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;
}
}
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
WriteCmdXLCD(0x30);
__delay_us(1);
WriteCmdXLCD(0x32);
while( BusyXLCD() );
//------------------------------------------------------------------------------------------
// Clear display
while(BusyXLCD()); // Wait if LCD busy
WriteCmdXLCD(0x01); // Clear display
return;
}
2
{
while(BusyXLCD()); // Wait while LCD is busy
WriteDataXLCD(*buffer); // Write character to LCD
buffer++; // Increment buffer
}
return;
}
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;
}
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
}
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;
}
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;
}
2
return(data); // Return the data byte
}
2
#endif
return;
}
2
#endif
}
#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.
/*
* File: TEMP_CONFIG.c
* Author: U18302032
*
* Created on 9 de julio de 2023, 01:08 PM
*/
#include <xc.h>
// PIC18F4550 Configuration Bit Settings
// 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)
/*
* 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 principal
void main()
{
OSC_Init(); // Inicializar el oscilador interno
unsigned int sensor1, sensor2;
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
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