Subversion Repositories Code-Repo

Rev

Rev 122 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 122 Rev 123
Line 6... Line 6...
6
#include "spi.h"
6
#include "spi.h"
7
#include "nfc.h"
7
#include "nfc.h"
8
#include "led_backpack.h"
8
#include "led_backpack.h"
9
#include "oled_ssd1306.h"
9
#include "oled_ssd1306.h"
10
#include "oled_ssd1331.h"
10
#include "oled_ssd1331.h"
-
 
11
#include "adc.h"
11
#include <usart.h>
12
#include <usart.h>
12
#include <delays.h>
13
#include <delays.h>
13
#include <string.h>
14
#include <string.h>
14
 
15
 
15
#pragma config WDTEN = OFF          // Turn off watchdog timer
16
#pragma config WDTEN = OFF          // Turn off watchdog timer
Line 17... Line 18...
17
#pragma config OSC = HSPLL          // Use external oscillator (101)
18
#pragma config OSC = HSPLL          // Use external oscillator (101)
18
#pragma config PLLDIV = 3           // Set PPL prescaler to 3 (to get 4MHz)
19
#pragma config PLLDIV = 3           // Set PPL prescaler to 3 (to get 4MHz)
19
#pragma config CFGPLLEN = ON        // Enable PLL on startup
20
#pragma config CFGPLLEN = ON        // Enable PLL on startup
20
#pragma config PLLSEL = PLL96       // Use 96MHz PLL 4MHz -> 96MHz / 2 = 48MHz
21
#pragma config PLLSEL = PLL96       // Use 96MHz PLL 4MHz -> 96MHz / 2 = 48MHz
21
//#pragma config SOSCSEL = HIGH       // High Power T1OSC/SOSC circuit selected
22
//#pragma config SOSCSEL = HIGH       // High Power T1OSC/SOSC circuit selected
22
//#pragma config ADCSEL = BIT12       // 12-bit ADrC
23
#pragma config ADCSEL = BIT12       // 12-bit ADrC
23
//#pragma config IOL1WAY = OFF        // IOLOCK bit can be set and cleared as needed
24
//#pragma config IOL1WAY = OFF        // IOLOCK bit can be set and cleared as needed
24
 
25
 
25
/* ----------- IO Pins -----------
26
/* ----------- IO Pins -----------
26
 * RA0 - 
27
 * RA0 - A/D X
27
 * RA1 - 
28
 * RA1 - A/D Y
28
 * RA2 - 
29
 * RA2 - A/D Z
29
 * RA3 - 
30
 * RA3 - 
30
 * RA4 - [CANNOT BE USED (VDDCORE/VCAP)]
31
 * RA4 - [CANNOT BE USED (VDDCORE/VCAP)]
31
 * RA5 - 
32
 * RA5 - 
32
 * RA6 - Oscillator
33
 * RA6 - Oscillator
33
 * RA7 - Oscillator
34
 * RA7 - Oscillator
Line 630... Line 631...
630
        //        i++;
631
        //        i++;
631
    }
632
    }
632
}
633
}
633
#endif
634
#endif
634
 
635
 
-
 
636
#ifdef _TEST_ADC
-
 
637
 
-
 
638
void main(void) {
-
 
639
    unsigned int x,y,z;
-
 
640
    unsigned char buffer[60];
-
 
641
 
-
 
642
    /* --------------------- Oscillator Configuration --------------------- */
-
 
643
    //    OSCTUNEbits.PLLEN = 1;          // Enable 4x PLL
-
 
644
    OSCCONbits.IRCF = 0b111; // Set INTOSC postscaler to 8MHz
-
 
645
    OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
-
 
646
    /* -------------------------------------------------------------------- */
-
 
647
 
-
 
648
    // Set all ports as digial I/O except for AN0-AN2 (pins 2-4)
-
 
649
    ANCON0 = 0xF8;
-
 
650
    ANCON1 = 0x1F;
-
 
651
 
-
 
