max7219驱动共阳点阵

时间:2024-04-12 10:37:48

小白在共阳数码管上踩过的坑

                    工具      芯片max7219 共阳数码管 stm32精英版

首先要没有找到正确的资料,浪费一天时间,这种芯片手册有中文的,也很好找,在那个基础上开始搞。max7219驱动共阳点阵

在网上没有找到现成的驱动共阳的程序或者相关介绍,可能是我有毒找不到吧。
找了找共阳和共阴的区别发现其实就是点阵放的方式不同,全称为行共阳和行共阴,那么只要把共阴的接线的方法改一下就好如下
max7219驱动共阳点阵
按这个走线就好,另附上原理图,按照上面的两张图连线时,按照下面改一些这个连线就好了。
max7219驱动共阳点阵

	主函数

#include “led.h”
#include “delay.h”
#include “key.h”
#include “sys.h”
#include “usart.h”
#include “exti.h”
#include “beep.h”
#include “datMaxtrix.h”
int mode;
int main(void)
{
int i;uchar* adress;
uchar arrow[]={0x0,0x1C,0x38,0x70,0x38,0x1C,0x0,0x0};
uchar quanliang[]={0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF};
uchar clear[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
delay_init();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
uart_init(115200);
LED_Init();
BEEP_Init();
EXTIX_Init();
LED0=0;
dotMaxtrixGPIO_Init();
delay_ms(50);
datMaxtrix_Init();
mode=0;
while(1)
{
if(mode0)
{
adress=arrow;
}
else
if(mode
1)
{
adress=quanliang;
}
else
{
adress=clear;
}

		for(i=1;i<9;i++)
	{
		Write_Max7219(i,adress[i-1]);
		//delay_ms(10);
	}
	delay_ms(100);
}	 

}

datMaxtrix.c
#include “datMaxtrix.h”
#include “delay.h”

void Write_Max7219_byte(u8 DATA)
{
int i;
Max7219_pinCS=0;
for(i=0;i<8;i++)
{
delay_us(1);
Max7219_pinCLK=0;
Max7219_pinDIN=((DATA>>7)&0x01);
DATA=DATA<<1;
delay_us(1);
Max7219_pinCLK=1;
}
}
void Write_Max7219(u8 address,u8 dat)
{
Max7219_pinCS=0;
Write_Max7219_byte(address);
Write_Max7219_byte(dat);
Max7219_pinCS=1;
delay_us(1);
}
void datMaxtrix_Init(void)
{
Write_Max7219(DECODE_MODE,0X00);
Write_Max7219(INTENSITY,0x0FF);
Write_Max7219(SCAN_LIMIT,0x07);
Write_Max7219(SHUT_DOWN,0x01);
Write_Max7219(DISPLAY_TEST,0x00);
}
void dotMaxtrixGPIO_Init(void)
{

GPIO_InitTypeDef  GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);	

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;				 
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 		
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;		
	GPIO_Init(GPIOA, &GPIO_InitStructure);					
	GPIO_SetBits(GPIOA,GPIO_Pin_4);						

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;				 
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 		 
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;		 
	GPIO_Init(GPIOA, &GPIO_InitStructure);					 
	GPIO_SetBits(GPIOA,GPIO_Pin_5);						

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;			
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 		
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;		
	GPIO_Init(GPIOA, &GPIO_InitStructure);					
	GPIO_SetBits(GPIOA,GPIO_Pin_7);						

}
dotMatrix.h
#ifndef __datMaxtrix_H
#define __datMaxtrix_H
#include “sys.h”

#define uchar unsigned char
#define uint unsigned int

#define Max7219_pinCS PAout(4)

#define Max7219_pinCLK PAout(5)

#define Max7219_pinDIN PAout(7)

#define DECODE_MODE 0x09

#define INTENSITY 0x0A

#define SCAN_LIMIT 0x0B

#define SHUT_DOWN 0x0C

#define DISPLAY_TEST 0x0F

#define Max7219_pinCS PAout(4)
#define Max7219_pinCLK PAout(5)
#define Max7219_pinDIN PAout(7)

void datMaxtrix_Init(void);
void Write_Max7219(uchar address,uchar dat);
void Write_Max7219_byte(u8 dat);
void dotMaxtrixGPIO_Init(void);

#endif

exit.c { 在原子哥的按键中断那里直接改的 }
#include “exti.h”
#include “led.h”
#include “key.h”
#include “delay.h”
#include “usart.h”
#include “beep.h”
void EXTIX_Init(void)
{

EXTI_InitTypeDef EXTI_InitStructure;
  NVIC_InitTypeDef NVIC_InitStructure;

KEY_Init();	

RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);	



GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource3);
EXTI_InitStructure.EXTI_Line=EXTI_Line3;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;	
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_Init(&EXTI_InitStructure);	  


GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource4);
EXTI_InitStructure.EXTI_Line=EXTI_Line4;
EXTI_Init(&EXTI_InitStructure);	  	


  GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource0); 

EXTI_InitStructure.EXTI_Line=EXTI_Line0;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_Init(&EXTI_InitStructure);		

NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;			
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02;	
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x03;				
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;								
NVIC_Init(&NVIC_InitStructure); 

NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQn;		
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02;	
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01;				
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;							
NVIC_Init(&NVIC_InitStructure);  	 

NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn;			
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02;	
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;					
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;							
NVIC_Init(&NVIC_InitStructure);  	

}

void EXTI0_IRQHandler(void)
{
extern int mode;
delay_ms(10);
if(WK_UP==1)
{
//BEEP=!BEEP;
mode=1;
}
EXTI_ClearITPendingBit(EXTI_Line0);
}

//ÍⲿÖжÏ3·þÎñ³ÌÐò
void EXTI3_IRQHandler(void)
{
extern int mode;
delay_ms(10);//Ïû¶¶
if(KEY1==0) //°´¼üKEY1
{
LED1=!LED1;
mode=0;
}
EXTI_ClearITPendingBit(EXTI_Line3);
}

void EXTI4_IRQHandler(void)
{
delay_ms(10);//Ïû¶¶
if(KEY0==0) //°´¼üKEY0
{
extern int mode;
mode=3;
LED0=!LED0;
LED1=!LED1;
}
EXTI_ClearITPendingBit(EXTI_Line4); //Çå³ýLINE4ÉϵÄÖжϱê־λ
}

exit.h {一点没改都}

#ifndef __EXTI_H
#define __EXIT_H
#include “sys.h”

void EXTIX_Init(void);
#endif

小白第一次写忘见谅,有错请联系我谢谢。