Quantcast
Channel: Forums - Recent Threads
Viewing all articles
Browse latest Browse all 262198

Debugging Issue with F5438A

$
0
0

Hi there.

I am currently trying to use my laptop keyboard as input , via serial comms, to the F5438A. The keyboard input should control which pixel of my 16x16 RGB LED display is lit, and at which colour/saturation.

This program was previously run using the G2553, but I've had to map it over to the F5438A for more RAM. The program worked PERFECTLY using the G2553, but for some reason when I try to debug the F5438A version of the program, it just doesnt work!

I've had to use examples from sample code to calibrate the DCOCLK etc. - the only difference between the two programs (G2553 & F5438A) is that one clock is 16MHz and the other is 18MHz.

Code:

#include <msp430f5438a.h>
#include <stdint.h>
// TLC INPUTS
#define GSCLK BIT6 // P1.6 (SMCLK OUT pin 9 RF3)
#define MOSI BIT1 // P3.1 (UCB0MOSI pin 18 RF3)
#define SCLK BIT3 // P3.3 (UCB0CLK pin 16 RF3)
#define BLANK BIT2 // P1.2 (GPIO pin 5 RF3)
#define XLAT BIT5 // P1.5 (GPIO PIN 7  RF3)
// SERIAL COMMS
#define RX_PIN BIT5 // P3.5 (UCA0RXD bottom header)
// 74HC595
#define DATA BIT7 // P3.7 (UCB1MOSI)
#define CLOCK BIT5 // P5.5 (UCB1CLK)
#define LATCH BIT0 // P4.0 (GPIO)
//==============================================================
typedef unsigned char u_char;
typedef unsigned int u_int;
typedef unsigned short u_short;
typedef unsigned long u_long;
//=============================================================
// PROTOTYPES
void init(void); // put clock and GPIO pin directions etc
void init595(void);
void SPI_TLC(void); // for GSCLK + MOSI + DATA + CLOCK
void SPI_595(void);
void UART_init(void); // for RX
void updateTLC(void);
void SPI_shift(u_short);
void shiftOut(u_long);
void HSV2RGB(u_short*, u_short*, u_short*, short, u_char);
void SetVcoreUp (unsigned int level);
void calibrateClock(void);
void set_row_char_hue(u_char, u_long, short, u_char);


//=====================================================================
// GLOBAL DEFINES
//======================================================================
#define NUMBER_OF_OUTS 48
#define NUMBER_OF_COLUMNS 16
#define UPDATE {P1OUT |= BLANK;P1OUT |= XLAT;P1OUT &= ~XLAT;P1OUT &= ~BLANK; updateTLC();}
#define OFF 0
#define SAT 255
u_short leds[NUMBER_OF_OUTS];  // 0 - 15 Red Rows, 16 - 31 Blue Rows, 32 - 47 Green Rows {0, }
static u_long columnArray[NUMBER_OF_COLUMNS] = {0x8000,0x4000,0x2000,0x1000,0x0800,0x0400,0x0200,0x0100,0x0080,0x0040,0x0020,0x0010,0x0008,0x0004,0x0002,0x0001};
static u_char rowArray[NUMBER_OF_COLUMNS] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
u_char timerCounter = 0;
// SERIAL GLOBAL VARIABLES
u_char sug = 0;
signed short rug=0;
short hug = 0;
signed short cug = 0;
u_char serialON = 0;
u_char blank = 0;
u_char latch = 0;
u_short hueArray[16][16] = { // stores the value of HUE for each pixel (default value, 360, is off)
        { 360,360,360,360,360,360,360,360,360,360,360,360,360,360,360,360},
        { 360,360,360,360,360,360,360,360,360,360,360,360,360,360,360,360},
        { 360,360,360,360,360,360,360,360,360,360,360,360,360,360,360,360},
        { 360,360,360,360,360,360,360,360,360,360,360,360,360,360,360,360},
        { 360,360,360,360,360,360,360,360,360,360,360,360,360,360,360,360},
        { 360,360,360,360,360,360,360,360,360,360,360,360,360,360,360,360},
        { 360,360,360,360,360,360,360,360,360,360,360,360,360,360,360,360},
        { 360,360,360,360,360,360,360,360,360,360,360,360,360,360,360,360},
        { 360,360,360,360,360,360,360,360,360,360,360,360,360,360,360,360},
        { 360,360,360,360,360,360,360,360,360,360,360,360,360,360,360,360},
        { 360,360,360,360,360,360,360,360,360,360,360,360,360,360,360,360},
        { 360,360,360,360,360,360,360,360,360,360,360,360,360,360,360,360},
        { 360,360,360,360,360,360,360,360,360,360,360,360,360,360,360,360},
        { 360,360,360,360,360,360,360,360,360,360,360,360,360,360,360,360},
        { 360,360,360,360,360,360,360,360,360,360,360,360,360,360,360,360},
        { 360,360,360,360,360,360,360,360,360,360,360,360,360,360,360,360},
};
u_char satArray[16][16] = {0,}; // stores Saturation values

