初级会员
- 积分
- 100
- 金钱
- 100
- 注册时间
- 2017-9-13
- 在线时间
- 18 小时
|
1金钱
#include "stm32f10x.h"
#include "stm32f10x_spi.h"
unsigned char ly_Qtemp;
void Delay(unsigned int delay_ms)
{
u32 i=0;
for(;i<delay_ms;i++);
}
void RCC_Configuration(void)
{
ErrorStatus HSEStartUpStatus; //ía2¿¸ßËùê±Öó(HSE)μÄ1¤×÷×′쬱äá¿
RCC_DeInit(); //½«ËùóDóëê±ÖóÏà1ØμļÄ′æÆ÷éèÖÃÎaĬèÏÖμ
RCC_HSEConfig(RCC_HSE_ON); //Æô¶ˉía2¿¸ßËùê±ÖóHSE
HSEStartUpStatus = RCC_WaitForHSEStartUp(); //μè′yía2¿¸ßËùê±Öó(HSE)Î趨
if(SUCCESS == HSEStartUpStatus) //èç1ûía2¿¸ßËùê±ÖóòѾ-Î趨
{
/* Enable Prefetch Buffer */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); //FlashéèÖÃ
/* Flash 2 wait state */
FLASH_SetLatency(FLASH_Latency_2);
RCC_HCLKConfig(RCC_SYSCLK_Div1); //éèÖÃAHBê±ÖóμèóúÏμí3ê±Öó(1·ÖÆμ)/72MHZ
RCC_PCLK2Config(RCC_HCLK_Div1); //éèÖÃAPB2ê±ÖóoíHCLKê±ÖóÏàμè/72MHz(×î′óÎa72MHz)
RCC_PCLK1Config(RCC_HCLK_Div2); //éèÖÃAPB1ê±ÖóêÇHCLKê±ÖóμÄ2·ÖÆμ/36MHz(×î′óÎa36MHz)
#ifndef STM32F10X_CL //èç1ûê1óÃμÄ2»êÇSTM32F107x»òSTM32F105xÏμáDMCU,PLLòÔÏÂÅäÖÃ
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); //PLLCLK = 8MHz * 9 = 72 MHz
#else //èç1ûê1óÃμÄêÇSTM32F107x»òSTM32F105xÏμáDMCU,PLLòÔÏÂÅäÖÃ
/***** ÅäÖÃLLx *****/
/* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
RCC_PREDIV2Config(RCC_PREDIV2_Div5);
RCC_PLL2Config(RCC_PLL2Mul_8);
RCC_PLL2Cmd(ENABLE); //ê1ÄüPLL2
while (RCC_GetFlagStatus(RCC_FLAG_PLL2RDY) == RESET);//μè′yPLL2Î趨
/* PLL configuration: PLLCLK = (PLL2 / 5) * 9 = 72 MHz */
RCC_PREDIV1Config(RCC_PREDIV1_Source_PLL2, RCC_PREDIV1_Div5);
RCC_PLLConfig(RCC_PLLSource_PREDIV1, RCC_PLLMul_9);
#endif
RCC_PLLCmd(ENABLE); //ê1ÄüPLL
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET); //μè′yPLLÎ趨
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //éèÖÃÏμí3ê±ÖóμÄê±ÖóÔ′ÎaPLL
while(RCC_GetSYSCLKSource() != 0x08); //¼ì2éÏμí3μÄê±ÖóÔ′êÇ·ñêÇLL
RCC_ClockSecuritySystemCmd(ENABLE); //ê1ÄüÏμí3°2è«ê±Öó
/* Enable peripheral clocks --------------------------------------------------*/
/* Enable I2C1 and I2C1 clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
/* Enable GPIOA GPIOB SPI1 and USART1 clocks */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB
| RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD
| RCC_APB2Periph_AFIO, ENABLE);
}
}
void SPI_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_SPI1 | RCC_APB2Periph_GPIOC, ENABLE ); // | RCC_APB2Periph_AFIO
/* 3õê¼»ˉSCK¡¢MISO¡¢MOSIòy½Å */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_SetBits(GPIOA,GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7);
/* 3õê¼»ˉCSòy½Å */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_SetBits(GPIOA, GPIO_Pin_4);
/* PA0 */
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //????
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //LED1-->E.5 ????, ????
// GPIO_Init(GPIOA, &GPIO_InitStructure); //???? , IO ???? 50MHz
//
/* 3õê¼»ˉÅäÖÃSTM32 SPI1 */
SPI_InitStructure.SPI_Direction=SPI_Direction_2Lines_FullDuplex; //SPIéèÖÃÎaË«ÏßË«Ïòè«Ë«1¤
SPI_InitStructure.SPI_Mode=SPI_Mode_Master; //éèÖÃÎaÖ÷SPI
SPI_InitStructure.SPI_DataSize=SPI_DataSize_8b; //SPI·¢Ëí½óêÕ8λ֡½á11
SPI_InitStructure.SPI_CPOL=SPI_CPOL_Low; //ê±ÖóDü¿Õμí
SPI_InitStructure.SPI_CPHA=SPI_CPHA_1Edge; //êy¾Y2¶»ñóúμú1¸öê±ÖóÑØ
SPI_InitStructure.SPI_NSS=SPI_NSS_Soft; //NSSóéía2¿1ü½Å1üàí
SPI_InitStructure.SPI_BaudRatePrescaler=SPI_BaudRatePrescaler_2; //2¨ìØÂêÔ¤·ÖÆμÖμÎa2
SPI_InitStructure.SPI_FirstBit=SPI_FirstBit_MSB; //êy¾Y′«êä′óMSBλ¿aê¼
SPI_InitStructure.SPI_CRCPolynomial=7; //CRC¶àÏîê½Îa7
SPI_Init(SPI1,&SPI_InitStructure); //¸ù¾YSPI_InitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèSPI1¼Ä′æÆ÷
SPI_Cmd(SPI1,ENABLE); //STM32ê1ÄüSPI1
}
void SPI1_Send_Byte(unsigned char dat)
{
SPI_I2S_SendData(SPI1,dat);//D′1¸ö×Ö½úêy¾Y
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);//μè′yêy¾Y¼Ä′æÆ÷¿Õ
}
/*******************************************************************************
* oˉêyÃû : SPI1_Send_Short
* Ãèêö : SPI1·¢Ëí2¸ö×Ö½úêy¾Y(16λ)
* êäèë : dat:′y·¢ËíμÄ16λêy¾Y
* êä3ö : ÎT
* ·μ»ØÖμ : ÎT
* ËμÃ÷ : ÎT
*******************************************************************************/
void SPI1_Send_Short(unsigned short dat)
{
SPI1_Send_Byte(dat/256);//D′êy¾Y¸ßλ
SPI1_Send_Byte(dat); //D′êy¾Yμíλ
}
//¸′λw5500£¬
void ly_Rstw5500()
{
GPIO_ResetBits(GPIOC,GPIO_Pin_5);
Delay(50);
GPIO_SetBits(GPIOC,GPIO_Pin_5);
Delay(200);
}
//2¢Çò¼ì2aêÇ·ñòѾ-ᬽóéÏ
unsigned char ly_readbyte()
{
unsigned char i;
GPIO_ResetBits(GPIOA, GPIO_Pin_4);//ÖÃW5500μÄSCSÎaμíμçƽ
SPI1_Send_Short(0x002E);//í¨1ySPI1D′16λ¼Ä′æÆ÷μØÖ·
SPI1_Send_Byte(0X01);//í¨1ySPI1D′¿ØÖÆ×Ö½ú,1¸ö×Ö½úêy¾Y3¤¶è,¶áêy¾Y,Ñ¡Ôñí¨óüÄ′æÆ÷
i=SPI_I2S_ReceiveData(SPI1);
SPI1_Send_Byte(0x00);//·¢Ëíò»¸öαêy¾Y
i=SPI_I2S_ReceiveData(SPI1);//¶áè¡1¸ö×Ö½úêy¾Y
GPIO_SetBits(GPIOA, GPIO_Pin_4);//ÖÃW5500μÄSCSÎa¸ßμçƽ
return i;//·μ»Ø¶áè¡μ½μļÄ′æÆ÷êy¾Y
}
int main(void)
{
RCC_Configuration();
SPI_Configuration();
while(1)
{
ly_Rstw5500();
ly_Qtemp=ly_readbyte(); //读取不到数据
}
}
在读取 “PHYCFGR (W5500 PHY 配置寄存器) “ 该寄存器的时候没有返回的数据;
|
|