Hello There,
I have been trying to interface MAX7219 to PIC 16F877 using the code below. It doesn't work. Please help to identify the issues and correct it.
I have been trying to interface MAX7219 to PIC 16F877 using the code below. It doesn't work. Please help to identify the issues and correct it.
C:
// CONFIG
#pragma config FOSC = XT // Oscillator Selection bits (XT oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config CP = OFF // FLASH Program Memory Code Protection bits (Code protection off)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low Voltage In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EE Memory Code Protection (Code Protection off)
#pragma config WRT = ON // FLASH Program Memory Write Enable (Unprotected program memory may be written to by EECON control)
#define _XTAL_FREQ 4000000
#define SPI_CS PORTBbits.RB1
unsigned char i;
void spi_setup(void)
{
TRISC=0b00010000 ; // PORTC ping direction
TRISB=0b00000000 ; // PORTB ping direction
PIR1bits.SSPIF = 0; // Clear interrupt flag
PIE1bits.SSPIE = 0; // Disable MSSP interrupt
//INTCONbits.PEIE = 0; // Disable perriferal interrupts
INTCON = 0x0;
SSPSTATbits.SMP = 0; //input samled at middle of interval
SSPSTATbits.CKE = 0; //Data transmitted from low to high clock
SSPCON = 0b00000000; // Enable SSP, Master mode, clock Fosc/4
SSPCONbits.CKP = 0;
SSPCONbits.SSPEN = 1; // Enable
SSPCONbits.SSPOV = 0; // Overflow disable
}
unsigned char spi_transfer(unsigned char data)
{
SSPBUF = data;
while( !SSPSTATbits.BF );
SSPSTATbits.BF = 0;
return SSPBUF;
}
void max7219_init()
{
SPI_CS = 0; //ON MAX
spi_transfer(0x09); //BCD mode
spi_transfer(0xFF);
SPI_CS = 1; //OFF MAX
SPI_CS = 0; //ON MAX
spi_transfer(0x0A); //Intensity
spi_transfer(0x0F);
SPI_CS = 1; //OFF MAX
SPI_CS = 0; //ON MAX
spi_transfer(0x0B); //Display refresh
spi_transfer(0x07);
SPI_CS = 1; //OFF MAX
SPI_CS = 0; //ON MAX
spi_transfer(0x0C); //Turn on the display
spi_transfer(0x01);
PORTBbits.RB1 = 1; // OFF MAX
SPI_CS = 0; //ON MAX
spi_transfer(0x00); //NO Test
spi_transfer(0xFF);
SPI_CS = 1; //OFF MAX
}
void main(void) {
__delay_ms(100);
spi_setup();
__delay_ms(100);
SPI_CS = 1;
__delay_ms(100);
max7219_init();
for (i = 1; i<=8; i++)
{
SPI_CS = 0;
spi_transfer(i);
spi_transfer(8-i);
SPI_CS = 1;
__delay_ms(500);
} // <------ MISSING!!!
return;
}
Last edited by a moderator: