Repository navigation
Expand file tree
/
Copy pathuser.c
More file actions
141 lines (129 loc) · 4.21 KB
/
Copy pathuser.c
File metadata and controls
141 lines (129 loc) · 4.21 KB
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
/******************************************************************************/
/* Files to Include */
/******************************************************************************/
#ifdef __XC32
#include <xc.h> /* Defines special funciton registers, CP0 regs */
#endif
#define _SUPPRESS_PLIB_WARNING
#define _DISABLE_OPENADC10_CONFIGPORT_WARNING
#include <plib.h> /* Include to use PIC32 peripheral libraries */
#include <stdint.h> /* For uint32_t definition */
#include <stdbool.h> /* For true/false definition */
#include <stdio.h>
#include <proc/p32mx795f512h.h>
#include "user.h" /* variables/params used by user.c */
#include "system.h"
#define TOGGLE_PER_SEC_MS 40000
#define TOGGLE_PER_SEC_US 40
/******************************************************************************/
/* User Functions */
/******************************************************************************/
uint32_t elTime = 0; // elapsed time in ms
void InitApp(void)
{
/* Setup analog functionality and port direction */
//ALL Digital port
AD1CON1bits.ADON = 0;
AD1PCFG = 0xFFFF;
//Disable ChangeOnNotification
CNCONbits.ON = 0;
//Disable ChangeOnNotification
CNEN = 0;
//Disable PullUp/PullDown
CNPUE = 0;
TRISB = 0x0;
LATB = 0x0;
TRISC = 0x0;
LATC = 0x0;
TRISD = 0x0;
LATD = 0x0;
TRISE = 0x0;
LATE = 0x0;
TRISF = 0x0;
LATF = 0x0;
TRISG = 0x0;
LATG = 0x0;
// TRISFbits.TRISF5 = 0;
// LATFbits.LATF5 = 0;
TRISBbits.TRISB15 = 0; // bme280_CS
LATBbits.LATB15 = 1; // active low default high
TRISBbits.TRISB14 = 0; //SCK4 output
TRISFbits.TRISF4 = 1; //SDI4 input
TRISFbits.TRISF5 = 0; //SDO4 output
TRISEbits.TRISE0 = 0; //RX_IND MFB BM64
LATEbits.LATE0 = 0;
TRISEbits.TRISE1 = 1; //TX_IND P3_7 BM64
TRISEbits.TRISE2 = 0; //RST_N active low BM64
LATEbits.LATE2 = 1; //active low default high
TRISEbits.TRISE5 = 0; //LCD_RST
TRISEbits.TRISE6 = 0; //LCD_DC
TRISEbits.TRISE7 = 0; //LCD_CS
TRISEbits.TRISE7 = 1; //active low default high
TRISGbits.TRISG9 = 0; //TOUCH_CS output
LATGbits.LATG9 = 1; //active low default high
TRISDbits.TRISD6 = 0; //BME_CS
LATDbits.LATD6 = 1; //active low default high
TRISGbits.TRISG6 = 0; //SCK2 output
TRISGbits.TRISG7 = 1; //SDI2 input
TRISGbits.TRISG8 = 0; //SDO2 output
// mConfigIntCoreTimer(CT_INT_ON | CT_INT_PRIOR_0);
// mEnableIntCoreTimer();
// OpenCoreTimer(0);
/* Initialize peripherals */
}
void delay_ms(uint16_t t)
{
uint32_t ms = TOGGLE_PER_SEC_MS * t;
_CP0_SET_COUNT(0);
while(ms > _CP0_GET_COUNT());
}
void delay_us(uint16_t t)
{
uint32_t us = TOGGLE_PER_SEC_US * t;
_CP0_SET_COUNT(0);
while(us > _CP0_GET_COUNT());
}
uint32_t millis()
{
return elTime;
}
void UART1Setup(void)
{
UARTConfigure(UART1,UART_ENABLE_PINS_TX_RX_ONLY);
UARTSetFifoMode(UART1, UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY);
UARTSetLineControl(UART1, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1);
UARTSetDataRate(UART1, SYS_FREQ, 115200);
UARTEnable(UART1, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX));
}
uint8_t UART1Receive(void)
{
uint8_t data;
while(!UARTReceivedDataIsAvailable(UART1))
;
data = UARTGetDataByte(UART1);
return data;
}
void UART1Transmit(uint8_t data)
{
while(!UARTTransmitterIsReady(UART1))
;
UARTSendDataByte(UART1, data);
while(!UARTTransmissionHasCompleted(UART1))
;
}
void UART1HexTransmit(uint8_t val)
{
unsigned char upperNibble = (val & 0xF0) >> 4;
unsigned char lowerNibble = val & 0x0F;
upperNibble += upperNibble > 9 ? 'A' - 10 : '0';
lowerNibble += lowerNibble > 9 ? 'A' - 10 : '0';
UART1Transmit(upperNibble);
UART1Transmit(lowerNibble);
// printf("\n\r");
UART1Transmit(' ');
}
void __ISR(_TIMER_1_VECTOR, ipl2auto)_Timer1Handler(void)
{
elTime++;
mT1ClearIntFlag();
}