Subversion Repositories Code-Repo

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
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"
285 Kevin 28
#include "UART.h"
283 Kevin 29
#include "CONTROLLER.h"
30
 
31
void Pins_Init(void) {
32
    // Set all pins to digital I/O
33
    ANSELA = 0x0;
34
    ANSELC = 0x0;
35
 
36
//    // Enable weak pull-up if WPU bit is set
37
//    OPTION_REGbits.nWPUEN = 0;
38
 
39
    // CCP2 on RC3
40
    APFCON1bits.CCP2SEL = 0;
285 Kevin 41
 
42
    // TX/CK function on RA0
43
    APFCON0bits.TXCKSEL = 1;
44
    // RX/DT function on RA1
45
    APFCON0bits.RXDTSEL = 1;
283 Kevin 46
 
47
    LED_1_LAT  = 1;
48
    LED_1_TRIS = 0;
49
    LED_2_LAT  = 0;
50
    LED_2_TRIS = 0;
51
 
52
    CCP_1_LAT  = 0;
53
    CCP_1_TRIS = 0;
54
    CCP_2_LAT  = 0;
55
    CCP_2_TRIS = 0;
56
 
57
}
58
 
59
void LED_1_On(void) {
60
    LED_1_LAT = 0;
61
}
62
 
63
void LED_1_Off(void) {
64
    LED_1_LAT = 1;
65
}
66
 
67
void LED_2_On(void) {
68
    LED_2_LAT = 1;
69
}
70
 
71
void LED_2_Off(void) {
72
    LED_2_LAT = 0;
73
}
74
 
