主题中讨论的其他器件: MSP-FET430UIF、 EVM430-FR6043
大家好、
我在 msp430fr6043中使用 SPI 协议。 我尝试连接 EEPROM 25lc040。
我选择的 SCLK 频率为800kHz、MOSI、CS 也正常工作、在示波器中观察到这些信号。 但我无法在示波器中获取 SOMI 数据。
请找到以下代码。
#include <msp430.h>
#include "msp430fr6043.h"
void clock_init();
void gpio_init();
void spi_a1_init();
void uart_a1_init();
void write_enable();
unsigned char E_write(unsigned char ch);
unsigned char E_read();
void WR_data(unsigned char addr, unsigned char data);
unsigned char RD_data(unsigned char addr);
void clock_init()
{
// Clock System Setup
// Startup clock system with max DCO setting ~8MHz
CSCTL0_H = CSKEY_H; // Unlock CS registers
CSCTL1 = DCOFSEL_3 | DCORSEL; // Set DCO to 1MHz
CSCTL2 = SELA__VLOCLK | SELS__DCOCLK | SELM__DCOCLK;
CSCTL3 = DIVA__1 | DIVS__1 | DIVM__1; // Set all dividers
CSCTL0_H = 0; // Lock CS registers
}
void gpio_init()
{
// Configure GPIO
//SCLK = 01 + ScLK set as output
PJSEL1 &=~ BIT0;
PJSEL0 |= BIT0;
PJDIR |= BIT0;
//MOSI = 01
PJSEL1 &=~ BIT2;
PJSEL0 |= BIT2;
//MISO = 01
PJSEL1 &=~ BIT3;
PJSEL0 |= BIT3;
// CS = P1.3 -----------------------------------------------------------------------
P1OUT |= BIT3; //P1.3 =1
P1DIR |= BIT3; //OUTPUT MODE
//------------------------------------------------------------------------
// Disable the GPIO power-on default high-impedance mode to activate
// previously configured port settings
PM5CTL0 &= ~LOCKLPM5;
}
void spi_a2_init()
{
// Software reset
UCA2CTLW0 = UCSWRST; // **Put state machine in reset**
// CONFIGURE CTLWO
UCA2CTLW0 |= UCSSEL__SMCLK; // Source for sclk is SMCLK
UCA2BRW = 10; // sclk = SMCLK/10 = 100 KHz
// 3-PIN AND MASTER MODE
UCA2CTLW0 |= UCSYNC; // Synchronous mode (i.e. SPI)
UCA2CTLW0 |= UCMST;
// CLOCK PHASE IS 1 (DATA CAPTURED FIRST )
UCA2CTLW0 |= UCCKPH;
//MSB FIRST
UCA2CTLW0 |= UCMSB;
// clear reset
UCA2CTLW0 &=~ UCSWRST;
}
void uart_a1_init()
{
// Software reset
UCA1CTLW0 |= UCSWRST;
//Source clock is SMCLK
UCA1CTLW0 |= UCSSEL__SMCLK;
// Baud rate calculated value
UCA1BRW = 52;
//Configure MCTLW
UCA1MCTLW |= UCOS16 | UCBRF1 | 0X4900;
//software reset clear
UCA1CTLW0 &=~ UCSWRST;
}
unsigned char E_write(unsigned char ch)
{
unsigned char rcv=0;
UCA2TXBUF = ch; // Send data in Txbuffer
/ This is added because suggestion from forum
while(!(UCA2IFG & UCRXIFG));
rcv=UCA2RXBUF; // receive data
return rcv;
}
void write_enable()
{
P1OUT &=~ BIT3; // cs as gpio (p1.3) is clear
E_write(0x06); // To enable write latch
P1OUT |= BIT3; // cs as gpio (p1.3) is set
}
unsigned char E_read()
{
unsigned char rcv2=0;
UCA2TXBUF = 0XFF; // Send dummy data
while(!(UCA2IFG & UCTXIFG)); // Wait until transmission is complete
while(!(UCA2IFG & UCRXIFG)); //wait until rxbuffer is filled with data
rcv2 = UCA2RXBUF; // Store in a variable
return rcv2;
}
void WR_data(unsigned char addr, unsigned char data) // No need to return anything...
{
unsigned char rcv=0;
write_enable(); // Enable write latch
P1OUT &=~ BIT3; // cs=0
E_write(0x02); // write instruction
E_write(addr); // send address byte
E_write(data); // send data byte
P1OUT |= BIT3; // cs=1
}
unsigned char RD_data(unsigned char addr)
{
unsigned char rcv=0;
write_enable(); // Enable write latch
P1OUT &=~ BIT3; // cs=0
E_write(0x03); // Read instruction
E_write(addr); // send address byte
rcv = E_read(); // store the received data
P1OUT |= BIT3;
return rcv;
}
void uart_char(unsigned char ch)
{
while(!(UCA1IFG & UCTXIFG));
UCA1TXBUF = ch;
}
void uart_string(unsigned char *data)
{
unsigned char i=0;
while(data[i])
{
uart_char(data[i]);
i++;
}
}
unsigned char msg = 0x5a;
unsigned char msg2=0;
unsigned char rcv_eeprom[4] = {0};
int main(void)
{
unsigned int addr=0,i=0,k=0,j=0;
// stop watchdog timer
WDTCTL = WDTPW | WDTHOLD;
// Initialize clock
clock_init();
// Initialize GPIO
gpio_init();
//Initialize SPI
spi_a2_init();
WR_data(addr, msg);
__delay_cycles(1000);
while(1)
{
msg2=RD_data(0);
__delay_cycles(1000);
}
return 0;
}
我正在使用
MSP430-FR6043微控制器
2. MSP430 USB 调试-用于编程的接口(MSP-FET430UIF)。
请指引我出错的地方。
谢谢、
Ashok。