AnsweredHot!PIC18F4550 SPI with 8x8 dot matrix isn't working

Page: 123 > Showing page 1 of 3 - Powered by APG vNext Trial
Author
eagle1
Super Member
  • Total Posts : 282
  • Reward points : 0
  • Joined: 2014/11/02 03:04:06
  • Location: Saudi Arabia
  • Status: offline
2018/01/05 14:57:35 (permalink)
0

PIC18F4550 SPI with 8x8 dot matrix isn't working

Hi,
 
What could be the problem?
 
The chip is tested with LED blink and works OK, but when I connect the dot matrix is won't work.
 
The code is from my Arduino C code, I just changed the SPI SFR bits and port names. I want to test how I can copy a C code to another microcontroller and run it. 
 
Hardware:
SDO : LATC7
CLK : LATB1
CS : LATC6
 
 
Software:

#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();
    }
    return;
}

void SPI_Init(void)
{
    ADCON0 = 0;       // disable ADC
    ADCON1 = 0x0f;      // disable ADC pins
    TRISC = 0;           // configure SDO output
    TRISB = 0;           // configure CLK output
    TRISA = 0;           // configure SS output
    PIR1 = 0;        // clear any flags
    SSPCON1 = 0x40;      // enable SPI
}
void SPI_TX(uint8_t data)
{
  rx_temp = SSPBUF; 
  PIR1bits.SSPIF = 0;
  SSPBUF = data;
  while(!PIR1bits.SSPIF);
}
void MAX7219_init(void)
{
  LATC &= ~(0x40);
  SPI_TX(0x0C); // shutdown
  SPI_TX(1);
  LATC |= (0x40);
  LATC &= ~(0x40);
  SPI_TX(0x0A); // intensity
  SPI_TX(1);
  LATC |= (0x40);
  LATC &= ~(0x40);
  SPI_TX(0x09); // decode mode
  SPI_TX(0x00);
  LATC |= (0x40);
  LATC &= ~(0x40);
  SPI_TX(0x0B); // scan limit
  SPI_TX(0x07); 
  LATC |= (0x40);
}
void draw(void)
{
  uint8_t k;
    for (k=0;k<9;k++)
    {
      LATC &= ~(0x40);
      SPI_TX(k);
      __delay_ms(200);     
        SPI_TX(0xf0);
      __delay_ms(200);
      LATC |= (0x40);
    }
}
void clr(void)
{
  uint8_t i;
  for (i=1;i<9;i++)
  {
    LATC &= ~(0x40);
    SPI_TX(i);
    SPI_TX(0x00);
    LATC |= (0x40);
   }



 
post edited by eagle1 - 2018/01/06 00:50:19
#1
qɥb
Monolothic Member
  • Total Posts : 3248
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: online
Re: PIC18F4550 SPI with 8x8 dot matrix isn't working 2018/01/05 16:33:01 (permalink)
0
Get rid of this line
#include <p18f4550.h>

Including xc.h pulls in the appropriate device specific header already, don't do it twice.
 
This line says it's configuring one pin, but you're actually configuring the entire 8 bit port
    TRISC = 0; // configure SDO output

 
If you're initialising a whole register for the first time, write the whole lot rather than individual bits
    SSPCON1 |= (0x40); // enable SPI

becomes
    SSPCON1 = 0x40; // enable SPI. Master mode, CLK=Fosc/4

I'd also recommend initialising SSPSTAT, as the CKE and SMP bits are in there.

You are NEVER clearing SSPIF, so your wait code isn't working.
Change your SPITX function to:
void SPI_TX(uint8_t data)
{
    PIR1bits.SSPIF = 0;
    SSPBUF = data;
    while(!PIR1bits.SSPIF);
}

(also note the much more readable way to access SFR bits)
 

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
#2
dan1138
Super Member
  • Total Posts : 2781
  • Reward points : 0
  • Joined: 2007/02/21 23:04:16
  • Location: 0
  • Status: offline
Re: PIC18F4550 SPI with 8x8 dot matrix isn't working 2018/01/05 20:56:14 (permalink)
0
qɥb
You are NEVER clearing SSPIF, so your wait code isn't working.
Change your SPITX function to:
void SPI_TX(uint8_t data)
{
    PIR1bits.SSPIF = 0;
 
    SSPBUF = data;
    while(!PIR1bits.SSPIF);
}

(also note the much more readable way to access SFR bits)

Also remember it is a good idea to read the SSPBUF register BEFORE sending another byte to the SPI transmitter.
 
Not doing this can result in an error condition that may block the SSPIF flag from being cleared or set, can't remember which.
#3
qɥb
Monolothic Member
  • Total Posts : 3248
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: online
Re: PIC18F4550 SPI with 8x8 dot matrix isn't working 2018/01/05 21:05:10 (permalink)
0
Nahh, you're thinking of the BF flag.
You can either read SSPBUF (to clear BF), then wait for BF to be set,
or clear SSPIF, and wait for SSPIF to be set.
No need to do both when you're the Master, if you don't care what data is coming back.
 

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
#4
dan1138
Super Member
  • Total Posts : 2781
  • Reward points : 0
  • Joined: 2007/02/21 23:04:16
  • Location: 0
  • Status: offline
Re: PIC18F4550 SPI with 8x8 dot matrix isn't working 2018/01/05 22:47:58 (permalink)
0
OK I must be recalling issues I had with another controller.
#5
eagle1
Super Member
  • Total Posts : 282
  • Reward points : 0
  • Joined: 2014/11/02 03:04:06
  • Location: Saudi Arabia
  • Status: offline
Re: PIC18F4550 SPI with 8x8 dot matrix isn't working 2018/01/06 00:51:18 (permalink)
0
I edited the code in the main post.
 
Configuring the whole port is OK for now as I only need output pins.
#6
eagle1
Super Member
  • Total Posts : 282
  • Reward points : 0
  • Joined: 2014/11/02 03:04:06
  • Location: Saudi Arabia
  • Status: offline
Re: PIC18F4550 SPI with 8x8 dot matrix isn't working 2018/01/06 01:48:35 (permalink)
0
dan1138
OK I must be recalling issues I had with another controller.

NO NO it's working with my Arduino board, I just want to test changing the code to another microcontroller with the necessary modifications. And it's not working as I'm looking into the code and there's no big issues, it should work with the PIC18F4550.
#7
qɥb
Monolothic Member
  • Total Posts : 3248
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: online
Re: PIC18F4550 SPI with 8x8 dot matrix isn't working 2018/01/06 02:40:39 (permalink)
0
eagle1
I edited the code in the main post.

Please do NOT go changing the first post after people have started commenting on it.
That makes it very confusing when the comments apply to code which is no longer there.
 

Configuring the whole port is OK for now as I only need output pins.

You know that, but it should be in the comment. Code that doesn't match comments is a huge source of later bugs.

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
#8
qɥb
Monolothic Member
  • Total Posts : 3248
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: online
Re: PIC18F4550 SPI with 8x8 dot matrix isn't working 2018/01/06 02:46:56 (permalink)
0
Not a bug, but the "return" at the end of main() is pointless. It shouldn't be there.
Also not a bug, but this
  LATC &= ~(0x40);
is much more readable if written as:
  LATCbit.LATC6 = 0;    // lower STB
 
and
  LATC |= (0x40);
is nicer as:
  LATCbit.LATC6 = 1;    // raise STB
 
Now, are you able to observe the signals with a scope to see what they are doing?
 
 

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
#9
eagle1
Super Member
  • Total Posts : 282
  • Reward points : 0
  • Joined: 2014/11/02 03:04:06
  • Location: Saudi Arabia
  • Status: offline
Re: PIC18F4550 SPI with 8x8 dot matrix isn't working 2018/01/06 04:22:49 (permalink)
0
I started to comment most the parts of the code.
 
SPI_Init works well. 
 
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
    SSPCON1 = 0x40;     // enable SPI
}

 
The probem is in SPI_tx, in the while loop
 
