Телесистемы
 Разработка, производство и продажа радиоэлектронной аппаратуры
На главную   | Карта сайта | Пишите нам | В избранное
Требуется программист в Зеленограде
- обработка данных с датчиков; ColdFire; 40 тыс.
e-mail:jobsmp@pochta.ru

Телесистемы | Электроника | Конференция «Микроконтроллеры и их применение»

Свой код не дам, а вот чужой подкину:

Отправлено Trashy 13 сентября 2009, г. 01:19
В ответ на: Здравствуйте, уважаемые! Нужна помощь по I2C MSP430 отправлено пользователем skn 13 сентября 2009, г. 01:10


/*****************************************************************************/
/* Communication with an EEPROM (e.g. 2465) via I2C bus */
/* The I2C module of the MSP430F169 is used to communicate with the EEPROM. */
/* The "Byte Write", "Current Address Read", "Random Read", an */
/* "Acknowledge Polling" commands or the EEPROM are realized. */
/* */
/* developed with IAR Embedded Workbench V2.20 */
/* */
/* Texas Instruments Deutschland GmbH */
/* Christian Hernitscheck */
/* July 2004 */
/*---------------------------------------------------------------------------*/
/* updates */
/* Jan 2005: */
/* - updated initialization sequence */
/*****************************************************************************
;
; THIS PROGRAM IS PROVIDED "AS IS". TI MAKES NO WARRANTIES OR
; REPRESENTATIONS, EITHER EXPRESS, IMPLIED OR STATUTORY,
; INCLUDING ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
; FOR A PARTICULAR PURPOSE, LACK OF VIRUSES, ACCURACY OR
; COMPLETENESS OF RESPONSES, RESULTS AND LACK OF NEGLIGENCE.
; TI DISCLAIMS ANY WARRANTY OF TITLE, QUIET ENJOYMENT, QUIET
; POSSESSION, AND NON-INFRINGEMENT OF ANY THIRD PARTY
; INTELLECTUAL PROPERTY RIGHTS WITH REGARD TO THE PROGRAM OR
; YOUR USE OF THE PROGRAM.
;
; IN NO EVENT SHALL TI BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
; CONSEQUENTIAL OR INDIRECT DAMAGES, HOWEVER CAUSED, ON ANY
; THEORY OF LIABILITY AND WHETHER OR NOT TI HAS BEEN ADVISED
; OF THE POSSIBILITY OF SUCH DAMAGES, ARISING IN ANY WAY OUT
; OF THIS AGREEMENT, THE PROGRAM, OR YOUR USE OF THE PROGRAM.
; EXCLUDED DAMAGES INCLUDE, BUT ARE NOT LIMITED TO, COST OF
; REMOVAL OR REINSTALLATION, COMPUTER TIME, LABOR COSTS, LOSS
; OF GOODWILL, LOSS OF PROFITS, LOSS OF SAVINGS, OR LOSS OF
; USE OR INTERRUPTION OF BUSINESS. IN NO EVENT WILL TI'S
; AGGREGATE LIABILITY UNDER THIS AGREEMENT OR ARISING OUT OF
; YOUR USE OF THE PROGRAM EXCEED FIVE HUNDRED DOLLARS
; (U.S.$500).
;
; Unless otherwise stated, the Program written and copyrighted
; by Texas Instruments is distributed as "freeware". You may,
; only under TI's copyright in the Program, use and modify the
; Program without any charge or restriction. You may
; distribute to third parties, provided that you transfer a
; copy of this license to the third party and the third party
; agrees to these terms by its first use of the Program. You
; must reproduce the copyright notice and any other legend of
; ownership on each copy or partial copy, of the Program.
;
; You acknowledge and agree that the Program contains
; copyrighted material, trade secrets and other TI proprietary
; information and is protected by copyright laws,
; international copyright treaties, and trade secret laws, as
; well as other intellectual property laws. To protect TI's
; rights in the Program, you agree not to decompile, reverse
; engineer, disassemble or otherwise translate any object code
; versions of the Program to a human-readable form. You agree
; that in no event will you alter, remove or destroy any
; copyright notice included in the Program. TI reserves all
; rights not specifically granted under this license. Except
; as specifically provided herein, nothing in this agreement
; shall be construed as conferring by implication, estoppel,
; or otherwise, upon you, any license or other right under any
; TI patents, copyrights or trade secrets.
;
; You may not use the Program in non-TI devices.
;
******************************************************************************/
#include "msp430x16x.h"

#define SlaveAddress 0x68;//0x50