652
    UART1_Init(); // Initialize the UART handler code
-
 
653
    SPI2_Init(SPI2_FOSC_8); // Initialize the SPI module
-
 
654
    SSD1331_Init(); // Initialize the SSD1331 OLED display (uses SPI2)
-
 
655
    ADC_Init(ADC_TAD_0, ADC_FOSC_64);
-
 
656
 
-
 
657
    //    I2C_Configure_Master(I2C_400KHZ);
-
 
658
    SSD1331_Begin();
-
 
659
 
-
 
660
    interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
-
 
661
    interrupt_init(); // Initialize the interrupt priorities
-
 
662
 
-
 
663
    DBG_PRINT_MAIN("\r\nBegin Program\r\n");
-
 
664
 
-
 
665
    memset(buffer, 0, 60);
-
 
666
    GFX_clearScreen();
-
 
667
    GFX_setRotation(3);
-
 
668
 
-
 
669
    while (1) {
-
 
670
        // ADC read from AN0-AN2 and prints to display
-
 
671
        ADC_Start(ADC_CHANNEL_AN0);
-
 
672
        GFX_fillRect(0,0,GFX_width(),8,SSD1331_BLACK);
-
 
673
        GFX_setCursor(0,0);
-
 
674
        while (!ADC_Get_Result(&x));
-
 
675
        GFX_writeString("X: %u", x);
-
 
676
 
-
 
677
        ADC_Start(ADC_CHANNEL_AN1);
-
 
678
        GFX_fillRect(0,8,GFX_width(),8,SSD1331_BLACK);
-
 
679
        GFX_setCursor(0,8);
-
 
680
        while (!ADC_Get_Result(&y));
-
 
681
        GFX_writeString("Y: %u", y);
-
 
682
 
-
 
683
        ADC_Start(ADC_CHANNEL_AN2);
-
 
684
        GFX_fillRect(0,16,GFX_width(),8,SSD1331_BLACK);
-
 
685
        GFX_setCursor(0,16);
-
 
686
        while (!ADC_Get_Result(&z));
-
 
687
        GFX_writeString("Z: %u", z);
-
 
688
    }
-
 
689
}
-
 
690
 
-
 
691
#endif
-
 
692
 
635
#if !defined(_TEST_UART) && !defined(_TEST_I2C_MASTER) && \
693
#if !defined(_TEST_UART) && !defined(_TEST_I2C_MASTER) && \
636
    !defined(_TEST_I2C_SLAVE) && !defined(_TEST_SPI) && \
694
    !defined(_TEST_I2C_SLAVE) && !defined(_TEST_SPI) && \
637
    !defined(_TEST_NFC) && !defined(_TEST_LED_BACKPACK) && \
695
    !defined(_TEST_NFC) && !defined(_TEST_LED_BACKPACK) && \
638
    !defined(_TEST_SSD1306_OLED) && !defined(_TEST_SSD1331_OLED)
696
    !defined(_TEST_SSD1306_OLED) && !defined(_TEST_SSD1331_OLED) && \
639
 
-
 
640
//static unsigned char lcd_buffer[8][17];
-
 
641
//
-
 
642
//void main(void) {
-
 
643
//    char i = 0;
-
 
644
//    char j = 0;
-
 
645
//    unsigned char length = 0;
-
 
646
//    unsigned char buffer[60];
-
 
647
//    char buffer_ind = 0;
-
 
648
//
-
 
649
//    /* --------------------- Oscillator Configuration --------------------- */
-
 
650
//    //    OSCTUNEbits.PLLEN = 1;          // Enable 4x PLL
-
 
651
//    OSCCONbits.IRCF = 0b111; // Set INTOSC postscaler to 8MHz
-
 
652
//    OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
-
 
653
//    /* -------------------------------------------------------------------- */
-
 
654
//
-
 
655
//    // Set all ports as digial I/O
-
 
656
//    ANCON0 = 0xFF;
-
 
