|
- #include "stm32f10x.h"
- #include "delay.h"
- void Delay(u16 nCount);
- void GPIO_Config(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
- 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);
-
- 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);
- }
-
- void SPI_Config(void)
- {
- SPI_InitTypeDef SPI1_InitStructure;
- SPI1_InitStructure.SPI_Direction=SPI_Direction_2Lines_FullDuplex;//设置SPI单向或者双向的数据模式:SPI设置为双线模式
- SPI1_InitStructure.SPI_BaudRatePrescaler=SPI_BaudRatePrescaler_64;//定义波特率预分频的值:波特率预分频值为64
- SPI1_InitStructure.SPI_DataSize=SPI_DataSize_8b;//设置SPI的数据大小:SPI发送接收8位帧结构
- SPI1_InitStructure.SPI_Mode=SPI_Mode_Master;//设置SPI工作模式,设置为主模式
- 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_Soft; // 使用软件模式
- SPI1_InitStructure.SPI_CRCPolynomial = 7; //CRC值计算的多项式
- SPI_I2S_DeInit(SPI1);
- SPI_Init(SPI1, &SPI1_InitStructure); //初始化
- SPI_NSSInternalSoftwareConfig(SPI1, SPI_NSSInternalSoft_Set);//内部制高电平
- SPI_Cmd(SPI1, ENABLE);
- }
-
- void Write_Max7219(u8 address,u8 dat)
- {
- //功能:向MAX7219写入数据
- //入口参数:address、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
- }
- int main(void)
- {
-
-
-
-
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOA|RCC_APB2Periph_SPI1, ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
- delay_init();
- GPIO_Config();
- SPI_Config();
- Init_MAX7219();
-
- GPIO_SetBits(GPIOA, GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10);
-
-
-
- while(1)
- {
- GPIO_SetBits(GPIOA, GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10);
- GPIO_ResetBits(GPIOA,GPIO_Pin_10|GPIO_Pin_1);
-
-
- Write_Max7219(0x01, 0x04);
- Write_Max7219(0x02, 0x01);
- Write_Max7219(0x03, 0x0f);
- Write_Max7219(0x04, 0x0f);
- Write_Max7219(0x05, 0x0f);
- Write_Max7219(0x06, 0x0f);
- Write_Max7219(0x07, 0x01);
- Write_Max7219(0x08, 0x01);
- delay_ms(1000);
- Write_Max7219(0x01, 0x03);
- Write_Max7219(0x02, 0x01);
- Write_Max7219(0x07, 0x00);
- Write_Max7219(0x08, 0x01);
- Write_Max7219(0x03, 0x0f);
- Write_Max7219(0x04, 0x0f);
- Write_Max7219(0x05, 0x0f);
- Write_Max7219(0x06, 0x0f);
- delay_ms(1000);
- Write_Max7219(0x01, 0x02);
- Write_Max7219(0x02, 0x01);
- Write_Max7219(0x03, 0x09);
- Write_Max7219(0x04, 0x00);
- Write_Max7219(0x03, 0x0f);
- Write_Max7219(0x04, 0x0f);
- Write_Max7219(0x05, 0x0f);
- Write_Max7219(0x06, 0x0f);
- delay_ms(1000);
- Write_Max7219(0x01, 0x01);
- Write_Max7219(0x02, 0x01);
- Write_Max7219(0x07, 0x08);
- Write_Max7219(0x08, 0x00);
- Write_Max7219(0x03, 0x0f);
- Write_Max7219(0x04, 0x0f);
- Write_Max7219(0x05, 0x0f);
- Write_Max7219(0x06, 0x0f);
- delay_ms(1000);
- Write_Max7219(0x01, 0x00);
- Write_Max7219(0x02, 0x01);
- Write_Max7219(0x07, 0x07);
- Write_Max7219(0x08, 0x00);
- Write_Max7219(0x03, 0x0f);
- Write_Max7219(0x04, 0x0f);
- Write_Max7219(0x05, 0x0f);
- Write_Max7219(0x06, 0x0f);
- delay_ms(1000);
- Write_Max7219(0x01, 0x09);
- Write_Max7219(0x02, 0x00);
- Write_Max7219(0x07, 0x06);
- Write_Max7219(0x08, 0x00);
- Write_Max7219(0x03, 0x0f);
- Write_Max7219(0x04, 0x0f);
- Write_Max7219(0x05, 0x0f);
- Write_Max7219(0x06, 0x0f);
- delay_ms(1000);
- Write_Max7219(0x01, 0x08);
- Write_Max7219(0x02, 0x00);
- Write_Max7219(0x07, 0x05);
- Write_Max7219(0x08, 0x00);
- Write_Max7219(0x03, 0x0f);
- Write_Max7219(0x04, 0x0f);
- Write_Max7219(0x05, 0x0f);
- Write_Max7219(0x06, 0x0f);
- delay_ms(1000);
- Write_Max7219(0x01, 0x07);
- Write_Max7219(0x02, 0x00);
- Write_Max7219(0x07, 0x04);
- Write_Max7219(0x08, 0x00);
- Write_Max7219(0x03, 0x0f);
- Write_Max7219(0x04, 0x0f);
- Write_Max7219(0x05, 0x0f);
- Write_Max7219(0x06, 0x0f);
- delay_ms(1000);
- Write_Max7219(0x01, 0x06);
- Write_Max7219(0x02, 0x00);
- Write_Max7219(0x07, 0x03);
- Write_Max7219(0x08, 0x00);
- Write_Max7219(0x03, 0x0f);
- Write_Max7219(0x04, 0x0f);
- Write_Max7219(0x05, 0x0f);
- Write_Max7219(0x06, 0x0f);
- delay_ms(1000);
- Write_Max7219(0x01, 0x05);
- Write_Max7219(0x02, 0x00);
- Write_Max7219(0x07, 0x02);
- Write_Max7219(0x08, 0x00);
- Write_Max7219(0x03, 0x0f);
- Write_Max7219(0x04, 0x0f);
- Write_Max7219(0x05, 0x0f);
- Write_Max7219(0x06, 0x0f);
- delay_ms(1000);
- Write_Max7219(0x01, 0x04);
- Write_Max7219(0x02, 0x00);
- Write_Max7219(0x07, 0x01);
- Write_Max7219(0x08, 0x00);
- Write_Max7219(0x03, 0x0f);
- Write_Max7219(0x04, 0x0f);
- Write_Max7219(0x05, 0x0f);
- Write_Max7219(0x06, 0x0f);
- delay_ms(1000);
- GPIO_SetBits(GPIOA, GPIO_Pin_10);
-
-
-
- GPIO_ResetBits(GPIOA, GPIO_Pin_9);
- Write_Max7219(0x01, 0x03);
- Write_Max7219(0x02, 0x00);
- Write_Max7219(0x07, 0x03);
- Write_Max7219(0x08, 0x00);
- Write_Max7219(0x03, 0x0f);
- Write_Max7219(0x04, 0x0f);
- Write_Max7219(0x05, 0x0f);
- Write_Max7219(0x06, 0x0f);
- delay_ms(1000);
- Write_Max7219(0x01, 0x02);
- Write_Max7219(0x02, 0x00);
- Write_Max7219(0x07, 0x02);
- Write_Max7219(0x08, 0x00);
- Write_Max7219(0x03, 0x0f);
- Write_Max7219(0x04, 0x0f);
- Write_Max7219(0x05, 0x0f);
- Write_Max7219(0x06, 0x0f);
- delay_ms(1000);
- Write_Max7219(0x01, 0x01);
- Write_Max7219(0x02, 0x00);
- Write_Max7219(0x07, 0x01);
- Write_Max7219(0x08, 0x00);
- Write_Max7219(0x03, 0x0f);
- Write_Max7219(0x04, 0x0f);
- Write_Max7219(0x05, 0x0f);
- Write_Max7219(0x06, 0x0f);
- delay_ms(1000);
- GPIO_SetBits(GPIOA, GPIO_Pin_9|GPIO_Pin_1);
-
- GPIO_ResetBits(GPIOA, GPIO_Pin_8|GPIO_Pin_2);
- Write_Max7219(0x01, 0x02);
- Write_Max7219(0x02, 0x01);
- Write_Max7219(0x07, 0x05);
- Write_Max7219(0x08, 0x01);
- Write_Max7219(0x03, 0x0f);
- Write_Max7219(0x04, 0x0f);
- Write_Max7219(0x05, 0x0f);
- Write_Max7219(0x06, 0x0f);
- delay_ms(1000);
- Write_Max7219(0x01, 0x01);
- Write_Max7219(0x02, 0x01);
- Write_Max7219(0x07, 0x04);
- Write_Max7219(0x08, 0x01);
- Write_Max7219(0x03, 0x0f);
- Write_Max7219(0x04, 0x0f);
- Write_Max7219(0x05, 0x0f);
- Write_Max7219(0x06, 0x0f);
- delay_ms(1000);
- Write_Max7219(0x01, 0x00);
- Write_Max7219(0x02, 0x01);
- Write_Max7219(0x07, 0x03);
- Write_Max7219(0x08, 0x01);
- Write_Max7219(0x03, 0x0f);
- Write_Max7219(0x04, 0x0f);
- Write_Max7219(0x05, 0x0f);
- Write_Max7219(0x06, 0x0f);
- delay_ms(1000);
- Write_Max7219(0x01, 0x09);
- Write_Max7219(0x02, 0x00);
- Write_Max7219(0x07, 0x02);
- Write_Max7219(0x08, 0x01);
- Write_Max7219(0x03, 0x0f);
- Write_Max7219(0x04, 0x0f);
- Write_Max7219(0x05, 0x0f);
- Write_Max7219(0x06, 0x0f);
- delay_ms(1000);
- Write_Max7219(0x01, 0x08);
- Write_Max7219(0x02, 0x00);
- Write_Max7219(0x07, 0x01);
- Write_Max7219(0x08, 0x01);
- Write_Max7219(0x03, 0x0f);
- Write_Max7219(0x04, 0x0f);
- Write_Max7219(0x05, 0x0f);
- Write_Max7219(0x06, 0x0f);
- delay_ms(1000);
- Write_Max7219(0x01, 0x07);
- Write_Max7219(0x02, 0x00);
- Write_Max7219(0x03, 0x00);
- Write_Max7219(0x04, 0x01);
- Write_Max7219(0x03, 0x0f);
- Write_Max7219(0x04, 0x0f);
- Write_Max7219(0x05, 0x0f);
- Write_Max7219(0x06, 0x0f);
- delay_ms(1000);
- Write_Max7219(0x01, 0x06);
- Write_Max7219(0x02, 0x00);
- Write_Max7219(0x07, 0x09);
- Write_Max7219(0x08, 0x00);
- Write_Max7219(0x03, 0x0f);
- Write_Max7219(0x04, 0x0f);
- Write_Max7219(0x05, 0x0f);
- Write_Max7219(0x06, 0x0f);
- delay_ms(1000);
- Write_Max7219(0x01, 0x05);
- Write_Max7219(0x02, 0x00);
- Write_Max7219(0x07, 0x08);
- Write_Max7219(0x08, 0x00);
- Write_Max7219(0x03, 0x0f);
- Write_Max7219(0x04, 0x0f);
- Write_Max7219(0x05, 0x0f);
- Write_Max7219(0x06, 0x0f);
- delay_ms(1000);
- Write_Max7219(0x01, 0x04);
- Write_Max7219(0x02, 0x00);
- Write_Max7219(0x03, 0x07);
- Write_Max7219(0x04, 0x00);
- Write_Max7219(0x03, 0x0f);
- Write_Max7219(0x04, 0x0f);
- Write_Max7219(0x05, 0x0f);
- Write_Max7219(0x06, 0x0f);
- delay_ms(1000);
- Write_Max7219(0x01, 0x03);
- Write_Max7219(0x02, 0x00);
- Write_Max7219(0x07, 0x06);
- Write_Max7219(0x08, 0x00);
- Write_Max7219(0x03, 0x0f);
- Write_Max7219(0x04, 0x0f);
- Write_Max7219(0x05, 0x0f);
- Write_Max7219(0x06, 0x0f);
- delay_ms(1000);
- Write_Max7219(0x01, 0x02);
- Write_Max7219(0x02, 0x00);
- Write_Max7219(0x07, 0x05);
- Write_Max7219(0x08, 0x00);
- Write_Max7219(0x03, 0x0f);
- Write_Max7219(0x04, 0x0f);
- Write_Max7219(0x05, 0x0f);
- Write_Max7219(0x06, 0x0f);
- delay_ms(1000);
- Write_Max7219(0x01, 0x01);
- Write_Max7219(0x02, 0x00);
- Write_Max7219(0x07, 0x04);
- Write_Max7219(0x08, 0x00);
- Write_Max7219(0x03, 0x0f);
- Write_Max7219(0x04, 0x0f);
- Write_Max7219(0x05, 0x0f);
- Write_Max7219(0x06, 0x0f);
- delay_ms(1000);
-
- GPIO_SetBits(GPIOA, GPIO_Pin_2);
-
- GPIO_ResetBits(GPIOA, GPIO_Pin_0);
- Write_Max7219(0x01, 0x03);
- Write_Max7219(0x02, 0x00);
- Write_Max7219(0x07, 0x03);
- Write_Max7219(0x08, 0x00);
- Write_Max7219(0x03, 0x0f);
- Write_Max7219(0x04, 0x0f);
- Write_Max7219(0x05, 0x0f);
- Write_Max7219(0x06, 0x0f);
- delay_ms(1000);
- Write_Max7219(0x01, 0x02);
- Write_Max7219(0x02, 0x00);
- Write_Max7219(0x07, 0x02);
- Write_Max7219(0x08, 0x00);
- Write_Max7219(0x03, 0x0f);
- Write_Max7219(0x04, 0x0f);
- Write_Max7219(0x05, 0x0f);
- Write_Max7219(0x06, 0x0f);
- delay_ms(1000);
- Write_Max7219(0x01, 0x01);
- Write_Max7219(0x02, 0x00);
- Write_Max7219(0x07, 0x01);
- Write_Max7219(0x08, 0x00);
- Write_Max7219(0x03, 0x0f);
- Write_Max7219(0x04, 0x0f);
- Write_Max7219(0x05, 0x0f);
- Write_Max7219(0x06, 0x0f);
- delay_ms(1000);
-
- GPIO_SetBits(GPIOA, GPIO_Pin_4|GPIO_Pin_0);
- }
- }
- void Delay(u16 nCount)
- {
- for(; nCount != 0; nCount--);
- }
复制代码
|
|