Спойлер
Код:
#include<stm32f4xx_spi.h>
#include<stm32f4xx_gpio.h>
#include "stm32f4xx.h"
#include "stm32f4xx_rcc.h"
#include<delay.h>
/*
MOSI-->PC12
SCK-->PC10
CS-->PC11
RS-->PC9
RESET-->PC8
*/
uint8_t i;
uint8_t j;
const uint16_t mas0[2] = {0xFDFD,0xFDFD};
const uint16_t mas1[10] = {0xEF00,0xEE04,0x1B04,0xFEFE,0xFEFE,0xEF90,0x4A04,0x7F3F,0xEE04,0x4306};
const uint16_t mas2[20] = {0xEF90,0x0983,0x0800,0x0BAF,0x0A00,0x0500,0x0600,0x0700,0xEF00,0xEE0C,0xEF90,0x0080,0xEFB0,0x4902,0xEF00,0x7F01,0xE181,0xE202,0xE276,0xE183};
const uint16_t mas3[3] = {0x8001,0xEF90,0x0000};
#define CSH GPIO_SetBits(GPIOC, GPIO_Pin_11);
#define CSL GPIO_ResetBits(GPIOC, GPIO_Pin_11);
#define RESETH GPIO_SetBits(GPIOC, GPIO_Pin_8);
#define RESETL GPIO_ResetBits(GPIOC, GPIO_Pin_8);
#define RSH GPIO_SetBits(GPIOC, GPIO_Pin_9);
#define RSL GPIO_ResetBits(GPIOC, GPIO_Pin_9);
SPI_InitTypeDef SPI_InitStructure;
void io_init()
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11|GPIO_Pin_8|GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
void LS020Cmd(uint16_t data)
{
while (!(SPI3->SR & SPI_SR_TXE));
CSL;
RSH;
while (SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI3, data);
while (SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_BSY) == RESET);
CSH;
}
void SPI_init()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_SPI3);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource12, GPIO_AF_SPI3);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_12;
GPIO_Init(GPIOC, &GPIO_InitStructure);
SPI_I2S_DeInit(SPI3);
SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
/* Initializes the SPI communication */
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_Init(SPI3, &SPI_InitStructure);
SPI_Cmd(SPI3, ENABLE);
}
void LS020_INIT(void)
{
RESETL;
delay_ms(150);
RESETH;
for(i=0; i<2; i++)
{
LS020Cmd (mas0[i]);
}
delay_ms(50);
for(i=0; i<10; i++)
{
LS020Cmd (mas1[i]);
}
delay_ms(7);
for(i=0; i<20; i++)
{
LS020Cmd (mas2[i]);
}
delay_ms(50);
for(i=0; i<3; i++)
{
LS020Cmd (mas3[i]);
}
delay_ms(10);
CSH;
}
int main(void)
{
SPI_init();
io_init();
LS020_INIT();
while(1)
{
}
}