netjob @2008
http://bbs.21ic.com/club/bbs/bbsView.asp?
//=============================================================================
//
//=============================================================================
void STM32_SPI_Configuration(void)
{
//SPI_InitTypeDef SPI_InitStructure;
//GPIO_InitTypeDef GPIO_InitStructure;
/* Enable SPI1 and GPIOA clocks */
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
STM32_Rcc_Regs->apb2enr.all|=RCC_SPI1EN; // 时钟使能
/* Configure SPI1 pins: NSS, SCK, MISO and MOSI */
//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);
STM32_Gpioa_Regs->crl.bit.CNF5=Output_Af_push_pull;
STM32_Gpioa_Regs->crl.bit.MODE5=Output_Mode_50mhz;
STM32_Gpioa_Regs->crl.bit.CNF6=Output_Af_push_pull;
STM32_Gpioa_Regs->crl.bit.MODE6=Output_Mode_50mhz;
STM32_Gpioa_Regs->crl.bit.CNF7=Output_Af_push_pull;
STM32_Gpioa_Regs->crl.bit.MODE7=Output_Mode_50mhz;
/* Configure PB.12 as Output push-pull, used as Flash Chip select */
//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);
STM32_Gpioa_Regs->crl.bit.CNF4=Output_push_pull;
STM32_Gpioa_Regs->crl.bit.MODE4=Output_Mode_50mhz;
/* Deselect the FLASH: Chip Select high */
//GPIOA->BSRR = GPIO_Pin_4;
STM32_Gpioa_Regs->bsrr.bit.BS4=1;// 置位
/* SPI2 configuration */
//SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
//SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
//SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
//SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
//SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
//SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
//SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;
//SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
//SPI_InitStructure.SPI_CRCPolynomial = 7;
//SPI_Init(SPI1, &SPI_InitStructure);
STM32_Spi1_Regs->cr1.bit.BR=2; // 72mhz/8=9mhz 010=Fpclk/8
STM32_Spi1_Regs->cr1.bit.CPHA=1; // 1: 数据采样从第二个时钟边沿开始
STM32_Spi1_Regs->cr1.bit.CPOL=1; // 1: 空闲状态时,SCK保持高电平
STM32_Spi1_Regs->cr1.bit.DFF=0; // 0:使用8位数据帧格式进行发送/接收
STM32_Spi1_Regs->cr1.bit.LSBFIRST=0; // 0:先发送MSB
STM32_Spi1_Regs->cr1.bit.MSTR=1; // 1:配置为主设备
STM32_Spi1_Regs->cr1.bit.SSM=1; // 1:启用软件从设备管理。
STM32_Spi1_Regs->cr1.bit.BIDIMODE=0; //0:选择“双线双向”模式
/* Enable SPI1 */
//SPI_Cmd(SPI1, ENABLE);
STM32_Spi1_Regs->cr1.bit.SPE=1; //1:开启SPI设备
}//end sub
用户392262 2010-7-31 11:53
用户68740 2007-1-4 15:56