ADVERTISEMENT

slva302.zip

MSP430G2553 I2C z TPL0401A - brak komunikacji z potencjometrem cyfrowym

Jakby ktoś potrzebował gotowca to tutaj jest opis programowego mastera: http://www.ti.com/lit/an/slva302/slva302.pdf a dodatkowo pliki do projektu daje w załączniku tam są opisane 2 tryby: sprzętowy i programowy (o który chodzi) Wszystko na architekturę MSP430 więc nie będzie tu problemu żeby sobie to przepisać. sam slave: http://www.ti.com/general/docs/litabsmultiplefilelist.tsp?literatureNumber=slaa330


Download file - link to post
  • slva302.zip
    • MSP430_TPS60250.c
    • MSP430_SWI2C_Master.c
    • MSP430_TPS60250.eww
    • MSP430_TPS60250.h
    • MSP430_TPS60250.ewd
    • MSP430_SWI2C_Master.h
    • MSP430_TPS60250.dep
    • USI_I2CMaster.s43
    • MSP430_TPS60250.ewp
    • USI_I2CMaster.h


slva302.zip > MSP430_SWI2C_Master.h

//******************************************************************************
// MSP430F20xx - I2C Master Transmitter and Receiver via CPU Software (GPIO)
//
// Description: This code library configures the MSP430 as an I2C master device
// capable of transmitting and receiving bytes using GPIO pins. Specific I/O
// pins can be selected in the corresponding header file. By default, the same
// pins that are used by the USI module are selected.
//
// Master
// MSP430
// -----------------
// /|\| XIN|-
// | | |
// --|RST XOUT|-
// | |
// | |
// | |
// | SDA/P2.7|------- & gt; [I2C SLAVE SDA]
// | SCL/P2.6|------- & gt; [I2C SLAVE SCL]
//
// Note: Internal pull-ups are NOT used for SDA & SCL [DISABLED]
//
// R. Wu
// Texas Instruments Inc.
// March 2008
//
//******************************************************************************

#include & lt; msp430x20x2.h & gt;

#define PxSEL P2SEL // Port selection
#define PxDIR P2DIR // Port direction
#define PxOUT P2OUT // Port output
#define PxIN P2IN // Port input

#define SDA BIT7 // Controls SDA line (pull-up used for logic 1)
#define SCL BIT6 // Controls SCL line (pull-up used for logic 1)

void MSP430_SWI2CMST_init(void);

void MSP430_SWI2CMST_readBlock(unsigned char SlaveAddress,
unsigned int numBytes, void* RxData);

void MSP430_SWI2CMST_writeBlock(unsigned char SlaveAddress,
unsigned int numBytes, void* TxData);

void MSP430_SWI2CMST_start(void);

void MSP430_SWI2CMST_txByte(unsigned char data);

void MSP430_SWI2CMST_ack(void);

unsigned char MSP430_SWI2CMST_rxByte(void);

void MSP430_SWI2CMST_stop(void);


slva302.zip > USI_I2CMaster.h

#ifndef TI_USI_I2C
#define TI_USI_I2C

#define TI_USI_EXIT_LPM 1
#define TI_USI_STAY_LPM 0

extern void TI_USI_I2C_MasterInit(unsigned char ClockConfig, int(*StatusCallback)(unsigned char) );
extern unsigned char TI_USI_I2CSelect(unsigned char SlaveAddress);
extern unsigned char TI_USI_I2CRead(unsigned char SlaveAddress, unsigned int Length, unsigned char Multi, void* RxData);
extern unsigned char TI_USI_I2CWrite(unsigned char SlaveAddress, unsigned int Length, unsigned char Multi,void* TxData);
extern void TI_USI_I2CStop(void);

#endif


slva302.zip > MSP430_TPS60250.c

