100% encontró este documento útil (1 voto)
38 vistas9 páginas

Configuración MPLAB XIDE para PIC 18F

Este documento contiene el código para configurar un PIC18F46K22 utilizando MPLAB X IDE y el compilador XC8. Incluye la configuración de la frecuencia de trabajo a 16MHz, la configuración de los pines I/O, y el código principal para controlar un proceso de llenado y tapado mediante el control de diferentes pines.

Cargado por

Jorge Sh
Derechos de autor
© All Rights Reserved
Nos tomamos en serio los derechos de los contenidos. Si sospechas que se trata de tu contenido, reclámalo aquí.
Formatos disponibles
Descarga como DOCX, PDF, TXT o lee en línea desde Scribd
100% encontró este documento útil (1 voto)
38 vistas9 páginas

Configuración MPLAB XIDE para PIC 18F

Este documento contiene el código para configurar un PIC18F46K22 utilizando MPLAB X IDE y el compilador XC8. Incluye la configuración de la frecuencia de trabajo a 16MHz, la configuración de los pines I/O, y el código principal para controlar un proceso de llenado y tapado mediante el control de diferentes pines.

Cargado por

Jorge Sh
Derechos de autor
© All Rights Reserved
Nos tomamos en serio los derechos de los contenidos. Si sospechas que se trata de tu contenido, reclámalo aquí.
Formatos disponibles
Descarga como DOCX, PDF, TXT o lee en línea desde Scribd

PROGRAMA MPLAB XIDE

COMPILADOR XC8
PICKIT3
PIC 18F46K22
CODE CONFIGURATOR

CODIGO DE CABECERA DE FRECUENCIA DE TRABAJO 16MHZ

#ifndef MCC_H
#define MCC_H
#include <xc.h>
#include "pin_manager.h"
#include <stdint.h>
#include <stdbool.h>

#define _XTAL_FREQ 16000000

void SYSTEM_Initialize(void);
void OSCILLATOR_Initialize(void);

#endif

CONFIGURACION DE PINES

#ifndef PIN_MANAGER_H
#define PIN_MANAGER_H

#define INPUT 1
#define OUTPUT 0

#define HIGH 1
#define LOW 0

#define ANALOG 1
#define DIGITAL 0

#define PULL_UP_ENABLED 1
#define PULL_UP_DISABLED 0

// get/set IO_RA0 aliases


#define IO_RA0_TRIS TRISAbits.TRISA0
#define IO_RA0_LAT LATAbits.LATA0
#define IO_RA0_PORT PORTAbits.RA0
#define IO_RA0_ANS ANSELAbits.ANSA0
#define IO_RA0_SetHigh() do { LATAbits.LATA0 = 1; } while(0)
#define IO_RA0_SetLow() do { LATAbits.LATA0 = 0; } while(0)
#define IO_RA0_Toggle() do { LATAbits.LATA0 = ~LATAbits.LATA0; } while(0)
#define IO_RA0_GetValue() PORTAbits.RA0
#define IO_RA0_SetDigitalInput() do { TRISAbits.TRISA0 = 1; } while(0)
#define IO_RA0_SetDigitalOutput() do { TRISAbits.TRISA0 = 0; } while(0)
#define IO_RA0_SetAnalogMode() do { ANSELAbits.ANSA0 = 1; } while(0)
#define IO_RA0_SetDigitalMode() do { ANSELAbits.ANSA0 = 0; } while(0)

// get/set IO_RA1 aliases


#define IO_RA1_TRIS TRISAbits.TRISA1
#define IO_RA1_LAT LATAbits.LATA1
#define IO_RA1_PORT PORTAbits.RA1
#define IO_RA1_ANS ANSELAbits.ANSA1
#define IO_RA1_SetHigh() do { LATAbits.LATA1 = 1; } while(0)
#define IO_RA1_SetLow() do { LATAbits.LATA1 = 0; } while(0)
#define IO_RA1_Toggle() do { LATAbits.LATA1 = ~LATAbits.LATA1; } while(0)
#define IO_RA1_GetValue() PORTAbits.RA1
#define IO_RA1_SetDigitalInput() do { TRISAbits.TRISA1 = 1; } while(0)
#define IO_RA1_SetDigitalOutput() do { TRISAbits.TRISA1 = 0; } while(0)
#define IO_RA1_SetAnalogMode() do { ANSELAbits.ANSA1 = 1; } while(0)
#define IO_RA1_SetDigitalMode() do { ANSELAbits.ANSA1 = 0; } while(0)

// get/set IO_RA2 aliases


