新手上路
- 积分
- 45
- 金钱
- 45
- 注册时间
- 2016-8-17
- 在线时间
- 9 小时
|
1金钱
#include "stm32f0xx.h"
#include "SPI.h"
#include "stm32f0xx_spi.h"
void SPI1_Init(void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); //??SPI1??
GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_0); //SCK
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_0); //MISO
GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_0); //MOSI
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; //SPI CS
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_ResetBits(GPIOA,GPIO_Pin_4);
// GPIO_SetBits(GPIOA,GPIO_Pin_4);
/*
*****************************************************************
* SPI????,GPIOB.3--SCK GPIOB.4--MISO GPIOB.5--MOSI *
*****************************************************************
*/
//// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
//// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
//// GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
//// GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
//// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3;
//// GPIO_Init(GPIOA, &GPIO_InitStructure);
//// GPIO_SetBits(GPIOA,GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7);
/*SCK */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;//#define FLASH_SCK_PIN GPIO_Pin_13
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);//#define FLASH_SCK_PORT GPIOB
/*MOSI */
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_7;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/*MISO */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_SetBits(GPIOA,GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7);
//GPIO_SetBits(GPIOA,GPIO_Pin_5);
/*
*************************************************************************************
* SPI???:SPI????????,?SPI??,?????????8??? *
* ?????????(W25X16????3),?????????? *
* ?????(1????????????,2?W25X16?????????) *
* ????????????????????SPI???2??,36MHz *
* W25X16???SPI?????75MHz,???????, *
*************************************************************************************
*/
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //??SPI???????????:SPI??????????
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //??SPI????:????SPI
SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b; //??SPI?????:SPI????8????
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; //??????????:?????
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; //???????????
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //NSS?????(NSS??)????(??SSI?)??:??NSS???SSI???
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16; //??????????:????????2
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //???????MSB???LSB???:?????MSB???
SPI_InitStructure.SPI_CRCPolynomial = 7; //CRC???????
SPI_Init(SPI1, &SPI_InitStructure); //??SPI_InitStruct???????????SPIx???
SPI_Cmd(SPI1, ENABLE); //??SPI??
SPI_RxFIFOThresholdConfig(SPI1,SPI_RxFIFOThreshold_QF);
//SPI1_ReadWriteByte(0xFFFF);
}
//uint16_t SPI1_ReadWriteByte(uint16_t TxData)
//{
// unsigned char retry=0;
// while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET) //?????SPI???????:????????
// {
// retry++;
// if(retry>200)return 0;
// }
// SPI_I2S_SendData16(SPI1, TxData); //????SPIx??????
// retry=0;
// while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET) //?????SPI???????:?????????
// {
// retry++;
// if(retry>200)return 0;
// }
// return SPI_I2S_ReceiveData16(SPI1); //????SPIx???????
//}
uint16_t SPI1_ReadWriteByte(uint16_t TxData)
{
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData16(SPI1, TxData);
// SPI_I2S_SendData16(SPI1, TxData);
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
return SPI_I2S_ReceiveData16(SPI1);
// return SPI_I2S_ReceiveData16(SPI1);
}
//SPI_RxFIFOThresholdConfig(SPI1,SPI_RxFIFOThreshold_QF);
void ADC78H90_Init()
{
SPI1_Init();
//???SPI
//SPI2_SetSpeed(SPI_BaudRatePrescaler_2);//???18M??,????
}
uint16_t ADC78H90_Read(uint8_t next_channel)
{
uint16_t ch=0x3800;
//uint16_t ch=0x1C00;
if(next_channel>2)
return 0;
ch=ch&(next_channel<<11);
//_printf("spi_read ch=%x %d \r\n ", ch, next_channel);
return SPI1_ReadWriteByte(ch);
}
|
最佳答案
查看完整内容[请看2#楼]
你能收到数配置的就都对
至于你说的数值偏小 具体的情况不知道,不好说
可能因为基准偏小
也可能是因为片子自己校准了外部失调,不知道你做过没有
|