Stm32F4 StdPeriph with MAX7219 5x7 Segment Display

Stm32F4 StdPeriph with MAX7219 5x7 Segment Display

Hello everyone, in this article we are going to talk about driving MAX7219 with STM32F4 and StdPeriph Libraries. We will drive a 5 Digits 7 Segment Common Cathode Display.

Let's Begin.

Firstly What is MAX7219
MAX7219 is a common cathode 7 segment display driver. You can also drive the 8x8 dot matrix displays with MAX7219. It communicates with other microcontrollers using SPI transfer protocol. All configurations can be setted on SPI via values on preconfigured registers.

IMPORTANT : I always strongly suggest to read datasheets of all external chips.

Firstly below image you can see the pin configurations of MAX7219 and my 5x7 Segment Display: Pin Configurations of MAX7219 and 5 Digit 7 Segment Display

Also It is important to add specified resistor on datasheet, also add a pull-up resistor between 5v and Chip Select pin. Also adding a 10uF capacitor between MAX7219 GND and microcontroller GND may be good for filtering. This will filter the unwanted staying low at every changes.

Below image you can see a part of datasheet which show serial communication timing and registers format addresses. We will send the datas according to this format. MAX7219 Communication Timing Graph and Register Format table
I have connected
  • PB5 Data In
  • PB6 Clock
  • PB7 Chip Select
Now we can start to code our program.

In here we are going to use STM32F4 discovery board and StdPeriph libaries. We will use only GPIO here. This method known as Software SPI communication. We will send the datas through making the data pin LOW and HIGH with specified clock pin

Firstly Let's declare the required variables.

#define DECODE_REGISTER        0x09     
#define INTENSITY_REGISTER     0x0A   
#define SCAN_LIMIT_REGISTER    0x0B  
#define SHUTDOWN_REGISTER      0x0C
#define DISPLAY_TEST_REGISTER  0x0F 

#define DIGIT_1_REGISTER       0X01
#define DIGIT_2_REGISTER       0X02
#define DIGIT_3_REGISTER       0X03
#define DIGIT_4_REGISTER       0X05
#define DIGIT_5_REGISTER       0X04

#define DELAY_TIME 1
And below delay function may be required in this project.

void the_delay(int i)
{
	while(i) i--;
}

Now Firstly we are going to write essential function. This function will send the data to related register via software SPI communication. This putByte method will produce a Clock signal and data signal for every clock clock signal. We are going to send 8 bit data so our essential variable is equal to 8 here. We will mask this data and to send it byte by byte.


void MAX7219_PutByte(uint8_t data)
{
  uint8_t i = 8;
  uint8_t mask;
  while(i > 0) {
    mask = 0x01 << (i - 1);
	GPIO_ResetBits(GPIOB, GPIO_Pin_6);
    if (data & mask){ 
	GPIO_SetBits(GPIOB, GPIO_Pin_5);
    }else{
	GPIO_ResetBits(GPIOB, GPIO_Pin_5);
    }
     GPIO_SetBits(GPIOB, GPIO_Pin_6);
    --i; 
  } 
}
And then, we are going to write a send data which will call PutByte function. This will first send the adress register and then send the data So we will send the data with without waiting.

void MAX7219_SendData(char reg, char data)
{
  GPIO_ResetBits(GPIOB, GPIO_Pin_7); 
  MAX7219_PutByte(reg);  
  MAX7219_PutByte(data);  
  GPIO_SetBits(GPIOB, GPIO_Pin_7); 
  GPIO_SetBits(GPIOB, GPIO_Pin_7); 
	
	the_delay(DELAY_TIME);
}

Until here our required functions are ready. Now we are going to use these functions via main function.

Below you can see our main function.

int main()
{
	//Firstly activate the periph bus
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
	
	//and activate the required pins
	GPIO_InitTypeDef GPIO_InitStructure;
	
	GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_OUT;  
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; 
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;
	GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;
	GPIO_InitStructure.GPIO_Pin 	= GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7; 
	GPIO_Init(GPIOB, &GPIO_InitStructure); 
	
	//then we are going to prepare the MAX7219 chip
	MAX7219_SendData(SHUTDOWN_REGISTER, 1);  
	MAX7219_SendData(SCAN_LIMIT_REGISTER, 0x04); 
	MAX7219_SendData(DECODE_REGISTER, 0xff); 
	MAX7219_SendData(INTENSITY_REGISTER, 0xff);  
	
	//and write the characters on display.
	MAX7219_SendData(DIGIT_1_REGISTER, 1); 
	MAX7219_SendData(DIGIT_2_REGISTER, 2); 
	MAX7219_SendData(DIGIT_3_REGISTER, 3); 
	MAX7219_SendData(DIGIT_4_REGISTER, 4); 
	MAX7219_SendData(DIGIT_5_REGISTER, 5); 
	
	while(1)
	{

	}
}
Above function firstly will enable the required periphal bus. Then I will configure the GPIO. We will communicate through GPIOB pins. After initialization of GPIO then we are going to configure the MAX7219.
  • Firstly we have started and then
  • we are going to set digit count which I will use.
  • Then we are going to set the decode mode for digits.
  • Lastly we will set the intensity of the display digits.
I have set the decode mode as BCD format. So I will sent the values with non hexadecimal format.
Below image you can see the output of the program. Output of Example Application

That is all in this article.

Burak Hamdi TUFAN.


Tags


Share this Post

Send with Whatsapp

Post a Comment

Success! Your comment sent to post. It will be showed after confirmation.
Error! There was an error sending your comment.

Comments

  • There is no comment. Be the owner of first comment...