#define IO_RA2_TRIS TRISAbits.TRISA2
#define IO_RA2_LAT LATAbits.LATA2
#define IO_RA2_PORT PORTAbits.RA2
#define IO_RA2_ANS ANSELAbits.ANSA2
#define IO_RA2_SetHigh() do { LATAbits.LATA2 = 1; } while(0)
#define IO_RA2_SetLow() do { LATAbits.LATA2 = 0; } while(0)
#define IO_RA2_Toggle() do { LATAbits.LATA2 = ~LATAbits.LATA2; } while(0)
#define IO_RA2_GetValue() PORTAbits.RA2
#define IO_RA2_SetDigitalInput() do { TRISAbits.TRISA2 = 1; } while(0)
#define IO_RA2_SetDigitalOutput() do { TRISAbits.TRISA2 = 0; } while(0)
#define IO_RA2_SetAnalogMode() do { ANSELAbits.ANSA2 = 1; } while(0)
#define IO_RA2_SetDigitalMode() do { ANSELAbits.ANSA2 = 0; } while(0)

// get/set IO_RA3 aliases


#define IO_RA3_TRIS TRISAbits.TRISA3
#define IO_RA3_LAT LATAbits.LATA3
#define IO_RA3_PORT PORTAbits.RA3
#define IO_RA3_ANS ANSELAbits.ANSA3
#define IO_RA3_SetHigh() do { LATAbits.LATA3 = 1; } while(0)
#define IO_RA3_SetLow() do { LATAbits.LATA3 = 0; } while(0)
#define IO_RA3_Toggle() do { LATAbits.LATA3 = ~LATAbits.LATA3; } while(0)
#define IO_RA3_GetValue() PORTAbits.RA3
#define IO_RA3_SetDigitalInput() do { TRISAbits.TRISA3 = 1; } while(0)
#define IO_RA3_SetDigitalOutput() do { TRISAbits.TRISA3 = 0; } while(0)
#define IO_RA3_SetAnalogMode() do { ANSELAbits.ANSA3 = 1; } while(0)
#define IO_RA3_SetDigitalMode() do { ANSELAbits.ANSA3 = 0; } while(0)

// get/set IO_RA4 aliases


#define IO_RA4_TRIS TRISAbits.TRISA4
#define IO_RA4_LAT LATAbits.LATA4
#define IO_RA4_PORT PORTAbits.RA4
#define IO_RA4_SetHigh() do { LATAbits.LATA4 = 1; } while(0)
#define IO_RA4_SetLow() do { LATAbits.LATA4 = 0; } while(0)
#define IO_RA4_Toggle() do { LATAbits.LATA4 = ~LATAbits.LATA4; } while(0)
#define IO_RA4_GetValue() PORTAbits.RA4
#define IO_RA4_SetDigitalInput() do { TRISAbits.TRISA4 = 1; } while(0)
#define IO_RA4_SetDigitalOutput() do { TRISAbits.TRISA4 = 0; } while(0)

// get/set IO_RA5 aliases


#define IO_RA5_TRIS TRISAbits.TRISA5
#define IO_RA5_LAT LATAbits.LATA5
#define IO_RA5_PORT PORTAbits.RA5
#define IO_RA5_ANS ANSELAbits.ANSA5
#define IO_RA5_SetHigh() do { LATAbits.LATA5 = 1; } while(0)
#define IO_RA5_SetLow() do { LATAbits.LATA5 = 0; } while(0)
#define IO_RA5_Toggle() do { LATAbits.LATA5 = ~LATAbits.LATA5; } while(0)
#define IO_RA5_GetValue() PORTAbits.RA5
#define IO_RA5_SetDigitalInput() do { TRISAbits.TRISA5 = 1; } while(0)
#define IO_RA5_SetDigitalOutput() do { TRISAbits.TRISA5 = 0; } while(0)
#define IO_RA5_SetAnalogMode() do { ANSELAbits.ANSA5 = 1; } while(0)
#define IO_RA5_SetDigitalMode() do { ANSELAbits.ANSA5 = 0; } while(0)

#define IO_RE0_TRIS TRISEbits.TRISE0


#define IO_RE0_LAT LATEbits.LATE0
#define IO_RE0_PORT PORTEbits.RE0
#define IO_RE0_ANS ANSELEbits.ANSE0
#define IO_RE0_SetHigh() do { LATEbits.LATE0 = 1; } while(0)
#define IO_RE0_SetLow() do { LATEbits.LATE0 = 0; } while(0)
#define IO_RE0_Toggle() do { LATEbits.LATE0 = ~LATEbits.LATE0; } while(0)
#define IO_RE0_GetValue() PORTEbits.RE0
#define IO_RE0_SetDigitalInput() do { TRISEbits.TRISE0 = 1; } while(0)
#define IO_RE0_SetDigitalOutput() do { TRISEbits.TRISE0 = 0; } while(0)
#define IO_RE0_SetAnalogMode() do { ANSELEbits.ANSE0 = 1; } while(0)
#define IO_RE0_SetDigitalMode() do { ANSELEbits.ANSE0 = 0; } while(0)

