[英]SPI connection with MSP430F2013(Master) to Arduino uno(SLAVE)
我正在做一個項目,我需要通過 MCU 測量電橋電路(應變計)。 為此,我使用的是帶有 16 位 ADC 的 MSP430F2013 MCu。 我沒有此 MCU 的數據記錄硬件,因此我嘗試將其與 Arduino Uno 連接以記錄測量結果。 我不擅長編程,所以到目前為止,我已經拼湊代碼來配置 MSP430 的 ADC,我看到它通過在 Code Composer Studio 中應用斷點來工作。 我曾嘗試在 MSP430 和 Arduino 中設置 SPI,但不知道哪里出了問題(這是對我的問題的直截了當的解釋:即使我聽起來有點無知/愚蠢)。 如果有人可以在這里指出錯誤,那將有很大幫助。
這是兩個MCU的代碼
#include <msp430.h>
volatile unsigned int i;
volatile unsigned int s;
int main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
BCSCTL1 = CALBC1_16MHZ;
DCOCTL=CALDCO_16MHZ;
//SMCLK on Pin6 (P1.4) - just for service and testing
P1DIR |= 0x10; //P1.4 as output
P1SEL = 0x10; //SMCLK to P1.4
//I/O-Pin preparation
P1DIR |= BIT0; //set P1.0 to output direction - for using the LED on Pin2
P1DIR |= BIT7; //set P1.7 to output direction - as signal for data are ready to use
//SPI settings
USICTL0 &= ~USISWRST; //USI released for operation
USICTL1 &= ~USII2C; //switch USI to SPI-Mode
USICTL0 |= USIMST; //set to SPI-Master-Mode
USICKCTL = USIDIV_3+USISSEL_2+USICKPL;
USICTL0 |= USIPE6 + USIPE5; //SDO-Port enabled; Pin8
USICTL0 |= USIPE7; //SDI-Port disabled; Pin9 can be used for normal in/out
USICTL0 |= USIOE; //activate output (data goes from MSP to Warrior56)
USICNT |= USI16B; //init load counter for 16-bit-data
//inactive state is low
//data is changed on the first SCLK edge and captured on the following edge
P1DIR |= 0x01; // Set P1.0 to output direction
SD16CTL = SD16REFON + SD16SSEL_1; // 1.2V ref, SMCLK
SD16INCTL0 = SD16INCH_1; // A1+/-
SD16CCTL0 = SD16UNI + SD16IE; // 256OSR, unipolar, interrupt enable
SD16AE = SD16AE1; // P1.1 A1+, A1- = VSS
SD16CCTL0 |= SD16SC; // Set bit to start conversion
while(1)
{
SD16CCTL0 |= SD16SC;
s = SD16MEM0;
for (i = 0xFFFF; i > 0; i--); // Delay
USICTL1 |= USIIFG; // Set flag and start communication
USISR = s;
if (USISRL = s)
P1OUT |= 0x01;
else
P1OUT &= ~0x01;
__bis_SR_register(LPM0_bits + GIE);
}
}
#pragma vector = SD16_VECTOR; // Interrupt service routine for
__interrupt void SD16ISR(void) // conversion
{
s = SD16MEM0;
}
//////////////////////////////////////////////////
Here is arduino code as slave
#include <SPI.h>
char buff [50];
volatile byte indx;
volatile boolean process;
void setup (void) {
Serial.begin (115200);
pinMode(MOSI, INPUT); // have to send on master in so it set as output
SPCR |= _BV(SPE); // turn on SPI in slave mode
indx = 0; // buffer empty
process = false;
SPI.attachInterrupt(); // turn on interrupt
}
ISR (SPI_STC_vect) // SPI interrupt routine
{
byte c = SPDR; // read byte from SPI Data Register
if (indx < sizeof buff) {
buff [indx++] = c; // save data in the next index in the array buff
if (c == '\r') //check for the end of the word
process = true;
}
}
void loop (void) {
if (process) {
process = false; //reset the process
Serial.println (buff); //print the array on serial monitor
indx= 0; //reset button to zero
}
}
我得到了 ADC 的結果,但沒有進一步的結果(我不知道如何測試 usi。
當您使用啟動板時,不要打擾 arduino。 Launchpad 還提供 USB-UART。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.