657
//    ANCON1 = 0x1F;
-
 
658
//
-
 
659
//    UART1_Init(); // Initialize the UART handler code
-
 
660
//    //    I2C_Init(); // Initialize the I2C handler code
-
 
661
//    SPI2_Init(SPI2_FOSC_4); // Initialize the SPI module
-
 
662
//    SSD1331_Init(); // Initialize the SSD1331 OLED display (uses SPI2)
-
 
663
//    //    NFC_Init(); // Initialize the NFC chip (uses I2C)
-
 
664
//    //    LED_Init(); // Initialize the LED backpack (uses I2C);
-
 
665
//
-
 
666
//    //    I2C_Configure_Master(I2C_400KHZ);
-
 
667
//    SSD1331_Begin();
-
 
668
//
-
 
669
//    interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
-
 
670
//    interrupt_init(); // Initialize the interrupt priorities
-
 
671
//
-
 
672
//    DBG_PRINT_MAIN("\r\nBegin Program\r\n");
-
 
673
//
-
 
674
//    memset(lcd_buffer, 0, 136);
-
 
675
//    GFX_clearScreen();
697
    !defined(_TEST_ADC)
676
//
-
 
677
//    while (1) {
-
 
678
//        length = UART1_Read_Buffer(buffer);
-
 
679
//        if (length != 0) {
-
 
680
//            // Save string into lcd_buffer at location buffer_ind
-
 
681
//            if (length < 17) { // If string is < 16 char + \n
-
 
682
//                for (i = 0; i < length; i++) // Copy everything including \n
-
 
683
//                    lcd_buffer[buffer_ind][i] = buffer[i];
-
 
684
//                lcd_buffer[buffer_ind][length] = 0;
-
 
685
//            } else {
-
 
686
//                for (i = 0; i < 16; i++) // Copy the first 16 char
-
 
687
//                    lcd_buffer[buffer_ind][i] = buffer[i];
-
 
688
//                lcd_buffer[buffer_ind][16] = 0;
-
 
689
//            }
-
 
690
//
-
 
691
////            for (i = 0; i < 8; i++) {
-
 
692
////                for (j = 0; j < 17; j++) {
-
 
693
////                    DBG_PRINT_MAIN("%c", lcd_buffer[i][j]);
-
 
694
////                }
-
 
695
////                DBG_PRINT_MAIN("\r\n");
-
 
696
////            }
-
 
697
////            DBG_PRINT_MAIN("\r\n");
-
 
698
//
-
 
699
//            // Print strings in lcd_buffer to display starting at buffer_ind
-
 
700
//            GFX_clearScreen();
-
 
701
//            GFX_setCursor(0, 0);
-
 
702
//            for (i = 0; i < 8; i++) {
-
 
703
//                j = buffer_ind - i;
-
 
704
//                if (j >= 0) {
-
 
705
//                    GFX_writeString("%s", lcd_buffer[j]);
-
 
706
//                } else {
-
 
707
//                    GFX_writeString("%s", lcd_buffer[j + 8]);
-
 
708
//                }
-
 
709
//            }
-
 
710
//
-
 
711
//            buffer_ind++;
-
 
712
//            if (buffer_ind > 7)
-
 
713
//                buffer_ind = 0;
-
 
714
//        }
-
 
715
//    }
-
 
716
//}
-
 
717
 
698
 
