Subversion Repositories Code-Repo

Compare Revisions

Ignore whitespace Rev 122 → Rev 123

/PIC Stuff/PIC_27J13/main.c
8,6 → 8,7
#include "led_backpack.h"
#include "oled_ssd1306.h"
#include "oled_ssd1331.h"
#include "adc.h"
#include <usart.h>
#include <delays.h>
#include <string.h>
19,13 → 20,13
#pragma config CFGPLLEN = ON // Enable PLL on startup
#pragma config PLLSEL = PLL96 // Use 96MHz PLL 4MHz -> 96MHz / 2 = 48MHz
//#pragma config SOSCSEL = HIGH // High Power T1OSC/SOSC circuit selected
//#pragma config ADCSEL = BIT12 // 12-bit ADrC
#pragma config ADCSEL = BIT12 // 12-bit ADrC
//#pragma config IOL1WAY = OFF // IOLOCK bit can be set and cleared as needed
 
/* ----------- IO Pins -----------
* RA0 -
* RA1 -
* RA2 -
* RA0 - A/D X
* RA1 - A/D Y
* RA2 - A/D Z
* RA3 -
* RA4 - [CANNOT BE USED (VDDCORE/VCAP)]
* RA5 -
632,92 → 633,71
}
#endif
 
#ifdef _TEST_ADC
 
void main(void) {
unsigned int x,y,z;
unsigned char buffer[60];
 
/* --------------------- Oscillator Configuration --------------------- */
// OSCTUNEbits.PLLEN = 1; // Enable 4x PLL
OSCCONbits.IRCF = 0b111; // Set INTOSC postscaler to 8MHz
OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
/* -------------------------------------------------------------------- */
 
// Set all ports as digial I/O except for AN0-AN2 (pins 2-4)
ANCON0 = 0xF8;
ANCON1 = 0x1F;
 
UART1_Init(); // Initialize the UART handler code
SPI2_Init(SPI2_FOSC_8); // Initialize the SPI module
SSD1331_Init(); // Initialize the SSD1331 OLED display (uses SPI2)
ADC_Init(ADC_TAD_0, ADC_FOSC_64);
 
// I2C_Configure_Master(I2C_400KHZ);
SSD1331_Begin();
 
interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
interrupt_init(); // Initialize the interrupt priorities
 
DBG_PRINT_MAIN("\r\nBegin Program\r\n");
 
memset(buffer, 0, 60);
GFX_clearScreen();
GFX_setRotation(3);
 
while (1) {
// ADC read from AN0-AN2 and prints to display
ADC_Start(ADC_CHANNEL_AN0);
GFX_fillRect(0,0,GFX_width(),8,SSD1331_BLACK);
GFX_setCursor(0,0);
while (!ADC_Get_Result(&x));
GFX_writeString("X: %u", x);
 
ADC_Start(ADC_CHANNEL_AN1);
GFX_fillRect(0,8,GFX_width(),8,SSD1331_BLACK);
GFX_setCursor(0,8);
while (!ADC_Get_Result(&y));
GFX_writeString("Y: %u", y);
 
ADC_Start(ADC_CHANNEL_AN2);
GFX_fillRect(0,16,GFX_width(),8,SSD1331_BLACK);
GFX_setCursor(0,16);
while (!ADC_Get_Result(&z));
GFX_writeString("Z: %u", z);
}
}
 
#endif
 
#if !defined(_TEST_UART) && !defined(_TEST_I2C_MASTER) && \
!defined(_TEST_I2C_SLAVE) && !defined(_TEST_SPI) && \
!defined(_TEST_NFC) && !defined(_TEST_LED_BACKPACK) && \
!defined(_TEST_SSD1306_OLED) && !defined(_TEST_SSD1331_OLED)
!defined(_TEST_SSD1306_OLED) && !defined(_TEST_SSD1331_OLED) && \
!defined(_TEST_ADC)
 
