SPI的配置和使用

SPI的配置和使用

@(MSP432P401R)

SPI的配置

在这里插入图片描述


SPI的API的使用

SPI_Transaction

在这里插入图片描述

  1. count即帧数,传输到 SPI 总线上的最小单个数据单元称为 SPI 帧

  2. txBuf即发送缓冲区

  3. rxBuf即接收缓冲区

  4. arg即自定义参数

  5. status即SPI状态
    在这里插入图片描述

  6. nextPtr即下一个指针,驱动程序内部使用

SPI参数

在这里插入图片描述

  1. transferMode即传输模式
    在这里插入图片描述

  2. transferTimeout即传输超时时间

  3. transferCallbackFxn即回调函数

  4. mode即SPI模式
    在这里插入图片描述

  5. bitRate即比特率
    在这里插入图片描述

  6. dataSize即数据帧的位数

  7. frameFormat即帧格式
    在这里插入图片描述

  8. custom即自定义参数

函数表(略)

全局配置(略)

功能函数

在这里插入图片描述

重要函数

  1. 传输函数(发送/接收数据)
    bool SPI_transfer(SPI_Handle handle,SPI_Transaction * transaction )
    在这里插入图片描述

文档链接:

  1. file:///D:/MSP%20SDK/simplelink_msp432p4_sdk_3_40_01_02/docs/tidrivers/doxygen/html/_s_p_i_8h.html#ab9ea76c6529d6076eee5e1c4a5a92c6f
  2. https://blog.csdn.net/u010632165/article/details/109460814
  3. https://blog.csdn.net/zangqihu/article/details/122626009?spm=1001.2101.3001.6650.7&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-7-122626009-blog-109460814.pc_relevant_multi_platform_whitelistv2&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-7-122626009-blog-109460814.pc_relevant_multi_platform_whitelistv2&utm_relevant_index=13

SPI驱动OLED(未成功)

//#include "msp.h"
#include <stdint.h>
/* Driver Header files */
#include <ti/drivers/SPI.h>
#include <ti/drivers/GPIO.h>
#include <ti/drivers/apps/LED.h>
/* Driver Configuration */
#include "ti_drivers_config.h"
/**
 * main.c
 */