void main(void)
{
    init();
    updateTLC();

    P1OUT |= (XLAT);
    P1OUT &= ~(XLAT);
    _bis_SR_register(GIE);
    for(;;)
    {
        if( latch !=0 )                                                                  // this is what is preventing the program from running. If i take this "if" statement out, the program runs fine.
        {                                                                                      // it is very frustrating!
            hueArray[rug][cug] = hug;
            satArray[rug][cug] = sug;
        }
        else {
            set_row_char_hue(rowArray[rug],columnArray[cug],hug,sug);
        }
        _bis_SR_register(LPM0_bits);       // Enter LPM0, interrupts enabled
    }
}


void calibrateClock(void)
{
    SetVcoreUp (PMMCOREV_2);                  // Set VCore = 1.8V for 18MHz clock
    UCSCTL3 |= SELREF_2;                      // Set DCO FLL reference = REFO
    UCSCTL4 |= SELA_2;                        // Set ACLK = REFO
    __bis_SR_register(SCG0);                  // Disable the FLL control loop
    UCSCTL0 = 0x0000;                         // Set lowest possible DCOx, MODx
    UCSCTL1 = DCORSEL_6;                      // Select DCO range 24MHz operation
    UCSCTL2 = FLLD_1 + 547;  //547                 // Set DCO Multiplier for 18MHz
    __bic_SR_register(SCG0);                   // (N + 1) * FLLRef = Fdco
    __delay_cycles(562500);                      // (547 + 1) * 32768 = 18MHz
    do{                                            // Loop until XT1,XT2 & DCO fault flag is cleared
        UCSCTL7 &= ~(XT2OFFG + XT1LFOFFG + XT1HFOFFG + DCOFFG);
        // Clear XT2,XT1,DCO fault flags
        SFRIFG1 &= ~OFIFG;                      // Clear fault flags
    } while (SFRIFG1&OFIFG);                   // Test oscillator fault flag
                                              // Set FLL Div = fDCOCLK/2
                                                    // Enable the FLL control loop
    // Worst-case settling time for the DCO when the DCO range bits have been
    // changed is n x 32 x 32 x f_MCLK / f_FLL_reference.
    // 32 x 32 x 18 MHz / 32,768 Hz = 562500 = MCLK cycles for DCO to settle
}



void init(void)
{
    WDTCTL = WDTPW + WDTHOLD; // Kill watchdog timer
    calibrateClock();
//  SETUP GPIO PINS AS OUTPUTS
    P1DIR |= (GSCLK + BLANK + XLAT + BIT0);
    P1SEL |= GSCLK; // P1.6 AS SMCLK OUT
    P1OUT &= ~(BLANK + XLAT);
    TA0CCR0 = 0xFFF;
    TA0CTL = TASSEL_2 + MC_1 + ID_0; // SMCLK, up mode, 1:1
    TA0CCTL0 = CCIE; // CCR0 interrupt enabled
    init595(); // only if SPI isnt used.
    SPI_TLC();
    UART_init();
}