//static unsigned char lcd_buffer[8][17];
//
//void main(void) {
// char i = 0;
// char j = 0;
// unsigned char length = 0;
// unsigned char buffer[60];
// char buffer_ind = 0;
//
// /* --------------------- Oscillator Configuration --------------------- */
// // OSCTUNEbits.PLLEN = 1; // Enable 4x PLL
// OSCCONbits.IRCF = 0b111; // Set INTOSC postscaler to 8MHz
// OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
// /* -------------------------------------------------------------------- */
//
// // Set all ports as digial I/O
// ANCON0 = 0xFF;
// ANCON1 = 0x1F;
//
// UART1_Init(); // Initialize the UART handler code
// // I2C_Init(); // Initialize the I2C handler code
// SPI2_Init(SPI2_FOSC_4); // Initialize the SPI module
// SSD1331_Init(); // Initialize the SSD1331 OLED display (uses SPI2)
// // NFC_Init(); // Initialize the NFC chip (uses I2C)
// // LED_Init(); // Initialize the LED backpack (uses I2C);
//
// // I2C_Configure_Master(I2C_400KHZ);
// SSD1331_Begin();
//
// interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
// interrupt_init(); // Initialize the interrupt priorities
//
// DBG_PRINT_MAIN("\r\nBegin Program\r\n");
//
// memset(lcd_buffer, 0, 136);
// GFX_clearScreen();
//
// while (1) {
// length = UART1_Read_Buffer(buffer);
// if (length != 0) {
// // Save string into lcd_buffer at location buffer_ind
// if (length < 17) { // If string is < 16 char + \n
// for (i = 0; i < length; i++) // Copy everything including \n
// lcd_buffer[buffer_ind][i] = buffer[i];
// lcd_buffer[buffer_ind][length] = 0;
// } else {
// for (i = 0; i < 16; i++) // Copy the first 16 char
// lcd_buffer[buffer_ind][i] = buffer[i];
// lcd_buffer[buffer_ind][16] = 0;
// }
//
//// for (i = 0; i < 8; i++) {
//// for (j = 0; j < 17; j++) {
//// DBG_PRINT_MAIN("%c", lcd_buffer[i][j]);
//// }
//// DBG_PRINT_MAIN("\r\n");
//// }
//// DBG_PRINT_MAIN("\r\n");
//
// // Print strings in lcd_buffer to display starting at buffer_ind
// GFX_clearScreen();
// GFX_setCursor(0, 0);
// for (i = 0; i < 8; i++) {
// j = buffer_ind - i;
// if (j >= 0) {
// GFX_writeString("%s", lcd_buffer[j]);
// } else {
// GFX_writeString("%s", lcd_buffer[j + 8]);
// }
// }
//
// buffer_ind++;
// if (buffer_ind > 7)
// buffer_ind = 0;
// }
// }
//}
 
void main(void) {
unsigned char i = 0;
unsigned char j = 0;
unsigned int x,y,z;
unsigned char length = 0;
unsigned char buffer[60];
 
727,8 → 707,8
OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
/* -------------------------------------------------------------------- */
 
// Set all ports as digial I/O
ANCON0 = 0xFF;
// Set all ports as digial I/O except for AN0-AN2 (pins 2-4)
ANCON0 = 0xF8;
ANCON1 = 0x1F;
 
UART1_Init(); // Initialize the UART handler code
736,7 → 716,8
SPI2_Init(SPI2_FOSC_8); // Initialize the SPI module
SSD1331_Init(); // Initialize the SSD1331 OLED display (uses SPI2)
// NFC_Init(); // Initialize the NFC chip (uses I2C)
// LED_Init(); // Initialize the LED backpack (uses I2C);
// LED_Init(); // Initialize the LED backpack (uses I2C)
ADC_Init(ADC_TAD_0, ADC_FOSC_64);
 
// I2C_Configure_Master(I2C_400KHZ);
SSD1331_Begin();
752,11 → 733,31
 
while (1) {
 
length = UART1_Read_Buffer(buffer);
if (length != 0) {
buffer[length] = 0;
GFX_appendString("%s", buffer);
}
// Reads input from UART and prints to display
// length = UART1_Read_Buffer(buffer);
// if (length != 0) {
// buffer[length] = 0;
// GFX_appendString("%s", buffer);
// }
 
// ADC read from AN0-AN2 and prints to display
ADC_Start(ADC_CHANNEL_AN0);
GFX_fillRect(0,0,GFX_width(),8,SSD1331_BLACK);
GFX_setCursor(0,0);
while (!ADC_Get_Result(&x));
GFX_writeString("X: %u", x);
 
ADC_Start(ADC_CHANNEL_AN1);
GFX_fillRect(0,8,GFX_width(),8,SSD1331_BLACK);
GFX_setCursor(0,8);
while (!ADC_Get_Result(&y));
GFX_writeString("Y: %u", y);
 
ADC_Start(ADC_CHANNEL_AN2);
GFX_fillRect(0,16,GFX_width(),8,SSD1331_BLACK);
GFX_setCursor(0,16);
while (!ADC_Get_Result(&z));
GFX_writeString("Z: %u", z);
}
}
#endif