MSP430 SPI driver code design process

Publisher:QuantumPulseLatest update time:2018-05-05 Source: eefocusKeywords:MSP430 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

In daily work, if you use MSP430 as the main control chip, you often need to write SPI or I2C drivers to read and control peripherals (such as LCD screens and some sensors). In order to reduce repetitive work, this article summarizes the detailed steps of SPI driver writing with a specific example (using MSP430FR6989 to drive the integrated analog front end AFE4400):

  • MCU SPI pin settings

  • SPI read and write timing settings

  • Register Write

  • Final Thoughts


MCU SPI pin settings

Generally, SPI has 3-wire and 4-wire, the difference lies in whether it has a chip select terminal - STE pin. The function description of the 4 pins is: 
UCxS0MI: data input in master mode, data output in slave mode; 
UCxSIMO: data output in master mode, data input in slave mode; 
UCxCLK: USCI SPI clock; 
UCxSTE: USCI SPI enable terminal;

Pin setup code:

void Set_UCB0_SPI(void)

{

  P1SEL0 |= BIT4 | BIT6 | BIT7; // Activate the corresponding pin as SPI function, here use USCI_B0 (SPI P1.4 UCB0CLK P1.7 UCB0SOMI)                  

  P1DIR &= ~BIT6; // P1.6 UCB0SOMI The MCU here is the host, set to input direction

  P1DIR |= BIT5 | BIT4 | BIT7; // P1.5 (SPI STE) P1.4 (UCB0CLK) P1.7 (UCB0SOMI) are all output pins

  P1OUT |= BIT5; // Enable the terminal to be high, and SPI communication is not performed at this time

  PM5CTL0 &= ~LOCKLPM5; // Activate the setting of the above pins of the MCU. Attention!!! Special commands of MSP430FR series MCU. I have not found this before.

                             // commands, all pin configurations don't work, I was screwed by 00 for a long time!!!

  UCB0CTLW0 |= UCSWRST;                     // Enable SW reset

  UCB0CTLW0 |= UCMSB+UCCKPH+UCMST+UCSYNC;   

    //  1 -  Synchronous mode 

    // [b2-1] 00-  3-pin SPI

    // [b3]   1 -  Master mode

    // [b4] 0 - 8-bit data

    // [b5]   1 - MSB first

    // [b6]   0 - Clock polarity high.

    //[b7] 1 - Clock phase - Data is captured on the first UCLK edge and changed on the following edge.

    //For the above settings, refer to the manual of the module to be driven, pay attention to the [b3] [b4] bits!

  UCB0CTLW0 |= UCSSEL_2;          // SMCLK

  UCB0BR0 = 0x01;                 // 16 MHz

  UCB0BR1 = 0; //

  UCB0CTLW0 &= ~UCSWRST;          // Clear SW reset, resume operation

  //UCB0IE = 0x00;

}

SPI read and write timing settings

According to the data sheet, understanding the module's SPI read and write timing is the key step to successful programming! ! ! 
The following is the SPI read and write timing diagram of AFE4400:


When reading data: pull STE low, first send a byte of register address to AFE4400, wait for a while, AFE4400 will return the data of the address to the microcontroller, send byte by byte, a total of 3 bytes of 24-bit data. (The microcontroller needs to send three times at a time) 
When writing data: pull STE low, first send the register address you want to write, and then send 3 bytes of 24-bit data in sequence, you can change the data of the corresponding register in AFE4400. (The microcontroller needs to send three times at a time)

SPI reads the code of AFE4400 register value:

unsigned long AFE4400_Reg_Read(unsigned char Reg_address)

{

  unsigned char SPI_Rx_buf[4]; //Store the read register value

  unsigned long retVal;

  retVal = 0;

  P1OUT&= ~BIT5; // Pull STE low


  UCB0TXBUF = Reg_address; // Send the register address to be read

  while ( (UCB0STAT & UCBUSY) );    // USCI_B0 TX buffer ready?

  SPI_Rx_buf[0] = UCB0RXBUF; // Read the received data, which is empty data at this time

  UCB0TXBUF = 0; // Empty instruction, waiting for delay effect

  while ( (UCB0STAT & UCBUSY) );    // USCI_B0 TX buffer ready?

  SPI_Rx_buf[1] = UCB0RXBUF; // Read received data: Data[23:16]

  UCB0TXBUF = 0; // Empty instruction, waiting for delay effect

  while ( (UCB0STAT & UCBUSY) );    // USCI_B0 TX buffer ready?

  SPI_Rx_buf[2] = UCB0RXBUF; // Read received data: Data[15:8]

  UCB0TXBUF = 0; // Empty instruction, waiting for delay effect

  while ( (UCB0STAT & UCBUSY) );        // USCI_B0 TX buffer ready?

  SPI_Rx_buf[3] = UCB0RXBUF; // Read received data: Data[7:0]


  P1OUT|=BIT5; // Reading completed, pull STE high

  retVal = SPI_Rx_buf[1]; //Integrate data into 24-bit data

  retVal = (retVal << 8) | SPI_Rx_buf[2];

  retVal = (retVal << 8) | SPI_Rx_buf[3]; 


  return    retVal;

}


