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 CCS Technical Support

MAX7219 and built-in SPI commands (CCS C)
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
The Puma



Joined: 23 Apr 2004
Posts: 227
Location: The Netherlands

View user's profile Send private message

MAX7219 and built-in SPI commands (CCS C)
PostPosted: Sun Jan 15, 2006 10:42 am     Reply with quote

Please can someone post a source with use the built-in SPI command from CCS-C

Commands are:
setup_spi, write_spi

The code has only put an 1 on digit 1, that's all

I can't get it work Sad
sebdey



Joined: 11 Sep 2003
Posts: 17
Location: Switzerland

View user's profile Send private message

Driver for max7221
PostPosted: Sun Jan 15, 2006 11:42 am     Reply with quote

The max7219 is not spi compatible, the max7221 IS the spi display driver.

Please find my code for the spi driver, read it, and if you don't understand something, don't hesitate to ask... (but take the time to read the code)

MAX7221.h
Code:

/*! \file max7221.h
 *  \brief Driver pour le driver d'affichages 7-segments SPI MAX7221
 *
 *    !! MAX7221_CS doit �tre d�fini dans le fichier principal  !!
*/

// Addresses des diff�rents registres du MAX7221

#define   NOP            0x00
#define   DIG_0            0x01
#define   DIG_1            0x02
#define   DIG_2            0x03
#define   DIG_3            0x04
#define   DIG_4            0x05
#define   DIG_5            0x06
#define   DIG_6            0x07
#define   DIG_7            0x08
#define   DECODE_MODE      0x09
#define   INTENSITY      0x0A
#define   SCAN_LIMIT      0x0B
#define   SHUTDOWN         0x0C
#define   TEST            0x0F

// Diverses valeurs pour la configuration de ces registes

// Registre SHUTDOWN
#define   SHUTDOWN_MODE   0x00
#define   NORMAL_MODE      0x01         // Valable aussi pour le registre TEST

// Registre DECODE_MODE
#define   NO_DECODE      0x00
#define   BCD_DIG0         0x01
#define   BCD_DIG0to3      0x0F
#define   BCD_ALL         0xFF

// Registre SCAN_LIMIT
#define   DIG0_ONLY      0x00
#define   DIG0to1         0x01
#define   DIG0to2         0x02
#define   DIG0to3         0x03
#define   DIG0to4         0x04
#define   DIG0to5         0x05
#define   DIG0to6         0x06
#define   DIG0to7         0x07

// Registre TEST
#define   TEST_MODE      0x00
//#define   NORMAL_MODE      0x01         // D�fini pour le registre Shutdown

///////////////////////////////////////////////////////////////////////////////
/*! \brief Allumage/Extinction de l'affichage
 *
 *
 *  \param bool�en, true=on, false=off
 */
///////////////////////////////////////////////////////////////////////////////
void setDisplayOnOffState(short int state)
{

   output_low(MAX7221_CS);
   delay_cycles(4);

   spi_write(SHUTDOWN);

   if(state)
      spi_write(NORMAL_MODE);
   else
      spi_write(SHUTDOWN_MODE);

   output_high(MAX7221_CS);
}
///////////////////////////////////////////////////////////////////////////////
/*! \brief Entr�e, sortie du mode de test
 *
 *
 *  \param bool�en, true=test, false=std
 */
///////////////////////////////////////////////////////////////////////////////
void setDisplayTest(short int state)
{

   output_low(MAX7221_CS);
   delay_cycles(4);

   spi_write(TEST);

   if(state)
      spi_write(TEST_MODE);
   else
      spi_write(NORMAL_MODE);

   output_high(MAX7221_CS);
}
///////////////////////////////////////////////////////////////////////////////
/*! \brief Setup de l'intensit� de l'affichage
 *
 *
 *  \param int, l'intensit�, <16
 */
///////////////////////////////////////////////////////////////////////////////
void setDisplayIntensity(int intensity)
{
   if(intensity<16)
   {
      output_low(MAX7221_CS);
      delay_cycles(4);

      spi_write(INTENSITY);
      spi_write(intensity);

      output_high(MAX7221_CS);
   }
}
///////////////////////////////////////////////////////////////////////////////
/*! \brief Setup du mode de d�codage pour les digits
 *
 *
 *  \param int, indiquant le mode � utiliser
 */
///////////////////////////////////////////////////////////////////////////////
void setDisplayDecodeMode(int decodeMode)
{

   output_low(MAX7221_CS);
   delay_cycles(4);

   spi_write(DECODE_MODE);
   spi_write(decodeMode);

   output_high(MAX7221_CS);
}
///////////////////////////////////////////////////////////////////////////////
/*! \brief Setup du nombre de digits affich�s
 *
 *
 *  \param int, le nombre de digits � afficher, 1-8
 */