SPI_Handle      spi;
SPI_Params      spiParams;
SPI_Transaction spiTransaction;
bool transferOK;
uint8_t receiveBuffer[100];
uint8_t F8x16[]=
{0x00,0x00,0x00,0x30,0x00,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//".",0
        /* (8 X 16 , 宋体 ) */
0x00,0x00,0x00,0x60,0x00,0x18,0x00,0x07,0xC0,0x00,0x38,0x00,0x04,0x00,0x00,0x00,//"/",1
        /* (8 X 16 , 宋体 ) */
0x00,0x00,0xE0,0x0F,0x10,0x10,0x08,0x20,0x08,0x20,0x10,0x10,0xE0,0x0F,0x00,0x00,//"0",2
        /* (8 X 16 , 宋体 ) */
0x00,0x00,0x00,0x00,0x10,0x20,0x10,0x20,0xF8,0x3F,0x00,0x20,0x00,0x20,0x00,0x00,//"1",3
        /* (8 X 16 , 宋体 ) */
0x00,0x00,0x70,0x30,0x08,0x28,0x08,0x24,0x08,0x22,0x08,0x21,0xF0,0x30,0x00,0x00,//"2",4
        /* (8 X 16 , 宋体 ) */
0x00,0x00,0x30,0x18,0x08,0x20,0x08,0x21,0x08,0x21,0x88,0x22,0x70,0x1C,0x00,0x00,//"3",5
        /* (8 X 16 , 宋体 ) */
0x00,0x00,0x00,0x06,0x80,0x05,0x40,0x24,0x30,0x24,0xF8,0x3F,0x00,0x24,0x00,0x24,//"4",6
        /* (8 X 16 , 宋体 ) */
0x00,0x00,0xF8,0x19,0x88,0x20,0x88,0x20,0x88,0x20,0x08,0x11,0x08,0x0E,0x00,0x00,//"5",7
        /* (8 X 16 , 宋体 ) */
0x00,0x00,0xE0,0x0F,0x10,0x11,0x88,0x20,0x88,0x20,0x90,0x20,0x00,0x1F,0x00,0x00,//"6",8
        /* (8 X 16 , 宋体 ) */
0x00,0x00,0x18,0x00,0x08,0x00,0x08,0x3E,0x88,0x01,0x68,0x00,0x18,0x00,0x00,0x00,//"7",9
        /* (8 X 16 , 宋体 ) */
0x00,0x00,0x70,0x1C,0x88,0x22,0x08,0x21,0x08,0x21,0x88,0x22,0x70,0x1C,0x00,0x00,//"8",10
        /* (8 X 16 , 宋体 ) */
0x00,0x00,0xF0,0x01,0x08,0x12,0x08,0x22,0x08,0x22,0x10,0x11,0xE0,0x0F,0x00,0x00,//"9",11
        /* (8 X 16 , 宋体 ) */

};
uint8_t F16x16[]=
{
0x08,0x00,0x08,0x80,0x08,0x40,0x08,0x20,0x08,0x18,0xF8,0x07,0x89,0x00,0x8E,0x00,
0x88,0x40,0x88,0x80,0x88,0x40,0x88,0x3F,0x08,0x00,0x08,0x00,0x08,0x00,0x00,0x00,//"方",0
/* (16 X 16 , 宋体 ) */

0x10,0x04,0x60,0x04,0x02,0x7C,0x0C,0x03,0xC0,0x80,0x00,0x60,0xF8,0x1F,0x88,0x80,
0x88,0x43,0x88,0x2C,0xFF,0x10,0x88,0x28,0x88,0x46,0xA8,0x81,0x18,0x80,0x00,0x00,//"波",1
/* (16 X 16 , 宋体 ) */
0x00,0x40,0x02,0x40,0x02,0x40,0xC2,0x7F,0x02,0x40,0x02,0x40,0x02,0x40,0xFE,0x7F,
0x82,0x40,0x82,0x40,0x82,0x40,0x82,0x40,0x82,0x40,0x02,0x40,0x00,0x40,0x00,0x00,//"正",2
/* (16 X 16 , 宋体 ) */

0x02,0x00,0xE2,0x43,0x22,0x82,0x22,0x42,0x3E,0x3E,0x00,0x00,0x08,0x21,0x88,0x71,
0x48,0x29,0x39,0x25,0x0E,0x23,0x08,0x21,0xC8,0x28,0x08,0x70,0x08,0x00,0x00,0x00,//"弦",3
/* (16 X 16 , 宋体 ) */
0x00,0x20,0x04,0x20,0x84,0x20,0x84,0x20,0x84,0x20,0x84,0x20,0x84,0x20,0x84,0x20,
0x84,0x20,0x84,0x20,0x84,0x20,0x84,0x20,0x84,0x20,0x04,0x20,0x00,0x20,0x00,0x00,//"三",4
/* (16 X 16 , 宋体 ) */

0x20,0x80,0x10,0x60,0xE8,0x1F,0x24,0x09,0x27,0x09,0x24,0x09,0x24,0x09,0xE4,0x7F,
0x24,0x09,0x34,0x09,0x2C,0x49,0x20,0x89,0xE0,0x7F,0x00,0x00,0x00,0x00,0x00,0x00,//"角",5
/* (16 X 16 , 宋体 ) */
};


