MCU: PIC18F45K20
IDE: MPLAB X IDE v6.00
Compiler: XC8
안녕하세요.
이번 시간에는 데이터시트를 보고 PIC18F45K20의 GPIO를 제어하도록 해 보겠습니다
보드의 정보는 아래 링크에서 확인 할 수 있습니다.
DM164130-4 - PICKIT 44-PIN DEMO BOARD (PIC18F45K20) | Microchip Technology
1. System Clock 설정
1) 내부 오실레이터 사용
2) PLL 사용
2. GPIO 설정
우선 프로젝트가 생성되면 Source Files에 main.c를 생성해줍니다.
그러면 아래와 같이 작성된 파일이 생성됩니다.
#include <xc.h>
void main(void) {
return;
}
1. System Clock 설정
아래 사진은 PIC18F45K20의 클럭 소스 블럭 다이어그램을 나타냅니다.
저희는 16MHz의 내부 오실레이터와 PLL을 사용할 것이기 때문에 빨간 라인을 따라 설정하게 됩니다.
FOSC<3:0>, OSCCON<1:0>, OSCCON<6:4>, OSCTUNE<6> 가 사용됩니다.
여기서 FOSC<3:0>은 CONFIG1H 레지스터 안에 있습니다.
하지만 이를 코드로 직접 접근 할 수 없고 Set Configuration Bits 에서 설정해야합니다. Produection > Set Configuration Bits를 클릭합니다.
그러면 아래 이미지와 같은 Configuration Bits 창이 생성됩니다. 여기서 FOSC를 INTIO67로 설정하고, Watchdog Timer Enable bit를 OFF로 설정하겠습니다.
설정이 완료되면 아래 "Generate Source Code to Output 버튼을 클릭합니다.
그러면 Config Bits Source 창이 생성되며 안에 내용을 복사하여 main.c 의 맨 위에 붙여넣기 합니다.
// PIC18F45K20 Configuration Bit Settings
// 'C' source line config statements
// CONFIG1H
#pragma config FOSC = INTIO67 // Oscillator Selection bits (Internal oscillator block, port function on RA6 and RA7)
#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 BOREN = SBORDIS // Brown-out Reset Enable bits (Brown-out Reset enabled in hardware only (SBOREN is disabled))
#pragma config BORV = 18 // Brown Out Reset Voltage bits (VBOR set to 1.8 V nominal)
// CONFIG2H
#pragma config WDTEN = OFF // Watchdog Timer Enable bit (WDT is controlled by SWDTEN bit of the WDTCON register)
#pragma config WDTPS = 32768 // Watchdog Timer Postscale Select bits (1:32768)
// CONFIG3H
#pragma config CCP2MX = PORTC // 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 Timer1 Oscillator Enable bit (Timer1 configured for higher power operation)
#pragma config HFOFST = ON // HFINTOSC Fast Start-up (HFINTOSC starts clocking the CPU without waiting for the oscillator to stablize.)
#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 XINST = OFF // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))
// CONFIG5L
#pragma config CP0 = OFF // Code Protection Block 0 (Block 0 (000800-001FFFh) not code-protected)
#pragma config CP1 = OFF // Code Protection Block 1 (Block 1 (002000-003FFFh) not code-protected)
#pragma config CP2 = OFF // Code Protection Block 2 (Block 2 (004000-005FFFh) not code-protected)
#pragma config CP3 = OFF // Code Protection Block 3 (Block 3 (006000-007FFFh) 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-001FFFh) not write-protected)
#pragma config WRT1 = OFF // Write Protection Block 1 (Block 1 (002000-003FFFh) not write-protected)
#pragma config WRT2 = OFF // Write Protection Block 2 (Block 2 (004000-005FFFh) not write-protected)
#pragma config WRT3 = OFF // Write Protection Block 3 (Block 3 (006000-007FFFh) 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-001FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF // Table Read Protection Block 1 (Block 1 (002000-003FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF // Table Read Protection Block 2 (Block 2 (004000-005FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF // Table Read Protection Block 3 (Block 3 (006000-007FFFh) 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)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
void main(void) {
return;
}
이제 나머지 설정을 하겠습니다.
16MHz로 설정하기 위해 OSCCON.IRCF 필드를 7로 설정
PLL 적용하기 위해 OSCCON.SCS 필드를 0으로 설정, OSCTUNE.PLLEN 필드를 1로 설정합니다.
void Initialize_SystemClock(void)
{
OSCCONbits.IRCF = 0b111;
OSCCONbits.SCS = 0b00;
OSCTUNEbits.PLLEN = 1;
}
2. GPIO 설정
gpio설정을 위해서는 TRISx, PORTx, LATx 가 있습니다.
// EX) PORTD TRISD: D포트 input(1) / Output(0) 설정 LATD: D포트 출력 Low(0) / High(1) 설정 PORTD: D포트 입력레벨 Read (=TRISD == 1), D포트 출력 Low(0), High(1) (=TRISD == 0) 설정 |
D포트라서 그렇지 아날로그 기능이 있는 포트의 경우 ANSELx 레지스터도 반드시 Digital로 설정해주어야 정상 동작합니다. PIC을 처음 사용하는 많은 분들이 놓치는 부분입니다.
여기서 저는 D포트를 출력포트로 사용할 것이기 때문에 TRISD = 0x00으로 설정하고 1초마다 포트 출력이 토글되도록 프로그래밍 합니다.
void Initialize_SystemClock(void)
{
OSCCONbits.IRCF = 0b111;
OSCCONbits.SCS = 0b00;
OSCTUNEbits.PLLEN = 1;
}
void Initialize_Port(void)
{
TRISD = 0x00;
}
void main(void) {
Initialize_SystemClock();
Initialize_Port();
while(1) {
LATD ^= 0xFF;
__delay_ms(1000);
}
return;
}
위 코드를 컴파일하면 아래와 같이 _XTAL_FREQ를 식별할 수 없다는 에러 메세지가 나타납니다.
이를 해결하기 위해 #define _XTAL_FREQ (64000000UL)을 main.c 상단에 넣어줍니다.
아래는 전체 코드 입니다.
// PIC18F45K20 Configuration Bit Settings
// 'C' source line config statements
// CONFIG1H
#pragma config FOSC = INTIO67 // Oscillator Selection bits (HS oscillator, PLL enabled (Clock Frequency = 4 x FOSC1))
#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 BOREN = SBORDIS // Brown-out Reset Enable bits (Brown-out Reset enabled in hardware only (SBOREN is disabled))
#pragma config BORV = 18 // Brown Out Reset Voltage bits (VBOR set to 1.8 V nominal)
// CONFIG2H
#pragma config WDTEN = OFF // Watchdog Timer Enable bit (WDT is controlled by SWDTEN bit of the WDTCON register)
#pragma config WDTPS = 32768 // Watchdog Timer Postscale Select bits (1:32768)
// CONFIG3H
#pragma config CCP2MX = PORTC // 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 Timer1 Oscillator Enable bit (Timer1 configured for higher power operation)
#pragma config HFOFST = ON // HFINTOSC Fast Start-up (HFINTOSC starts clocking the CPU without waiting for the oscillator to stablize.)
#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 XINST = OFF // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))
// CONFIG5L
#pragma config CP0 = OFF // Code Protection Block 0 (Block 0 (000800-001FFFh) not code-protected)
#pragma config CP1 = OFF // Code Protection Block 1 (Block 1 (002000-003FFFh) not code-protected)
#pragma config CP2 = OFF // Code Protection Block 2 (Block 2 (004000-005FFFh) not code-protected)
#pragma config CP3 = OFF // Code Protection Block 3 (Block 3 (006000-007FFFh) 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-001FFFh) not write-protected)
#pragma config WRT1 = OFF // Write Protection Block 1 (Block 1 (002000-003FFFh) not write-protected)
#pragma config WRT2 = OFF // Write Protection Block 2 (Block 2 (004000-005FFFh) not write-protected)
#pragma config WRT3 = OFF // Write Protection Block 3 (Block 3 (006000-007FFFh) 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-001FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF // Table Read Protection Block 1 (Block 1 (002000-003FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF // Table Read Protection Block 2 (Block 2 (004000-005FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF // Table Read Protection Block 3 (Block 3 (006000-007FFFh) 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)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
#define _XTAL_FREQ (64000000UL)
void Initialize_SystemClock(void)
{
OSCCONbits.IRCF = 0b111;
OSCCONbits.SCS = 0b00;
OSCTUNEbits.PLLEN = 1;
}
void Initialize_Port(void)
{
TRISD = 0x00;
}
void main(void) {
Initialize_SystemClock();
Initialize_Port();
while(1) {
LATD ^= 0xFF;
__delay_ms(1000);
}
return;
}
컴파일을 다시 하면 성공메세지가 나타납니다.
Make and Program Device Main Project 아이콘을 클릭하여 MCU에 프로그램을 넣습니다.
정상적으로 다운로드가 완료되었다면 Programming/Verify complete 메세지를 확인할 수 있고,
1초마다 PORTD가 토글되는 것을 볼 수 있습니다.
'MCU > PIC' 카테고리의 다른 글
[PIC][MPLABX][XC8]TIMER0 + 인터럽트 (0) | 2022.06.17 |
---|---|
Microchip Programmer & Debugger (0) | 2022.06.15 |
[PIC][MPLABX][XC8][MCC]TIMER0 + 인터럽트 (0) | 2022.06.15 |
[PIC][MPLABX][XC8] 개발환경 구축 (0) | 2022.06.11 |
[PIC][XC8][MPLAB X][MCC] GPIO (0) | 2022.06.11 |
[XC8] 컴파일 Warning 메세지 띄우지 않기 (0) | 2022.03.28 |