///////////////////////////////////////////////////////////////////////////////
void setDisplayDigitsCount(int digitCount)
{

   if(digitCount>0 && digitCount<9)
   {
      output_low(MAX7221_CS);
      delay_cycles(4);

      spi_write(SCAN_LIMIT);
      spi_write(digitCount-1);

      output_high(MAX7221_CS);
   }
}
///////////////////////////////////////////////////////////////////////////////
/*! \brief Envoi d'une valeur au digit consid�r�
 *           L'affichage d�pendra du mode de d�codage du digit consid�r�
 *
 *  \param int, le digit (<8)
 *           int, la valeur � charger
 */
///////////////////////////////////////////////////////////////////////////////
void setDisplayDigit(int digit, int value)
{
   if(digit<8)
   {
      output_low(MAX7221_CS);
      delay_cycles(4);

      spi_write(digit+1);
      spi_write(value);

      output_high(MAX7221_CS);
   }
}


In your main you need to setup the spi, please read the datasheet of your pic and for the max7221..., you'll also have to define MAX7221_CS, which is the pin where the Chip Select of the MAX7221 is connected.

Then, to init the display (in my case a 4 digits display) call:

Code:

       // Digits 0-3 devront �tre d�cod�s en BCD
   setDisplayDecodeMode(BCD_DIG0to3);
   // On s�lectionne une intensit� moyenne
   setDisplayIntensity(8);
   // On lui indique que seuls les digits 0-3 seront utilis�s
   setDisplayDigitsCount(4);
   // On le met en mode standard
   setDisplayTest(NORMAL_MODE);
   // Et on l'allume
   setDisplayOnOffState(TRUE);


And use setDisplayDigit(int digit, int value) to show 'value' on 'digit'...
sebdey



Joined: 11 Sep 2003
Posts: 17
Location: Switzerland

View user's profile Send private message

SPI setup...
PostPosted: Sun Jan 15, 2006 11:46 am     Reply with quote

I can't give you the CCS command for setting the SPI, I'm directly writing to the pic registers, if someone in this forum can translate these values in CCS, please don't hesitate to do it...

Code:

   SSPSTAT=0b01000000;            
        SSPCON=0b00100000;
The Puma



Joined: 23 Apr 2004
Posts: 227
Location: The Netherlands

View user's profile Send private message

PostPosted: Sun Jan 15, 2006 11:57 am     Reply with quote

I have a board with the MAX7219 with 8 7-segment displays.
can i replace the max7219 with te max7221??
sebdey



Joined: 11 Sep 2003
Posts: 17
Location: Switzerland

View user's profile Send private message

PostPosted: Sun Jan 15, 2006 1:32 pm     Reply with quote

No, my fault, I didn't read the dadasheet carefully enough, the 7219 IS spi compatible... sorry Sad

So you can just use the code provided as is!

Good luck !!
The Puma



Joined: 23 Apr 2004
Posts: 227
Location: The Netherlands

View user's profile Send private message

PostPosted: Sun Jan 15, 2006 2:25 pm     Reply with quote

sebdey wrote:
No, my fault, I didn't read the dadasheet carefully enough, the 7219 IS spi compatible... sorry Sad

So you can just use the code provided as is!

Good luck !!

No, 7221 is spi comp.
See here
The MAX7219/MAX7221 are compact, serial input/output common-cathode display drivers that interface microprocessors (�Ps) to 7-segment numeric LED displays of up to 8 digits, bar-graph displays, or 64 individual LEDs. Included on-chip are a BCD code-B decoder, multiplex scan circuitry, segment and digit drivers, and an 8x8 static RAM that stores each digit. Only one external resistor is required to set the segment current for all LEDs. The MAX7221 is compatible with SPI�, QSPI�, and MICROWIRE�, and has slew-rate-limited segment drivers to reduce EMI.
A convenient 4-wire serial interface connects to all common �Ps. Individual digits may be addressed and updated without rewriting the entire display. The MAX7219/MAX7221 also allow the user to select code-B decoding or no-decode for each digit.

The devices include a 150�A low-power shutdown mode, analog and digital brightness control, a scan-limit register that allows the user to display from 1 to 8 digits, and a test mode that forces all LEDs on.

For applications requiring 3V operation or segment blinking, refer to the MAX6951 data sheet
sebdey



Joined: 11 Sep 2003
Posts: 17
Location: Switzerland

View user's profile Send private message

PostPosted: Mon Jan 16, 2006 1:17 am     Reply with quote

Yes, the 7221 is the SPI compatible chip, but what I wanted to say is that the serial interface of the 7219 is also compatible with SPI, if you look on page 6, you'll see that the signals (Data, Clock, Load/_CS) are the same.

Quote:

The MAX7219 and MAX7221 are identical except for
two parameters: the MAX7221 segment drivers are
slew-rate limited to reduce electromagnetic interference
(EMI), and its serial interface is fully SPI compatible.


