TI公司的一个关于MSP430F5438A的spi例子。
下面是msater的程序。
#define SPICLK 500000
unsigned char transmitData = 0x00, receiveData = 0x00;
unsigned short returnValue = 0x00;
void main (void)
{
//Stop watchdog timer
WDT_hold(__MSP430_BASEADDRESS_WDT_A__);
//Set P1.1 for slave reset
GPIO_setOutputHighOnPin(
__MSP430_BASEADDRESS_PORT1_R__,
GPIO_PORT_P1,
GPIO_PIN1
);
//Set P1.0 to output direction
GPIO_setAsOutputPin(__MSP430_BASEADDRESS_PORT1_R__,
GPIO_PORT_P1,
GPIO_PIN0
);
//P3.5,4,0 option select
GPIO_setAsPeripheralModuleFunctionInputPin(__MSP430_BASEADDRESS_PORT3_R__,
GPIO_PORT_P3,
GPIO_PIN5 + GPIO_PIN4 + GPIO_PIN0
);
//Initialize Master
returnValue = SPI_masterInit(__MSP430_BASEADDRESS_USCI_A0__,
SPI_CLOCKSOURCE_SMCLK,
UCS_getSMCLK(__MSP430_BASEADDRESS_UCS__),
SPICLK,
SPI_MSB_FIRST,
SPI_PHASE_DATA_CHANGED_ONFIRST_CAPTURED_ON_NEXT,
SPI_CLOCKPOLARITY_INACTIVITY_HIGH
);
if (STATUS_FAIL == returnValue){
return;
}
//Enable SPI module
SPI_enable(__MSP430_BASEADDRESS_USCI_A0__);
//Enable Receive interrupt
SPI_enableInterrupt(__MSP430_BASEADDRESS_USCI_A0__, SPI_RECEIVE_INTERRUPT);
//Now with SPI signals initialized, reset slave
GPIO_setOutputLowOnPin(
__MSP430_BASEADDRESS_PORT1_R__,
GPIO_PORT_P1,
GPIO_PIN1
);
//LED On
GPIO_setOutputHighOnPin(
__MSP430_BASEADDRESS_PORT1_R__,
GPIO_PORT_P1,
GPIO_PIN0
);
//Wait for slave to initialize
__delay_cycles(100);
//Initialize data values
transmitData = 0x00;
//USCI_A0 TX buffer ready?
while (!SPI_getInterruptStatus(__MSP430_BASEADDRESS_USCI_A0__,
SPI_TRANSMIT_INTERRUPT)) ;
//Transmit Data to slave
SPI_transmitData(__MSP430_BASEADDRESS_USCI_A0__, transmitData);
//CPU off, enable interrupts CPU停止,启用中断LPM0
__bis_SR_register(LPM0_bits + GIE);
}
//******************************************************************************
//
//This is the USCI_B0 interrupt vector service routine
//
//******************************************************************************
#pragma vector=USCI_A0_VECTOR
__interrupt void USCI_A0_ISR (void)
{
switch (__even_in_range(UCA0IV,4)){
//Vector 2 - RXIFG
case 2:
//USCI_A0 TX buffer ready? USCI_A0 TX 寄存器准备好了吗?
while (!SPI_getInterruptStatus(__MSP430_BASEADDRESS_USCI_A0__,
SPI_TRANSMIT_INTERRUPT)) ;
receiveData = SPI_receiveData(__MSP430_BASEADDRESS_USCI_A0__);
//Increment
transmitData++;
//Send next value
SPI_transmitData(__MSP430_BASEADDRESS_USCI_A0__,
transmitData
);
//Delay between transmissions for slave to process information
__delay_cycles(40);
break;
default: break;
}
}
还有一个slaver的程序。两个我分别使用两块MSP430F5438A的板子,接口就是两块板子的P3.0,P3.4,P3.5分别相连,然后主机的P1.1连接从机的RST(我的板子的RST是在JTAG里的一个脚),连接的就这些,还有就是我给主机供电,然后单步运行master的程序,能看到从机的led1灯亮但是很暗,这个是正常现象吗?能证明程序或是我连接使用的正确吗?不正确的话,那应该怎么修改呢?正确的现象又是什么呢?
此帖出自
小平头技术问答
一周热门 更多>