#define _XTAL_FREQ 4000000
#include <stdint.h>
#include <xc.h>
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
// CONFIG1H
#pragma config FOSC = INTOSC_XT // Oscillator Selection bits (Internal oscillator, XT used by USB (INTXT))
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
#pragma config IESO = OFF // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)
// CONFIG3H
#pragma config CCP2MX = ON // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
#pragma config PBADEN = ON // LATC A/D Enable bit (LATC<4:0> pins are configured as analog input channels on Reset)
#pragma config LPT1OSC = OFF // Low-Power Timer 1 Oscillator Enable bit (Timer1 configured for higher power operation)
#pragma config MCLRE = ON // MCLR Pin Enable bit (MCLR pin enabled; RE3 input pin disabled)
// CONFIG4L
#pragma config STVREN = OFF // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
#pragma config LVP = OFF // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled)
#pragma config ICPRT = OFF // Dedicated In-Circuit Debug/Programming Port (ICPORT) Enable bit (ICPORT disabled)
#pragma config XINST = OFF // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))
void SPI_Init(void);
void SPI_TX(uint8_t data);
void MAX7219_init(void);
void draw(void);
void clr(void);
uint8_t rx_temp;
void main(void) {
SPI_Init();
MAX7219_init();
while (1)
{
clr();
draw();
}
}
void SPI_Init(void)
{
ADCON0 = 0x00; // disable ADC
ADCON1 = 0x0f; // disable ADC pins
TRISC = 0x00; // configure SDO output
TRISB = 0x00; // configure CLK output
TRISA = 0x00; // configure SS output
PIR1 = 0x00; // clear any flags
SSPSTAT = 0x00; // set CKP and CKE to zero
SSPCON1 = 0x20; // enable SPI
}
void MAX7219_init(void)
{
LATCbits.LATC6 = 0;
SPI_TX(0x0C); // shutdown
SPI_TX(1);
LATCbits.LATC6 = 1;
__delay_ms(1);
LATCbits.LATC6 = 0;
SPI_TX(0x0A); // intensity
SPI_TX(0x05);
LATCbits.LATC6 = 1;
__delay_ms(1);
LATCbits.LATC6 = 0;
SPI_TX(0x09); // decode mode
SPI_TX(0x00);
LATCbits.LATC6 = 1;
__delay_ms(1);
LATCbits.LATC6 = 0;
SPI_TX(0x0B); // scan limit
SPI_TX(0x07);
LATCbits.LATC6 = 1;
}
void SPI_TX(uint8_t data)
{
rx_temp = SSPBUF;
PIR1bits.SSPIF = 0;
SSPBUF = data;
while(!PIR1bits.SSPIF);
}
void draw(void)
{
uint8_t k;
for (k=0;k<9;k++)
{
LATCbits.LATC6 = 0;
SPI_TX(k);
__delay_ms(1);
SPI_TX(0xaa);
__delay_ms(1);
LATCbits.LATC6 = 1;
}
}
void clr(void)
{
uint8_t i;
for (i=1;i<9;i++)
{
LATCbits.LATC6 = 0;
SPI_TX(i);
SPI_TX(0x00);
LATCbits.LATC6 = 1;
}
}