-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEXAMPLE_Main.c
144 lines (108 loc) · 4.78 KB
/
EXAMPLE_Main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
////////////////////////////////////////////////////////////////////////////////
////////////////////////////// STANDARD LIBRARIES /////////////////////////////
////////////////////////////////////////////////////////////////////////////////
#include <stdbool.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <MCP4XXX_LIB.h>
////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////// DEFINES ///////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
#define LED_TEST LATE.RE0
#define ON 1
#define OFF 0
#define HIGH 1
#define LOW 0
////////////////////////////////////////////////////////////////////////////////
//////////////////////////// CONSTANTS AND VARIABLES ///////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Temporary Pot values to be sent through SPI
int P0W_Value, P1W_Value;
// Value for general Stepping delay between pot value changes
const unsigned long Stepping_delay_value = 20;
// Buffer for UART Debugging messages
unsigned char buffer[128];
////////////////////////////////////////////////////////////////////////////////
//////////////////////////////// SETUP FUNCTION ////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
void Setup(){
// All ports as Digital
ANSELA = ANSELB = ANSELC = ANSELD = ANSELE= 0;
// PortA as Output
TRISA = 0b00000000;
// PortB as Output
TRISB = 0b00000000;
// PortC as Output
TRISC = 0b00000000;
// PortE as Output
TRISE = 0b00000000;
// Clears the portB for debug purposes
LATB = 0;
// SPI1 init
SPI1_Init();
// I2C init
//I2C2_Init(100000);
// UART init for debugging
UART1_Init(9600);
}
////////////////////////////////////////////////////////////////////////////////
///////////////////////////////// MAIN FUNCTION ////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
void main(void){
setup(); // Configuration function
PORTB = 0;
LED_TEST = 1; // LED Test on
UART1_Write_Text("System Init");
delay_ms(100);
UART1_Write_Text(".");
delay_ms(100);
UART1_Write_Text(".");
delay_ms(100);
UART1_Write_Text(".");
delay_ms(100);
UART1_Write_Text(".");
delay_ms(100);
UART1_Write_Text(".");
delay_ms(100);
UART1_Write_Text(".\n\n");
//////////////////// SOME RANDOM POT VALUES ////////////////////
do{
// RAMDON INCREMENT FOR OUTPUT TESTING: WIPER 0, FROM 0 TO 128
for(P0W_Value = 0; P0W_Value <= 128; P0W_Value++){
MCP4XXX_General_Command(MCP4XXX_VOLATILE_WIPER_OUT_0, P0W_Value);
// Debug Purposes on UART1
sprintf(buffer, "POT 0 INCREMENT: WIPER_OUT_0 = %x, WIPER_OUT_1 = %x, \n", MCP4XXX_General_Read(MCP4XXX_VOLATILE_WIPER_OUT_0), MCP4XXX_General_Read(MCP4XXX_VOLATILE_WIPER_OUT_1));
UART1_Write_Text(buffer);
delay_ms(Stepping_delay_value);
}
UART1_Write_Text("\n\n");
// RAMDON INCREMENT FOR OUTPUT TESTING: WIPER 1, FROM 0 TO 128
for(P1W_Value = 0; P1W_Value <= 128; P1W_Value++){
MCP4XXX_General_Command(MCP4XXX_VOLATILE_WIPER_OUT_1, P1W_Value);
// Debug Purposes on UART1
sprintf(buffer, "POT 1 INCREMENT: WIPER_OUT_0 = %x, WIPER_OUT_1 = %x, \n", MCP4XXX_General_Read(MCP4XXX_VOLATILE_WIPER_OUT_0), MCP4XXX_General_Read(MCP4XXX_VOLATILE_WIPER_OUT_1));
UART1_Write_Text(buffer);
delay_ms(Stepping_delay_value);
}
UART1_Write_Text("\n\n");
// RAMDON INCREMENT FOR OUTPUT TESTING: WIPER 0, FROM 128 TO 0
for(P0W_Value = 128; P0W_Value >= 0; P0W_Value--){
MCP4XXX_General_Command(MCP4XXX_VOLATILE_WIPER_OUT_0, P0W_Value);
// Debug Purposes on UART1
sprintf(buffer, "POT 0 DECREMENT: WIPER_OUT_0 = %x, WIPER_OUT_1 = %x, \n", MCP4XXX_General_Read(MCP4XXX_VOLATILE_WIPER_OUT_0), MCP4XXX_General_Read(MCP4XXX_VOLATILE_WIPER_OUT_1));
UART1_Write_Text(buffer);
delay_ms(Stepping_delay_value);
}
UART1_Write_Text("\n\n");
// RAMDON INCREMENT FOR OUTPUT TESTING: WIPER 1, FROM 128 TO 0
for(P1W_Value = 128; P1W_Value >= 0; P1W_Value--){
MCP4XXX_General_Command(MCP4XXX_VOLATILE_WIPER_OUT_1, P1W_Value);
// Debug Purposes on UART1
sprintf(buffer, "POT 1 DECREMENT: WIPER_OUT_0 = %x, WIPER_OUT_1 = %x, \n", MCP4XXX_General_Read(MCP4XXX_VOLATILE_WIPER_OUT_0), MCP4XXX_General_Read(MCP4XXX_VOLATILE_WIPER_OUT_1));
UART1_Write_Text(buffer);
delay_ms(Stepping_delay_value);
}
UART1_Write_Text("\n\n");
}while(true);
}