| 284 |
Kevin |
1 |
// <editor-fold defaultstate="collapsed" desc="Configuration Bits">
|
|
|
2 |
// PIC16F1825 Configuration Bit Settings
|
|
|
3 |
|
|
|
4 |
// CONFIG1
|
|
|
5 |
#pragma config FOSC = INTOSC // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)
|
|
|
6 |
#pragma config WDTE = OFF // Watchdog Timer Enable (WDT software controlled)
|
|
|
7 |
#pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled)
|
|
|
8 |
#pragma config MCLRE = ON // MCLR Pin Function Select (MCLR/VPP pin function is digital input)
|
|
|
9 |
#pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled)
|
|
|
10 |
#pragma config CPD = OFF // Data Memory Code Protection (Data memory code protection is disabled)
|
|
|
11 |
#pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled)
|
|
|
12 |
#pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
|
|
|
13 |
#pragma config IESO = ON // Internal/External Switchover (Internal/External Switchover mode is enabled)
|
|
|
14 |
#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)
|
|
|
15 |
|
|
|
16 |
// CONFIG2
|
|
|
17 |
#pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)
|
|
|
18 |
#pragma config PLLEN = ON // PLL Enable (4x PLL enabled)
|
|
|
19 |
#pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
|
|
|
20 |
#pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
|
|
|
21 |
#pragma config LVP = OFF // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)
|
|
|
22 |
// </editor-fold>
|
|
|
23 |
|
|
|
24 |
#include "defines.h"
|
|
|
25 |
#include "INTERRUPTS.h"
|
|
|
26 |
#include "I2C1.h"
|
|
|
27 |
#include "ADC.h"
|
|
|
28 |
|
|
|
29 |
uint8_t btn_values[6];
|
|
|
30 |
|
|
|
31 |
void IO_Init(void) {
|
|
|
32 |
ANSELA = 0x0;
|
|
|
33 |
ANSELB = 0x0;
|
|
|
34 |
ANSELC = 0x0;
|
|
|
35 |
|
|
|
36 |
WPUA = 0x0;
|
|
|
37 |
WPUB = 0x0;
|
|
|
38 |
WPUC = 0x0;
|
|
|
39 |
|
|
|
40 |
OPTION_REGbits.nWPUEN = 0;
|
|
|
41 |
|
|
|
42 |
I2C_1_CLK_TRIS = 1;
|
|
|
43 |
I2C_1_DAT_TRIS = 1;
|
|
|
44 |
|
|
|
45 |
ANALOG_L_BTN_TRIS = 1;
|
|
|
46 |
ANALOG_R_BTN_TRIS = 1;
|
|
|
47 |
|
|
|
48 |
ANALOG_L_BTN_WPU = 1;
|
|
|
49 |
ANALOG_R_BTN_WPU = 1;
|
|
|
50 |
|
|
|
51 |
ANALOG_L_X_TRIS = 1;
|
|
|
52 |
ANALOG_L_Y_TRIS = 1;
|
|
|
53 |
|
|
|
54 |
ANALOG_R_X_TRIS = 1;
|
|
|
55 |
ANALOG_R_Y_TRIS = 1;
|
|
|
56 |
|
|
|
57 |
ANALOG_L_X_ANSEL = 1;
|
|
|
58 |
ANALOG_L_Y_ANSEL = 1;
|
|
|
59 |
|
|
|
60 |
ANALOG_R_X_ANSEL = 1;
|
|
|
61 |
ANALOG_R_Y_ANSEL = 1;
|
|
|
62 |
|
|
|
63 |
LED_A_LAT = 0;
|
|
|
64 |
LED_B_LAT = 0;
|
|
|
65 |
LED_A_TRIS = 0;
|
|
|
66 |
LED_B_TRIS = 0;
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
void LED_On(void) {
|
|
|
70 |
LED_A_LAT = 1;
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
void LED_Off(void) {
|
|
|
74 |
LED_A_LAT = 0;
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
int main(void) {
|
|
|
78 |
|
|
|
79 |
// Set internal oscillator speed to 32MHz
|
|
|
80 |
OSCCONbits.SPLLEN = 1; // 4x PLL enable (overwritten by config bits)
|
|
|
81 |
OSCCONbits.IRCF = 0xE; // Base frequency @ 8MHz
|
|
|
82 |
OSCCONbits.SCS = 0b00; // System clock determined by config bits
|
|
|
83 |
|
|
|
84 |
// Initialize I/O
|
|
|
85 |
IO_Init();
|
|
|
86 |
|
|
|
87 |
// Delay a bit to allow I2C lines to stabilize
|
|
|
88 |
__delay_ms(10);
|
|
|
89 |
|
|
|
90 |
// Initialize I2C1 in slave mode
|
|
|
91 |
I2C1_DATA i2c1_data;
|
|
|
92 |
I2C1_Init(&i2c1_data);
|
|
|
93 |
I2C1_Configure_Slave(I2C1_SLAVE_ADDR);
|
|
|
94 |
|
|
|
95 |
// Initialize ADC
|
|
|
96 |
ADC_Init();
|
|
|
97 |
|
|
|
98 |
// Initialize interrupts
|
|
|
99 |
Interrupt_Init();
|
|
|
100 |
Interrupt_Enable();
|
|
|
101 |
|
|
|
102 |
LED_On();
|
|
|
103 |
__delay_ms(1000);
|
|
|
104 |
LED_Off();
|
|
|
105 |
|
|
|
106 |
uint8_t buffer[8];
|
|
|
107 |
uint8_t length, result;
|
|
|
108 |
|
|
|
109 |
while (1) {
|
|
|
110 |
btn_values[0] = ADC_Read(ANALOG_REF_CH) >> 2; // 0xFF
|
|
|
111 |
btn_values[1] = ADC_Read(ANALOG_L_X_CH) >> 2; // 0x7E (-1)
|
|
|
112 |
btn_values[2] = ADC_Read(ANALOG_L_Y_CH) >> 2; // 0x7C (-3)
|
|
|
113 |
btn_values[3] = ADC_Read(ANALOG_R_X_CH) >> 2; // 0x81 (+2)
|
|
|
114 |
btn_values[4] = ADC_Read(ANALOG_R_Y_CH) >> 2; // 0x7D (-2)
|
|
|
115 |
btn_values[5] = (ANALOG_L_BTN_PORT << 1) | ANALOG_R_BTN_PORT;
|
|
|
116 |
|
|
|
117 |
// // Check if an I2C message was received
|
|
|
118 |
// result = I2C1_Get_Status();
|
|
|
119 |
// if (result) {
|
|
|
120 |
// length = I2C1_Read_Buffer(buffer);
|
|
|
121 |
// if (length == 2 && buffer[0] == CMD_SET_LEDS) {
|
|
|
122 |
// if (buffer[1])
|
|
|
123 |
// LED_On();
|
|
|
124 |
// else
|
|
|
125 |
// LED_Off();
|
|
|
126 |
// }
|
|
|
127 |
// }
|
|
|
128 |
}
|
|
|
129 |
}
|