// get/set IO_RE1 aliases


#define IO_RE1_TRIS TRISEbits.TRISE1
#define IO_RE1_LAT LATEbits.LATE1
#define IO_RE1_PORT PORTEbits.RE1
#define IO_RE1_ANS ANSELEbits.ANSE1
#define IO_RE1_SetHigh() do { LATEbits.LATE1 = 1; } while(0)
#define IO_RE1_SetLow() do { LATEbits.LATE1 = 0; } while(0)
#define IO_RE1_Toggle() do { LATEbits.LATE1 = ~LATEbits.LATE1; } while(0)
#define IO_RE1_GetValue() PORTEbits.RE1
#define IO_RE1_SetDigitalInput() do { TRISEbits.TRISE1 = 1; } while(0)
#define IO_RE1_SetDigitalOutput() do { TRISEbits.TRISE1 = 0; } while(0)
#define IO_RE1_SetAnalogMode() do { ANSELEbits.ANSE1 = 1; } while(0)
#define IO_RE1_SetDigitalMode() do { ANSELEbits.ANSE1 = 0; } while(0)

// get/set IO_RE2 aliases


#define IO_RE2_TRIS TRISEbits.TRISE2
#define IO_RE2_LAT LATEbits.LATE2
#define IO_RE2_PORT PORTEbits.RE2
#define IO_RE2_ANS ANSELEbits.ANSE2
#define IO_RE2_SetHigh() do { LATEbits.LATE2 = 1; } while(0)
#define IO_RE2_SetLow() do { LATEbits.LATE2 = 0; } while(0)
#define IO_RE2_Toggle() do { LATEbits.LATE2 = ~LATEbits.LATE2; } while(0)
#define IO_RE2_GetValue() PORTEbits.RE2
#define IO_RE2_SetDigitalInput() do { TRISEbits.TRISE2 = 1; } while(0)
#define IO_RE2_SetDigitalOutput() do { TRISEbits.TRISE2 = 0; } while(0)
#define IO_RE2_SetAnalogMode() do { ANSELEbits.ANSE2 = 1; } while(0)
#define IO_RE2_SetDigitalMode() do { ANSELEbits.ANSE2 = 0; } while(0)
void PIN_MANAGER_Initialize (void);
void PIN_MANAGER_IOC(void);

#endif

CODIGO PRINCIPAL

#include "mcc_generated_files/mcc.h"

void faja();
void llenadora();
void llentap();
void tapado();