//******************************************************************************
// MSP430F20xx Demo - I2C Master Transmitter and Receiver
//
// Description: MSP430 I2C Master communicates with a TPS60250EVM (I2C Slave)
// using an I2C Master software driver. Whenever master device transmits data
// to the slave device, the data is read back for verification purposes.
// If the data is incorrect, the program will toggle the P1.0 LED forever.
// The various LED's on the TPS60250EVM are enabled/disabled to demonstrate
// I2C communications.
//
// ACLK = n/a, MCLK = SMCLK = Calibrated 1MHz
//
// Slave Master
// TPS60250 MSP430F20xx
// ----------------- /|\ -----------------
// | | | | XIN|-
// | | | | |
// | | | | XOUT|-
// | | 10K | |
// | | | | |
// | | | | P1.0|--- & gt; LED
// | SDAT|---|--- & gt; |P2.7/SDA |
// | SCLK| & lt; --|----|P2.6/SCL |
//
// Note: Internal pull-ups are NOT used in this example for SDA & SCL
// TPS60250 Slave address = 1110111b
//
// R. Wu
// Texas Instruments Inc.
// March 2008
// Built with IAR Embedded Workbench Version: 4.10A
//******************************************************************************

#include " USI_I2CMaster.h " // For using the USI module
#include " MSP430_SWI2C_Master.h " // For using GPIO bit-banging
#include " MSP430_TPS60250.h " // Header file for this example

unsigned char TxData[BUFFERSIZE]; // Stores data bytes to be TX'd
unsigned char RxData[BUFFERSIZE]; // Stores data bytes that are RX'd

void LED_delay(void)
{
for (unsigned int i = 0; i & lt; 3000; i++); // Busy loop delay
}

#ifdef _I2C_USI
int StatusCallback(unsigned char c)
{
return TI_USI_EXIT_LPM; // Exit active for next transfer
}
#endif /* _I2C_USI */

unsigned char MSP430_TPS60250_read(unsigned char reg)
{
TxData[TX_REG_IDX] = reg;
#ifdef _I2C_USI
__disable_interrupt();
TI_USI_I2CWrite(I2CSLAVEADDR, (NUMBYTES_WRITE-1), 0, TxData);
__bis_SR_register(LPM0_bits + GIE);
__disable_interrupt();
TI_USI_I2CRead(I2CSLAVEADDR, NUMBYTES_READ, 0, RxData);
__bis_SR_register(LPM0_bits + GIE);
#endif /* _I2C_USI */
#ifdef _I2C_BITBANG
MSP430_SWI2CMST_writeBlock(I2CSLAVEADDR, (NUMBYTES_WRITE-1), TxData);
MSP430_SWI2CMST_readBlock(I2CSLAVEADDR, NUMBYTES_READ, RxData);
#endif /* _I2C_BITBANG */
return (RxData[RX_DAT_IDX]);
}

void MSP430_TPS60250_write(unsigned char reg, unsigned char data)
{
TxData[TX_REG_IDX] = reg;
TxData[TX_DAT_IDX] = data;
#ifdef _I2C_USI
__disable_interrupt();
TI_USI_I2CWrite(I2CSLAVEADDR, NUMBYTES_WRITE, 0, TxData);
__bis_SR_register(LPM0_bits + GIE);
#endif /* _I2C_USI */
#ifdef _I2C_BITBANG
MSP430_SWI2CMST_writeBlock(I2CSLAVEADDR, NUMBYTES_WRITE, TxData);
#endif /* _I2C_BITBANG */
if (TxData[TX_DAT_IDX] != MSP430_TPS60250_read(TxData[TX_REG_IDX]))
MSP430_TPS60250_error();
}

void MSP430_TPS60250_cycleCurrent(unsigned char reg)
{
for (unsigned int i = 0; i & lt; TPS60250_NUMSTEPSCURRENT; i++) {
MSP430_TPS60250_write(reg, i);
for (unsigned int j = 0; j & lt; (20*TPS60250_NUMSTEPSCURRENT); j++)
_NOP();
MSP430_TPS60250_read(reg);
}
}

