Tivec 마이크로컨트롤러 기본 연습 1.6 소스

이 실험 소스는 참조 코드로서 EK-TM4C123GXL에서 검증할 수 있습니다.
시범 코드는 가장 좋은 코드가 아니라 이해하기 쉬운 측면에서 가장 잘 이해할 수 있는 코드를 얻는 것이다.
//********************************************************************************//기초연습 1.6: 키를 눌러 LED 유수등을 제어///프로세스 설명://1.하드웨어 회로에서 알 수 있듯이 버튼을 누르지 않았을 때, 읽기 가이드의 값은 고전평이다.버튼을 눌렀을 때 접지에서 발을 끄는 값을 읽는 것은 저전평이다.//키와 관련된 가이드를 GPIO로 설정하고 입력하십시오.폴링 방식으로 키를 읽습니다(무중단 방식).// 2. 트랙을 GPIO, 출력으로 설정합니다.높은 레벨 및 낮은 레벨 출력을 통해 LED의 밝기와 끄기를 제어합니다.// 3. 저전기 평상시로 읽고 사용자 버튼을 설명한다.//버튼에 따라 상응하는 동작을 한다.// 4.//D2, D3, D4는 유수등을 실현하고 버튼을 통해 제어할 수 있다.//시스템에서 3개의 램프가 모두 켜져 있으며 기본적으로 왼쪽에서 오른쪽의 순서로 켜져 있습니다.//K1을 눌러 자동 모드로 들어가기;설정된 순서대로 자동 표시;//K2를 누르면 유수등을 멈추고 3개의 불이 모두 켜집니다.//K3를 누르고 수동 모드로 들어가 왼쪽에서 오른쪽으로 순서를 바꿉니다.한 번씩 누르면 디스플레이//K4를 누르고 수동 모드로 들어가 오른쪽에서 왼쪽으로 순서가 바뀐다.클릭할 때마다 디스플레이///하드웨어 설명://LED2(파란색)--PF0//LED3(녹색)--PA4//LED4(빨간색)--PD6//K1 - PD7 //K2 - PF4 //K3 - PA3 //K4--PA2 //주의사항://트랙 PF0과 PD7은 비교적 특수하고 기본적으로 보호됩니다.다시 프로그래밍을 하려면 먼저 잠금 해제//팁://SysConfig 도구를 통해 발을 끄는 초기 설정을 해야 합니다. 이런 요소를 고려할 필요가 없습니다.//SysConfig 사용 참조 링크:https://www.bilibili.com/read/cv6258251/////사고://1.어떻게 중단을 통해 키를 누릅니까?///**********************************************************************************************
#include 
#include 
#include "inc/hw_gpio.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/gpio.h"
#include "driverlib/sysctl.h"

//   ,           
#define SYSTEM_MODE_STOP      0
#define SYSTEM_MODE_AUTO      1
#define SYSTEM_MODE_MAMUAL    2

//   ,         LED  
#define D4_ON_100     1
#define D3_ON_010     2
#define D2_ON_001     3
#define ALL_ON_111    4

//   ,    LED     
#define FROM_L_TO_R   0
#define FROM_R_TO_L   1

//   ,               LED
#define LED_OPERATION_TODO  0
#define LED_OPERATION_DONE  1


//    

uint8_t system_led_direction; //led     
uint8_t system_mode; //           ,      
uint8_t system_led_status; //      LED   .      
uint8_t flag_led_operation;  //     ,      LED


//      ,     LED 
//    :
//  D4_ON_100 / D3_ON_010 / D2_ON_001         LED   
//      ,    
//
void display_led(uint8_t led_config)
{
    switch (led_config)
    {
        case D2_ON_001:
            GPIOPinWrite(GPIO_PORTD_BASE,GPIO_PIN_6,0xFF);
            GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_4,0xFF);
            GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_0,0x00);
            break;
        case D3_ON_010:
            GPIOPinWrite(GPIO_PORTD_BASE,GPIO_PIN_6,0xFF);
            GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_4,0x00);
            GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_0,0xFF);
            break;
        case D4_ON_100:
            GPIOPinWrite(GPIO_PORTD_BASE,GPIO_PIN_6,0x00);
            GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_4,0xFF);
            GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_0,0xFF);
            break;
        default:
            GPIOPinWrite(GPIO_PORTD_BASE,GPIO_PIN_6,0x00);
            GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_4,0x00);
            GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_0,0x00);
            break;
    }

}

//led    .
//           
// system_led_direction:LED     
// system_led_status:  LED   
//         ,            LED
//
void move_led(void)
{
    if(FROM_L_TO_R == system_led_direction)
    {
        switch(system_led_status)
        {
            case D4_ON_100:
                system_led_status=D3_ON_010;
                display_led(D3_ON_010);
                break;
            case D3_ON_010:
                system_led_status=D2_ON_001;
                display_led(D2_ON_001);
                break;
            case D2_ON_001:
                system_led_status=D4_ON_100;
                display_led(D4_ON_100);
                break;
            case ALL_ON_111:
                //        
                system_led_status=D4_ON_100;
                display_led(D4_ON_100);
                break;
            default:
                break;
        }
    }
    else
    {
        switch(system_led_status)
        {
            case D4_ON_100:
                system_led_status=D2_ON_001;
                display_led(D2_ON_001);
                break;
            case D3_ON_010:
                system_led_status=D4_ON_100;
                display_led(D4_ON_100);
                break;
            case D2_ON_001:
                system_led_status=D3_ON_010;
                display_led(D3_ON_010);
                break;
            case ALL_ON_111:
                //        
                system_led_status=D2_ON_001;
                display_led(D2_ON_001);
                break;
            default:
                break;
        }

    }
}

