|
|||||||||||
| 技術(shù)交流 | 電路欣賞 | 工控天地 | 數(shù)字廣電 | 通信技術(shù) | 電源技術(shù) | 測控之家 | EMC技術(shù) | ARM技術(shù) | EDA技術(shù) | PCB技術(shù) | 嵌入式系統(tǒng) 驅(qū)動編程 | 集成電路 | 器件替換 | 模擬技術(shù) | 新手園地 | 單 片 機 | DSP技術(shù) | MCU技術(shù) | IC 設(shè)計 | IC 產(chǎn)業(yè) | CAN-bus/DeviceNe |
求助: AVR中USI與SPI接口串行通訊編程例程 |
| 作者:hotpower 欄目:單片機 |
在網(wǎng)上和好友那里都不找到可心的例程,十分郁悶. 想用M16的SPI(主)與T26的USI(從)進行SPI協(xié)議進行主從串行通訊,而且USI最好是采用中斷方式. 謝謝!!! HotPower@126.com |
| 2樓: | >>參與討論 |
| 作者: javie 于 2005/12/4 10:10:00 發(fā)布:
正在實驗中...用的是M128 |
|
| 3樓: | >>參與討論 |
| 作者: hotpower 于 2005/12/4 11:47:00 發(fā)布:
謝謝...不會這樣簡單吧 // This file has been prepared for Doxygen automatic documentation generation. /*! \file ******************************************************************** * * ATMEL CORPORATION * * \li File: spi_via_usi_driver.c * \li Compiler: IAR EWAAVR 3.10c * \li SUPPORT mail: avr@atmel.com * * \li SUPPORTed devices: All devices with Universal Serial Interface (USI) * capabilities can be used. * The example is written for ATmega169. * * \li AppNote: AVR319 - Using the USI MODULE for SPI communication. * * \li DESCRIPTION: Example on how to use the USI MODULE for communicating * with SPI compatible devices. The functions and variables * prefixed 'spiX_' can be renamed to be able to use several * spi drivers (using different interfaces) with similar names. * Some basic SPI knowledge is assumed. * * $Revision: 1.4 $ * $Date: Monday, September 13, 2004 12:08:54 UTC $ ****************************************************************************/ #include <ioavr.h> #include <inavr.h> /* USI port and pin definitions. */ #define USI_OUT_REG PORTE //!< USI port OUTPUT register. #define USI_IN_REG PINE //!< USI port input register. #define USI_DIR_REG DDRE //!< USI port direction register. #define USI_CLOCK_PIN PE4 //!< USI clock I/O pin. #define USI_DATAIN_PIN PE5 //!< USI data input pin. #define USI_DATAOUT_PIN PE6 //!< USI data OUTPUT pin. /* Speed configuration: * Bits per second = CPUSPEED / PRESCALER / (COMPAREVALUE+1) / 2. * Maximum = CPUSPEED / 64. */ #define TC0_PRESCALER_VALUE 256 //!< Must be 1, 8, 64, 256 or 1024. #define TC0_COMPARE_VALUE 1 //!< Must be 0 to 255. MINIMUM 31 with prescaler CLK/1. /* Prescaler VALUE converted to bit settings. */ #if TC0_PRESCALER_VALUE == 1 #define TC0_PS_SETTING (1<<CS00) #elif TC0_PRESCALER_VALUE == 8 #define TC0_PS_SETTING (1<<CS01) #elif TC0_PRESCALER_VALUE == 64 #define TC0_PS_SETTING (1<<CS01)|(1<<CS00) #elif TC0_PRESCALER_VALUE == 256 #define TC0_PS_SETTING (1<<CS02) #elif TC0_PRESCALER_VALUE == 1024 #define TC0_PS_SETTING (1<<CS02)|(1<<CS00) #else #error Invalid T/C0 prescaler setting. #endif /*! \brief Data input register buffer. * * Incoming bytes are stored in this byte until the next transfer is complete. * This byte can be used the same way as the SPI data register in the native * SPI MODULE, which means that the byte must be read before the next transfer * completes and overwrites the current VALUE. */ unsigned CHAR storedUSIDR; /*! \brief Driver status bit structure. * * This struct contains status flags for the driver. * The flags have the same meaning as the corresponding status flags * for the native SPI MODULE. The flags should not be changed by the user. * The driver takes care of updating the flags when required. */ struct usidriverStatus_t { unsigned CHAR MASTERMode : 1; //!< True if in MASTER mode. unsigned CHAR transferComplete : 1; //!< True when transfer completed. unsigned CHAR writeCollision : 1; //!< True if put attempted during transfer. }; volatile struct usidriverStatus_t spiX_status; //!< The driver status bits. /*! \brief Timer/Counter 0 Compare Match Interrupt handler. * * This interrupt handler is ONLY enabled when transferring data * in MASTER mode. It toggles the USI clock pin, i.e. two interrupts * results in one clock period on the clock pin and for the USI counter. */ #pragma vector=TIMER0_COMP_vect __interrupt void timer0comp_handler() { USICR |= (1<<USITC); // Toggle clock OUTPUT pin. } /*! \brief USI Timer Overflow Interrupt handler. * * This handler disables the compare match interrupt if in MASTER mode. * When the USI counter overflows, a byte has been transferred, and we * have to stop the timer tick. * For all modes the USIDR contents are stored and flags are updated. */ #pragma vector=USI_OVF_vect __interrupt void usiovf_handler() //USI計數(shù)器溢出中斷程序 { // MASTER must now disable the compare match interrupt // to prevent more USI counter clocks. if( spiX_status.MASTERMode == 1 ) { TIMSK0 &= ~(1<<OCIE0A); } // Update flags and clear USI counter USISR = (1<<USIOIF); //清除計數(shù)器溢出標志 spiX_status.transferComplete = 1; // Copy USIDR to buffer to prevent overwrite on next transfer. storedUSIDR = USIDR; //讀USI接收數(shù)據(jù) } /*! \brief Initialize USI as SPI MASTER. * * This function sets up all pin directions and MODULE configurations. * Use this function initially or when changing from slave to MASTER mode. * Note that the stored USIDR VALUE is cleared. * * \param spi_mode Required SPI mode, must be 0 or 1. */ void spiX_initMASTER( CHAR spi_mode ) { // Configure port directions. &nbs |
|
| 4樓: | >>參與討論 |
| 作者: javie 于 2005/12/4 14:42:00 發(fā)布:
狂ft。。。 |
|
| 5樓: | >>參與討論 |
| 作者: hotpower 于 2005/12/4 19:02:00 發(fā)布:
暈到,在水池里做完實驗否???
|
|
| 6樓: | >>參與討論 |
| 作者: javie 于 2005/12/4 20:49:00 發(fā)布:
不敢了... 北京冷死了 零下9度 |
|
|
|
| 免費注冊為維庫電子開發(fā)網(wǎng)會員,參與電子工程師社區(qū)討論,點此進入 |
Copyright © 1998-2006 www.udpf.com.cn 浙ICP證030469號 |