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 support@ccsinfo.com

MAX7219 Display Driver using hardware SPI

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



Joined: 07 Sep 2010
Posts: 24
Location: West Australia

View user's profile Send private message

MAX7219 Display Driver using hardware SPI
PostPosted: Wed Sep 22, 2010 10:34 pm     Reply with quote

Code tested on PIC18F8722 with 10MHz xtal. Runs OK at 10MHz(HS) and at 40MHz(PLL or H4).
The 'BAUD=10000' in the '#use spi' can also be left out.

Code:

/////////////////////////////////////////////////////////////////////////////
// Project:    8722 SPI_02\main.c                                          //
// Author:     WVL                                                         //
// Date:       September 2010                                              //
// Outline:    Basic hardware SPI to drive ETT display with decode on:     //
//             BIGPIC5 board                                               //
//             Heartbeat portH.0                                           //
//             10Mhz Xtal run at 10Mhz using HS                            //
//             40MHz OK but slower used for good scope display
//             Using hwr SPI driving MAX7219                               //
/////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////
// Pin assignments BIGPIC5 board                                           //
//             PIN         FUNCTION                                        //
//             C1          MAX load                                        //
//             C3          SPI clock                                       //
//             C4          SPI data in                                     //
//             C5          SPI data out                                    //
// Pin assignments ETT 8 digit display                                     //
//             C0          SPI data in                                     //
//             C1          Max load                                        //
//             C2          SPI clock                                       //
/////////////////////////////////////////////////////////////////////////////

#define     DATA       PIN_C5    // SPI1 hardware SPI data out
#define     LOAD       PIN_C1    // software SPI
#define     CLK        PIN_C3    // SPI1 hardware clock

#include <main.h>
#define  LED PIN_H0
// MAX7219 needs: MSB first(default),data on rising clock edge
// Works at maximum rate OK, BAUD rate set for good scope display
#use spi(FORCE_HW,BAUD=10000,SAMPLE_RISE,BITS=8,)

// function to write two bytes to the display
// address_1=RH digit, address_8=LH digit
// decode is on so dis_data=3 displays '3'
void display(char address,char dis_data)
   {
   output_low(LOAD);
   spi_write(address);     // spi1 write to default pins
   delay_us(10);           // used for scope display but not necessary
   spi_write(dis_data);    // spi1 write to default pins
   output_high(LOAD);
   }
   
void main()
   {
   unsigned int8 x;
   unsigned int8 y;

   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_CLOCK_DIV_2|ADC_TAD_MUL_0);
   setup_spi2(SPI_SS_DISABLED);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_OFF);            // PIC18 only
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_timer_4(T4_DISABLED,0,1);
   setup_ccp1(CCP_OFF);
   setup_comparator(NC_NC_NC_NC);

   display(0x0f,0x00);                 // test mode off
   display(0x0B,0x07);                 // scan limit - 8 columns
   display(0x0C,0x01);                 // Shutdown - normal mode
   display(0x09,0xFF);                 // decode mode on
   display(0x0A,0x07);                 // intensity
   for(x=1;x<=8;x++)display(x,0x0f);   // blank all digits
   
   while(true)
   {   
   for(x=1;x<=8;x++)       // write 1 to 8 on digits 1 to 8
      { 
      display(x,x);   
      delay_ms(500);     
      output_toggle(LED);
      }// for loop     
   for (y=0;y<=9;y++)      // write 0 to 9 on all digits
      {
      for (x=1;x<=8;x++)
         {
         display(x,y);
         }// for x loop
      delay_ms(500);
      }// for y loop   
   delay_ms(500);
   for(x=1;x<=8;x++)display(x,0x0f);
   }// while loop
}// main loop


Anyone spots anything daft - please let me know?
_________________
Denmark in West Australia
'Where the forest meets the sea'
Bill Legge



Joined: 07 Sep 2010
Posts: 24
Location: West Australia

View user's profile Send private message

MAX7219 Display Driver using hardware SPI
PostPosted: Fri Sep 24, 2010 5:36 pm     Reply with quote

Code works OK but 'PCM programmer' has just kindly pointed out that the line that I thought turned 'SPI2' off did not do so - I've corrected the line in this listing:

Code:

/////////////////////////////////////////////////////////////////////////////
// Project:    8722 SPI_02\main.c                                          //
// Author:     WVL                                                         //
// Date:       September 2010                                              //
// Outline:    Basic hardware SPI to drive ETT display with decode on:     //
//             BIGPIC5 board                                               //
//             Heartbeat portH.0                                           //
//             10Mhz Xtal run at 10Mhz using HS                            //
//             40MHz OK but slower used for good scope display
//             Using hwr SPI driving MAX7219                               //
/////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////
// Pin assignments BIGPIC5 board                                           //
//             PIN         FUNCTION                                        //
//             C1          MAX load                                        //
//             C3          SPI clock                                       //
//             C4          SPI data in                                     //
//             C5          SPI data out                                    //
// Pin assignments ETT 8 digit display                                     //
//             C0          SPI data in                                     //
//             C1          Max load                                        //
//             C2          SPI clock                                       //
/////////////////////////////////////////////////////////////////////////////

#define     DATA       PIN_C5    // SPI1 hardware SPI data out
#define     LOAD       PIN_C1    // software SPI
#define     CLK        PIN_C3    // SPI1 hardware clock

#include <main.h>
#define  LED PIN_H0
// MAX7219 needs: MSB first(default),data on rising clock edge
// Works at maximum rate OK, BAUD rate set for good scope display
#use spi(FORCE_HW,BAUD=10000,SAMPLE_RISE,BITS=8,)

// function to write two bytes to the display
// address_1=RH digit, address_8=LH digit
// decode is on so dis_data=3 displays '3'
void display(char address,char dis_data)
   {
   output_low(LOAD);
   spi_write(address);     // spi1 write to default pins
   delay_us(10);           // used for scope display but not necessary
   spi_write(dis_data);    // spi1 write to default pins
   output_high(LOAD);
   }
   
void main()
   {
   unsigned int8 x;
   unsigned int8 y;

   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_CLOCK_DIV_2|ADC_TAD_MUL_0);
   setup_spi2(FALSE);              // this is the corrected line ************
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_OFF);            // PIC18 only
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_timer_4(T4_DISABLED,0,1);
   setup_ccp1(CCP_OFF);
   setup_comparator(NC_NC_NC_NC);

   display(0x0f,0x00);                 // test mode off
   display(0x0B,0x07);                 // scan limit - 8 columns
   display(0x0C,0x01);                 // Shutdown - normal mode
   display(0x09,0xFF);                 // decode mode on
   display(0x0A,0x07);                 // intensity
   for(x=1;x<=8;x++)display(x,0x0f);   // blank all digits
   
   while(true)
   {   
   for(x=1;x<=8;x++)       // write 1 to 8 on digits 1 to 8
      { 
      display(x,x);   
      delay_ms(500);     
      output_toggle(LED);
      }// for loop     
   for (y=0;y<=9;y++)      // write 0 to 9 on all digits
      {
      for (x=1;x<=8;x++)
         {
         display(x,y);
         }// for x loop
      delay_ms(500);
      }// for y loop   
   delay_ms(500);
   for(x=1;x<=8;x++)display(x,0x0f);
   }// while loop
}// main loop


Regards Bill Legge
_________________
Denmark in West Australia
'Where the forest meets the sea'
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