For me, both devices are equal in terms of serial interface (by looking at the timing diagram of the signals...)
The Puma



Joined: 23 Apr 2004
Posts: 227
Location: The Netherlands

View user's profile Send private message

PostPosted: Mon Jan 16, 2006 12:34 pm     Reply with quote

I can't compile this code
Get 21 errors

Is this code for CCS-C?? Sad

UPDATE

Fault found (intensity double)


void setDisplayIntensity(int intensity)
{
if(intensity<16)
{
output_low(MAX7221_CS);
delay_cycles(4);

spi_write(INTENSITY);
spi_write(intensity);

output_high(MAX7221_CS);
}
}


I need a source code to count from 0 to 9999999
Please can someone help?
-sebdey
Guest







PostPosted: Tue Jan 17, 2006 2:06 am     Reply with quote

Of course this is ccs code, and works very well for me...
But you need to create your main.c, make sure that all needed declarations are done, and maybe also understand the code and what you are doing...
It will need some personal work...

If you want to find someone that makes everything for you, I am not the right person, sorry...
The Puma



Joined: 23 Apr 2004
Posts: 227
Location: The Netherlands

View user's profile Send private message

PostPosted: Tue Jan 17, 2006 4:08 pm     Reply with quote

It working so far.

But it counts only to 65536. Why??? Is haved used long variables
Howcan i count further than 65536?

void writeDisplayValue(unsigned long value)
{
dig5 = (value%1000000)/100000;
setDisplayDigit(DIGIT_5,dig5);
dig4 = (value%100000)/10000;
setDisplayDigit(DIGIT_4,dig4);
dig3 = (value%10000)/1000;
setDisplayDigit(DIGIT_3,dig3);
dig2 = (value%1000)/100;
setDisplayDigit(DIGIT_2,dig2);
dig1 = (value%100)/10;
setDisplayDigit(DIGIT_1,dig1);
dig0 = (value%10);
setDisplayDigit(DIGIT_0,dig0);
}


void main()
{
unsigned long tel;

setup_spi(SPI_MASTER | SPI_H_TO_L | SPI_CLK_DIV_16);

setDisplayDecodeMode(BCD_ALL);
setDisplayIntensity(7);
setDisplayDigitsCount(6);
setDisplayTest(NORMAL_MODE);
setDisplayOnOffState(TRUE);


while (1)
{
writeDisplayValue(tel);
tel++;
delay_ms(5);
}
}
newguy



Joined: 24 Jun 2004
Posts: 1911

View user's profile Send private message

PostPosted: Tue Jan 17, 2006 4:23 pm     Reply with quote

This is why I never use words like "long" when I declare variables. A long int is 16 bits in CCS, thus the reason your counter tops out at 65,535.

Try an int32 instead.
Guest








PostPosted: Sat Nov 04, 2006 1:38 am     Reply with quote

Does this work on a 7219?

I've defined the CS pin, and output a test digit but with no go. The digit pins are all high, and the segments are all low =/

Code:
       // Digits 0-3 devront �tre d�cod�s en BCD
   setDisplayDecodeMode(BCD_DIG0to3);
   // On s�lectionne une intensit� moyenne
   setDisplayIntensity(8);
   // On lui indique que seuls les digits 0-3 seront utilis�s
   setDisplayDigitsCount(4);
   // On le met en mode standard
   setDisplayTest(NORMAL_MODE);
   // Et on l'allume
   setDisplayOnOffState(TRUE);

   setDisplayDigit(0,1);
}
Guest








PostPosted: Sat Nov 04, 2006 1:49 am     Reply with quote

Oh sorry oops forgot to setup_spi (: ..now it responds but it still doesn't work. Segments doesn't go high
Guest








PostPosted: Thu Jun 28, 2007 3:08 pm     Reply with quote

Good driver, but how do I turn on/off a segment DP without changing any other segments? For example the display already shows 123, and I want to turn on the decimal 12.3
Guest








PostPosted: Thu Jun 28, 2007 5:53 pm     Reply with quote

Whoops wrong one. I'm actually using the bit bang one, how do I turn on DP? I'm not fluent with this stuff, more with PHP (:

Quote:

void display(char adresse,char wert)
{
char x;
output_low(LOAD);

for (x=0;x<4>0;x--)
{
if (bit_test(adresse,x-1)) output_high(DATA);
else output_low(DATA);
output_high(CLK);
delay_us(1);
output_low(CLK);
delay_us(1);
}

for (x=8;x>0;x--)
{
if (bit_test(wert,x-1)) output_high(DATA);
else output_low(DATA);
output_high(CLK);
delay_us(1);
output_low(CLK);
delay_us(1);
}

output_high(LOAD);
}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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