uint32_t oled_pow(uint8_t m,uint8_t n);
void delay_ms(uint32_t ms);
//void User_SPICallback(SPI_Handle spi, SPI_Transaction *tran);
void OLED_Set_Pos_16x16(uint8_t x,uint8_t y);
void OLED_Set_Pos_8x16(uint8_t x,uint8_t y);
void OLED_Display_On(void);
void OLED_Display_Off(void);
void OLED_Clear(void);
void OLED_ShowChar(uint8_t x,uint8_t y,uint8_t chr);
void OLED_ShowNum_int(uint8_t x,uint8_t y,uint32_t num);
void OLED_ShowNum_float(uint8_t x,uint8_t y,uint8_t n,float num);
void OLED_ShowString(uint8_t x,uint8_t y,uint8_t *chr);
void OLED_ShowCHinese(uint8_t x,uint8_t y,uint8_t no);
void OLED_Init(void);

/**
 * main.c
 */
void *mainThread(void *arg0)
{
    GPIO_init();
    SPI_init();  // Initialize the SPI driver
    SPI_Params_init(&spiParams);  // Initialize SPI parameters
    //spiParams.transferMode=SPI_MODE_CALLBACK;
    spiParams.transferMode=SPI_MODE_BLOCKING;
    spiParams.transferTimeout=100000;
    //spiParams.transferCallbackFxn=User_SPICallback;
    spiParams.mode=SPI_MASTER;
    spiParams.dataSize=8;
    spiParams.frameFormat=SPI_POL0_PHA0;
    spiParams.bitRate=5000000;
      // 8-bit data size
    spi = SPI_open(CONFIG_SPI_0, &spiParams);
    if (spi == NULL)
    {
        while (1);  // SPI_open() failed
    }
    // Fill in transmitBuffer
    //GPIO_write(CONFIG_GPIO_3,CONFIG_GPIO_LED_ON);
    OLED_Init();
    OLED_ShowCHinese(8,1,0);
    GPIO_write(CONFIG_GPIO_3,CONFIG_GPIO_LED_ON);
    return (NULL);
}


//void User_SPICallback(SPI_Handle spi, SPI_Transaction *tran)
//{
//}

uint32_t oled_pow(uint8_t m,uint8_t n)
{
    uint32_t result=1;
    while(n--)result*=m;
    return result;
}

void delay_ms(uint32_t ms)
{
    uint16_t a;
    while(ms)
    {
        a=4000;
        while(a--);
        ms--;
    }
    return;
}