void init595(void)
{
    P4DIR |= LATCH; P4OUT &= ~LATCH;
    P3DIR |= DATA; P3OUT &= ~DATA;
    P5DIR |= CLOCK; P5OUT &= ~CLOCK;
}

void SPI_TLC(void)
{
    // TLC: SPI setup UCB0
    P3SEL |= MOSI + SCLK; // P3.1 + P3.3, UCB0SIMO + UCB0CLK
    UCB0CTL0 = UCCKPH + UCMSB + UCMST + UCSYNC; // data captured on 1st UCLK edge/changed on follwing edge, MSB first, master, 3-pin SPI,synchronous
    UCB0CTL1 |= UCSSEL_2; // SMCLK
    UCB0BR0 |= 0x01; // 1:1
    UCB0BR1 = 0;
    UCB0CTL1 &= ~UCSWRST; // clear SW
}
void UART_init(void)
{
    // UART Setup UCA0
    P3SEL |= 0x30;          // P3.4,5 = USCI_A0 TXD/RXD
    UCA0CTL1 |= UCSWRST;   // **Put state machine in reset**
    UCA0CTL1 |= UCSSEL_2;    //SMCLK
    UCA0BR0 |= 9;            //115200 @ 18MHz
    UCA0BR1 = 0;            //115200
    UCA0MCTL = UCOS16 + UCBRF3 + UCBRF2; //Modulation
    UCA0CTL1 &= ~UCSWRST;    //Start USCI
    UCA0IE |= UCRXIE;      // Enable USCI_A0 RX interrupt
}

void updateTLC(void)
{
    u_char ledCounter = NUMBER_OF_OUTS >> 1;
    while (ledCounter-- > 0)
    {
        u_char i = ledCounter << 1;
        UCB0TXBUF = leds[i + 1] >> 4;
        while (!(UCB0IFG & UCTXIFG)); // TX buffer ready? ?????????  NEED TO CHECK THIS
        u_char unib = leds[i + 1] << 4;
        u_char lnib = (leds[i] >> 8) & 0x0F;
        UCB0TXBUF = unib | lnib;
        while (!(UCB0IFG & UCTXIFG)); // TX buffer ready? ?????????  NEED TO CHECK THIS
        UCB0TXBUF = leds[i];
        while (!(UCB0IFG & UCTXIFG)); // TX buffer ready? ?????????  NEED TO CHECK THIS
    }
}


void shiftOut(u_long val)
{
    P4OUT &= ~LATCH;
    int i;
    for (i = 0; i < 16; i++) // iterate over each bit 16
    {
        if(val & (1 << i)) {
            P3OUT |= DATA;
        } else {
            P3OUT &= ~DATA;
        }
        P5OUT |= CLOCK; // PULSE CLOCK
        P5OUT ^= CLOCK;
    }
    P4OUT |= LATCH; // PULSE LATCH
    P4OUT &= ~LATCH;
}

void HSV2RGB(u_short* r, u_short* g, u_short* b, short h, u_char s)
{
    u_char v = 255;
    u_char i, p, q, t;
    u_short fs;
    if( s == 0 )
    {
        *r = 2000;
        *g = 3500;
        *b = 4095;
        UPDATE;
        return;
    }
    else {

        if (h < 60 ) i = 0;
        else if (h < 120) i = 1;
        else if (h < 180) i = 2;
        else if (h < 240) i = 3;
        else if (h < 300) i = 4;
        else if (h < 360) i = 5;
        else return;

        fs = ((h - i * 60) * s)/ 60;
        p = 255 - s;
        q = 255 - fs;
        t = 255 - s + fs;

        switch(i)
        {
        case 0:
            *r = 255;*g = t;*b = p;break;
        case 1:
            *r = q;*g = 255;*b = p;break;
        case 2:
            *r = p;*g = 255;*b = t;break;
        case 3:
            *r = p;*g = q;*b = 255;break;
        case 4:
            *r = t;*g = p;*b = 255;break;
        case 5:
            *r = 255;*g = p;*b = q;break;
        }
        *r *= v/24;
        *g *= v/16;
        *b *= v/16;
        UPDATE;
    }
}