void SPI_TX(uint8_t data)
{
  rx_temp = SSPBUF;  
  PIR1bits.SSPIF = 0;
  SSPBUF = data;
  while(!(PIR1)& 0x08);
}

 
This one gives me a warning: "main.c:109: warning: (759) expression generates no code"
 
Even it's a warning, the code won't work, I connected an LED with and include it with a test code in the main:
    for (i=0;i<5;i++)
    {
        LATC &= ~(0x40);
        __delay_ms(100);
        SPI_TX(0x0C); // shutdown
        SPI_TX(1);
        LATC |= (0x40);
        __delay_ms(100);
    }

 
So, when I uncomment the two lines of SPI_TX, the LED won't flash but when I comment the two lines of SPI_TX, the LED works, so the problem is in SPI_TX.
 
I think the while loop doesn't terminate.
post edited by eagle1 - 2018/01/06 04:23:55
#10
qɥb
Monolothic Member
  • Total Posts : 3248
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: online
Re: PIC18F4550 SPI with 8x8 dot matrix isn't working 2018/01/06 04:26:42 (permalink)
0
You commented out the line clearing SSPIF,
and put this line of rubbish in
while(!(PIR1)& 0x08);

Please put back the good code I gave you, and look elsewhere for errors.
 

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
#11
qɥb
Monolothic Member
  • Total Posts : 3248
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: online
Re: PIC18F4550 SPI with 8x8 dot matrix isn't working 2018/01/06 04:28:47 (permalink)
0
I see your edited your post while I was typing, and restored the line clearing SSPIF.
You still have that awful while test. I'm not surprised the compiler complains about it.
 
 

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
#12
eagle1
Super Member
  • Total Posts : 282
  • Reward points : 0
  • Joined: 2014/11/02 03:04:06
  • Location: Saudi Arabia
  • Status: offline