void OLED_Set_Pos_16x16(uint8_t x,uint8_t y)
{

    uint8_t CMD[6]={0x21,0x00+x,0x00+x+15,0x22,0x00+y,0x00+y+1};
    GPIO_write(CONFIG_GPIO_1,0);//CS
    GPIO_write(CONFIG_GPIO_0,0);//DC
    spiTransaction.count = 6;
    spiTransaction.txBuf = (void*)(CMD);
    //spiTransaction.rxBuf = (void *)receiveBuffer;
    transferOK = SPI_transfer(spi, &spiTransaction);
    if (!transferOK)
    {
        // Error in SPI or transfer already in progress.
        while (1);
    }
    GPIO_write(CONFIG_GPIO_1,1);//CS
}
void OLED_Set_Pos_8x16(uint8_t x,uint8_t y)
{

    uint8_t CMD[6]={0x21,0x00+x,0x00+x+7,0x22,0x00+y,0x00+y+1};
    GPIO_write(CONFIG_GPIO_1,0);//CS
    GPIO_write(CONFIG_GPIO_0,0);//DC
    spiTransaction.count = 6;
    spiTransaction.txBuf = (void*)(CMD);
    //spiTransaction.rxBuf = (void *)receiveBuffer;
    transferOK = SPI_transfer(spi, &spiTransaction);
    if (!transferOK)
    {
        // Error in SPI or transfer already in progress.
        while (1);
    }
    GPIO_write(CONFIG_GPIO_1,1);//CS
}
void OLED_Display_On(void)
{

    uint8_t CMD[3]={0x8D,0x14,0xAF};
    GPIO_write(CONFIG_GPIO_1,0);//CS
    GPIO_write(CONFIG_GPIO_0,0);//DC
    spiTransaction.count = 3;
    spiTransaction.txBuf = (void*)(CMD);
    //spiTransaction.rxBuf = (void *)receiveBuffer;
    transferOK = SPI_transfer(spi, &spiTransaction);
    if (!transferOK)
    {
        // Error in SPI or transfer already in progress.
        while (1);
    }
    GPIO_write(CONFIG_GPIO_1,1);//CS

}
void OLED_Display_Off(void)
{
    uint8_t CMD[3]={0x8D,0x10,0xAE};
    GPIO_write(CONFIG_GPIO_1,0);//CS
    GPIO_write(CONFIG_GPIO_0,0);//DC
    spiTransaction.count = 3;
    spiTransaction.txBuf = (void*)(CMD);
    //spiTransaction.rxBuf = (void *)receiveBuffer;
    transferOK = SPI_transfer(spi, &spiTransaction);
    if (!transferOK)
    {
        // Error in SPI or transfer already in progress.
        while (1);
    }
    GPIO_write(CONFIG_GPIO_1,1);//CS
}
void OLED_Clear(void)
{
    uint32_t i;
    uint8_t CMD[6]={0x21,0x00,0x7f,0x22,0x00,0x07};
    uint8_t DATA[128]={0x00};
    GPIO_write(CONFIG_GPIO_1,0);//CS
    GPIO_write(CONFIG_GPIO_0,0);//DC
    spiTransaction.count = 6;
    spiTransaction.txBuf = (void*)(CMD);
    //spiTransaction.rxBuf = (void *)receiveBuffer;
    transferOK = SPI_transfer(spi, &spiTransaction);
    if (!transferOK)
    {
        // Error in SPI or transfer already in progress.
        while (1);
    }
    GPIO_write(CONFIG_GPIO_1,1);//CS

    GPIO_write(CONFIG_GPIO_1,0);//CS
    GPIO_write(CONFIG_GPIO_0,1);//DC
    spiTransaction.count = 1024;
    spiTransaction.txBuf = (void*)(DATA);
    //spiTransaction.rxBuf = (void *)receiveBuffer;
    transferOK = SPI_transfer(spi, &spiTransaction);
    if (!transferOK)
    {
        // Error in SPI or transfer already in progress.
        while (1);
    }

    for(i=0;i<8;i++)
    {
        spiTransaction.count = 128;
        spiTransaction.txBuf = (void*)(DATA);
        //spiTransaction.rxBuf = (void *)receiveBuffer;
        transferOK = SPI_transfer(spi, &spiTransaction);
        if (!transferOK)
        {
            // Error in SPI or transfer already in progress.
            while (1);
        }
        delay_ms(30);
    } //更新显示

    GPIO_write(CONFIG_GPIO_1,1);//CS

}
void OLED_ShowChar(uint8_t x,uint8_t y,uint8_t chr)
{
    uint8_t c;
    c=chr-'.';
    if(x>127){x=0;y=y+2;}
    OLED_Set_Pos_8x16(x,y);
    GPIO_write(CONFIG_GPIO_1,0);//CS
    GPIO_write(CONFIG_GPIO_0,1);
    spiTransaction.count = 16;
    spiTransaction.txBuf = (void*)(F8x16+c*16);
    //spiTransaction.rxBuf = (void *)receiveBuffer;
    transferOK = SPI_transfer(spi, &spiTransaction);
    if (!transferOK)
    {
        // Error in SPI or transfer already in progress.
        while (1);
    }
    GPIO_write(CONFIG_GPIO_1,1);//CS
}
void OLED_ShowNum_int(uint8_t x,uint8_t y,uint32_t num)
{
    uint32_t t,temp,len;
    uint32_t Num=num;
    len=1;
    while(Num/10)
    {
        len++;
        Num=Num/10;
    }
    for(t=0;t<len;t++)
    {
        temp=(num/oled_pow(10,len-t-1))%10;
        OLED_ShowChar(x+8*t,y,temp+'0');//显示每一位

    }
}