void set_row_char_hue(u_char row, u_long col_data, short hue, u_char sat)
{
    HSV2RGB( &leds[row], &leds[row + 32], &leds[row + 16], hue, sat); //on;
    shiftOut( col_data );
    // turn off leds
    leds[row] = OFF;
    leds[row+32] = OFF;
    leds[row+16] =  OFF;
    UPDATE;
}
void SetVcoreUp (unsigned int level)
{
  // Open PMM registers for write
  PMMCTL0_H = PMMPW_H;
  // Set SVS/SVM high side new level
  SVSMHCTL = SVSHE + SVSHRVL0 * level + SVMHE + SVSMHRRL0 * level;
  // Set SVM low side to new level
  SVSMLCTL = SVSLE + SVMLE + SVSMLRRL0 * level;
  // Wait till SVM is settled
  while ((PMMIFG & SVSMLDLYIFG) == 0);
  // Clear already set flags
  PMMIFG &= ~(SVMLVLRIFG + SVMLIFG);
  // Set VCore to new level
  PMMCTL0_L = PMMCOREV0 * level;
  // Wait till new level reached
  if ((PMMIFG & SVMLIFG))
    while ((PMMIFG & SVMLVLRIFG) == 0);
  // Set SVS/SVM low side to new level
  SVSMLCTL = SVSLE + SVSLRVL0 * level + SVMLE + SVSMLRRL0 * level;
  // Lock PMM registers for write access
  PMMCTL0_H = 0x00;
}

#pragma vector = TIMER0_A0_VECTOR
__interrupt void Timer_A0(void)
{_bic_SR_register_on_exit(LPM0_bits);}

#pragma vector = USCI_A0_VECTOR
__interrupt void USCI0RX_ISR(void)
{
    if(UCA0RXBUF == 83)                            // INCREASE SATURATION 'S'
    {
        P1OUT ^= BIT0;
        sug++;
        if(sug==256)
            sug=0;
    }
    if(UCA0RXBUF == 72)                            // CHANGE HUE 'H'
    {
            hug=hug+6;
            if(hug==360)
                hug=6;
    }
    if(UCA0RXBUF == 44)                            // COLUMN LEFT ','
    {
        cug--;
        if(cug<0)
            cug=15;
    }
    if(UCA0RXBUF == 46)                            // COLUMN RIGHT '.'
    {
        cug++;
        if(cug>15)
            cug=0;
    }
    if(UCA0RXBUF == 86)                            // DOWN 'V'
    {
        rug++;
        if(rug>15)
            rug=0;
    }
    if(UCA0RXBUF == 70)                            // UP 'F'
    {
        rug--;
        if(rug<0)
            rug=15;
    }
    if(UCA0RXBUF == 48)
    {
        serialON=0;            // TURNS OFF FUNCTION
    }
    if(UCA0RXBUF == 49)
    {
        serialON=1;            // TURNS ON FUNCTION
    }
    if(UCA0RXBUF == 50)
    {
        serialON=2;            // TURNS ON FUNCTION
    }
    if(UCA0RXBUF == 32)    // space bar
    {
        latch = 1;// TURNS ON FUNCTION
    }
    if(UCA0RXBUF == 77)    // M
    {
        latch = 0;// TURNS ON FUNCTION
    }
}

Can anyone see something obviously wrong with this?

Cheers


Viewing all articles
Browse latest Browse all 262198

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>