718
void main(void) {
699
void main(void) {
719
    unsigned char i = 0;
-
 
720
    unsigned char j = 0;
700
    unsigned int x,y,z;
721
    unsigned char length = 0;
701
    unsigned char length = 0;
722
    unsigned char buffer[60];
702
    unsigned char buffer[60];
723
 
703
 
724
    /* --------------------- Oscillator Configuration --------------------- */
704
    /* --------------------- Oscillator Configuration --------------------- */
725
    //    OSCTUNEbits.PLLEN = 1;          // Enable 4x PLL
705
    //    OSCTUNEbits.PLLEN = 1;          // Enable 4x PLL
726
    OSCCONbits.IRCF = 0b111; // Set INTOSC postscaler to 8MHz
706
    OSCCONbits.IRCF = 0b111; // Set INTOSC postscaler to 8MHz
727
    OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
707
    OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
728
    /* -------------------------------------------------------------------- */
708
    /* -------------------------------------------------------------------- */
729
 
709
 
730
    // Set all ports as digial I/O
710
    // Set all ports as digial I/O except for AN0-AN2 (pins 2-4)
731
    ANCON0 = 0xFF;
711
    ANCON0 = 0xF8;
732
    ANCON1 = 0x1F;
712
    ANCON1 = 0x1F;
733
 
713
 
734
    UART1_Init(); // Initialize the UART handler code
714
    UART1_Init(); // Initialize the UART handler code
735
    //    I2C_Init(); // Initialize the I2C handler code
715
    //    I2C_Init(); // Initialize the I2C handler code
736
    SPI2_Init(SPI2_FOSC_8); // Initialize the SPI module
716
    SPI2_Init(SPI2_FOSC_8); // Initialize the SPI module
737
    SSD1331_Init(); // Initialize the SSD1331 OLED display (uses SPI2)
717
    SSD1331_Init(); // Initialize the SSD1331 OLED display (uses SPI2)
738
    //    NFC_Init(); // Initialize the NFC chip (uses I2C)
718
    //    NFC_Init(); // Initialize the NFC chip (uses I2C)
739
    //    LED_Init(); // Initialize the LED backpack (uses I2C);
719
    //    LED_Init(); // Initialize the LED backpack (uses I2C)
-
 
720
    ADC_Init(ADC_TAD_0, ADC_FOSC_64);
740
 
721
 
741
    //    I2C_Configure_Master(I2C_400KHZ);
722
    //    I2C_Configure_Master(I2C_400KHZ);
742
    SSD1331_Begin();
723
    SSD1331_Begin();
743
 
724
 
744
    interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
725
    interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
Line 750... Line 731...
750
    GFX_clearScreen();
731
    GFX_clearScreen();
751
    GFX_setRotation(3);
732
    GFX_setRotation(3);
752
 
733
 
753
    while (1) {
734
    while (1) {
754
 
735
 
-
 
736
        // Reads input from UART and prints to display
755
        length = UART1_Read_Buffer(buffer);
737
//        length = UART1_Read_Buffer(buffer);
756
        if (length != 0) {
738
//        if (length != 0) {
757
            buffer[length] = 0;
739
//            buffer[length] = 0;
758
            GFX_appendString("%s", buffer);
740
//            GFX_appendString("%s", buffer);
759
        }
741
//        }
-
 
742
 
-
 
743
        // ADC read from AN0-AN2 and prints to display
-
 
744
        ADC_Start(ADC_CHANNEL_AN0);
-
 
745
        GFX_fillRect(0,0,GFX_width(),8,SSD1331_BLACK);
-
 
746
        GFX_setCursor(0,0);
-
 
747
        while (!ADC_Get_Result(&x));
-
 
748
        GFX_writeString("X: %u", x);
-
 
749
 
-
 
750
        ADC_Start(ADC_CHANNEL_AN1);
-
 
751
        GFX_fillRect(0,8,GFX_width(),8,SSD1331_BLACK);
-
 
752
        GFX_setCursor(0,8);
-
 
753
        while (!ADC_Get_Result(&y));
-
 
754
        GFX_writeString("Y: %u", y);
-
 
755
 
-
 
756
        ADC_Start(ADC_CHANNEL_AN2);
-
 
757
        GFX_fillRect(0,16,GFX_width(),8,SSD1331_BLACK);
-
 
758
        GFX_setCursor(0,16);
-
 
759
        while (!ADC_Get_Result(&z));
-
 
760
        GFX_writeString("Z: %u", z);
760
    }
761
    }
761
}
762
}
762
#endif
763
#endif