ikelectro
Member
Here is the code I'm using. In simple when I send instruction to the digits it is showing perfect. But whenever I'm sending instruction to the MAX7219 in the function call "digit_separation", It is not showing the 2nd and 1st digit. Showing only 3rd digit. Can anyone help me on that?
Code:
#include <avr/io.h>
#define F_CPU 12000000UL
#include <util/delay.h>
#include <avr/interrupt.h>
#define MOSI PORTB5
#define SCK PORTB7
#define SS PORTB4
#define setbit(port, bit) (port) |= (1 << (bit))
#define clearbit(port, bit) (port) &= ~(1 << (bit))
void execute (unsigned char cmd, unsigned char data)
{
PORTB &= ~(1<<SS);
SPDR = cmd;
while (!(SPSR & (1<<SPIF)));
PORTB |= (1<<SS);
PORTB &= ~(1<<SS);
SPDR = data;
while (!(SPSR & (1<<SPIF)));
PORTB |= (1<<SS);
}
void digit_separation (unsigned char y)
{ unsigned char a = ((y/100)%10);
unsigned char b =((a/10)%10);
unsigned char c = (b%10);
execute (0x09,0XFF); // enable decoding
execute (0X0A,0X04); // intensity level
execute (0X0B,0X02); // scan four segments
execute (0X0C,0X01); //TURN ON THE CHIP
execute (0X0F,0X00); //normal mode
execute(0X01,c);
execute(0X02,b);
execute(0X03,a);
}
int main (void)
{
//
DDRB = 0XFF;
clearbit (PORTB,0);
_delay_ms(1000);
setbit(PORTB,0);
_delay_ms(1000);
DDRB = (1<< MOSI) | (1<<SCK) | (1<<SS); // making it out put
SPCR = (1<<SPE)| (1<<MSTR)|(1<<SPR0); // SPI AS MASTER
execute (0x09,0XFF); // enable decoding
execute (0X0A,0X04); // intensity level
execute (0X0B,0X02); // scan four segments
execute (0X0C,0X01); //TURN ON THE CHIP
execute (0X0F,0X01); // test mode
_delay_ms(1000); //test mode wait 1 seconds
execute (0X0F,0X00); // normal mode
execute (0X01,0X03);
execute (0X02,0X86);// FOR DECIMAL 128+8 setting the 7th bit
execute (0X03,0X08);
_delay_ms(3000);
unsigned char x = 250;
while (1)
{
for (;x>=0;x--)
{
digit_separation (x);
_delay_ms(1000);
//_delay_ms(1000);
}
}
//return 0;
}