⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 spi_driver.c

📁 ISP driver for msp430
💻 C
字号:
#include <msp430x16x.h>

#include "SPI_Driver.h"
#include "SPI_Driver_Priv.h"

unsigned char g_SPIRxFlag;

void spiInit(void)
{
	/* P5.1-3 SPI option select*/
	P5DIR  |=   (1 << BIT1) | (1 << BIT3);
	 
	/*P5.2 SPI option select*/                            
	P5DIR  &=  ~(1 << BIT2);
	
	/*Select SPI Peripherals*/
	P5SEL  |=   (1 << BIT1) | (1 << BIT2) | (1 << BIT3);
	
	/* 8-bit SPI Master **SWRST**/        		  
	U1CTL = CHAR | SYNC | MM | SWRST;
	
	/* Enable USART0 SPI mode*/
	ME2 |= (1 << USPIE1);   
	
	/*Baud rate = SYSCLK/4*/
    U1BR0 = 0x4;
    U1BR1 = 0x0;
    
    /* no modulation*/
    UMCTL0 = 0x00; 
    
    /* Initialize USART state machine */
    U1CTL &= ~(SWRST);  
     
    /* Polarity, SMCLK, 3-wire*/                     
 	//U1TCTL = (1 << CKPL) |(1 << SSEL1) |(1 << STC);              
	U1TCTL = CKPH | SSEL1 | STC; 
	
	/*Enable Recv Interrupt*/
	IE2 |=  URXIE1;
	
	/*Enable GLobal interrupt*/
	_BIS_SR(GIE);
	
	CS_HIGH;                 
}

void spiTransmitData(unsigned char f_Data)
{
	CS_LOW;
	
	/*Transmit Data*/
	U1TXBUF = f_Data;
	
	/*Wait until Trnasmission is Completed*/
	while ((IFG2 & UTXIFG1) == 0);
	
	CS_HIGH;
}

unsigned char spiReceiveData(void)
{
	unsigned char l_Data = 0;
	
	CS_LOW;
	
	/*Send Dummy Value*/
	U1TXBUF = 0; 
	
	/*Waitfor the receving data*/
  	while ((IFG2 & URXIFG1) == 0);
  	
  	/*Copy the Data to temp buff*/
  	l_Data = U1RXBUF;
  	
  	CS_HIGH;
  	
return l_Data;
}

/*Interrupt Services Routien for SPI*/
#pragma vector=USART0RX_VECTOR
__interrupt void SPI0_rx (void)
{
	unsigned char l_ReceiveData = 0;
	
	/*Copy the Recived Byte*/
	l_ReceiveData = U0RXBUF;
	
	/*Set the global flag*/
	g_SPIRxFlag = 1;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -