| 283 |
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 enabled)
|
|
|
7 |
#pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled)
|
|
|
8 |
#pragma config MCLRE = OFF // 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 "PWM.h"
|
|
|
27 |
#include "I2C1.h"
|
|
|
28 |
#include "CONTROLLER.h"
|
|
|
29 |
|
|
|
30 |
void Pins_Init(void) {
|
|
|
31 |
// Set all pins to digital I/O
|
|
|
32 |
ANSELA = 0x0;
|
|
|
33 |
ANSELC = 0x0;
|
|
|
34 |
|
|
|
35 |
// // Enable weak pull-up if WPU bit is set
|
|
|
36 |
// OPTION_REGbits.nWPUEN = 0;
|
|
|
37 |
|
|
|
38 |
// CCP2 on RC3
|
|
|
39 |
APFCON1bits.CCP2SEL = 0;
|
|
|
40 |
|
|
|
41 |
LED_1_LAT = 1;
|
|
|
42 |
LED_1_TRIS = 0;
|
|
|
43 |
LED_2_LAT = 0;
|
|
|
44 |
LED_2_TRIS = 0;
|
|
|
45 |
|
|
|
46 |
CCP_1_LAT = 0;
|
|
|
47 |
CCP_1_TRIS = 0;
|
|
|
48 |
CCP_2_LAT = 0;
|
|
|
49 |
CCP_2_TRIS = 0;
|
|
|
50 |
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
void LED_1_On(void) {
|
|
|
54 |
LED_1_LAT = 0;
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
void LED_1_Off(void) {
|
|
|
58 |
LED_1_LAT = 1;
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
void LED_2_On(void) {
|
|
|
62 |
LED_2_LAT = 1;
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
void LED_2_Off(void) {
|
|
|
66 |
LED_2_LAT = 0;
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
int main(void) {
|
|
|
70 |
// Set internal oscillator speed to 32MHz
|
|
|
71 |
OSCCONbits.SPLLEN = 1; // 4x PLL enable (overwritten by config bits)
|
|
|
72 |
OSCCONbits.IRCF = 0xE; // Base frequency @ 8MHz
|
|
|
73 |
OSCCONbits.SCS = 0b00; // System clock determined by config bits
|
|
|
74 |
|
|
|
75 |
// Initialize I/O
|
|
|
76 |
Pins_Init();
|
|
|
77 |
|
|
|
78 |
// Initialize PWM
|
|
|
79 |
PWM_1_Init();
|
|
|
80 |
PWM_2_Init();
|
|
|
81 |
|
|
|
82 |
// Initialize I2C
|
|
|
83 |
I2C1_DATA i2c_data;
|
|
|
84 |
I2C1_Init(&i2c_data);
|
|
|
85 |
I2C1_Configure_Master(I2C_100KHZ);
|
|
|
86 |
|
|
|
87 |
Controller_Init();
|
|
|
88 |
|
|
|
89 |
// Initialize interrupts
|
|
|
90 |
Interrupt_Init();
|
|
|
91 |
Interrupt_Enable();
|
|
|
92 |
|
|
|
93 |
PWM_1_Set(PWM_NOMINAL);
|
|
|
94 |
PWM_2_Set(PWM_NOMINAL);
|
|
|
95 |
|
|
|
96 |
LED_1_On();
|
|
|
97 |
LED_2_On();
|
|
|
98 |
|
|
|
99 |
__delay_ms(1000);
|
|
|
100 |
|
|
|
101 |
LED_1_Off();
|
|
|
102 |
LED_2_Off();
|
|
|
103 |
|
|
|
104 |
CTRL_BTN_STATUS ctrl;
|
|
|
105 |
Controller_Read(&ctrl);
|
|
|
106 |
|
|
|
107 |
uint8_t median;
|
|
|
108 |
int16_t L_X, L_Y, R_X, R_Y;
|
|
|
109 |
uint16_t pwm_left = PWM_NOMINAL;
|
|
|
110 |
uint16_t pwm_right = PWM_NOMINAL;
|
|
|
111 |
|
|
|
112 |
while (1) {
|
|
|
113 |
if (!Controller_Read(&ctrl)) {
|
|
|
114 |
median = ctrl.REF / 2;
|
|
|
115 |
L_X = (int16_t)median - (int16_t)ctrl.L_X_CH;
|
|
|
116 |
L_Y = (int16_t)median - (int16_t)ctrl.L_Y_CH;
|
|
|
117 |
R_X = (int16_t)median - (int16_t)ctrl.R_X_CH;
|
|
|
118 |
R_Y = (int16_t)median - (int16_t)ctrl.R_Y_CH;
|
|
|
119 |
|
|
|
120 |
// Left stick = throttle, Right stick = steering
|
|
|
121 |
// if (L_X > -5 && L_X < 5 &&
|
|
|
122 |
// R_Y > -5 && R_Y < 5) {
|
|
|
123 |
// pwm_left = PWM_NOMINAL;
|
|
|
124 |
// pwm_right = PWM_NOMINAL;
|
|
|
125 |
// } else {
|
|
|
126 |
// pwm_left = PWM_NOMINAL + (L_X * 4);
|
|
|
127 |
// pwm_right = PWM_NOMINAL + (L_X * 4);
|
|
|
128 |
//
|
|
|
129 |
// pwm_left += R_Y * 4;
|
|
|
130 |
// pwm_right -= R_Y * 4;
|
|
|
131 |
// }
|
|
|
132 |
|
|
|
133 |
// Left stick = left control, Right stick = right control
|
|
|
134 |
if (L_X > -5 && L_X < 5 &&
|
|
|
135 |
R_X > -5 && R_X < 5) {
|
|
|
136 |
pwm_left = PWM_NOMINAL;
|
|
|
137 |
pwm_right = PWM_NOMINAL;
|
|
|
138 |
} else {
|
|
|
139 |
pwm_left = PWM_NOMINAL + (R_X * 4);
|
|
|
140 |
pwm_right = PWM_NOMINAL + (L_X * 4);
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
if (L_X < -5 || L_X > 5) LED_2_On();
|
|
|
144 |
else LED_2_Off();
|
|
|
145 |
|
|
|
146 |
if (R_X < -5 || R_X > 5) LED_1_On();
|
|
|
147 |
else LED_1_Off();
|
|
|
148 |
|
|
|
149 |
|
|
|
150 |
if (pwm_left > PWM_MAX)
|
|
|
151 |
pwm_left = PWM_MAX;
|
|
|
152 |
if (pwm_left < PWM_MIN)
|
|
|
153 |
pwm_left = PWM_MIN;
|
|
|
154 |
|
|
|
155 |
if (pwm_right > PWM_MAX)
|
|
|
156 |
pwm_right = PWM_MAX;
|
|
|
157 |
if (pwm_right < PWM_MIN)
|
|
|
158 |
pwm_right = PWM_MIN;
|
|
|
159 |
|
|
|
160 |
PWM_1_Set(pwm_left);
|
|
|
161 |
PWM_2_Set(pwm_right);
|
|
|
162 |
} else {
|
|
|
163 |
PWM_1_Set(PWM_NOMINAL);
|
|
|
164 |
PWM_2_Set(PWM_NOMINAL);
|
|
|
165 |
|
|
|
166 |
LED_1_Off();
|
|
|
167 |
LED_2_Off();
|
|
|
168 |
}
|
|
|
169 |
}
|
|
|
170 |
}
|