Solar panel
Loading...
Searching...
No Matches
eusart1.c
Go to the documentation of this file.
1
24/*
25 (c) 2018 Microchip Technology Inc. and its subsidiaries.
26
27 Subject to your compliance with these terms, you may use Microchip software and any
28 derivatives exclusively with Microchip products. It is your responsibility to comply with third party
29 license terms applicable to your use of third party software (including open source software) that
30 may accompany Microchip software.
31
32 THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
33 EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY
34 IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS
35 FOR A PARTICULAR PURPOSE.
36
37 IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
38 INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
39 WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP
40 HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO
41 THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL
42 CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT
43 OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS
44 SOFTWARE.
45*/
46
50#include "eusart1.h"
51
56#define EUSART1_TX_BUFFER_SIZE 8
57#define EUSART1_RX_BUFFER_SIZE 8
58
63volatile uint8_t eusart1RxHead = 0;
64volatile uint8_t eusart1RxTail = 0;
67volatile uint8_t eusart1RxCount;
69
74
78
82
84{
85 // disable interrupts before changing states
86 PIE1bits.RC1IE = 0;
88 // Set the EUSART1 module to the options selected in the user interface.
89
90 // ABDOVF no_overflow; SCKP async_noninverted_sync_fallingedge; BRG16 16bit_generator; WUE disabled; ABDEN disabled; RXDTP not_inverted;
91 BAUDCON1 = 0x08;
92
93 // SPEN enabled; RX9 9-bit; CREN enabled; ADDEN disabled; SREN disabled;
94 RCSTA1 = 0xD0;
95
96 // TX9 9-bit; TX9D 1; SENDB sync_break_complete; TXEN enabled; SYNC asynchronous; BRGH hi_speed; CSRC master_mode;
97 TXSTA1 = 0xE5;
98
99 //
100 SPBRG1 = 0x8A;
101
102 //
103 SPBRGH1 = 0x02;
104
105
109
111
112
113 eusart1RxHead = 0;
114 eusart1RxTail = 0;
115 eusart1RxCount = 0;
116
117 // enable receive interrupt
118 PIE1bits.RC1IE = 1;
119}
120
122{
123 return (bool)(PIR1bits.TX1IF && TXSTA1bits.TXEN);
124}
125
127{
128 return (eusart1RxCount ? true : false);
129}
130
132{
133 return TXSTA1bits.TRMT;
134}
135
137 return eusart1RxLastError;
138}
139
140uint8_t EUSART1_Read(void)
141{
142 uint8_t readValue = 0;
143
144 while(0 == eusart1RxCount)
145 {
146 }
147
149
150 readValue = eusart1RxBuffer[eusart1RxTail++];
151 if(sizeof(eusart1RxBuffer) <= eusart1RxTail)
152 {
153 eusart1RxTail = 0;
154 }
155 PIE1bits.RC1IE = 0;
157 PIE1bits.RC1IE = 1;
158
159 return readValue;
160}
161
162void EUSART1_Write(uint8_t txData)
163{
164 while(0 == PIR1bits.TX1IF)
165 {
166 }
167
168 TXREG1 = txData; // Write the data byte to the USART.
169}
170
171
172
174{
175
177
178 if(RCSTA1bits.FERR){
181 }
182
183 if(RCSTA1bits.OERR){
186 }
187
190 } else {
192 }
193
194 // or set custom function using EUSART1_SetRxInterruptHandler()
195}
196
198 // use this default receive interrupt handler code
199 eusart1RxBuffer[eusart1RxHead++] = RCREG1;
200 if(sizeof(eusart1RxBuffer) <= eusart1RxHead)
201 {
202 eusart1RxHead = 0;
203 }
205}
206
208
210 // EUSART1 error - restart
211
212 RCSTA1bits.CREN = 0;
213 RCSTA1bits.CREN = 1;
214
215}
216
219}
220
221void EUSART1_SetFramingErrorHandler(void (* interruptHandler)(void)){
222 EUSART1_FramingErrorHandler = interruptHandler;
223}
224
225void EUSART1_SetOverrunErrorHandler(void (* interruptHandler)(void)){
226 EUSART1_OverrunErrorHandler = interruptHandler;
227}
228
229void EUSART1_SetErrorHandler(void (* interruptHandler)(void)){
230 EUSART1_ErrorHandler = interruptHandler;
231}
232
233
234void EUSART1_SetRxInterruptHandler(void (* interruptHandler)(void)){
235 EUSART1_RxDefaultInterruptHandler = interruptHandler;
236}
void EUSART1_RxDataHandler(void)
Definition: eusart1.c:197
void EUSART1_Initialize(void)
Definition: eusart1.c:83
void EUSART1_SetErrorHandler(void(*interruptHandler)(void))
Definition: eusart1.c:229
void(* EUSART1_FramingErrorHandler)(void)
Definition: eusart1.c:75
volatile eusart1_status_t eusart1RxStatusBuffer[EUSART1_RX_BUFFER_SIZE]
Definition: eusart1.c:66
bool EUSART1_is_tx_ready(void)
Definition: eusart1.c:121
void EUSART1_DefaultOverrunErrorHandler(void)
Definition: eusart1.c:209
eusart1_status_t EUSART1_get_last_status(void)
Definition: eusart1.c:136
volatile uint8_t eusart1RxHead
Definition: eusart1.c:63
bool EUSART1_is_rx_ready(void)
Definition: eusart1.c:126
#define EUSART1_RX_BUFFER_SIZE
Definition: eusart1.c:57
void EUSART1_SetRxInterruptHandler(void(*interruptHandler)(void))
Definition: eusart1.c:234
void EUSART1_Write(uint8_t txData)
Definition: eusart1.c:162
volatile uint8_t eusart1RxTail
Definition: eusart1.c:64
bool EUSART1_is_tx_done(void)
Definition: eusart1.c:131
void EUSART1_SetOverrunErrorHandler(void(*interruptHandler)(void))
Definition: eusart1.c:225
void(* EUSART1_RxDefaultInterruptHandler)(void)
Definition: eusart1.c:73
volatile eusart1_status_t eusart1RxLastError
Definition: eusart1.c:68
void EUSART1_DefaultErrorHandler(void)
Definition: eusart1.c:217
volatile uint8_t eusart1RxBuffer[EUSART1_RX_BUFFER_SIZE]
Definition: eusart1.c:65
void EUSART1_SetFramingErrorHandler(void(*interruptHandler)(void))
Definition: eusart1.c:221
uint8_t EUSART1_Read(void)
Definition: eusart1.c:140
volatile uint8_t eusart1RxCount
Definition: eusart1.c:67
void(* EUSART1_ErrorHandler)(void)
Definition: eusart1.c:77
void(* EUSART1_OverrunErrorHandler)(void)
Definition: eusart1.c:76
void EUSART1_DefaultFramingErrorHandler(void)
Definition: eusart1.c:207
void EUSART1_Receive_ISR(void)
Definition: eusart1.c:173
unsigned oerr
Definition: eusart1.h:79
uint8_t status
Definition: eusart1.h:82
unsigned ferr
Definition: eusart1.h:78