SPI writes data into the AFE4400 register code:


void AFE4400_Reg_Write (unsigned char reg_address, unsigned long data)

{

  unsigned char dummy_rx;


  P1OUT&= ~BIT5; // Pull STE low


  UCB0TXBUF = reg_address; // Send the register address to be written

  while ( (UCB0STAT & UCBUSY) );  // USCI_B0 TX buffer ready?

  dummy_rx = UCB0RXBUF; // Empty instruction, waiting for delay effect

  UCB0TXBUF = (unsigned char)(data >>16); // Pass the data to be written: Data[23:16] to the transmit buffer

  while ( (UCB0STAT & UCBUSY) );        // USCI_B0 TX buffer ready?

  dummy_rx = UCB0RXBUF; // Empty instruction, waiting for delay effect

  UCB0TXBUF = (unsigned char)(((data & 0x00FFFF) >>8)); // Pass the data to be written: Data[15:8] to the transmit buffer 

  while ( (UCB0STAT & UCBUSY) );              // USCI_B0 TX buffer ready?

  dummy_rx = UCB0RXBUF; // Empty instruction, waiting for delay effect

  UCB0TXBUF = (unsigned char)(((data & 0x0000FF))); // Pass the data to be written: Data[7:0] to the transmit buffer

  while ( (UCB0STAT & UCBUSY) );                        // USCI_B1 TX buffer ready?

  dummy_rx = UCB0RXBUF; // Empty instruction, waiting for delay effect


  P1OUT|=BIT5; // Writing completed, pull STE high

}

Register Write

After completing the above two steps, AFE4400 can be used by us obediently and listen to us very much! By checking the register function manual and writing the corresponding values ​​to configure the functions of AFE4400, we can achieve the functions we want. 
Some registers of AFE4400:

Write the picture description here


Keywords:MSP430 Reference address:MSP430 SPI driver code design process

Previous article:【TI MSP430】How to implement analog serial communication
Next article:MSP430 SPI communication routine (SD card initialization-theoretical explanation)

Recommended ReadingLatest update time:2024-11-23 11:29

MSP430 MCU matrix keyboard and digital tube experiment
#include "msp430x44x.h" #define ROW P2OUT            //matrix keyboard row macro definition #define COL P2IN              //matrix keyboard column macro definition #define DPYOUT P3OUT      //digital tube output macro definition unsigned char keyval;    //key value //Code table of common anode digital tube       
[Microcontroller]
MSP430 MCU Timer B Interrupt Experiment
This program is based on the internal timer B interrupt experiment of MSP430 single chip. Function: Use timer B to accurately set the time interval of the running light. #include "MSP430F149.h" #define uchar unsigned char #define uint unsigned int  uchar LedData=0x80; uchar num=50;//Interrupt 50 times to shift the
[Microcontroller]
MSP430F44X MCU SPI interface driver C language program
#include     char MST_Data=0X00,SLV_Data=0XFF;     void Init(void);     void main(void)     {         unsigned int i;        WDTCTL=WDTPW+WDTHOLD;        Heat();        _UNITE();        P3OUT&=~0X020;        P3OUT|=0X020;        i=50000;        do(i--);        while(i!=0);        while(1)
[Microcontroller]
MSP430F149 Timer
(1) Timing data implemented using timer A mode 2 (continuous) The timing data implemented using timer A mode 2 (continuous) controls TACTL in the program. When TACTL is assigned the following value: TACTL = TASSEL_2 + MC_2 + TAIE; // SMCLK, contmode, interrupt TASSEL_X can be changed to achieve different timin
[Microcontroller]
MSP430F149 Timer
Design of CCD camera dimming photoelectric control system based on MSP430 microcontroller
MagTek card reader chip 21006450 is widely used, and its research will help the actual application of card reader chip driver. The Android system architecture, chip working principle and interface call studied here are the theoretical basis for the design of card reader chip driver, and provide guidance for the implem
[Power Management]
Design of CCD camera dimming photoelectric control system based on MSP430 microcontroller
MSP430G2553 test program (breathing light)
//************************************************ **************************** //Modified by http://jiwm.blog.163.com     //MSP430G2553 breathing light demonstration program - using Timer_A, Up Mode, DCO SMCLK // //   Introduction: This program uses the UP mode of TIMER A to generate PWM output on the P1.6 pin.
[Microcontroller]
MSP430 Study Notes 11- Eight-way ADC acquisition Nokia 5110 LCD display
This program uses the eight-channel ADC single acquisition mode. According to the configuration, ADC12SC will automatically reset after the conversion is completed in the single mode, so it is necessary to perform the ADC12CTL0 |= ADC12SC; operation in the loop. If it is configured as a continuous acquisition mode, it
[Microcontroller]
Application of MSP430 MCU in Micro Low Power Data Broadcast Receiver
1. Introduction We independently developed a data broadcast encoder and a miniature low-power FM data broadcast receiver. The entire system uses continuous phase frequency shift keying (CPFSK) modulation, with a subcarrier frequency of 72KHz, an occupied bandwidth of about 16Khz, and a data transmission rate of
[Microcontroller]
Application of MSP430 MCU in Micro Low Power Data Broadcast Receiver
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号