//gpio      
//       ,         
void gpio_init(void)
{
    //-----------    -------------
    //  PA     ,   Ready
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
    while(!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOA))
    {
    }

    //  PD     ,   Ready
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
    while(!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOD))
    {
    }

    //  PF     ,   Ready
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
    while(!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOF))
    {
    }

    //-----------    -------------

    //PA4->  LED, PA4   GPIO,  
    GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE, GPIO_PIN_4);

    //PD6->  LED, PD6   GPIO,  
    GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_6);

    //PF0->  LED, PF0   GPIO,  
    HWREG(GPIO_PORTF_BASE+GPIO_O_LOCK) = GPIO_LOCK_KEY;
    HWREG(GPIO_PORTF_BASE+GPIO_O_CR)   |= GPIO_PIN_0;
    HWREG(GPIO_PORTF_BASE+GPIO_O_LOCK) = 0x0;
    GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_0);

    //PD7->K1, PD7   GPIO,  
    HWREG(GPIO_PORTD_BASE+GPIO_O_LOCK) = GPIO_LOCK_KEY;
    HWREG(GPIO_PORTD_BASE+GPIO_O_CR)   |= GPIO_PIN_7;
    HWREG(GPIO_PORTD_BASE+GPIO_O_LOCK) = 0x0;
    GPIOPinTypeGPIOInput(GPIO_PORTD_BASE, GPIO_PIN_7);

    //PF4->K2, PF4   GPIO,  
    GPIOPinTypeGPIOInput(GPIO_PORTF_BASE, GPIO_PIN_4);

    //PA3->K3, PA3   GPIO,  
    GPIOPinTypeGPIOInput(GPIO_PORTA_BASE, GPIO_PIN_3);

    //PA2->K4, PA2   GPIO,  
    GPIOPinTypeGPIOInput(GPIO_PORTA_BASE, GPIO_PIN_2);

}

void main(void)
{
    uint32_t ui32_delay;

    //   LED     
    gpio_init();

    //LED     
    display_led(ALL_ON_111);

    //         
    system_led_direction = FROM_L_TO_R;
    system_mode =SYSTEM_MODE_STOP;
    system_led_status = ALL_ON_111;
    flag_led_operation = LED_OPERATION_DONE;

    while(1)
    {
        //               

        //-----------------------------------------------------
        //     。
        //        ,               ,           
        //-------------------------------------------------------

        //PD7->K1
        if(0 == GPIOPinRead(GPIO_PORTD_BASE,GPIO_PIN_7))
        {
            system_mode =SYSTEM_MODE_AUTO;

            //      
            while(0 == GPIOPinRead(GPIO_PORTD_BASE,GPIO_PIN_7));
        }

        //PF4->K2
        if(0 == GPIOPinRead(GPIO_PORTF_BASE,GPIO_PIN_4))
        {
            system_mode =SYSTEM_MODE_STOP;
            flag_led_operation = LED_OPERATION_TODO;
            //      
            while(0 == GPIOPinRead(GPIO_PORTF_BASE,GPIO_PIN_4));
        }

        //PA3->K3
        if(0 == GPIOPinRead(GPIO_PORTA_BASE,GPIO_PIN_3))
        {
            system_mode =SYSTEM_MODE_MAMUAL;
            system_led_direction = FROM_L_TO_R;
            flag_led_operation = LED_OPERATION_TODO;

            //      
            while(0 == GPIOPinRead(GPIO_PORTA_BASE,GPIO_PIN_3));
        }

        //PA2->K4
        if(0 == GPIOPinRead(GPIO_PORTA_BASE,GPIO_PIN_2))
        {
            system_mode =SYSTEM_MODE_MAMUAL;
            system_led_direction = FROM_R_TO_L;
            flag_led_operation = LED_OPERATION_TODO;

            //      
            while(0 == GPIOPinRead(GPIO_PORTA_BASE,GPIO_PIN_2));
        }

        //-----------------------------------------------------
        //
        // LED    
        //
        //-------------------------------------------------------
        if(SYSTEM_MODE_STOP == system_mode)
        {
            //        LED    ,      ,         
            if(LED_OPERATION_TODO == flag_led_operation)
            {
                flag_led_operation = LED_OPERATION_DONE;
                system_led_status = ALL_ON_111;
                display_led(ALL_ON_111);
            }

        }
        else if(SYSTEM_MODE_MAMUAL == system_mode)
        {
            //     ,         ,           
            if(LED_OPERATION_TODO == flag_led_operation)
            {
                flag_led_operation = LED_OPERATION_DONE;
                move_led();
            }
        }
        else if(SYSTEM_MODE_AUTO == system_mode)
        {
            move_led();
            //  ,      
            for(ui32_delay=0;ui32_delay<300000;ui32_delay++);
        }
    }

}

좋은 웹페이지 즐겨찾기