| 228 |
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)
|
| 236 |
Kevin |
6 |
#pragma config WDTE = ON // Watchdog Timer Enable (WDT enabled)
|
| 228 |
Kevin |
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 "base_INTERRUPTS.h"
|
|
|
26 |
#include "base_I2C.h"
|
|
|
27 |
|
|
|
28 |
void Pins_Init(void) {
|
| 236 |
Kevin |
29 |
|
|
|
30 |
|
| 228 |
Kevin |
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 |
// Set buttons as inputs and enable weak pull-ups
|
|
|
39 |
BTN_L_N_TRIS = 1;
|
| 233 |
Kevin |
40 |
BTN_L_N_WPU = 1;
|
| 228 |
Kevin |
41 |
BTN_L_S_TRIS = 1;
|
| 233 |
Kevin |
42 |
BTN_L_S_WPU = 1;
|
| 228 |
Kevin |
43 |
|
|
|
44 |
BTN_R_N_TRIS = 1;
|
| 233 |
Kevin |
45 |
BTN_R_N_WPU = 1;
|
| 228 |
Kevin |
46 |
BTN_R_E_TRIS = 1;
|
| 233 |
Kevin |
47 |
BTN_R_E_WPU = 1;
|
| 228 |
Kevin |
48 |
BTN_R_S_TRIS = 1;
|
| 233 |
Kevin |
49 |
BTN_R_S_WPU = 1;
|
| 228 |
Kevin |
50 |
BTN_R_W_TRIS = 1;
|
| 233 |
Kevin |
51 |
BTN_R_W_WPU = 1;
|
| 228 |
Kevin |
52 |
|
|
|
53 |
// Set leds as outputs and initialize to off
|
|
|
54 |
LED_1_TRIS = 0;
|
| 233 |
Kevin |
55 |
LED_1_LAT = 0;
|
| 228 |
Kevin |
56 |
LED_2_TRIS = 0;
|
| 233 |
Kevin |
57 |
LED_2_LAT = 0;
|
| 228 |
Kevin |
58 |
LED_3_TRIS = 0;
|
| 233 |
Kevin |
59 |
LED_3_LAT = 0;
|
| 228 |
Kevin |
60 |
LED_4_TRIS = 0;
|
| 233 |
Kevin |
61 |
LED_4_LAT = 0;
|
| 228 |
Kevin |
62 |
}
|
|
|
63 |
|
|
|
64 |
// Function to read button status into a data structure
|
|
|
65 |
void Pins_Read(BTN_STATUS *btns) {
|
|
|
66 |
btns->BTN_L_N = BTN_L_N_PORT;
|
|
|
67 |
btns->BTN_L_S = BTN_L_S_PORT;
|
|
|
68 |
|
|
|
69 |
btns->BTN_R_N = BTN_R_N_PORT;
|
|
|
70 |
btns->BTN_R_E = BTN_R_E_PORT;
|
|
|
71 |
btns->BTN_R_S = BTN_R_S_PORT;
|
|
|
72 |
btns->BTN_R_W = BTN_R_W_PORT;
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
// Function to write led values from the data structure
|
|
|
76 |
void Leds_Write(LED_STATUS *leds) {
|
|
|
77 |
LED_1_LAT = leds->LED_1;
|
|
|
78 |
LED_2_LAT = leds->LED_2;
|
|
|
79 |
LED_3_LAT = leds->LED_3;
|
|
|
80 |
LED_4_LAT = leds->LED_4;
|
|
|
81 |
}
|
|
|
82 |
|
| 236 |
Kevin |
83 |
// TODO: Set watchdog timer to reset device if no I2C messages are received every x seconds
|
|
|
84 |
// TODO: Use a timer to manually PWM the LEDs
|
|
|
85 |
|
| 228 |
Kevin |
86 |
int main(void) {
|
| 233 |
Kevin |
87 |
uint8_t buffer[32];
|
|
|
88 |
uint8_t result, length;
|
| 228 |
Kevin |
89 |
|
|
|
90 |
// Set internal oscillator speed to 32MHz
|
|
|
91 |
OSCCONbits.SPLLEN = 1; // 4x PLL enable (overwritten by config bits)
|
|
|
92 |
OSCCONbits.IRCF = 0xE; // Base frequency @ 8MHz
|
|
|
93 |
OSCCONbits.SCS = 0b00; // System clock determined by config bits
|
|
|
94 |
|
| 236 |
Kevin |
95 |
// Set watchdog timer to reset device every 1s
|
|
|
96 |
// CLRWDT is issued upon receiving data over I2C
|
|
|
97 |
WDTCON = 0x0A;
|
|
|
98 |
|
| 228 |
Kevin |
99 |
// Initialize I/O
|
|
|
100 |
Pins_Init();
|
|
|
101 |
|
|
|
102 |
// Initialize I2C in slave mode
|
|
|
103 |
I2C_DATA i2c_data;
|
|
|
104 |
I2C_Init(&i2c_data);
|
| 230 |
Kevin |
105 |
I2C_Configure_Slave(0x25);
|
| 228 |
Kevin |
106 |
|
|
|
107 |
// Initialize interrupts
|
|
|
108 |
Interrupt_Init();
|
|
|
109 |
Interrupt_Enable();
|
|
|
110 |
|
| 233 |
Kevin |
111 |
// Check for received data over I2C
|
| 228 |
Kevin |
112 |
while (1) {
|
|
|
113 |
do {
|
|
|
114 |
result = I2C_Get_Status();
|
|
|
115 |
} while (!result);
|
| 236 |
Kevin |
116 |
CLRWDT();
|
| 228 |
Kevin |
117 |
length = I2C_Read_Buffer(buffer);
|
|
|
118 |
if (length == 2 && buffer[0] == CMD_SET_LEDS) {
|
|
|
119 |
LED_STATUS leds;
|
|
|
120 |
leds.value = buffer[1];
|
|
|
121 |
Leds_Write(&leds);
|
|
|
122 |
}
|
|
|
123 |
}
|
|
|
124 |
}
|
|
|
125 |
|