01第一節(jié) 配置字
前言:器件選擇PIC18F4520
(1)配置字的含義:PIC系列的單片機都有配置字,這是從宏觀設置單片機功能的寄存器,我們不需要手動配置。
首先我們新建一個源文件main.c
在包含頭文件的前面單擊,讓光標在前面。
在工具欄中點擊Production->set Configutation Bits


配置好之后點擊紅色框就將配置的信息插入到main.c 的頭文件包含語句的前面了。這個必須在包含頭文件的語句之前。
(2)配置字的解釋:
// CONFIG1H
#pragma config OSC = HS         // Oscillator Selection bits (HS oscillator)--振蕩器選擇位(HS振蕩器)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)--失效保護時鐘監(jiān)視器啟用位(失效保護時鐘監(jiān)視器禁用)
#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)--通電定時器啟用位(PWRT禁用)
#pragma config BOREN = SBORDIS  // Brown-out Reset Enable bits (Brown-out Reset enabled in hardware only (SBOREN is disabled))--失電輸出復位啟用位(僅在硬件中啟用失電輸出復位(禁用SBOREN))
#pragma config BORV = 3         // Brown Out Reset Voltage bits (Minimum setting)--失電復位電壓位(最小設置)
// 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)--看門狗定時器啟用位(WDT禁用(控制置于SWDTEN位))
// CONFIG3H
#pragma config CCP2MX = PORTC   // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)--看門狗定時器啟用位(WDT禁用(控制置于SWDTEN位上))CCP2 MUX位(CCP2輸入/輸出與RC1多路復用)
#pragma config PBADEN = OFF      // PORTB A/D Enable bit (PORTB< 4:0 >pins are configured as analog input channels on Reset)--端口B A/D啟用位(端口B< 4:0 >引腳在復位時配置為模擬輸入通道)
#pragma config LPT1OSC = OFF    // Low-Power Timer1 Oscillator Enable bit (Timer1 configured for higher power operation)--端口B A/D啟用位(復位時端口B< 4:0 >引腳配置為模擬輸入通道)低功率定時器1振蕩器啟用位(定時器1配置為高功率操作)
#pragma config MCLRE = OFF      // MCLR Pin Enable bit (RE3 input pin enabled; MCLR disabled) ---MCLR引腳啟用位(RE3輸入引腳啟用;MCLR禁用)
// CONFIG4L
#pragma config STVREN = OFF     // Stack Full/Underflow Reset Enable bit (Stack full/underflow will not cause Reset)--堆棧滿/下溢復位啟用位(堆棧滿/下溢不會導致復位)
#pragma config LVP = OFF        // Single-Supply ICSP Enable bit (Single-Supply ICSP disabled)--單電源ICSP啟用位(單電源ICSP禁用)
#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) not code-protected)--代碼保護位(塊0(000800-001FFFh)不受代碼保護)
#pragma config CP1 = OFF        // Code Protection bit (Block 1 (002000-003FFFh) not code-protected)--代碼保護位(塊1(002000-003FFFh)不受代碼保護)
#pragma config CP2 = OFF        // Code Protection bit (Block 2 (004000-005FFFh) not code-protected)--代碼保護位(塊2(004000-005FFFh)不受代碼保護)
#pragma config CP3 = OFF        // Code Protection bit (Block 3 (006000-007FFFh) not code-protected)--代碼保護位(塊3(006000-007FFFh)不受代碼保護)
// CONFIG5H
#pragma config CPB = OFF        // Boot Block Code Protection bit (Boot block (000000-0007FFh) not code-protected)--引導塊代碼保護位(引導塊(000000-0007FFh)不受代碼保護)
#pragma config CPD = OFF        // Data EEPROM Code Protection bit (Data EEPROM not code-protected)--數據EEPROM代碼保護位(數據EEPROM不受代碼保護)
// CONFIG6L
#pragma config WRT0 = OFF       // Write Protection bit (Block 0 (000800-001FFFh) not write-protected)--寫保護位(塊0(000800-001FFFh)未寫保護)
#pragma config WRT1 = OFF       // Write Protection bit (Block 1 (002000-003FFFh) not write-protected)--寫保護位(塊1(002000-003FFFh)未寫保護)
#pragma config WRT2 = OFF       // Write Protection bit (Block 2 (004000-005FFFh) not write-protected)--寫保護位(塊2(004000-005FFFh)不寫保護)
#pragma config WRT3 = OFF       // Write Protection bit (Block 3 (006000-007FFFh) not write-protected)--寫保護位(塊3(006000-007FFFh)未寫保護)
// CONFIG6H
#pragma config WRTC = OFF       // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) not write-protected)--配置寄存器寫保護位(配置寄存器(300000-3000FFh)不寫保護)
#pragma config WRTB = OFF       // Boot Block Write Protection bit (Boot block (000000-0007FFh) not write-protected)--引導塊寫保護位(引導塊(000000-0007FFh)未寫保護)
#pragma config WRTD = OFF       // Data EEPROM Write Protection bit (Data EEPROM not write-protected)--數據EEPROM寫保護位(數據EEPROM不寫保護)
// CONFIG7L
#pragma config EBTR0 = OFF      // Table Read Protection bit (Block 0 (000800-001FFFh) not protected from table reads executed in other blocks)--表讀取保護位(塊0(000800-001FFFh)不受其他塊中執(zhí)行的表讀取的保護)
#pragma config EBTR1 = OFF      // Table Read Protection bit (Block 1 (002000-003FFFh) not protected from table reads executed in other blocks)--表讀取保護位(塊1(002000-003FFFh)不受其他塊中執(zhí)行的表讀取的保護)
#pragma config EBTR2 = OFF      // Table Read Protection bit (Block 2 (004000-005FFFh) not protected from table reads executed in other blocks)--表讀取保護位(塊2(004000-005FFFh)不受其他塊中執(zhí)行的表讀取的保護)
#pragma config EBTR3 = OFF      // Table Read Protection bit (Block 3 (006000-007FFFh) not protected from table reads executed in other blocks)--表讀取保護位(塊3(006000-007FFFh)不受在其他塊中執(zhí)行的表讀取的保護)
// CONFIG7H
#pragma config EBTRB = OFF      // Boot Block Table Read Protection bit (Boot block (000000-0007FFh) not protected from table reads executed in other blocks)--引導塊表讀取保護位(引導塊(000000-0007FFh)不受在其他塊中執(zhí)行的表讀取的保護)
// #pragma config statements should precede project file includes.--pragma config語句應該在project file includes之前
// Use project enums instead of #define for ON and OFF.
我們主要選擇配置好時鐘、關閉看門狗,其他的默認就可以了。如果在產品中要保護代碼段可以根據具體要求設置,使用ADC是要打開PBADED ON;
02第二節(jié) 代碼編寫
我們在main.c 中添加宏:
#define _XTAL_FREQ 40000000 //定義時鐘為40MHz
#define LED_PORT_DIR TRISD
#define LED_PORT_DATD LATD
const unsigned char LED[8]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
第一個宏是定義時鐘的頻率,第二是定義led燈的端口方向寄存器,第三個是定義端口的寄存器。
第一個函數:初始化控制LED燈的端口,配置為輸出方向,初始值為0;
第四個是變量,實現流水燈的。
void pic18_led_port_init(void)
{
  LED_PORT_DIR=0;   //Setting output direction for LED stream .
  LED_PORT_DATD=0;  // Init LED port data to low voltage.
}
第二個函數是實現流水燈:
void led_stream_show(void)
{
    unsigned char i;
    for(i=0;i< < span="" >8;i++)
    {
        LATD=LED[i];
        __delay_ms(1000);  //延時1s
    }
    LATD=0xFF;
    __delay_ms(1000);
    LATD=0;
    __delay_ms(1000);
}
使用_delayms(1000);這個是系統函數,前面定義了時鐘頻率,就可以直接使用了。
我們編寫main函數看效果:
void main(void)
{
    pic18_led_port_init();
    while(1)
    {
        led_stream_show();
    }
}


因為手頭開發(fā)板不在,所以在proteus中PORTD端口安放了8個led燈仿真了下,是可以的。
03總結
LED燈的實現主要使用了單片機的I/O 端口,方向寄存器設置為0,輸出,在PORTD端口的數據鎖存器寫入值就可以控制LED燈的亮滅。

從圖中就可以看出,如果要讀PORTD端口的值則要使用PORTD寄存器。寫使用LATD。再加一點延遲函數,就實現流水燈的效果。
 電子發(fā)燒友App
                        電子發(fā)燒友App
                     
                 
                 
           
        
 
        








 
            
             
             
                 
             工商網監(jiān)
工商網監(jiān)
        
評論