ENTRADAS Y SALIDAS DIGITALES CON EL PIC18F4550
DESCRIPCIÓN
Los pines de entradas y salidas presentan las siguientes características en sus puertos IO
- 3 Registro independientes para Configurar, Escribir y Leer cada PUERTO (TRISx, LATx y PORTx).
- Selección de resistencia PULL-UP solo para el Puerto B.
En las siguientes imágenes se detallaran los registros involucrados a las Entradas y Salidas de cada PUERTO;
A continuación se explicará cómo configurar, escribir o leer un Pin de cada PUERTO;
- TRISX: Escribiendo en cada bit (“1” lógico = Entrada y “0” lógico = Salida).
- LATX: Escribiendo en cada bit la salida tendrá un nivel de Voltaje (“1” lógico = 5v | “0” lógico = 0v).
- PORTX: Este registro es de solo Lectura (5v =”1” lógico | 0v =“0” lógico).
-
Configuración de los pines IO de un PUERTO
- TRISA = 0b11111111; Configura todos los pines del Puerto A como Entrada.
- TRISA = 0b00000000; Configura todos los pines del Puerto A como Salida.
- TRISA = 0b00001111; Los pines Altos como Salida y el resto como Entrada.
-
Escritura de pines en un PUERTO
- LATA = 0b11111111; Todos los pines del Puerto A con una salida de 5v.
- LATA = 0b00000000; Todos los pines del Puerto A con una salida de 0v.
- LATA = 0b00001111; Los pines Altos a 0v y los pines Bajos a 5v.
-
Lectura de un PUERTO
- Valor = PORTA [ RA0 ] : El estado lógico del pin A0 se escribe en la variable valor
- Valor = PORTA : El estado lógico del todo el Puerto A se escribe en valor.
Para poder Habilitar la resistencia PULL-UP en un pin determinado, es necesario que dicho pin este configurado como entrada y luego escribir “0” lógico en el bit RBPU del registro INTCON2.
EJEMPLO N°1 – CIRCUITO DE CONEXCIÓN PIC18F4550 + 4 BUTTON + 4 LED
A continuación se muestra como configurar pines del PORTB y utlizarlo como entradas digitales hacia 4 pulsadores, teniendo en cuenta que deben estar en estado de pull-up a través de resistencia de 10kΩ. También, se configura pines del PORTD como salida digital hacia 4 led para visualizar el estado actual de los pulsadores. A continuación se muestra el código.
Código Principal MAIN
#define _XTAL_FREQ 8000000
#include
#include "fuses.h"
int main(void) {
//Configura los pines RD7-RD6-RD5-RD4 como salida digital
TRISDbits.TRISD7 = 0;
TRISDbits.TRISD6 = 0;
TRISDbits.TRISD5 = 0;
TRISDbits.TRISD4 = 0;
//Configura los pines RB7-RB6-RB5-RB4 como entrada digital
TRISBbits.TRISB7 = 1;
TRISBbits.TRISB6 = 1;
TRISBbits.TRISB5 = 1;
TRISBbits.TRISB4 = 1;
while (1) {
if(!PORTBbits.RB4){LATDbits.LATD4=1;}else{LATDbits.LATD4=0;}
if(!PORTBbits.RB5){LATDbits.LATD5=1;}else{LATDbits.LATD5=0;}
if(!PORTBbits.RB6){LATDbits.LATD6=1;}else{LATDbits.LATD6=0;}
if(!PORTBbits.RB7){LATDbits.LATD7=1;}else{LATDbits.LATD7=0;}
}
return 1;
}
}
EJEMPLO N°2 – CIRCUITO DE CONEXCIÓN PIC18F4550 + SECUENCIAL DE LUCES DE 8 LED + 4 EFECTOS
En este ejemplo se muestra como realizar un secuencial de luces de 8 led con 4 efectos y velocidad programable. Todos los pines de salida digital estarán conectado al PORTD, mientras que, los pines de control de efecto y velocidad estarán conectados a los pines RB7 y RB6 del PORTB respectivamente. Estos pines de control se conectarán hacia pulsadores que tendrán una resistencia PULL-UP interna (habilitado a través del mismo PIC). A continuación se muestra el código.
Código Principal MAIN
#define _XTAL_FREQ 8000000
#include
#include "fuses.h"
void secuencial_Efecto1(void);
void secuencial_Efecto2(void);
void secuencial_Efecto3(void);
void secuencial_Efecto4(void);
unsigned char efecto = 1;
unsigned long velocidad = 1;
int main(void) {
TRISD = 0;
LATD = 0;
TRISBbits.TRISB7 = 1;
TRISBbits.TRISB6 = 1;
INTCON2bits.RBPU = 0;
while (1) {
//efecto
if (!PORTBbits.RB7) {
__delay_ms(50);
if(++efecto==5){efecto=1;}
}
switch (efecto) {
case 1: secuencial_Efecto1();
break;
case 2: secuencial_Efecto2();
break;
case 3: secuencial_Efecto3();
break;
case 4: secuencial_Efecto4();
break;
}
}
return 1;
}
void secuencial_Efecto1(void) {
for (int i = 0; i < 8; i++) {
LATD = 1 << i;
if(velocidad==1){__delay_ms(50);}
else if(velocidad==2){__delay_ms(100);}
else if(velocidad==3){__delay_ms(200);}
else if(velocidad==4){__delay_ms(400);}
//velocidad
if (!PORTBbits.RB6) {
__delay_ms(50);
if(++velocidad==5){velocidad=1;}
}
}
}
void secuencial_Efecto2(void) {
for (int i = 0, j = 7; i < 8; i++, j--) {
LATD = (1 << i) + (1 << j);
if(velocidad==1){__delay_ms(50);}
else if(velocidad==2){__delay_ms(100);}
else if(velocidad==3){__delay_ms(200);}
else if(velocidad==4){__delay_ms(400);}
//velocidad
if (!PORTBbits.RB6) {
__delay_ms(50);
if(++velocidad==5){velocidad=1;}
}
}
}
void secuencial_Efecto3(void) {
for (int i = 0; i < 9; i++) {
LATD = (1 << i) - 1;
if(velocidad==1){__delay_ms(50);}
else if(velocidad==2){__delay_ms(100);}
else if(velocidad==3){__delay_ms(200);}
else if(velocidad==4){__delay_ms(400);}
//velocidad
if (!PORTBbits.RB6) {
__delay_ms(50);
if(++velocidad==5){velocidad=1;}
}
}
}
void secuencial_Efecto4(void) {
volatile unsigned int m=0,n=0;
for (int i = 0; i < 5; i++) {
LATD = m+n;
m += (1<
Código FUSES
#ifndef _H_FUSES_H_
#define _H_FUSES_H_
// 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 = HS // Oscillator Selection bits (HS oscillator (HS))
#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 = ON // Power-up Timer Enable bit (PWRT enabled)
#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 = OFF // PORTB A/D Enable bit (PORTB pins are configured as digital I/O on Reset)
#pragma config LPT1OSC = OFF // Low-Power Timer 1 Oscillator Enable bit (Timer1 configured for higher power operation)
#pragma config MCLRE = OFF // MCLR Pin Enable bit (RE3 input pin enabled; MCLR 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)
// 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)
#endif
Deja una respuesta
Lo siento, debes estar conectado para publicar un comentario.