element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Path to Programmable 3
  • Challenges & Projects
  • Design Challenges
  • Path to Programmable 3
  • More
  • Cancel
Path to Programmable 3
Blog Blog 4: Functional programming and communication protocols
  • Blog
  • Forum
  • Documents
  • Leaderboard
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Path to Programmable 3 to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: pandoramc
  • Date Created: 20 Jul 2023 11:16 PM Date Created
  • Views 494 views
  • Likes 6 likes
  • Comments 1 comment
  • Ultra96-V2 Board
  • amd
  • Path to Programmable III
  • Path to Programmable 3
Related
Recommended

Blog 4: Functional programming and communication protocols

pandoramc
pandoramc
20 Jul 2023

Table of Contents

  • The MAX7219 Peripheral
  • The Software
  • Result

The MAX7219 Peripheral

The MAX7219 IC is a "Serially Interfaced 8-Digit LED Display Driver", which interface is based on SPI, with an specific protocol for configuration and data management. According to the datasheet, the IC starts with all registers in reset state and the data registers in blank. This means that the display will show nothing when it is powered up. The Address Map connotes that we could have other processor, but the state machine brings the behavior of the system according data handled in the SPI communication.

image

image

You must start the communication, send the register to be modified, and the data to be recorded. This brings data representation over 7 segment displays. Remember that the MAX7219 has a power and logic levels of 5V, but don't worry, you can change the voltage levels on the Mezzanite board using the VDD SEL switch. The VREF SEL changes the analog reference for the ADC.

image

The Software

The new software must be able to encode and send the information to the device since the SPI peripheral only stablishes the communication port, but not the correct interpretation of the information. This think carries the following header file.

#ifndef __MAX7219_H__
#define __MAX7219_H__

#define MAX72XX_NOP			0x00
#define MAX72XX_D0			0x01
#define MAX72XX_D1			0x02
#define MAX72XX_D2			0x03
#define MAX72XX_D3			0x04
#define MAX72XX_D4			0x05
#define MAX72XX_D5			0x06
#define MAX72XX_D6			0x07
#define MAX72XX_D7			0x08
#define MAX72XX_DECODE		0x09
#define MAX72XX_INTENSITY	0x0A
#define MAX72XX_SCAN		0x0B
#define MAX72XX_SHUTDOWN	0x0C
#define MAX72XX_TEST		0x0F

#include <stdint.h>
#include "xspips.h"

void clearDisplay(XSpiPs *Spi);
void DecodeInt(XSpiPs *Spi, uint16_t number);
void sendCommand(XSpiPs *Spi, uint8_t cmd, uint8_t val);
void displayInit(XSpiPs *Spi, u8 scanLimit);

#endif

here, all the available commands on the MAX7219 are defined by macros. We must include the xspips.h since there are the definitions of the SPI controller on that archive. The definitions of each function are shown below.

#include "MAX7219.h"
#include <math.h>

void clearDisplay(XSpiPs *Spi){
	for(int d = 7; d >= 0; d--){
		sendCommand(Spi, d + 1, 0x0F);
	}
}

void DecodeInt(XSpiPs *Spi, uint16_t number){
	static u8 nDigits;
	static u16 IntConditioner;
	nDigits = ceil(log(number)/log(10));
	IntConditioner = number;
	static double IntMultiplier;
	clearDisplay(Spi);
	for(int d = nDigits - 1; d >= 0; d--){
		IntMultiplier = pow(10, d);
		IntConditioner = number/IntMultiplier;
		number -= IntConditioner*IntMultiplier;
		sendCommand(Spi, d + 1, IntConditioner);
	}
}

void sendCommand(XSpiPs *Spi, uint8_t cmd, uint8_t val){
	static u8 dummy[2];
	static u8 CmdData[2];
	CmdData[0] = cmd;
	CmdData[1] = val;
	XSpiPs_PolledTransfer(Spi, CmdData, dummy, 2);
}

void displayInit(XSpiPs *Spi, u8 scanLimit){
	sendCommand(Spi, MAX72XX_SCAN, scanLimit);
	sendCommand(Spi, MAX72XX_DECODE, 0xFF);
	sendCommand(Spi, MAX72XX_SHUTDOWN, 1);
	sendCommand(Spi, MAX72XX_TEST, 0);
	sendCommand(Spi, MAX72XX_INTENSITY, 5);
	clearDisplay(Spi);
}

As you can see, the math.h header file isbeing  used. As you may know, the math library is not considered an API like other files, it is refered to as an ABI and this library should be linked with the project. To do this, the developer needs to add the -lm flag through the C/C++ Settings GUI, located on the project properties.

imageimage

After that, you will be able to compile without any errors.

The main code consists of the SPI controller initialization, MAX7219 initialization, and data encoding representation. With this in mind, the following code solves this logic,

#include <stdio.h>
#include "platform.h"
#include "xil_printf.h"
#include "xspips.h"
#include <sleep.h>
#include "MAX7219.h"

int main(){
    init_platform();
    int Status;
    XSpiPs Spi;
    XSpiPs_Config *SpiConfig;
    print("MAX7219 numerical decoding\r\n");
    SpiConfig = XSpiPs_LookupConfig(XPAR_XSPIPS_0_DEVICE_ID);
    if (NULL == SpiConfig) {
    	xil_printf("Error initializing device.\r\n");
    	while(1){}
    }
    Status = XSpiPs_CfgInitialize(&Spi, SpiConfig, SpiConfig->BaseAddress);
    if (Status != XST_SUCCESS) {
    	xil_printf("Error initializing device configuration.\r\n");
    	while(1){}
    }
    XSpiPs_SetOptions(&Spi, XSPIPS_MANUAL_START_OPTION | \
    			XSPIPS_MASTER_OPTION | XSPIPS_FORCE_SSELECT_OPTION);
    XSpiPs_SetClkPrescaler(&Spi, XSPIPS_CLK_PRESCALE_8);
    XSpiPs_SetSlaveSelect(&Spi, 1);
    displayInit(&Spi, 7);
    u16 counter = 1;
    while(1){
    	DecodeInt(&Spi, counter++);
    	xil_printf("%u decoded.\r\n", counter);
    	sleep_A53(1);
    }
    return 0;
}

From the last code in Blog 3, you may see that the code has been reduced to a minimum to be functional and a counter variable updates the data to be displayed.

Result

The practical behavior has a small logic error on small numbers, less than or equal to ten, but it represents in an acceptable way the information generated by the one second delay in counting. You can see this in the video below.

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

Finally, each time that the information is decoded, the processor informs about this event,

image

If you have any recommendations please, don't hesitate to let me know.

  • Sign in to reply
  • navadeepganeshu
    navadeepganeshu over 2 years ago

    Good one. Nice to see the LED driver interfaced. Other way of using individual LEDs for n segments would work the same way?

    I think if doing individually, using verilog/vhdl would be easier - probably through a custom RTL block.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube