- void RCC_Configuration(void);
- void GPIO_Configration(void);
- void SPI_Configuration(void);
- u8 Write_Max7219_byte(u8 data);
- void Write_Max7219_data(u8 add,u8 data);
- void init_MAX7219(void);
- int main(void)
- {
- RCC_Configuration();
- GPIO_Configration();
- SPI_Configuration();
- init_MAX7219();
- while(1)
- {
- Write_Max7219_data(1,1);
- Write_Max7219_data(2,2);
- Write_Max7219_data(3,3);
- }
- }
- void RCC_Configuration(void)
- {
- SystemInit();
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1,ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
- }
- void GPIO_Configration(void)
- {
- GPIO_InitTypeDef GPIO_Initstructure;
-
- //êy¾YDin:PA7 ê±ÖóClk:PA5
- 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);
-
- //ƬѡCE:PA3 êy¾YÖ¸áîDC£oPA3 ¸′λRST:PA2
- GPIO_Initstructure.GPIO_Pin=GPIO_Pin_3;
- GPIO_Initstructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_Initstructure.GPIO_Mode=GPIO_Mode_Out_PP;
- GPIO_Init(GPIOA,&GPIO_Initstructure);
- }
- void SPI_Configuration(void)
- {
- SPI_InitTypeDef SPI_InitStructure;
-
- 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_Low;
- SPI_InitStructure.SPI_CPHA=SPI_CPHA_1Edge;
- SPI_InitStructure.SPI_NSS=SPI_NSS_Soft;
- SPI_InitStructure.SPI_BaudRatePrescaler=SPI_BaudRatePrescaler_32;
- SPI_InitStructure.SPI_FirstBit=SPI_FirstBit_MSB;
- SPI_InitStructure.SPI_CRCPolynomial=7;
- SPI_Init(SPI1,&SPI_InitStructure);
-
- SPI_Cmd(SPI1,ENABLE);
- }
- u8 Write_Max7219_byte(u8 data)
- {
- u8 temp;
- while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE)==RESET);
- SPI_I2S_SendData(SPI1,data);
- while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_RXNE)==RESET);
- temp=SPI_I2S_ReceiveData(SPI1);
- return temp;
- }
- void Write_Max7219_data(u8 add,u8 data)
- {
- GPIO_ResetBits(GPIOA,GPIO_Pin_3);
- Write_Max7219_byte(add);
- Write_Max7219_byte(data);
- GPIO_SetBits(GPIOA,GPIO_Pin_3);
- }
- void init_MAX7219(void)
- {
- Write_Max7219_data(0x09,0xff);
- Write_Max7219_data(0x0A,0x08);
- Write_Max7219_data(0x0B,0x02);
- Write_Max7219_data(0x0c,0x01);
- Write_Max7219_data(0x0f,0x01);
- }
- 程序如上,数码管全亮!!!
复制代码
|