void main(void)
{
SYSTEM_Initialize();

while (1){

/*******************empieza com la faja y solo llenado*******/


faja();
llenadora();
faja();
/****************empieza proceso doble*******/
for(int a=0;a<=8; a++){

llentap();
faja();

}
tapado();
}

void faja(){

IO_RA0_SetLow();
IO_RA1_SetLow();
IO_RA2_SetLow();

__delay_ms(2000);
IO_RA0_SetHigh();
IO_RA1_SetHigh();
IO_RA2_SetHigh();
}
void llenadora(){

IO_RA3_SetLow();
IO_RA4_SetLow();
IO_RA5_SetLow();

__delay_ms(4000);
IO_RA3_SetHigh();
IO_RA4_SetHigh();
IO_RA5_SetHigh();
}
void llentap(){

IO_RA3_SetLow();
IO_RA4_SetLow();
IO_RA5_SetLow();
IO_RE0_SetLow();
IO_RE1_SetLow();
IO_RE2_SetLow();
__delay_ms(3000);
IO_RE0_SetHigh();
IO_RE1_SetHigh();
IO_RE2_SetHigh();
__delay_ms(1000);
IO_RA3_SetHigh();
IO_RA4_SetHigh();
IO_RA5_SetHigh();
}
void tapado(){

IO_RE0_SetLow();
IO_RE1_SetLow();
IO_RE2_SetLow();
__delay_ms(2000);
IO_RE0_SetHigh();
IO_RE1_SetHigh();
IO_RE2_SetHigh();

}
CONFIGURACION DE FUSES ES PARA CONFIGURAR OSCILADOR INTERNO Y EL PERRITO
GUARDIAN

#pragma config FOSC = INTIO67 // Oscillator Selection bits->Internal oscillator block


#pragma config PLLCFG = ON // 4X PLL Enable->Oscillator multiplied by 4
#pragma config PRICLKEN = ON // Primary clock enable bit->Primary clock is always enabled
#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 PWRTEN = OFF // Power-up Timer Enable bit->Power up timer disabled
#pragma config BOREN = SBORDIS // Brown-out Reset Enable bits->Brown-out Reset enabled
in hardware only (SBOREN is disabled)
#pragma config BORV = 190 // Brown Out Reset Voltage bits->VBOR set to 1.90 V nominal

// CONFIG2H
#pragma config WDTEN = OFF // Watchdog Timer Enable bits->Watch dog timer is always
disabled. SWDTEN has no effect.
#pragma config WDTPS = 32768 // Watchdog Timer Postscale Select bits->1:32768

// CONFIG3H
#pragma config CCP2MX = PORTC1 // CCP2 MUX bit->CCP2 input/output is multiplexed with
RC1
#pragma config PBADEN = ON // PORTB A/D Enable bit->PORTB<5:0> pins are configured as
analog input channels on Reset
#pragma config CCP3MX = PORTB5 // P3A/CCP3 Mux bit->P3A/CCP3 input/output is
multiplexed with RB5
#pragma config HFOFST = ON // HFINTOSC Fast Start-up->HFINTOSC output and ready status
are not delayed by the oscillator stable status
#pragma config T3CMX = PORTC0 // Timer3 Clock input mux bit->T3CKI is on RC0
#pragma config P2BMX = PORTD2 // ECCP2 B output mux bit->P2B is on RD2
#pragma config MCLRE = EXTMCLR // 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 XINST = OFF // Extended Instruction Set Enable bit->Instruction set extension
and Indexed Addressing mode disabled (Legacy mode)
#pragma config DEBUG = OFF // Background Debug->Disabled

// CONFIG5L
#pragma config CP0 = OFF // Code Protection Block 0->Block 0 (000800-003FFFh) not code-
protected
#pragma config CP1 = OFF // Code Protection Block 1->Block 1 (004000-007FFFh) not code-
protected
#pragma config CP2 = OFF // Code Protection Block 2->Block 2 (008000-00BFFFh) not code-
protected
#pragma config CP3 = OFF // Code Protection Block 3->Block 3 (00C000-00FFFFh) not code-
protected

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

// CONFIG6L
#pragma config WRT0 = OFF // Write Protection Block 0->Block 0 (000800-003FFFh) not
write-protected
#pragma config WRT1 = OFF // Write Protection Block 1->Block 1 (004000-007FFFh) not
write-protected
#pragma config WRT2 = OFF // Write Protection Block 2->Block 2 (008000-00BFFFh) not
write-protected
#pragma config WRT3 = OFF // Write Protection Block 3->Block 3 (00C000-00FFFFh) not
write-protected

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

// CONFIG7L
#pragma config EBTR0 = OFF // Table Read Protection Block 0->Block 0 (000800-003FFFh) not
protected from table reads executed in other blocks
#pragma config EBTR1 = OFF // Table Read Protection Block 1->Block 1 (004000-007FFFh) not
protected from table reads executed in other blocks
#pragma config EBTR2 = OFF // Table Read Protection Block 2->Block 2 (008000-00BFFFh) not
protected from table reads executed in other blocks
#pragma config EBTR3 = OFF // Table Read Protection Block 3->Block 3 (00C000-00FFFFh) not
protected from table reads executed in other blocks

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

#include "mcc.h"

void SYSTEM_Initialize(void)
{

PIN_MANAGER_Initialize();
OSCILLATOR_Initialize();
}

void OSCILLATOR_Initialize(void)
{
// SCS INTOSC; IRCF 16MHz_HFINTOSC; IDLEN disabled;
OSCCON = 0x72;
// PRISD enabled; SOSCGO disabled; MFIOSEL disabled;
OSCCON2 = 0x04;
// INTSRC disabled; PLLEN disabled; TUN 0;
OSCTUNE = 0x00;
}

CONFIGURACION DE REGISTROS TIPO FUCES

#include <xc.h>
#include "pin_manager.h"
#include "stdbool.h"

void PIN_MANAGER_Initialize(void)
{
/**
LATx registers
*/
LATE = 0x00;
LATD = 0x00;
LATA = 0x00;
LATB = 0x00;
LATC = 0x00;

/**
TRISx registers
*/
TRISE = 0x00;
TRISA = 0x00;
TRISB = 0xFF;
TRISC = 0xF0;
TRISD = 0xFC;

/**
ANSELx registers
*/
ANSELC = 0x00;
ANSELB = 0x00;
ANSELD = 0x00;
ANSELE = 0x00;
ANSELA = 0x00;

/**
WPUx registers
*/
WPUB = 0xFF;
[Link] = 0;

}
void PIN_MANAGER_IOC(void)
{

También podría gustarte