显示1个浮点数数字(带小数)
x,y :起点坐标
n:显示的小数部分位数
num:数值
void OLED_ShowNum_float(uint8_t x,uint8_t y,uint8_t n,float num)
{
    uint8_t len;
    uint32_t Num1,Num2,Num_Float;
    float num1,num2;
    Num1=Num2=(int)num;
    OLED_ShowNum_int(x,y,Num1);//整数部分
    len=1;
    while(Num1/10)
    {
        len++;
        Num1=Num1/10;
    }
    OLED_ShowChar(x+len*8,y,'.');//小数点
    num1=num-Num2;
    num2=num1*oled_pow(10,n);
    //Num_Float=(int)((num-Num2)*oled_pow(10,n));
    Num_Float=(int)num2;
    OLED_ShowNum_int(x+(len+1)*8,y,Num_Float);//小数部分

}
/**
  * @brief   显示一个字符串
  * @param   x : uint8_t 起点列坐标
  * @param   y : uint8_t 起点行坐标
  * @param   chr : uint8_t *字符串指针
  * @retval  无
  */
void OLED_ShowString(uint8_t x,uint8_t y,uint8_t *chr)
{
    uint8_t j=0;
    while (chr[j]!='\0')
    {       OLED_ShowChar(x,y,chr[j]);
            x+=8;
            if(x>120){x=0;y+=2;}//下一行
            j++;
    }
}
void OLED_ShowCHinese(uint8_t x,uint8_t y,uint8_t no)
{
    OLED_Set_Pos_16x16(x,y);
    GPIO_write(CONFIG_GPIO_1,0);//CS
    GPIO_write(CONFIG_GPIO_0,1);
    spiTransaction.count = 32;
    spiTransaction.txBuf = (void*)(F16x16+no*32);
    //spiTransaction.rxBuf = (void *)receiveBuffer;
    transferOK = SPI_transfer(spi, &spiTransaction);
    if (!transferOK)
    {
        // Error in SPI or transfer already in progress.
        while (1);
    }
    GPIO_write(CONFIG_GPIO_1,1);//CS
}
void OLED_Init(void)
{

    GPIO_write(CONFIG_GPIO_2,1);
    ClockP_usleep(100000);
    GPIO_write(CONFIG_GPIO_2,0);
    ClockP_usleep(100000);
    GPIO_write(CONFIG_GPIO_2,1);

    uint8_t CMD1[]={0xAE,0x02,0x10,0x40,0x81,0xCF,0xA1,0xC8,0xA6,0xA8};
    uint8_t CMD2[]={0x3f,0xD3,0x00,0xD5,0x80,0xD9,0xF1,0xDA,0x12,0xDB};
    uint8_t CMD3[]={0x40,0x20,0x01,0x8D,0x14,0xA4,0xA6,0xAF,0xAF};
    GPIO_write(CONFIG_GPIO_1,0);//CS
    GPIO_write(CONFIG_GPIO_0,0);
    spiTransaction.count = 10;
    spiTransaction.txBuf = (void*)(CMD1);
    spiTransaction.rxBuf = (void *)receiveBuffer;
    transferOK = SPI_transfer(spi, &spiTransaction);
    if (!transferOK)
    {
        // Error in SPI or transfer already in progress.
        while (1);
    }
    spiTransaction.count = 10;
    spiTransaction.txBuf = (void*)(CMD2);
    spiTransaction.rxBuf = (void *)receiveBuffer;
    transferOK = SPI_transfer(spi, &spiTransaction);
     if (!transferOK)
     {
         // Error in SPI or transfer already in progress.
         while (1);
     }
     spiTransaction.count = 9;
     spiTransaction.txBuf = (void*)(CMD3);
     spiTransaction.rxBuf = (void *)receiveBuffer;
      transferOK = SPI_transfer(spi, &spiTransaction);
       if (!transferOK)
       {
           // Error in SPI or transfer already in progress.
           while (1);
       }
    GPIO_write(CONFIG_GPIO_1,1);//CS
    OLED_Clear();
}

版权声明:本文为m0_57608081原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。