Subversion Repositories Code-Repo

Rev

Blame | Last modification | View Log | RSS feed

#include "maindefs.h"
#include "led_driver.h"
#include "delays.h"

/* Output Pins:
 * RA0 - LED Display Latch Enable (PPS)
 * RA1 - LED Display CLK (PPS)
 * RA2 - LED Display DIN (PPS)
 * RA3 - LED Display Output Enable (PPS)
 */

void led_driver_init() {
    TRISAbits.TRISA0 = 0;   // LE
    TRISAbits.TRISA1 = 0;   // CLK
    TRISAbits.TRISA2 = 0;   // DAT
    TRISAbits.TRISA3 = 0;   // OE
    
    LATAbits.LATA0 = 0; // LE
    LATAbits.LATA1 = 0; // CLK
    LATAbits.LATA2 = 0; // DAT
    LATAbits.LATA3 = 0; // OE

    led_driver_data(0,0);
}

void led_driver_clock() {
    LATAbits.LATA1 = 0x1; // Simple clock output toggle
    Nop();
    LATAbits.LATA1 = 0x0;
    Nop();
}

void led_driver_data(char left, char right) {
    int i;
    LATAbits.LATA0 = 0x0; // Set latch low to pause display
    for (i = 0; i < 8; i++) {   
        LATAbits.LATA2 = right & 0x1;   // Shift out right character first
        led_driver_clock();
        right >>= 1;
    }
    for (i = 0; i < 8; i++) {
        LATAbits.LATA2 = left & 0x1;   // Then shift out left character
        led_driver_clock();
        left >>= 1;
    }
    LATAbits.LATA0 = 0x1; // Set latch high to resume display
}

void led_driver_num(unsigned char data) {
    unsigned char left = 0;
    unsigned char right = 0;
    unsigned char tmp = 0;
    
//    if (data > 99) {
//        led_driver_data(0,0);
//        return;
//    }
    
    // Determine right character (1s digit)
    tmp = data % 10;
    switch (tmp) {
        case 0:
            right = LED_CHAR_0;
            break;
        case 1:
            right = LED_CHAR_1;
            break;
        case 2:
            right = LED_CHAR_2;
            break;
        case 3:
            right = LED_CHAR_3;
            break;
        case 4:
            right = LED_CHAR_4;
            break;
        case 5:
            right = LED_CHAR_5;
            break;
        case 6:
            right = LED_CHAR_6;
            break;
        case 7:
            right = LED_CHAR_7;
            break;
        case 8:
            right = LED_CHAR_8;
            break;
        case 9:
            right = LED_CHAR_9;
            break;
    }

    // Determine left character (10s digit)
    tmp = data / 10;
    switch (tmp) {
        case 0:
            left = LED_CHAR_0;
            break;
        case 1:
            left = LED_CHAR_1;
            break;
        case 2:
            left = LED_CHAR_2;
            break;
        case 3:
            left = LED_CHAR_3;
            break;
        case 4:
            left = LED_CHAR_4;
            break;
        case 5:
            left = LED_CHAR_5;
            break;
        case 6:
            left = LED_CHAR_6;
            break;
        case 7:
            left = LED_CHAR_7;
            break;
        case 8:
            left = LED_CHAR_8;
            break;
        case 9:
            left = LED_CHAR_9;
            break;
    }

    led_driver_data(left, right);
    
//    if (data <= 25) {
//        led_driver_data(LED_CHAR_BLANK,LED_CHAR_0);
//    } else if (data > 25 && data <= 50) {
//        led_driver_data(LED_CHAR_BLANK,LED_CHAR_1);
//    } else if (data > 50 && data <= 75) {
//        led_driver_data(LED_CHAR_BLANK,LED_CHAR_2);
//    } else if (data > 75 && data <= 100) {
//        led_driver_data(LED_CHAR_BLANK,LED_CHAR_3);
//    } else if (data > 100 && data <= 125) {
//        led_driver_data(LED_CHAR_BLANK,LED_CHAR_4);
//    } else if (data > 125 && data <= 150) {
//        led_driver_data(LED_CHAR_BLANK,LED_CHAR_5);
//    } else if (data > 150 && data <= 175) {
//        led_driver_data(LED_CHAR_BLANK,LED_CHAR_6);
//    } else if (data > 175 && data <= 200) {
//        led_driver_data(LED_CHAR_BLANK,LED_CHAR_7);
//    } else if (data > 200 && data <= 225) {
//        led_driver_data(LED_CHAR_BLANK,LED_CHAR_8);
//    } else {
//        led_driver_data(LED_CHAR_BLANK,LED_CHAR_9);
//    }
}