PIC18

PIC18, MPLAB X IDE, XC8, GPIO, BlinkLED

CoyoteUgly 2018. 9. 11. 17:24

PIC18, MPLAB X IDE, XC8, GPIO, BlinkLED



Did you have a look at the example codes for the MCU that is provided by TI for almost every processor?


www.ti.com/lit/zip/slac536


The following examples show how to set up the clocks:

msp430fr59xx_cs_01.c       Configure MCLK for 8MHz operation
msp430fr59xx_cs_02.c       Configure MCLK for 16MHz operation
msp430fr59xx_cs_03.c       Output 32768Hz crystal on XT1 and observe failsafe
msp430fr59xx_cs_04.c       ACLK = XT1 = 32768Hz, SMCLK= XT2 = 8MHz, MCLK = DCO
msp430fr59xx_cs_05.c       Using LFXT in bypass mode, failsafe operation shown


0. 참고 사항



출처: http://coyoteugly.tistory.com/188 [마이콤 개발자를 위한 여행]

0. 참고 사항


1. HW 연결

2. New Project > Microchip Embedded > Standalone Project


3. Device > PIC18F4520


4. Hardware Tools > PICkit3


5. Compiler > XC8


6. Project Name > BlinkLED


7. New File > Microchip Embedded > XC8 Compiler > main.c


8. File Name


9. main.c


10. Production > Set Configuration Bits


11. Set Configuration Bits & Generate Source Code to Output

  • OSC > INTIO67
    • Internal oscillator 쓸려고
  • WDT > OFF
    • watchdog 쓰지 않으므로
  • LVP > OFF
    • RB5 port에 영향을 줍니다.


12. Generate Source Code to Output

출력된 코드를 main 소스에 붙여넣기합니다.


13.


14. Production > Build Main Project [ F11 ]


15. Download

Make and Program Device Main Project


16. 소스 설명

full 소스는 아래 github를 통해 확인하세요.


// CONFIG1H
//#pragma config OSC = RCIO6      // Oscillator Selection bits (External RC oscillator, port function on RA6)
#pragma config OSC = INTIO67    // Oscillator Selection bits (Internal oscillator block, port function on RA6 and RA7)

// CONFIG2H
#pragma config WDT = OFF        // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))

// CONFIG4L
//#pragma config LVP = ON         // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled)
#pragma config LVP = OFF        // Single-Supply ICSP Enable bit (Single-Supply ICSP disabled)

#include <xc.h>


/*

    _XTAL_FREQ를 선언해 주고

    pic18.h를 include하면

    __delay_ms, _delay_us 등의 함수를 사용할 수 있습니다.

*/


#define _XTAL_FREQ 1000000

#include <pic18.h>

void main(void)                
{
    TRISB=0;
    PORTB=0;

    while(1)               
    {   
        PORTB=0x00;
        __delay_ms(500);
        PORTB=0xff;
        __delay_ms(500);
    }
    return;
}


17. 소스


18. 결과

500ms 단위로 동작합니다.