75
int main(void) {
76
    // Set internal oscillator speed to 32MHz
77
    OSCCONbits.SPLLEN = 1;  // 4x PLL enable (overwritten by config bits)
78
    OSCCONbits.IRCF = 0xE;  // Base frequency @ 8MHz
79
    OSCCONbits.SCS = 0b00;  // System clock determined by config bits
80
 
81
    // Initialize I/O
82
    Pins_Init();
83
 
84
    // Initialize PWM
85
    PWM_1_Init();
86
    PWM_2_Init();
87
 
88
    // Initialize I2C
89
    I2C1_DATA i2c_data;
90
    I2C1_Init(&i2c_data);
91
    I2C1_Configure_Master(I2C_100KHZ);
92
 
285 Kevin 93
    UART_DATA uart_data;
94
    UART_Init(&uart_data);
95
 
283 Kevin 96
    Controller_Init();
97
 
98
    // Initialize interrupts
99
    Interrupt_Init();
100
    Interrupt_Enable();
101
 
102
    PWM_1_Set(PWM_NOMINAL);
103
    PWM_2_Set(PWM_NOMINAL);
104
 
105
    LED_1_On();
106
    LED_2_On();
107
 
108
    __delay_ms(1000);
109
 
110
    LED_1_Off();
111
    LED_2_Off();
112
 
113
    CTRL_BTN_STATUS ctrl;
114
    Controller_Read(&ctrl);
115
 
116
    uint16_t pwm_left = PWM_NOMINAL;
117
    uint16_t pwm_right = PWM_NOMINAL;
118
 
285 Kevin 119
#ifdef CONTROL_FROM_UART
120
    uint8_t buffer[32];
121
    uint8_t length;
122
    uint8_t uart_state = UART_STATE_READ_CMD;
123
    uint8_t uart_cmd;
124
    while(1) {
125
        length = UART_Read(buffer);
126
        for (uint8_t i = 0; i < length; i++) {
127
            if (uart_state == UART_STATE_READ_CMD) {
128
                if (buffer[i] == UART_CMD_RESET) {
129
                    RESET();
130
                } else {
131
                    // Read and save first byte (command)
132
                    uart_cmd = buffer[i];
133
                    uart_state = UART_STATE_READ_DATA;
134
                }
135
            } else if (uart_state == UART_STATE_READ_DATA) {
136
                uart_state = UART_STATE_READ_CMD;
137
 
138
                // Process received data
139
                if (uart_cmd == UART_CMD_LEFT_FORWARD) {
140
                    pwm_left = PWM_NOMINAL + (uint16_t)(buffer[i] * 2);
141
                    if (buffer[i] != 0) LED_2_On();
142
                    else LED_2_Off();
143
                } else if (uart_cmd == UART_CMD_LEFT_BACKWARD) {
144
                    pwm_left = PWM_NOMINAL - (uint16_t)(buffer[i] * 2);
145
                    if (buffer[i] != 0) LED_2_On();
146
                    else LED_2_Off();
147
                } else if (uart_cmd == UART_CMD_RIGHT_FORWARD) {
148
                    pwm_right = PWM_NOMINAL + (uint16_t)(buffer[i] * 2);
149
                    if (buffer[i] != 0) LED_1_On();
150
                    else LED_1_Off();
151
                } else if (uart_cmd == UART_CMD_RIGHT_BACKWARD) {
152
                    pwm_right = PWM_NOMINAL - (uint16_t)(buffer[i] * 2);
153
                    if (buffer[i] != 0) LED_1_On();
154
                    else LED_1_Off();
155
                }
156
 
157
                if (pwm_left > PWM_MAX)
158
                    pwm_left = PWM_MAX;
159
                if (pwm_left < PWM_MIN)
160
                    pwm_left = PWM_MIN;
161
 
162
                if (pwm_right > PWM_MAX)
163
                    pwm_right = PWM_MAX;
164
                if (pwm_right < PWM_MIN)
165
                    pwm_right = PWM_MIN;
166
 
167
                PWM_1_Set(pwm_right);
168
                PWM_2_Set(pwm_left);
169
            }
170
        }
171
    }
172
#endif
173
 
174
#ifdef CONTROL_FROM_CONTROLLER
175
    uint8_t median;
176
    int16_t L_X, L_Y, R_X, R_Y;
283 Kevin 177
    while (1) {
178
        if (!Controller_Read(&ctrl)) {
179
            median = ctrl.REF / 2;
180
            L_X = (int16_t)median - (int16_t)ctrl.L_X_CH;
181
            L_Y = (int16_t)median - (int16_t)ctrl.L_Y_CH;
182
            R_X = (int16_t)median - (int16_t)ctrl.R_X_CH;
183
            R_Y = (int16_t)median - (int16_t)ctrl.R_Y_CH;
184
 
185
            // Left stick = throttle, Right stick = steering
186
    //        if (L_X > -5 && L_X < 5 &&
187
    //                R_Y > -5 && R_Y < 5) {
188
    //            pwm_left = PWM_NOMINAL;
189
    //            pwm_right = PWM_NOMINAL;
190
    //        } else {
191
    //            pwm_left = PWM_NOMINAL + (L_X * 4);
192
    //            pwm_right = PWM_NOMINAL + (L_X * 4);
193
    //
194
    //            pwm_left += R_Y * 4;
195
    //            pwm_right -= R_Y * 4;
196
    //        }
197
 
198
            // Left stick = left control, Right stick = right control
199
            if (L_X > -5 && L_X < 5 &&
200
                    R_X > -5 && R_X < 5) {
201
                pwm_left = PWM_NOMINAL;
202
                pwm_right = PWM_NOMINAL;
203
            } else {
204
                pwm_left = PWM_NOMINAL + (R_X * 4);
205
                pwm_right = PWM_NOMINAL + (L_X * 4);
206
            }
207
 
208
            if (L_X < -5 || L_X > 5) LED_2_On();
209
            else LED_2_Off();
210
 
211
            if (R_X < -5 || R_X > 5) LED_1_On();
212
            else LED_1_Off();
213
 
214
 
215
            if (pwm_left > PWM_MAX)
216
                pwm_left = PWM_MAX;
217
            if (pwm_left < PWM_MIN)
218
                pwm_left = PWM_MIN;
219
 
220
            if (pwm_right > PWM_MAX)
221
                pwm_right = PWM_MAX;
222
            if (pwm_right < PWM_MIN)
223
                pwm_right = PWM_MIN;
224
 
225
            PWM_1_Set(pwm_left);
226
            PWM_2_Set(pwm_right);
227
        } else {
228
            PWM_1_Set(PWM_NOMINAL);
229
            PWM_2_Set(PWM_NOMINAL);
230
 
231
            LED_1_Off();
232
            LED_2_Off();
233
        }
234
    }
285 Kevin 235
#endif
283 Kevin 236
}