Voor het bouwen van een LED Globe ga ik gebruikmaken van de PIC18F26K22 en de MAX6957.
Door middel van een SPI verbinding wil ik RGB LEDs aansturen via de MAX6957. Als testje heb ik hier een simpele schakeling op breadboard voor opgebouwd. Van de uC pin 14 (RC3 / SCK1) naar pin 25 (SCKL) van de LED driver. Van de uC pin 16 (RC5 / SDO1) naar pin 26 (DIN) van de LED driver. En tot slot van de uC pin 21 (RB0) naar pin 27 (CS) van de LED driver. Verder is uiteraard de voeding aangesloten en is pin 1 (ISET) van de LED driver via een 47K weerstand verbonden met de GND.
Als test wou ik een RGB LED aangesloten op P31, P30 en P29 laten oplichten.
Hier mijn code:
#include <stdio.h>
#include <stdlib.h>
#include <htc.h>
#include "header.h"
#include "main.h"
#define SPI_CS_0 LATBbits.LATB0
int main(void)
{
OSCCON = 0b01111000; //set internal oscillator to 16MHz
SPI_Setup();
//LED_Driver_Configuration();
//set LED on
//SPI_Communication(0x3D, 0x01);
//SPI_Communication(0x3E, 0x01);
//SPI_Communication(0x3F, 0x01);
//configuration
SPI_CS_0 = 0;
SSP1BUF = 0b0000010000000001;
while(!SSP1IF);
SPI_CS_0 = 1;
//global current
SPI_CS_0 = 0;
SSP1BUF = 0b0000001000001111;
while(!SSP1IF);
SPI_CS_0 = 1;
//port configuration p31 - p28; LED driver
SPI_CS_0 = 0;
SSP1BUF = 0b0000111100000000;
while(!SSP1IF);
SPI_CS_0 = 1;
//LED on
SPI_CS_0 = 0;
SSP1BUF = 0b0101110100000111;
while(!SSP1IF);
SPI_CS_0 = 1;
return 0;
}
void SPI_Setup(void)
{
//set MOSI (Port C5), SCK (Port C3) and CS pin (Port B0) as outputs
TRISBbits.TRISB0 = 0;
TRISCbits.TRISC3 = 0;
TRISCbits.TRISC5 = 0;
//set all CS pins high
SPI_CS_0 = 1;
//set to SPI master mode
SSP1CON1 = 0b00100000;
return;
}
void LED_Driver_Configuration(void)
{
SPI_Communication(0x04, 0x01); //configuration; normal operation, global current, transition detection disabled
SPI_Communication(0x02, 0x07); //global current; 8/16
SPI_Communication(0x09, 0x00); //port configuration p7 - p4; LED driver
SPI_Communication(0x0A, 0x00); //port configuration p11 - p8; LED driver
SPI_Communication(0x0B, 0x00); //port configuration p15 - p12; LED driver
SPI_Communication(0x0C, 0x00); //port configuration p19 - p16; LED driver
SPI_Communication(0x0D, 0x00); //port configuration p23 - p20; LED driver
SPI_Communication(0x0E, 0x00); //port configuration p27 - p24; LED driver
SPI_Communication(0x0F, 0x00); //port configuration p31 - p28; LED driver
return;
}
void SPI_Communication(unsigned char command, unsigned char data)
{
SPI_CS_0 = 0;
SSP1BUF = command; //send command
while(!SSP1IF); //wait till transfer is complete
SSP1BUF = data; //send data
while(!SSP1IF); //wait till transfer is complete
SPI_CS_0 = 1;
return;
}De eerste keer probeerde ik het zonder de 4 onderste blokjes code in de main functie en waren 4 functie aanroepen daarboven niet 'gecomment'. De tweede poging was precies zoals de code nu is. Het programma laat zonder foutmeldingen in de uC (via pickit3) de LED gaat echter niet aan. Nu de vraag wat is er fout aan de code?
Alvast erg bedankt!
Arco
Special Member
Arco - "Simplicity is a prerequisite for reliability" - hard-, firm-, en software ontwikkeling: www.arcovox.com
Ja, ik heb het net nogmaals getest de PIC werkt. Dit is trouwens de eerste keer dat ik helemaal zelf een (groot) programma schrijf dus is überhaupt de manier waarop ik de PIC instel en de SPI communicatie uitvoer wel goed?
Hier nog de twee bijbehorende header files:
header.h (gemaakt met behulp van PIC memory views/configuration bits van mplabx):
#include <xc.h>
// CONFIG1H
#pragma config FOSC = INTIO67 // Oscillator Selection bits (Internal oscillator block)
#pragma config PLLCFG = OFF // 4X PLL Enable (Oscillator used directly)
#pragma config PRICLKEN = ON // Primary clock enable bit (Primary clock enabled)
#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)
// CONFIG2L
#pragma config PWRTEN = OFF // Power-up Timer Enable bit (Power up timer disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable bits (Brown-out Reset disabled in hardware and software)
#pragma config BORV = 190 // Brown Out Reset Voltage bits (VBOR set to 1.90 V nominal)
// CONFIG2H
#pragma config WDTEN = OFF // Watchdog Timer Enable bits (Watch dog timer is always disabled. SWDTEN has no effect.)
#pragma config WDTPS = 32768 // Watchdog Timer Postscale Select bits (1:32768)
// CONFIG3H
#pragma config CCP2MX = PORTC1 // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
#pragma config PBADEN = OFF // PORTB A/D Enable bit (PORTB<5:0> pins are configured as digital I/O on Reset)
#pragma config CCP3MX = PORTB5 // P3A/CCP3 Mux bit (P3A/CCP3 input/output is multiplexed with RB5)
#pragma config HFOFST = ON // HFINTOSC Fast Start-up (HFINTOSC output and ready status are not delayed by the oscillator stable status)
#pragma config T3CMX = PORTC0 // Timer3 Clock input mux bit (T3CKI is on RC0)
#pragma config P2BMX = PORTB5 // ECCP2 B output mux bit (P2B is on RB5)
#pragma config MCLRE = EXTMCLR // MCLR Pin Enable bit (MCLR pin enabled, RE3 input pin disabled)
// CONFIG4L
#pragma config STVREN = ON // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
#pragma config LVP = ON // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled if MCLRE is also 1)
#pragma config XINST = OFF // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))
// CONFIG5L
#pragma config CP0 = OFF // Code Protection Block 0 (Block 0 (000800-003FFFh) not code-protected)
#pragma config CP1 = OFF // Code Protection Block 1 (Block 1 (004000-007FFFh) not code-protected)
#pragma config CP2 = OFF // Code Protection Block 2 (Block 2 (008000-00BFFFh) not code-protected)
#pragma config CP3 = OFF // Code Protection Block 3 (Block 3 (00C000-00FFFFh) not code-protected)
// CONFIG5H
#pragma config CPB = OFF // Boot Block Code Protection bit (Boot block (000000-0007FFh) not code-protected)
#pragma config CPD = OFF // Data EEPROM Code Protection bit (Data EEPROM not code-protected)
// CONFIG6L
#pragma config WRT0 = OFF // Write Protection Block 0 (Block 0 (000800-003FFFh) not write-protected)
#pragma config WRT1 = OFF // Write Protection Block 1 (Block 1 (004000-007FFFh) not write-protected)
#pragma config WRT2 = OFF // Write Protection Block 2 (Block 2 (008000-00BFFFh) not write-protected)
#pragma config WRT3 = OFF // Write Protection Block 3 (Block 3 (00C000-00FFFFh) not write-protected)
// CONFIG6H
#pragma config WRTC = OFF // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) not write-protected)
#pragma config WRTB = OFF // Boot Block Write Protection bit (Boot Block (000000-0007FFh) not write-protected)
#pragma config WRTD = OFF // Data EEPROM Write Protection bit (Data EEPROM not write-protected)
// CONFIG7L
#pragma config EBTR0 = OFF // Table Read Protection Block 0 (Block 0 (000800-003FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF // Table Read Protection Block 1 (Block 1 (004000-007FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF // Table Read Protection Block 2 (Block 2 (008000-00BFFFh) not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF // Table Read Protection Block 3 (Block 3 (00C000-00FFFFh) not protected from table reads executed in other blocks)
// CONFIG7H
#pragma config EBTRB = OFF // Boot Block Table Read Protection bit (Boot Block (000000-0007FFh) not protected from table reads executed in other blocks)main.h:
//function definitions
int main(void);
void SPI_Setup(void);
void LED_Driver_Configuration(void);
void SPI_Communication(unsigned char command, unsigned char data);Arco
Special Member
Arco - "Simplicity is a prerequisite for reliability" - hard-, firm-, en software ontwikkeling: www.arcovox.com
Ik weet niet wat de SPI init doet, moet wel hetzelfde zijn als wat de MAX chip verwacht (snelheid, clock polariteit,...)
Ik heb de clock polariteit en de clock edge in bijde mogelijkheden geprobeerd, niks werkt 
Over de snelheid kon ik alleen dit vinden van de MAX driver:
"The MAX6957’s SPI interface is guaranteed to operate at
26Mbps on a 2.5V supply, and on a 5V supply typically
operates at 50Mbps."
En op blz 3 van de datasheet staat nog een tabel TIMING CHARACTERISTICS.
De uC interne oscillator is ingesteld op 16MHz en de SPI snelheid op FOSC/4 ik weet niet of dit goed is dan?
IK heb echt alles geprobeerd, andere setup op mn breadboard met kortere data kabels, andere klokfrequentie, polariteit en edge maar niks werkt 
Echt niemand een idee waaraan het zou kunnen liggen?, je zou me daar echt onwijs mee helpen!
Zo ziet mn code er nu uit:
#include <stdio.h>
#include <stdlib.h>
#include <htc.h>
#include "header.h"
#include "main.h"
#define SPI_CS_0 LATBbits.LATB0
void main(void)
{
//set internal oscillator to 16MHz
//OSCCON: OSCILLATOR CONTROL REGISTER
//| 7 | 6 5 4 | 3 | 2 | 1 0 |
//| IDLEN | IRCF<2:0> | OSTS | HFIOFS | SCS<1:0> |
//see datasheet page 32
OSCCON = 0b01111000;
SPI_Setup();
LED_Driver_Configuration();
//set LED on
SPI_Communication(0x5D, 0x07);
while(1) continue;
}
void SPI_Setup(void)
{
//set SDO1 (Port C5), SCK1 (Port C3) and CS pin (Port B0) as outputs
TRISBbits.TRISB0 = 0;
TRISCbits.TRISC3 = 0;
TRISCbits.TRISC5 = 0;
//set SDI1 (Port C4) as output because it is not needed
TRISCbits.TRISC4 = 0;
//set all CS pins high
SPI_CS_0 = 1;
//set to SPI master mode:
//SSPxSTAT: SSPx STATUS REGISTER
//| 7 | 6 | 5 4 3 2 1 0 |
//| SMP | CKE | READ ONLY |
//see datasheet page 259
SSP1STAT = 0b01000000;
//SSPxCON1: SSPx CONTROL REGISTER 1
//| 7 | 6 | 5 | 4 | 3 2 1 0 |
//| WCOL | SSPxOV | SSPxEN | CKP | SSPxM<3:0> |
//see datasheet page 260
SSP1CON1 = 0b00100000;
return;
}
void LED_Driver_Configuration(void)
{
SPI_Communication(0x04, 0x01); //configuration; normal operation, global current, transition detection disabled
SPI_Communication(0x02, 0x0F); //global current; 16/16
// SPI_Communication(0x09, 0x00); //port configuration p7 - p4; LED driver
// SPI_Communication(0x0A, 0x00); //port configuration p11 - p8; LED driver
// SPI_Communication(0x0B, 0x00); //port configuration p15 - p12; LED driver
// SPI_Communication(0x0C, 0x00); //port configuration p19 - p16; LED driver
// SPI_Communication(0x0D, 0x00); //port configuration p23 - p20; LED driver
// SPI_Communication(0x0E, 0x00); //port configuration p27 - p24; LED driver
SPI_Communication(0x0F, 0x00); //port configuration p31 - p28; LED driver
return;
}
void SPI_Communication(unsigned char command, unsigned char data)
{
SPI_CS_0 = 0;
SSP1BUF = command; //send command
while(!SSP1IF); //wait till transfer is complete
SSP1BUF = data; //send data
while(!SSP1IF); //wait till transfer is complete
SPI_CS_0 = 1;
return;
}Na nog een paar uur zoeken doet hij het eindelijk 
void SPI_Communication(unsigned char command, unsigned char data)
{
SPI_CS_0 = 0;
SSP1BUF = command; //send command
while(!SSP1IF); //wait till transfer is complete
SSP1IF = 0;
SSP1BUF = data; //send data
while(!SSP1IF); //wait till transfer is complete
SSP1IF = 0;
SPI_CS_0 = 1;
return;
}SSP1IF moest nog op 0 terug gezet worden na een schrijf commando.