void MSP430_TPS60250_cycleAuxBrightness(void)
{
for (int j = 0; j & lt; 5; j++) {
if (j % (5 - 1)) {
MSP430_TPS60250_write(TPS60250REG_ENABLECONTROL, (ENold | ENaux | DM5H));
TxData[TX_DAT_IDX] = (2 & lt; & lt; j);
MSP430_TPS60250_write(TPS60250REG_AUXOUTPUTBRIGHTNESSANDOPERATIONMODE,
TxData[TX_DAT_IDX]);
}
else {
MSP430_TPS60250_write(TPS60250REG_ENABLECONTROL, 0);
}
LED_delay();
}
}

void MSP430_TPS60250_error(void)
{
while (1) // Loop forever
{
P1OUT ^= BIT0; // Toggle LED
LED_delay(); // Delay
}
}

void main(void)
{
unsigned int i;

WDTCTL = (WDTPW | WDTHOLD); // Disable Watchdog
BCSCTL1 = CALBC1_1MHZ; // Set DCO to 1 MHz
DCOCTL = CALDCO_1MHZ;
P1DIR |= BIT0; // P1.0 output direction
P1OUT & = ~BIT0; // Turn P1.0 LED OFF
#ifdef _I2C_BITBANG
MSP430_SWI2CMST_init(); // Initialize I2C pins
#endif /* _I2C_BITBANG */

#ifdef _I2C_USI
// Initialize USI module, clock = ~(SMCLK/128)
TI_USI_I2C_MasterInit((USIDIV_7 | USISSEL_2 | USICKPL), StatusCallback);

// Acknowledge polling function - LED blinks continuously until slave device
// provides an ACK
while (TI_USI_I2CSelect(I2CSLAVEADDR))
{
P1OUT ^= BIT0; // Toggle LED
LED_delay(); // Delay
}
P1OUT = 0x00; // Slave acknowledged, LED off
#endif /* _I2C_USI */

// Enable Main Display LEDs (DM1-DM4) and cycle through current settings
MSP430_TPS60250_write(TPS60250REG_ENABLECONTROL, (ENold | ENmain | DM5H));
for (i = 0; i & lt; TEST_NUMITERATIONS; i++)
MSP430_TPS60250_cycleCurrent(TPS60250REG_MAINDISPLAYCURRENT);

// Enable Sub Display LED2 (DS2) and cycle through current settings
MSP430_TPS60250_write(TPS60250REG_ENABLECONTROL, (ENold | ENsub2 | DM5H));
for (i = 0; i & lt; TEST_NUMITERATIONS; i++)
MSP430_TPS60250_cycleCurrent(TPS60250REG_SUBDISPLAYCURRENT);

// Enable Sub Display LED1 (DS1) and cycle through current settings
MSP430_TPS60250_write(TPS60250REG_ENABLECONTROL, (ENold | ENsub1 | DM5H));
for (i = 0; i & lt; TEST_NUMITERATIONS; i++)
MSP430_TPS60250_cycleCurrent(TPS60250REG_SUBDISPLAYCURRENT);

// Enable Aux Display LED (DM5) and cycle through current settings
MSP430_TPS60250_write(TPS60250REG_ENABLECONTROL, (ENold | ENaux | DM5H));
for (i = 0; i & lt; TEST_NUMITERATIONS; i++)
MSP430_TPS60250_cycleAuxBrightness();

// Disable all LED's on the TPS60250 EVM
MSP430_TPS60250_write(TPS60250REG_ENABLECONTROL, 0);

// Set P1.0 LED on MSP430 EVM to signal that command sequence was successful
P1OUT |= BIT0;

// Blink Main Display LEDs (DM1-DM4) at approximately 1 second intervals
while (1)
{
MSP430_TPS60250_write(TPS60250REG_ENABLECONTROL, (ENold | ENmain | DM5H));
for (i = 0; i & lt; 30; i++)
LED_delay();
MSP430_TPS60250_write(TPS60250REG_ENABLECONTROL, 0);
for (i = 0; i & lt; 30; i++)
LED_delay();
}
}


