Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
#include "maindefs.h"#include "led_driver.h"#include "delays.h"#include <spi.h>unsigned int led_last_value;#ifdef _SPI2_V1/* Output Pins:* RA0 - LED Display Latch Enable (PPS)* RA1 - LED Display CLK (PPS)* RA2 - LED Display DIN (PPS)* RA3 - LED Display Output Enable (PPS)*/#endif#ifdef _SPI2_V2/* Output Pins:* RA0 - LED Display CLK (PPS)* RA1 - LED Display DIN (PPS)* RA2 - LED Display Latch Enable (PPS)* RA3 - LED Display Output Enable (PPS)*/#endifvoid led_driver_init() {#ifdef _SPI2_V1TRISAbits.TRISA0 = 0; // LETRISAbits.TRISA1 = 0; // CLKTRISAbits.TRISA2 = 0; // DATTRISAbits.TRISA3 = 0; // OELATAbits.LATA0 = 0; // LELATAbits.LATA1 = 0; // CLKLATAbits.LATA2 = 0; // DATLATAbits.LATA3 = 0; // OE#endif#ifdef _SPI2_V2RPOR0 = 10; // CLKRPOR1 = 9; // DATAOpenSPI2(SPI_FOSC_4, MODE_00, SMPMID);#endifled_last_value = 0;led_driver_data(0);led_driver_data(0);}#ifdef _SPI2_V1void led_driver_clock() {LATAbits.LATA1 = 0x1; // Simple clock output toggleNop();LATAbits.LATA1 = 0x0;Nop();}#endifvoid led_driver_data(char val) {#ifdef _SPI2_V1int i;LATAbits.LATA0 = 0x0; // Set latch low to pause displayfor (i = 0; i < 8; i++) {LATAbits.LATA2 = val & 0x1; // Shift out bitsled_driver_clock();val >>= 1;}LATAbits.LATA0 = 0x1; // Set latch high to resume display#endif#ifdef _SPI2_V2LATAbits.LATA0 = 0x0;WriteSPI2(val);LATAbits.LATA0 = 0x1;#endif}void led_driver_num(unsigned char data) {unsigned char tmp = 0;led_last_value = 0;// Determine right character (1s digit)tmp = data % 10;switch (tmp) {case 0:led_last_value |= 0x0D70;break;case 1:led_last_value |= 0x0140;break;case 2:led_last_value |= 0x0E60;break;case 3:led_last_value |= 0x0760;break;case 4:led_last_value |= 0x0350;break;case 5:led_last_value |= 0x0730;break;case 6:led_last_value |= 0x0F30;break;case 7:led_last_value |= 0x0170;break;case 8:led_last_value |= 0x0F70;break;case 9:led_last_value |= 0x0770;break;}// Determine left character (10s digit)tmp = data / 10;switch (tmp) {case 0:led_last_value |= 0xE00D;break;case 1:led_last_value |= 0x2008;break;case 2:led_last_value |= 0xC00E;break;case 3:led_last_value |= 0x600E;break;case 4:led_last_value |= 0x200B;break;case 5:led_last_value |= 0x6007;break;case 6:led_last_value |= 0xE007;break;case 7:led_last_value |= 0x200D;break;case 8:led_last_value |= 0xE00F;break;case 9:led_last_value |= 0x600F;break;}led_driver_data(led_last_value & 0x00FF);led_driver_data((led_last_value & 0xFF00) >> 8);}void led_driver_show_last() {led_driver_data(led_last_value & 0x00FF);led_driver_data((led_last_value & 0xFF00) >> 8);}