bschriek
Joined: 18 Dec 2007 Posts: 80
|
|
Posted: Tue Dec 04, 2018 1:47 pm |
|
|
Code: |
// PCW compiler 5.078
// WS2812B led strip
// Microchip AN1890 logic cell settings and schematic.
#include <16F1509.H>
#Fuses INTRC_IO,ECL,ECM,ECH,NOWDT,PUT,NOMCLR,NOPROTECT,NOBROWNOUT,NOCLKOUT,IESO,FCMEN,NOLVP
#use delay(clock=16000000)
#use spi(Master, MODE=0, SPI1, STREAM = SRAM)
void BufferToLEDs(int16 address, int16 count);
#define LEDS_IN_STRING 10 // Number of LEDs that you are driving
#define WRITE_CMD 0x02
#define READ_CMD 0x03
int8 c,j, dummy;
int16 index;
#include <Config.c>
void main(void)
{
setup_oscillator(OSC_16MHZ);
setup_comparator(NC_NC_NC_NC);
setup_dac(DAC_OFF);
setup_adc_ports(No_analogs|VSS_VDD); //
setup_vref(VREF_OFF | VREF_COMP_DAC_OFF | TEMPERATURE_INDICATOR_DISABLED);
//setup_adc(ADC_CLOCK_DIV_2);
//setup_timer_0(RTCC_INTERNAL|RTCC_DIV_64); // 16.000.000/4/64
PORT_a_PULLUPS(00000000); // (1=enabled)
//PORT_b_PULLUPS(00000000); // (1=enabled)
PORT_c_PULLUPS(00000000); // (1=enabled)
//DISABLE_INTERRUPTS(PERIPH); //
//DISABLE_INTERRUPTS(GLOBAL);
SET_TRIS_A(0b11001011); // RA5, RA4, RA2 are outputs.
SET_TRIS_B(0b00111111); // RB7, RB6, als are outputs.
SET_TRIS_C(0b01001011); // RC7, RC4, RC2 are outputs. // RC5 = PWM1 output
output_high(pin_C2); // LED, optional
delay_ms(100);
output_low(pin_C2); // LED, optional
delay_ms(100);
output_low(Pin_A4); // P-MOSFET to switch-on 5V power to WS2812B ledstrip
SRAM_CS_PIN = 1; // Disable the external SRAM
///////////////////////////// WS2811_Init /////////////////////////////////////
CLC2GLS0 = 0x20; //0b0010 0000;
CLC2GLS1 = 0x00; //0b0000 0000;
CLC2GLS2 = 0x00; //0b0000 0000;
CLC2GLS3 = 0x00; //0b0000 0000;
CLC2SEL0 = 0x00; //0b0000 0000;
CLC2SEL1 = 0x06; //0b0000 0110;
CLC2POL = 0x0E; //0b0000 1110;
CLC2CON = 0x82; //0b1000 0010;
// Register values as copied from CLC Designer Tool. PWM1 comes through CLC2
CLC4GLS0 = 0x02;
CLC4GLS1 = 0xA4;
CLC4GLS2 = 0x00;
CLC4GLS3 = 0x90;
CLC4SEL0 = 0x54;
CLC4SEL1 = 0x05;
CLC4POL = 0x0A;
CLC4CON = 0xC5;
//
setup_timer_2(T2_DIV_BY_1, 2, 1); // pwm duty van 0 tot 11, 6=50% 12=100% // 16.000.000/4/1/2 = ????
setup_pwm1(PWM_ENABLED | PWM_ACTIVE_HIGH | PWM_OUTPUT); // TP = 750nSec en Th = 375nSec bij d=6 (50% duty cycle)
set_pwm1_duty (6); // 2TP = 1.5µSec en Th = 375nSec bij d=6
SETUP_SPI(SPI_MASTER|SPI_SCK_IDLE_HIGH|SPI_CLK_T2|SPI_SS_DISABLED|SPI_XMIT_L_TO_H); //
////////////////////////////// FillSRAMBuffer /////////////////////////////////
CLC4CONbitsLC4EN = 0; // SRAM Mode
SRAM_CS_PIN = 0; // Enable the external SRAM
spi_xfer(SRAM, WRITE_CMD, 8); // send write command 0x02
spi_xfer(SRAM, 0, 16); // send address 0x00 16bit because of 512 SRAM
for (j = 0; j < 10; ++j) // SRAM pos 0x00 to 0x09
{
spi_xfer(SRAM,0x030303,24); // send RGB
}
for (j = 0; j < 2; ++j) // SRAM pos 0c09 to 0x0B
{
spi_xfer(SRAM,0x0F0000,24); // send RGB Green
}
for (j = 0; j < 2; ++j) // SRAM pos
{
spi_xfer(SRAM,0x000F00,24); // send RGB Red
}
for (j = 0; j < 2; ++j) // SRAM pos
{
spi_xfer(SRAM,0x00000F,24); // send RGB Blue
}
for (j = 0; j < 250; ++j) // SRAM pos (in case of long led strip)
{
spi_xfer(SRAM,0x0F0F0F,24); // send RGB
}
dummy=spi_xfer(SRAM, 0x00); // Ensure the second transfer has completed, before raising CS
SRAM_CS_PIN = 1; // Disable the external SRAM
///////////////////////////////// MAIN LOOP ///////////////////////////////////
while (1)
{
for (index = 0; index < 51 ; index += 3) // 0 to 16 (17 steps) is the amount of different steps before it starts all over again.
{ // Index (0 to 48) is the amount of different steps before it starts all over again.
BufferToLEDs(index, LEDS_IN_STRING * 3);
delay_ms(1000);
output_toggle(pin_A2); // LED, alive sign
}
}
}
//--------------------------- SRAMtoLED ---------------------------------------
/* Receives a byte from the SRAM via SPI at the same time that a byte is being transmitted to the LEDs.
The received byte is immediately transmitted. This results in a 1-byte lag between the SRAM and the LEDs.
Make sure you send a dummy byte to allow reception of the first SRAM byte before enabling transmission to the LEDs
Special Note: Because this uses the interrupt flag bit as an indication of when the transmission is complete,
the SSP1IF flag MUST be set by software before the first time this function is called.
Also, if any modes are changed (SPI mode, etc) after this function is called, the byte being transmitted must be
allowed to complete before invoking any mode changes, otherwise, the byte may be lost. */
//------------------------------- BufferToLEDs --------------------------------
void BufferToLEDs(int16 address, int16 count)
{
CLC4CONbitsLC4EN = 0; // SRAM Mode
SRAM_CS_PIN = 0; // Enable the external SRAM
spi_xfer(SRAM, READ_CMD, 8); // send read command
spi_xfer(SRAM,address,16); // send SRAM address byte
spi_xfer(SRAM,0x00,8); // send 1 byte to solve 1-byte lag between SRAM and LEDS
CLC4CONbitsLC4EN = 1; // LedMode
while (count > 0)
{
while (!SSP1IF); // Wait until transmission is complete, then bit becomes "0".
SSP1IF = 0;
c = SSP1BUF; // receive byte from SRAM
SSP1BUF = c; // transmit byte to LEDs
//-----------------------------------
// dummy=spi_xfer(SRAM, 8); // Replace by this code but it doesn't work.
// spi_xfer(SRAM, dummy, 8); // Replace by this code but it doesn't work.
//-----------------------------------
--count;
}
dummy=spi_xfer(SRAM, 0x00); // Ensure the second transfer has completed, before raising CS
SRAM_CS_PIN = 1; // Disable the external SRAM
} |
|
|