首先確定APP001上的LED燈,LED D1 ~ D8使用到PIC18F45K22那些腳位?
由下圖可知,LED D1 ~ D8接到ERD0 ~ ERD7
而ERD0 ~ ERD7又接到PIC18F45K22的RD0 ~RD7
確認完腳位後就可以開始寫程式了
我想做一個跑馬燈從左至右
 
首先創建新專案,這部分因為在上一篇有分享,這邊就不重複了
檔名我取Lab1
 
專案建好後,啟動MCC,先去改包裝,因為我是用DIP
所以Package從UQFN40改成PDIP40
 
去設定RD0 ~ RD7為GPIO的Output
 
1.勾選右邊視窗Pin Module
2.Project Resources切到Pin Module
3.Custom Name改名及取消勾選Analog
最後Generate Code
 
 
初始化的Code產生完後,會記錄在.c與.h檔內,因此我們要用時需要在自己的程式中include
接下來可以開始寫Lab1所需要的功能了
 
這邊我發現使用MCC會幫你創建main.c
但若是使用MCC前先自行加入main.c那麼用MCC產生Code之後
就不會在創建main.c了,而是用原先創立的
而使用MCC幫你創建的main.c有一些好處
就是已經幫你include MCC先前創建的code
以及已經在程式開啟後做好初始化了
 
整段程式寫在main.c
會用到一些函式指標是因為要配合MCC產生的函式呼叫做搭配
的功能
這邊也使用到Timer1幫我做500ms的Delay
同樣也是之前有分享怎麼用MCC產生Timer
這邊也不再重複
 
/**
  Generated Main Source File
 
  Company:
    Microchip Technology Inc.
 
  File Name:
    main.c
 
  Summary:
    This is the main file generated using MPLAB(c) Code Configurator
 
  Description:
    This header file provides implementations for driver APIs for all modules selected in the GUI.
    Generation Information :
        Product Revision  :  MPLAB(c) Code Configurator - v3.00
        Device            :  PIC18F45K22
        Driver Version    :  2.00
    The generated drivers are tested against the following:
        Compiler          :  XC8 1.35
        MPLAB             :  MPLAB X 3.20
*/
 
/*
Copyright (c) 2013 - 2015 released Microchip Technology Inc.  All rights reserved.
 
Microchip licenses to you the right to use, modify, copy and distribute
Software only when embedded on a Microchip microcontroller or digital signal
controller that is integrated into your product or third party product
(pursuant to the sublicense terms in the accompanying license agreement).
 
You should refer to the license agreement accompanying this Software for
additional information regarding your rights and obligations.
 
SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF
MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
IN NO EVENT SHALL MICROCHIP OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER
CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR
OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE OR
CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT OF
SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
(INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
 */
 
#include "mcc_generated_files/mcc.h"
 
typedef void (*ActionFunc_Deftype)(void);
 
void action1()
{
    LED_D1_SetHigh();
    LED_D2_SetLow();
    LED_D3_SetLow();
    LED_D4_SetLow();
    LED_D5_SetLow();
    LED_D6_SetLow();
    LED_D7_SetLow();
    LED_D8_SetLow();
}
 
void action2()
{
    LED_D1_SetLow();
    LED_D2_SetHigh();
    LED_D3_SetLow();
    LED_D4_SetLow();
    LED_D5_SetLow();
    LED_D6_SetLow();
    LED_D7_SetLow();
    LED_D8_SetLow();
}
 
void action3()
{
    LED_D1_SetLow();
    LED_D2_SetLow();
    LED_D3_SetHigh();
    LED_D4_SetLow();
    LED_D5_SetLow();
    LED_D6_SetLow();
    LED_D7_SetLow();
    LED_D8_SetLow();
}
 
void action4()
{
    LED_D1_SetLow();
    LED_D2_SetLow();
    LED_D3_SetLow();
    LED_D4_SetHigh();
    LED_D5_SetLow();
    LED_D6_SetLow();
    LED_D7_SetLow();
    LED_D8_SetLow();
}
 
void action5()
{
    LED_D1_SetLow();
    LED_D2_SetLow();
    LED_D3_SetLow();
    LED_D4_SetLow();
    LED_D5_SetHigh();
    LED_D6_SetLow();
    LED_D7_SetLow();
    LED_D8_SetLow();
}
 
void action6()
{
    LED_D1_SetLow();
    LED_D2_SetLow();
    LED_D3_SetLow();
    LED_D4_SetLow();
    LED_D5_SetLow();
    LED_D6_SetHigh();
    LED_D7_SetLow();
    LED_D8_SetLow();
}
 
void action7()
{
    LED_D1_SetLow();
    LED_D2_SetLow();
    LED_D3_SetLow();
    LED_D4_SetLow();
    LED_D5_SetLow();
    LED_D6_SetLow();
    LED_D7_SetHigh();
    LED_D8_SetLow();
}
 
void action8()
{
    LED_D1_SetLow();
    LED_D2_SetLow();
    LED_D3_SetLow();
    LED_D4_SetLow();
    LED_D5_SetLow();
    LED_D6_SetLow();
    LED_D7_SetLow();
    LED_D8_SetHigh();
}
 
ActionFunc_Deftype actions[8]={
    action1,action2,action3,action4,
    action5,action6,action7,action8
};
 
/*
                         Main application
 */
void main(void)
{
    // Initialize the device
    SYSTEM_Initialize();
 
    // If using interrupts in PIC18 High/Low Priority Mode you need to enable the Global High and Low Interrupts
    // If using interrupts in PIC Mid-Range Compatibility Mode you need to enable the Global and Peripheral Interrupts
    // Use the following macros to:
 
    // Enable high priority global interrupts
    //INTERRUPT_GlobalInterruptHighEnable();
 
    // Enable low priority global interrupts.
    //INTERRUPT_GlobalInterruptLowEnable();
 
    // Disable high priority global interrupts
    //INTERRUPT_GlobalInterruptHighDisable();
 
    // Disable low priority global interrupts.
    //INTERRUPT_GlobalInterruptLowDisable();
 
    // Enable the Global Interrupts
    //INTERRUPT_GlobalInterruptEnable();
 
    // Enable the Peripheral Interrupts
    //INTERRUPT_PeripheralInterruptEnable();
 
    // Disable the Global Interrupts
    //INTERRUPT_GlobalInterruptDisable();
 
    // Disable the Peripheral Interrupts
    //INTERRUPT_PeripheralInterruptDisable();
 
    while (1)
    {
        // Add your application code
        unsigned char i;
        for(i=0;i<8;i++)
        {
            (actions[i])();
            // Delay 500 millsec
            while(TMR1_HasOverflowOccured() == 0);
            PIR1bits.TMR1IF = 0;
        }
    }
}
/**
 End of File
*/
 
 
 
 
 
 
 
 
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 凱文先生的部落格 的頭像
    凱文先生的部落格

    凱文的八卦俱樂部小天地

    凱文先生的部落格 發表在 痞客邦 留言(0) 人氣()