Re: PIC18F4550 SPI with 8x8 dot matrix isn't working 2018/01/06 04:38:42 (permalink)
0
Also, did this test but the while loop never get passed.
 
void SPI_TX(uint8_t data)
{  
  rx_temp = SSPBUF; 
  PIR1bits.SSPIF = 0; 
  SSPBUF = data; 
  while(!PIR1bits.SSPIF);
 
    for (i=0;i<5;i++)
    {
        LATCbits.LATC6 = 0;
        __delay_ms(100);
        LATCbits.LATC6 = 1;
        __delay_ms(100);
    } 
 
}

#13
qɥb
Monolothic Member
  • Total Posts : 3248
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: online
Re: PIC18F4550 SPI with 8x8 dot matrix isn't working 2018/01/06 05:19:07 (permalink)
0
I only just noticed, you're setting the wrong bit in SSPCON1 to enable the SPI peripheral.
SSPPEN is bit 5, not bit 6, so it should be 0x20, not 0x40.
You also ignored my suggestion to initialise SSPSTAT as well.
Is CKP=0 and CKE=0 correct for your SPI slave?
 
 

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
#14
eagle1
Super Member
  • Total Posts : 282
  • Reward points : 0
  • Joined: 2014/11/02 03:04:06
  • Location: Saudi Arabia
  • Status: offline
Re: PIC18F4550 SPI with 8x8 dot matrix isn't working 2018/01/06 06:11:04 (permalink)
0
WOW what a mistake!
#15
eagle1
Super Member
  • Total Posts : 282
  • Reward points : 0
  • Joined: 2014/11/02 03:04:06
  • Location: Saudi Arabia
  • Status: offline
Re: PIC18F4550 SPI with 8x8 dot matrix isn't working 2018/01/06 06:22:58 (permalink)
0
Another error:
 
        LATCbits.LATC = 1; // LED ON
        __delay_ms(100);
        LATCbits.LATC = 0; // LED OFF
        __delay_ms(100); 


make[2]: *** [build/default/production/main.p1] Error 1
main.c:43: error: (255) not a member of the struct/union ""
make[1]: *** [.build-conf] Error 2
main.c:43: error: (182) illegal conversion between types
make: *** [.build-impl] Error 2

#16
mbrowning
Just a Member
  • Total Posts : 1021
  • Reward points : 0
  • Joined: 2005/03/16 14:32:56
  • Location: Melbourne, FL
  • Status: offline
Re: PIC18F4550 SPI with 8x8 dot matrix isn't working 2018/01/06 06:42:47 (permalink)
0
Should be LATC6 or sumilar

Can't remember. I've slept since then - Mark
#17
Gort2015
Klaatu Barada Nikto
  • Total Posts : 2645
  • Reward points : 0
  • Joined: 2015/04/30 10:49:57
  • Location: 0
  • Status: offline
Re: PIC18F4550 SPI with 8x8 dot matrix isn't working 2018/01/06 06:44:56 (permalink)
0
LATCbits.LATCx = 1; // LED ON

x Select a bit.

MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
https://www.youtube.com/watch?v=Iu1qa8N2ID0
+ ST:Continues, "What Ships are Made for", Q's back.
#18
ctostado
New Member
  • Total Posts : 1
  • Reward points : 0
  • Joined: 2018/01/04 15:48:36
  • Location: 0
  • Status: offline
Re: PIC18F4550 SPI with 8x8 dot matrix isn't working 2018/01/06 09:03:44 (permalink)
0
Maybe you can`t get the response of the slave because you didn't initialize SSPSTAT, did you check what SPI Mode your slave IC use and what is the maximum clock speed allowed ?
#19
eagle1
Super Member
  • Total Posts : 282
  • Reward points : 0
  • Joined: 2014/11/02 03:04:06
  • Location: Saudi Arabia
  • Status: offline
Re: PIC18F4550 SPI with 8x8 dot matrix isn't working 2018/01/06 09:11:51 (permalink)
0
Thanks for the hint.
 
How about this warning: "main.c:116: warning: (346) declaration of "i" hides outer declaration"
 
void clr(void)
{
  uint8_t i;
  for (i=1;i<9;i++)
  {
    LATCbits.LATC6 = 0;
    SPI_TX(i);
    SPI_TX(0x00);
    LATCbits.LATC6 = 1;
   }
}

#20
Page: 123 > Showing page 1 of 3 - Powered by APG vNext Trial
Jump to:
© 2018 APG vNext Trial Version 4.5