Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

PIC18F4550 8x8 LED dot matrix not working

Status
Not open for further replies.

eagle1109

Full Member level 6
Joined
Nov 20, 2014
Messages
390
Helped
4
Reputation
10
Reaction score
7
Trophy points
1,298
Location
Saudi Arabia
Activity points
5,911
Hello,

I'm trying to initialize the MAX7219 with the PIC18F4550 from last night, as the code is originally working on my Arduino nano board with C code.

But I wanted to try SPI mode with the PIC microcontroller but it's different and not working, as I did the necessary modifications.

Now, the dot matrix module stays on all the time and the only working thing is shutdown register in MAX7219 initialize function.


Code:
#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;
   }
}
 

The clock rate may not be what you are expecting. In the code you have shown I can't see where you set up the oscillator (other than to use the INTOSC) which means that you will be using the 1MHz clock, and therefore getting an instruction clock of 250KHz. However at the top you tell the delay functions to expect a 4MHz clock rate. Therefore all of the delays will be much longer than you expect. You also have the MSSP (SPI) interface set to Fosc/4 which also will give you a 250KHz SCK.
Without looking at the MAX7219 spec, I would not expect that to be too much of a problem (normally you can take an SPI interface way down in speed).
Have you put as scope on the SDO, SCK and LATC6 pins to check that they are correct and the timing (especially with respect to the LATC6 - which I assume is the \SS\ to the MAX7219) is within spec?
I note that you main loop repeatedly clears and then draws ('clr() and then 'draw()') on the MAX7219. Are you doing this so quickly that the display appears to always be on?
Susan
 

First of all you need to check that interface is working. This pic can be simulated with proteus for example to check that we have a proper data comming to chip.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top