Sunteți pe pagina 1din 3

//#include <msp430.h> //#include <legacymsp430.h> #include "msp430g2553.

h"

//For mspgcc //For mspgcc

//Address word + "HELLO WORLD" unsigned char txdata[14] = {0x00, 0x00, 0x00, 0x48, 0x45, 0x4C, 0x4C, 0x4F, 0x20 , 0x57, 0x4F, 0x52, 0x4C, 0x44}; unsigned unsigned unsigned unsigned unsigned unsigned unsigned char char char char char char char rxdata[12]; tx_byte_count; rx_byte_count; tx_byte_counter; rx_byte_counter; i; tx_rx;

void i2c_tx(unsigned char tx_count); void i2c_rx(unsigned char rx_count); void main(void) { WDTCTL = WDTPW + WDTHOLD; BCSCTL1 = CALBC1_1MHZ; DCOCTL = CALDCO_1MHZ; P1SEL = BIT1 + BIT2; P1SEL2 = BIT1 + BIT2; UCA0CTL1 = UCSSEL_2; CLK UCA0BR0 = 104; UCA0BR1 = 0; UCA0MCTL = UCBRS_1; UCA0CTL1 &= ~UCSWRST; P1SEL = BIT6 + BIT7; P1SEL2 = BIT6 + BIT7; UCB0CTL1 = UCSWRST; UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC; UCB0CTL1 = UCSSEL_2 + UCSWRST; UCB0BR0 = 12; UCB0BR1 = 0; UCB0I2CSA = 0b1010000; UCB0CTL1 &= ~UCSWRST; IE2 = UCB0TXIE; IE2 = UCB0RXIE; __delay_cycles(20000); i2c_tx(13); LLO WORLD" __delay_cycles(20000); i2c_tx(2); i2c_rx(12); for(i = 1; i < 12; i++) { while(!(IFG2 & UCA0TXIFG)); UCA0TXBUF = rxdata[i]; }

//Stop WDT //Set DCO to 1MHz //Set RXD and TXD //Have USCI use SMCLK AKA 1MHz main //Baud = 9600 //Modulation //Start USCI //Set I2C pins //Enable SW reset //I2C Master, synchronous mode //Use SMCLK, keep SW reset //fSCL = SMCLK/12 = ~100kHz //Slave Address //Clear SW reset, resume operation //Enable TX interrupt //Enable RX interrupt //Just a start up delay //i2c TX 13 bytes(Address word + "HE //Allow 24LC256 to write data //i2c TX address //i2c RX data

//UART TX data

__bis_SR_register(CPUOFF + GIE); } void i2c_tx(unsigned char tx_count) { tx_rx = 0; tx_byte_count = tx_count + 1; tx_byte_counter = tx_count; UCB0CTL1 = UCTR + UCTXSTT; __bis_SR_register(CPUOFF + GIE); } void i2c_rx(unsigned char rx_count) { tx_rx = 1; rx_byte_count = rx_count + 1; rx_byte_counter = rx_count; UCB0CTL1 &= ~UCTR; UCB0CTL1 = UCTXSTT; __bis_SR_register(CPUOFF + GIE); }

//Wait for a reset

// Load TX byte counter // I2C TX, start condition // Enter LPM0 w/ interrupts // Remain in LPM0 until all data is TX'd

// Load RX byte counter // I2C RX // I2C start condition // Enter LPM0 w/ interrupts // Remain in LPM0 until all data is RX'd

//interrupt(USCIAB0TX_VECTOR) USCIAB0TX_ISR(void) #pragma vector = USCIAB0TX_VECTOR __interrupt void USCIAB0TX_ISR(void) //For mspgcc { if(tx_rx == 0) { if (tx_byte_counter > 0) //Check TX byte counter { UCB0TXBUF = txdata[tx_byte_count - tx_byte_counter]; // Load TX buffer tx_byte_counter--; //Decrement TX byte counter } else if(tx_byte_counter == 0) { UCB0CTL1 = UCTXSTP; //I2C stop condition while (UCB0CTL1 & UCTXSTP); //Ensure stop condition got sent IFG2 &= ~UCB0TXIFG; //Clear USCI_B0 TX int flag __bic_SR_register_on_exit(CPUOFF); //Exit LPM0 } } else if(tx_rx == 1) { if (rx_byte_counter > 0) //Check RX byte counter { rxdata[rx_byte_count - rx_byte_counter] = UCB0RXBUF; rx_byte_counter--; //Decrement RX byte counter } else if(rx_byte_counter == 0) { UCB0CTL1 = UCTXSTP; // I2C stop condition while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sen t rxdata[rx_byte_count - (rx_byte_counter + 1)] = UCB0RXBUF; rxdata[rx_byte_count - (rx_byte_counter + 1)] = UCB0RXBUF; IFG2 &= ~UCB0RXIFG; // Clear USCI_B0 RX int flag __bic_SR_register_on_exit(CPUOFF); // Exit LPM0

} } }

S-ar putea să vă placă și