slva302.zip > MSP430_SWI2C_Master.c

//******************************************************************************
// MSP430F20xx - I2C Master Transmitter and Receiver via CPU Software (GPIO)
//
// Description: This code library configures the MSP430 as an I2C master device
// capable of transmitting and receiving bytes using GPIO pins. Specific I/O
// pins can be selected in the corresponding header file. By default, the same
// pins that are used by the USI module are selected.
//
// Master
// MSP430
// -----------------
// /|\| XIN|-
// | | |
// --|RST XOUT|-
// | |
// | |
// | |
// | SDA/P2.7|------- & gt; [I2C SLAVE SDA]
// | SCL/P2.6|------- & gt; [I2C SLAVE SCL]
//
// Note: Internal pull-ups are NOT used for SDA & SCL [DISABLED]
//
// R. Wu
// Texas Instruments Inc.
// March 2008
//
//******************************************************************************

#include " MSP430_SWI2C_Master.h "

void MSP430_SWI2CMST_init(void)
{
PxSEL & = ~(SCL | SDA); // Set GPIO function
PxDIR |= (SCL | SDA); // Set output direction
PxOUT & = ~(SCL | SDA); // SCL & SDA = 0, low when = outputs
}

void MSP430_SWI2CMST_readBlock(unsigned char SlaveAddress,
unsigned int numBytes, void* RxData)
{
unsigned char* temp;

temp = (unsigned char *)RxData; // Initialize array pointer
MSP430_SWI2CMST_start(); // Send Start condition
for (unsigned int i = 0; i & lt; numBytes; i++) {
MSP430_SWI2CMST_txByte((SlaveAddress & lt; & lt; 1) | BIT0); // [ADDR] + R/W bit = 1
*(temp) = MSP430_SWI2CMST_rxByte(); // Read 8 bits of data and send ack
temp++; // Increment pointer to next element
}
MSP430_SWI2CMST_stop(); // Send Stop condition
}

void MSP430_SWI2CMST_writeBlock(unsigned char SlaveAddress,
unsigned int numBytes, void* TxData)
{
unsigned char *temp;

temp = (unsigned char *)TxData; // Initialize array pointer
MSP430_SWI2CMST_start(); // Send Start condition
MSP430_SWI2CMST_txByte((SlaveAddress & lt; & lt; 1) & ~BIT0); // [ADDR] + R/W bit = 0
for (unsigned int i = 0; i & lt; numBytes; i++) {
MSP430_SWI2CMST_txByte(*(temp)); // Send data and ack
temp++; // Increment pointer to next element
}
MSP430_SWI2CMST_stop(); // Send Stop condition
}

void MSP430_SWI2CMST_start(void) // Set up start condition for I2C
{
PxDIR |= SDA; // Set to output, data low [SDA = 0]
_NOP(); // Quick delay
_NOP(); // Quick delay
PxDIR |= SCL; // Set to output, data low [SCL = 0]
}

void MSP430_SWI2CMST_txByte(unsigned char data)
{
unsigned char bits, temp;

temp = data;
bits = 0x08; // Load I2C bit counter
while (bits != 0x00) // Loop until all bits are shifted
{
if (temp & BIT7) // Test data bit
PxDIR & = ~SDA; // Set to input, SDA = 1 via pull-up
else
PxDIR |= SDA; // Set to output, data low [SDA = 0]
PxDIR & = ~SCL; // Set to output, data low [SCL = 0]
temp = (temp & lt; & lt; 1); // Shift bits 1 place to the left
_NOP(); // Quick delay
PxDIR |= SCL; // Set to output, data low [SCL = 0]
bits = (bits - 1); // Loop until 8 bits are sent
}
PxDIR & = ~SDA; // Set to input, SDA = 1 via pull-up
MSP430_SWI2CMST_ack(); // Send acknowledge
}