int PtrTransmit;
unsigned char I2CBuffer[3];

/*---------------------------------------------------------------------------*/
void InitI2C(void)
// Description:
// Initialization of the I2C Module
{
P3SEL |= 0x0A; // select module function for the used I2C pins
P3DIR &= ~0x0A;

// Recommended initialisation steps of I2C module as shown in User Guide:
U0CTL |= I2C+SYNC; // (1) Select I2C mode with SWRST=1
U0CTL &= ~I2CEN; // (2) disable the I2C module
// (3) Configure the I2C module with I2CEN=0 :
// U0CTL default settings:
// 7-bit addressing, no DMA, no feedback
I2CTCTL = I2CTRX+I2CSSEL_2; // byte mode, repeat mode, clock source = SMCLK,
// transmit mode
I2CSA = SlaveAddress; // define Slave Address
// In this case the Slave Address defines the
// control byte that is sent to the EEPROM.
I2COA = 0x01A5; // own address.
I2CPSC = 0x06; // I2C clock = clock source/1
I2CSCLH = 0x03; // SCL high period = 5*I2C clock
I2CSCLL = 0x03; // SCL low period = 5*I2C clock
U0CTL |= I2CEN; // (4) set I2CEN via software
}

/*---------------------------------------------------------------------------*/
void I2CWriteInit(void)
// Description:
// Initialization of the I2C Module for Write operation.
{
U0CTL |= MST; // define Master Mode
I2CTCTL |= I2CTRX; // I2CTRX=1 => Transmit Mode (R/W bit = 0)
I2CIFG &= ~TXRDYIFG;
I2CIE = TXRDYIE; // enable Transmit ready interrupt
}

/*---------------------------------------------------------------------------*/
void I2CReadInit(void)
// Description:
// Initialization of the I2C Module for Read operation.
{
I2CTCTL &= ~I2CTRX; // I2CTRX=0 => Receive Mode (R/W bit = 1)
I2CIE = RXRDYIE; // enable Receive ready interrupt
}

/*---------------------------------------------------------------------------*/
void EEPROM_ByteWrite(unsigned int Address, unsigned char Data)
// Description:
// Byte Write Operation. The communication via the I2C bus with an EEPROM
// (2465) is realized. A data byte is written into a user defined address.
{
unsigned char adr_hi;
unsigned char adr_lo;

while (I2CDCTL&I2CBUSY); // wait until I2C module has finished all operations

adr_hi = Address >> 8; // calculate high byte
adr_lo = Address & 0xFF; // and low byte of address

I2CBuffer[2] = adr_hi; // store single bytes that have to be sent
I2CBuffer[1] = adr_lo; // in the I2CBuffer.
I2CBuffer[0] = Data;
PtrTransmit = 1; // set I2CBuffer Pointer

I2CWriteInit();
I2CNDAT = 2; // 1 control byte + 3 bytes should be transmitted
I2CTCTL |= I2CSTT+I2CSTP; // start and stop condition generation
// => I2C communication is started
}

/*---------------------------------------------------------------------------*/
unsigned char EEPROM_CurrentAddressRead(void)
// Description:
// Current Address Read Operation. Data is read from the EEPROM. The current
// address from the EEPROM is used.
{
while (I2CDCTL&I2CBUSY); // wait until I2C module has finished all operations
I2CReadInit();
U0CTL |= MST; // define Master Mode
I2CNDAT = 1; // 1 byte should be received
I2CIFG &= ~ARDYIFG; // clear Access ready interrupt flag
I2CTCTL |= I2CSTT+I2CSTP; // start receiving and finally generate
// re-start and stop condition
while ((~I2CIFG)&ARDYIFG); // wait untill transmission is finished
return I2CBuffer[0];
}

/*---------------------------------------------------------------------------*/
unsigned char EEPROM_RandomRead(unsigned int Address)
// Description:
// Random Read Operation. Data is read from the EEPROM. The EEPROM
// address is defined with the parameter Address.
{
unsigned char adr_hi;
unsigned char adr_lo;

while (I2CDCTL&I2CBUSY); // wait until I2C module has finished all operations

adr_hi = Address >> 8; // calculate high byte
adr_lo = Address & 0xFF; // and low byte of address

I2CBuffer[1] = adr_hi; // store single bytes that have to be sent
I2CBuffer[0] = adr_lo; // in the I2CBuffer.
PtrTransmit = 0; // set I2CBuffer Pointer

I2CWriteInit();
I2CNDAT = 1; // 1 control byte + 2 bytes should be transmitted
I2CIFG &= ~ARDYIFG; // clear Access ready interrupt flag
I2CTCTL |= I2CSTT; // start condition generation
// => I2C communication is started
while ((~I2CIFG)&ARDYIFG); // wait untill transmission is finished
I2CReadInit();
I2CNDAT = 1; // 1 byte should be received

I2CIFG &= ~ARDYIFG; // clear Access ready interrupt flag
I2CTCTL |= I2CSTT+I2CSTP; // start receiving and finally generate
// re-start and stop condition
while ((~I2CIFG)&ARDYIFG); // wait untill transmission is finished
return I2CBuffer[0];
}

