|
楼主想是用MAX7219数码管模块,使用SPI通讯,在网上找了一个例程,但是想改成SPI2通讯。楼主改了好长时间没有改好。求大神帮忙改下,谢谢。楼主还有一个疑问:代码标记部分不属于SPI通讯IO口啊,为什么会用到这个,而且改成其他IO口不好用。
- void SPI1_Init(void)
- {
- SPI_InitTypeDef SPI1_InitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
-
- //配置SPI1管脚
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOA, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1,ENABLE);
- <font color="#ff0000"> GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3; //此处是标记</font>
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
- GPIO_Init(GPIOA,&GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4|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);
-
- SPI1_InitStructure.SPI_Direction=SPI_Direction_2Lines_FullDuplex;
- SPI1_InitStructure.SPI_BaudRatePrescaler=SPI_BaudRatePrescaler_64;
- SPI1_InitStructure.SPI_DataSize=SPI_DataSize_8b;
- SPI1_InitStructure.SPI_Mode=SPI_Mode_Master;
- SPI1_InitStructure.SPI_FirstBit=SPI_FirstBit_MSB; //高位在先
- SPI1_InitStructure.SPI_CPOL = SPI_CPOL_High;
- SPI1_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
- SPI1_InitStructure.SPI_NSS = SPI_NSS_Hard; // 使用硬件模式,PA4需要接高电平
- SPI1_InitStructure.SPI_CRCPolynomial = 7;
-
- SPI_I2S_DeInit(SPI1);
-
- SPI_Init(SPI1, &SPI1_InitStructure);
-
- SPI_Cmd(SPI1, ENABLE);
- }
- //-------------------------------------------
- //功能:向MAX7219写入数据
- //入口参数:address、dat
- //出口参数:无
- //说明:
- void Write_Max7219(u8 address,u8 dat)
- {
- GPIO_ResetBits(GPIOA, GPIO_Pin_3);
- SPI_I2S_SendData(SPI1, address); //写入地址,即数码管编号
- delay_us(10);
- SPI_I2S_SendData(SPI1, dat); //写入数据,即数码管显示数字
- delay_us(10);
- GPIO_SetBits(GPIOA, GPIO_Pin_3);
- }
- void Init_MAX7219(void)
- {
- Write_Max7219(0x09, 0xff); //译码方式:BCD码
- Write_Max7219(0x0a, 0x05); //亮度
- Write_Max7219(0x0b, 0x07); //扫描界限;8个数码管显示
- Write_Max7219(0x0c, 0x01); //掉电模式:0,普通模式:1
- Write_Max7219(0x0f, 0x00); //显示测试:1;测试结束,正常显示:0
- }
复制代码
|
|