This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

求一个msp430f5438A的irda demo code

如题求一个红外传输例程,找好久没找到,TI的官方例程里也没找到

  • 目前没有直接的例程,但是给出了相关的文档,您可以参考一下

    另外有客户分享了之前的程序,您也可以参考一下

    usci_a_uart_IrDA.c
    /* --COPYRIGHT--,BSD
     * Copyright (c) 2017, Texas Instruments Incorporated
     * All rights reserved.
     *
     * Redistribution and use in source and binary forms, with or without
     * modification, are permitted provided that the following conditions
     * are met:
     *
     * *  Redistributions of source code must retain the above copyright
     *    notice, this list of conditions and the following disclaimer.
     *
     * *  Redistributions in binary form must reproduce the above copyright
     *    notice, this list of conditions and the following disclaimer in the
     *    documentation and/or other materials provided with the distribution.
     *
     * *  Neither the name of Texas Instruments Incorporated nor the names of
     *    its contributors may be used to endorse or promote products derived
     *    from this software without specific prior written permission.
     *
     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
     * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     * --/COPYRIGHT--*/
    #include "driverlib.h"
    
    //******************************************************************************
    //! This example shows how to configure the UART module to echo a received
    //! character. To echo a received character, RX ISR is used.
    //!
    //!                MSP430F6638
    //!             -----------------
    //!         /|\|                 |
    //!          | |                 |
    //!          --|RST              |
    //!            |                 |
    //!            |     P2.4/UCA0TXD|------------>
    //!            |                 | 9600
    //!            |     P2.5/UCA0RXD|<------------
    //!
    //!
    //! This example uses the following peripherals and I/O signals.  You must
    //! review these and change as needed for your own board:
    //! - UART peripheral
    //! - GPIO Port peripheral (for UART pins)
    //! - UCA0TXD
    //! - UCA0RXD
    //!
    //! This example uses the following interrupt handlers.  To use this example
    //! in your own application you must add these interrupt handlers to your
    //! vector table.
    //! - USCI_A0_VECTOR.
    //******************************************************************************
    
    #define BAUD_RATE                               9600
    #define RECEIVE_DATA_COUNT                      0x02
    #define USCI_A_UART_MULTIPROCESSOR_MODE_ADDRESS        0xAA
    
    #define UCS_XT2_TIMEOUT 50000		//Desired Timeout for XT2 initialization
    #define UCS_XT1_CRYSTAL_FREQUENCY    32768		//XT1 Crystal Frequency being used
    #define UCS_XT2_CRYSTAL_FREQUENCY   4000000		//XT2 Crystal Frequency being used
    
    
    uint8_t returnValue = 0;		//Variable to store returned STATUS_SUCCESS or STATUS_FAIL
    
    uint8_t receivedData = 0x00;
    uint8_t receiveDataCount = 0x00;
    
    void Init_Clock(void);
    
    void Init_UART(void);
    
    void Init_IrDA (void);
    
    void main (void)
    {
        //Stop WDT
        WDT_A_hold(WDT_A_BASE);
    		
    		Init_Clock();
    		
    		//Init_UART();	
    		Init_IrDA();
    		__bis_SR_register(GIE);
    		
    
    		while(1)
    		{
    			USCI_A_UART_transmitData(USCI_A0_BASE,
                'M'
                );
    		}
     }
    
    //******************************************************************************
    //
    //Function definition .
    //
    //******************************************************************************
    
    void Init_Clock(void)
    {
    	/**********Clock Setup***************************************/
    		//Port select HF XT2
        GPIO_setAsPeripheralModuleFunctionInputPin(
            GPIO_PORT_P7,
            GPIO_PIN2 + GPIO_PIN3
            );
    
        //Initializes the XT1 and XT2 crystal frequencies being used
        UCS_setExternalClockSource(
            UCS_XT1_CRYSTAL_FREQUENCY,
            UCS_XT2_CRYSTAL_FREQUENCY
            );
    		
        //Initialize XT2. Returns STATUS_SUCCESS if initializes successfully
        returnValue = UCS_turnOnXT2WithTimeout(
            UCS_XT2_DRIVE_4MHZ_8MHZ,
            UCS_XT2_TIMEOUT
            );
    		
        //Select XT2 as SMCLK source
        UCS_initClockSignal(
            UCS_SMCLK,
            UCS_XT2CLK_SELECT,
            UCS_CLOCK_DIVIDER_1
            );
    		/********End**************************************************/
    }
    
    
    void Init_UART(void)
    {	
    /**********UART 1 --> USCI_A0__________Start*****************/
    		
        //----------BR 9600 @ 4MHz---------------------------
        //Baudrate = 9600, clock freq = 4MHz
        //UCBRx = 26, UCBRFx = 10, UCBRSx = 6, UCOS16 = 1
        USCI_A_UART_initParam param = {0};
        param.selectClockSource = USCI_A_UART_CLOCKSOURCE_SMCLK;
        param.clockPrescalar = 26;
        param.firstModReg = 10;
        param.secondModReg = 0xD6;
        param.parity = USCI_A_UART_NO_PARITY;
        param.msborLsbFirst = USCI_A_UART_LSB_FIRST;
        param.numberofStopBits = USCI_A_UART_ONE_STOP_BIT;
        param.uartMode = USCI_A_UART_ADDRESS_BIT_MULTI_PROCESSOR_MODE;
        param.overSampling = USCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION;
    		//---------------------------------------------------
    		
    		//Initialize USCI_A0 UART
    		USCI_A_UART_init(USCI_A0_BASE, &param);
    //		if ( STATUS_FAIL == USCI_A_UART_init(USCI_A0_BASE, &param))
    //		{
    //        return;
    //    }
    		
    		
    	//P2.4,5 = USCI_A0 TXD/RXD
        GPIO_setAsPeripheralModuleFunctionInputPin(
            GPIO_PORT_P2,
            GPIO_PIN4 + GPIO_PIN5
            );
    		
    	//Enable UART module for operation
        USCI_A_UART_enable(USCI_A0_BASE);
    		
    	//---------Rx_Interrupt-----------------	
    		//Enable Receive Interrupt
    		USCI_A_UART_clearInterrupt(USCI_A0_BASE,
            USCI_A_UART_RECEIVE_INTERRUPT
            );
        USCI_A_UART_enableInterrupt(USCI_A0_BASE,
            USCI_A_UART_RECEIVE_INTERRUPT
            );
    		//------------------------------------
    		
    /**********UART 1 --> USCI_A0___________End******************/
    		 
    		
    		
    /**********UART 2 --> USCI_A1__________Start*****************/
    		
    		//-----------BR 9600 @ 4MHz-----------------
    		//Baudrate = 9600, clock freq = 4MHz
        //UCBRx = 26, UCBRFx = 10, UCBRSx = 6, UCOS16 = 0
    		USCI_A_UART_initParam param2 = {0};
    		param2.selectClockSource = USCI_A_UART_CLOCKSOURCE_SMCLK;
        param2.clockPrescalar = 26;
        param2.firstModReg = 10;
        param2.secondModReg = 0xD6;
        param2.parity = USCI_A_UART_NO_PARITY;
        param2.msborLsbFirst = USCI_A_UART_LSB_FIRST;
        param2.numberofStopBits = USCI_A_UART_ONE_STOP_BIT;
        param2.uartMode = USCI_A_UART_ADDRESS_BIT_MULTI_PROCESSOR_MODE;
        param2.overSampling = USCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION;
    		//-----------------------------------------
    		
    		//Initialize USCI_A1 UART
    		USCI_A_UART_init(USCI_A1_BASE, &param2);
    //		if ( STATUS_FAIL == USCI_A_UART_init(USCI_A1_BASE, &param2))
    //		{
    //        return;
    //    }
    			
    		//P8.2,3 = USCI_A1 TXD/RXD
        GPIO_setAsPeripheralModuleFunctionInputPin(
            GPIO_PORT_P8,
            GPIO_PIN2 + GPIO_PIN3
            );
    		//Enable UART module for operation
        USCI_A_UART_enable(USCI_A1_BASE);
    		
    		//---------Rx_Interrupt-----------------	
    		//Enable Receive Interrupt
    		USCI_A_UART_clearInterrupt(USCI_A1_BASE,
            USCI_A_UART_RECEIVE_INTERRUPT
            );
        USCI_A_UART_enableInterrupt(USCI_A1_BASE,
            USCI_A_UART_RECEIVE_INTERRUPT
            );
    		//------------------------------------
    		
    		
    /**********UART 2 --> USCI_A1__________End*****************/
    
    }
    
    
    void Init_IrDA (void)
    {
    		//P2.4,5 = USCI_A0 TXD/RXD
        GPIO_setAsPeripheralModuleFunctionInputPin(
            GPIO_PORT_P2,
            GPIO_PIN4 + GPIO_PIN5
            );
    		
    		// Enable IrDA
        UCA0IRCTL |= UCIREN;
    		
    		// Disable eUSCI_A
        USCI_A_UART_disable(USCI_A0_BASE);                        // Set UCSWRST bit
    
    		// IrDA encoder/decoder enabled
        HWREG16(USCI_A0_BASE + OFS_UCAxIRTCTL) |= UCIREN;
    		
    		// Transmit pulse clock source BITCLK16 (16 * 1/16 clock fractions)
       // HWREG16(USCI_A1_BASE + OFS_UCAxMCTLW) |= UCOS16;           // Oversampling mode enabled
        HWREG16(USCI_A0_BASE + OFS_UCAxIRTCTL) |= UCIRTXCLK;       // Select BITCLK16
    
    	
    		// Transmit pulse length 3/16 bit period (6 half clock cycles)
        // Set UCIRTXPLx to 5
        HWREG16(USCI_A0_BASE + OFS_UCAxIRTCTL) |= UCIRTXPL2 | UCIRTXPL0;    // b101
    
    		//----------BR 9600 @ 4MHz---------------------------
        //Baudrate = 9600, clock freq = 4MHz
        //UCBRx = 26, UCBRFx = 10, UCBRSx = 6, UCOS16 = 1
        USCI_A_UART_initParam param = {0};
        param.selectClockSource = USCI_A_UART_CLOCKSOURCE_SMCLK;
        param.clockPrescalar = 26;
        param.firstModReg = 10;
        param.secondModReg = 0xD6;
        param.parity = USCI_A_UART_NO_PARITY;
        param.msborLsbFirst = USCI_A_UART_LSB_FIRST;
        param.numberofStopBits = USCI_A_UART_ONE_STOP_BIT;
        param.uartMode = USCI_A_UART_ADDRESS_BIT_MULTI_PROCESSOR_MODE;
        param.overSampling = USCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION;
    		//---------------------------------------------------
    		
    		//Initialize USCI_A0 UART
    		USCI_A_UART_init(USCI_A0_BASE, &param);
    //		if ( STATUS_FAIL == USCI_A_UART_init(USCI_A0_BASE, &param))
    //		{
    //        return;
    //    }
    
    		// Enable eUSCI_A
        USCI_A_UART_enable(USCI_A0_BASE);                         // Clear UCSWRST bit
    		
    		//---------Rx_Interrupt-----------------	
    		//Enable Receive Interrupt
    		USCI_A_UART_clearInterrupt(USCI_A0_BASE,
            USCI_A_UART_RECEIVE_INTERRUPT
            );
        USCI_A_UART_enableInterrupt(USCI_A0_BASE,
            USCI_A_UART_RECEIVE_INTERRUPT
            );
    		
    }
    
    
    //******************************************************************************
    //
    //This is the USCI_A0 interrupt vector service routine.
    //
    //******************************************************************************
    
    #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
    #pragma vector=USCI_A0_VECTOR
    __interrupt
    #elif defined(__GNUC__)
    __attribute__((interrupt(USCI_A0_VECTOR)))
    #endif
    void USCI_A0_ISR (void)
    {
        switch (__even_in_range(UCA0IV,4)){
            //Vector 2 - RXIFG
            case 2:
    
                //Receive the data
                receivedData = USCI_A_UART_receiveData(USCI_A0_BASE);
    
                //Send data back "echo"
                USCI_A_UART_transmitData(USCI_A0_BASE,
                receivedData
                );
    
                break;
            default: break;
        }
    }
    
    
    //******************************************************************************
    //
    //This is the USCI_A1 interrupt vector service routine.
    //
    //******************************************************************************
    
    #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
    #pragma vector=USCI_A1_VECTOR
    __interrupt
    #elif defined(__GNUC__)
    __attribute__((interrupt(USCI_A1_VECTOR)))
    #endif
    void USCI_A1_ISR (void)
    {
        switch (__even_in_range(UCA1IV,4)){
            //Vector 2 - RXIFG
            case 2:
    
                //Receive the data
                receivedData = USCI_A_UART_receiveData(USCI_A1_BASE);
    
                //Send data back "echo"
                USCI_A_UART_transmitData(USCI_A1_BASE,
                receivedData
                );
    
                break;
            default: break;
        }
    }

  • 很高兴能帮到您!
  • 附上5438A的 IrDa驱动代码 红外模块选用 TFDU4101

    //红外传输功能
    static void IrDAInit (void)
    {
    //引脚复用UART
    P3SEL|=(BIT4+BIT5);
    //P3DS |=(BIT4+BIT5);

    //停止UART,并选择SMCLK作为UART时钟源
    UCA0CTL1 = UCSSEL__SMCLK+UCSWRST;
    //启用红外功能
    UCA0IRCTL = UCIREN;
    //UCA0MCTL=UCOS16;

    //配置速率
    UCA0BR0 = 0X15; // 16000000/57600bps=277.777 取整为277=0x0115
    UCA0BR1 = 0x01;
    //重启UART
    UCA0CTL1 &= ~UCSWRST; // 失能软件复位标志,初始化完成
    UCA0IE |= UCRXIE; // 启用接收中断
    }

    //IrDaSend("hello world!\n",sizeof("hello world!\n"));
    //发送
    static void IrDaSend(u8* string,i32 len)
    {
    i32 idx;

    for(idx=0;idx<len;idx++)
    {
    while (!(UCA0IFG & UCTXIFG));
    UCA0TXBUF = string[idx];
    }
    }

    //接收
    //UART中断
    #pragma vector=USCI_A0_VECTOR
    __interrupt void USCI_A0_ISR(void)
    {
    u8 ch;
    switch(__even_in_range(UCA0IV,4))
    {
    case 0:// 无中断 Vector 0 - no interrupt
    break;
    case 2:// 接收中断 Vector 2 - RXIFG
    ch = UCA0RXBUF; //发送单字节数据 TX -> RXed character
    while (!(UCA0IFG & UCTXIFG));// 等待发送缓冲准备 USCI_A0 TX buffer ready?
    UCA0TXBUF=ch;
    break;
    case 4:// 发送中断 Vector 4 - TXIFG
    break;
    default: break;
    }
    }
  • 谢谢您的分享!