/*---------------------------------------------------------------------------*/
void EEPROM_AckPolling(void)
// Description:
// Acknowledge Polling. The EEPROM will not acknowledge if a write cycle is
// in progress. It can be used to determine when a write cycle is completed.
{
unsigned int count;
while (I2CDCTL&I2CBUSY); // wait until I2C module has finished all operations
P5OUT ^= 0x10;
count=0;
U0CTL &= ~I2CEN; // clear I2CEN bit => necessary to re-configure I2C module
I2CTCTL |= I2CRM; // transmission is software controlled
U0CTL |= I2CEN; // enable I2C module
I2CIFG = NACKIFG; // set NACKIFG
while (NACKIFG & I2CIFG)
{
I2CIFG=0x00; // clear I2C interrupt flags
U0CTL |= MST; // define Master Mode
I2CTCTL |= I2CTRX; // I2CTRX=1 => Transmit Mode (R/W bit = 0)
I2CTCTL |= I2CSTT; // start condition is generated
while (I2CTCTL&I2CSTT); // wait till I2CSTT bit was cleared
I2CTCTL |= I2CSTP; // stop condition is generated after slave address was sent
// => I2C communication is started
while (I2CDCTL&I2CBUSY); // wait till stop bit is reset
count=count+1;
P5OUT ^= 0x10;
}
U0CTL &= ~I2CEN; // clear I2CEN bit => necessary to re-configure I2C module
I2CTCTL &= ~I2CRM; // transmission is by the I2C module
U0CTL |= I2CEN; // enable I2C module

return;
}

/*---------------------------------------------------------------------------*/
/* Interrupt Service Routines */
/* Note that the Compiler version is checked in the following code and */
/* depending of the Compiler Version the correct Interrupt Service */
/* Routine definition is used. */
#if __VER__ < 200
interrupt [USART0TX_VECTOR] void ISR_I2C(void)
#else
#pragma vector=USART0TX_VECTOR
__interrupt void ISR_I2C(void)
#endif

// Description:
// Byte Write Operation. The communication via the I2C bus with an EEPROM
{
switch (I2CIV)
{
case I2CIV_AL: /* I2C interrupt vector: Arbitration lost (ALIFG) */
{
}
break;
case I2CIV_NACK: /* I2C interrupt vector: No acknowledge (NACKIFG) */
{
}
break;
case I2CIV_OA: /* I2C interrupt vector: Own address (OAIFG) */
{
}
break;
case I2CIV_ARDY: /* I2C interrupt vector: Access ready (ARDYIFG) */
{
}
break;
case I2CIV_RXRDY: /* I2C interrupt vector: Receive ready (RXRDYIFG) */
{
I2CBuffer[0]=I2CDRB; // store received data in buffer
}
break;
case I2CIV_TXRDY: /* I2C interrupt vector: Transmit ready (TXRDYIFG) */
{
I2CDRB = I2CBuffer[PtrTransmit];
PtrTransmit--;
if (PtrTransmit<0)
{
I2CIE &= ~TXRDYIE; // disable interrupts
}
}
break;
case I2CIV_GC: /* I2C interrupt vector: General call (GCIFG) */
{
}
break;
case I2CIV_STT: /* I2C interrupt vector: Start condition (STTIFG) */
{
}
break;
}
}



Составить ответ | Вернуться на конференцию.

Ответы


Отправка ответа
Имя*: 
Пароль: 
E-mail: 
Тема*:

Сообщение:

Ссылка на URL: 
URL изображения: 

если вы незарегистрированный на форуме пользователь, то
для успешного добавления сообщения заполните поле, как указано ниже:
если прибавить четыре к четырём будет:

Перейти к списку ответов | Конференция | Раздел "Электроника" | Главная страница | Карта сайта

Rambler's Top100 Рейтинг@Mail.ru
 
Web telesys.ru