CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug Reports on this forum. Send them to

MAX7219 Serial 8 digit seven segment display driver

Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message

Joined: 25 Mar 2009
Posts: 5

View user's profile Send private message Yahoo Messenger

MAX7219 Serial 8 digit seven segment display driver
PostPosted: Tue May 12, 2009 12:49 am     Reply with quote

One of my student got some of these display drivers, MAX7219. As I was experimenting, I ended up writing the code to interface it with PIC18F4550. Here is the code and the example.

It won't compile with older version CCS C Compilers. With old version(I tested with 3.249) it will show compile time error "Arrays of bits are not permitted"

#define clk_delay 1
int1 nd;
unsigned int8 pos;
//concat two 8bit int and gives a 16 bit int
//helper function
unsigned int16 concat(unsigned int8 d1,unsigned int8 d2)
   unsigned int16 temp;
   return temp;
//since the segment sequence of MAX7219 is
//D7 D6 D5 D4 D3 D2 D1 D0
//DP  A  B  C  D  E  F  G   while according to convention it is
//DP  G  F  E  D  C  B  A
//One may avoide this my doing the proper changes in the circuit wiring but
//it will again lead to more complication because in CODE B decode mode the
//segment are just opposit to what u see in no decode mode. so in any case
//one need to use this helper function if he wants to use the chip in both
//no decode and decode modes.
unsigned int8 reverse(unsigned int8 nd_data)
   int1 D[8];
   unsigned int8 i;
   unsigned int8 temp;
      if(shift_left(&nd_data, 1, 0))

   return temp;
//Quite self explanatory
//it displays a data at the address given
//1 -> 1st digit
//8 -> 8th digit
//and dis_data is the data that is to be displayed there
void display(char adresse,char dis_data)
 char x;
 unsigned int16 dout;

 for (x=16;x>0;x--)
  if (bit_test(dout,x-1)) output_high(DATA);
  else output_low(DATA);


//use this function to continously put data in the digits
//without bothering about incremanting the address
//the address increment is done automatically
void seg_putc(unsigned int8 c)
//another variant of the display function if you use the hardware SPI
/*void display(char adresse,char dis_data)
//Takes an array of 8 datas and put them on the digits
//the dp decides the position of the decimal point
//1 -> decimal point on 1st digit
//8 -> decimal point on 8th digit
void display_digits(unsigned int8 arr[],unsigned int8 dp)
      unsigned int8 i;
      //int decimal;
      if(dp>8 || dp<1)
//intialize the display driver chip
//it takes the argument decode that decides whether the digits are
//decoded using CODE B scheme or they are not decoded at all
//0 -> No decode
//1 -> Decode
void display_init(int1 decode)
 display(0x0B,0x07);  // scan limit - 8 columns
 display(0x0C,0x01);  // Shutdown - normal mode
   display(0x09,0x00);  // decode mode = 0
   display(0x09,0xFF);  // decode mode = 1
 display(0x0A,0x07);  // intensity
 //display(0x0F,0x00);  // test

And the example code of how to use it

#include <18F4550.h>
#device adc=8

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES HSPLL                    //High Speed Crystal/Resonator with PLL enabled
#FUSES NOPROTECT                //Code not protected from reading
#FUSES BROWNOUT                 //Reset when brownout detected
#FUSES BORV20                   //Brownout reset at 2.0V
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOCPD                    //No EE protection
#FUSES STVREN                   //Stack full/underflow will cause reset
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOLVP                      //No Low Voltage Programming on B3(PIC16) or B5(PIC18)
#FUSES NOWRT                    //Program memory not write protected
#FUSES NOWRTD                   //Data EEPROM not write protected
#FUSES IESO                     //Internal External Switch Over mode enabled
#FUSES FCMEN                    //Fail-safe clock monitor enabled
#FUSES PBADEN                   //PORTB pins are configured as analog input channels on RESET
#FUSES NOWRTC                   //configuration not registers write protected
#FUSES NOWRTB                   //Boot block not write protected
#FUSES NOEBTR                   //Memory not protected from table reads
#FUSES NOEBTRB                  //Boot block not protected from table reads
#FUSES NOCPB                    //No Boot Block code protection
#FUSES MCLR                     //Master Clear pin enabled
#FUSES LPT1OSC                  //Timer1 configured for low-power operation
#FUSES NOXINST                    //Extended set extension and Indexed Addressing mode enabled
#FUSES PLL5                     //Divide By 5(20MHz oscillator input)
#FUSES CPUDIV4                  //System Clock by 4
#FUSES USBDIV                   //USB clock source comes from PLL divide by 2
#FUSES VREGEN                   //USB voltage regulator enabled
#FUSES ICPRT                    //ICPRT enabled

#use delay(clock=48000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)

#define     DATA       PIN_D7
#define     LOAD       PIN_D6
#define     CLK        PIN_D5

#include "MAX7219.c"

void main()
   int data1[8]={0x38,0x77,0x6F,0x54,0x77,0x1E,0x30,0x78};  //It writes LAgnAJit
   int data2[8]={1,2,3,4,5,6,7,8};

   display_init(0);           //No decode
   display_init(1);           //Decode as Code B
   seg_putc(4);               //update 1st digit to '4'
   display(8,3);              //Update 8th digit to '3'

Here are the code: the compiled hex and cof file and the Proteus test circuit.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library All times are GMT - 6 Hours
Page 1 of 1

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Powered by phpBB © 2001, 2005 phpBB Group