void MSP430_SWI2CMST_ack(void) // Set up for I2C acknowledge
{
PxDIR & = ~SCL; // Set to input, SCL = 1 via pull-up
_NOP(); // delay to meet I2C spec
_NOP(); // " " "
PxDIR |= SCL; // Set to output, data low [SCL = 0]
}

unsigned char MSP430_SWI2CMST_rxByte(void) // Read 8 bits of I2C data
{
unsigned char bits, temp = 0; // I2C bit counter

bits = 0x08; // Load I2C bit counter
while (bits & gt; 0) // Loop until all bits are read
{
PxDIR & = ~SCL; // Set to input, SDL = 1 via pull-up
temp = (temp & lt; & lt; 1); // Shift bits 1 place to the left
if (PxIN & SDA) // Check digital input
temp = (temp + 1); // If input is 'H' store a '1'
PxDIR |= SCL; // Set to output, data low [SCL = 0]
bits = (bits - 1); // Decrement I2C bit counter
}
MSP430_SWI2CMST_ack(); // Send acknowledge
return (temp); // Return 8-bit data byte
}

void MSP430_SWI2CMST_stop(void) // Send I2C stop command
{
PxDIR |= SDA; // Set to output, data low [SCA = 0]
_NOP(); // Quick delay
_NOP(); // Quick delay
PxDIR & = ~SCL; // Set to input, SCL = 1 via pull-up
_NOP(); // Quick delay
_NOP(); // Quick delay
PxDIR & = ~SDA; // Set to input, SDA = 1 via pull-up
}


slva302.zip > MSP430_TPS60250.h

/* File: " MSP430_TPS60250.h " */

//#define _I2C_USI
#define _I2C_BITBANG

#define I2CSLAVEADDR 0x77 // 7 bits plus 1 don't care least significant bit

#define TPS60250REG_ENABLECONTROL 0x00
#define ENold BIT6
#define ENmain BIT5
#define ENsub2 BIT4
#define ENsub1 BIT3
#define ENaux BIT2
#define DM5H BIT1
#define DM5L BIT0

#define TPS60250REG_SUBDISPLAYCURRENT 0x01
#define Isub5 BIT5
#define Isub4 BIT4
#define Isub3 BIT3
#define Isub2 BIT2
#define Isub1 BIT1
#define Isub0 BIT0

#define TPS60250REG_MAINDISPLAYCURRENT 0x02
#define Imain5 BIT5
#define Imain4 BIT4
#define Imain3 BIT3
#define Imain2 BIT2
#define Imain1 BIT1
#define Imain0 BIT0

#define TPS60250REG_AUXOUTPUTBRIGHTNESSANDOPERATIONMODE 0x03
#define Iaux5 BIT7
#define Iaux4 BIT6
#define Iaux3 BIT5
#define Iaux2 BIT4
#define Iaux1 BIT3
#define Iaux0 BIT2
#define Mode1 BIT1
#define Mode0 BIT0

#define TEST_NUMITERATIONS 5

#define BUFFERSIZE 2

#define TX_REG_IDX 0
#define TX_DAT_IDX 1
#define RX_DAT_IDX 0

#define NUMBYTES_WRITE 2
#define NUMBYTES_READ 1

#define TPS60250_NUMSTEPSCURRENT 64

int StatusCallback(unsigned char c);

unsigned char MSP430_TPS60250_read(unsigned char reg);

void MSP430_TPS60250_write(unsigned char reg, unsigned char data);

void MSP430_TPS60250_cycleCurrent(unsigned char reg);

void MSP430_TPS60250_cycleAuxBrightness(void);

void MSP430_TPS60250_error(void);