Final
unknown
plain_text
2 years ago
3.2 kB
11
Indexable
#include <xc.h>
//setting TX/RX
char mystring[20];
int lenStr = 0;
extern int mode;
void UART_Initialize() {
// TODObasic
// Serial Setting
// 1. Setting Baud rate
// 2. choose sync/async mode
// 3. enable Serial port (configures RX/DT and TX/CK pins as serial port pins)
// 3.5 enable Tx, Rx Interrupt(optional)
// 4. Enable Tx & RX
TRISCbits.TRISC6 = 1;
TRISCbits.TRISC7 = 1;
// Setting baud rate
TXSTAbits.SYNC = 0;
BAUDCONbits.BRG16 = 0;
TXSTAbits.BRGH = 0;
SPBRG = 51;
// Serial enable
RCSTAbits.SPEN = 1;
PIR1bits.TXIF = 1;
PIR1bits.RCIF = 0;
TXSTAbits.TXEN = 1;
RCSTAbits.CREN = 1;
// PIE1bits.TXIE = 1;
// IPR1bits.TXIP = ;
PIE1bits.RCIE = 1;
IPR1bits.RCIP = 1;
}
void UART_Write(unsigned char data) // Output on Terminal
{
while(!TXSTAbits.TRMT);
TXREG = data; //write to TXREG will send data
}
void UART_Write_Text(char* text) { // Output on Terminal, limit:10 chars
for(int i=0;text[i]!='\0';i++)
UART_Write(text[i]);
}
void ClearBuffer(){
for(int i = 0; i < 10 ; i++)
mystring[i] = '\0';
lenStr = 0;
}
void print_mode(unsigned char c) {
char message[] = "Enter your choice:";
char high[] = "High mode";
char medium[] = "Medium mode";
char low[] = "Low mode";
int i = 0;
if(c == 'a'){
while (high[i] != '\0') {
UART_Write(high[i]);
i++;
}
}
else if(c == 'b'){
while (medium[i] != '\0') {
UART_Write(medium[i]);
i++;
}
}
else if(c == 'c'){
while (low[i] != '\0') {
UART_Write(low[i]);
i++;
}
}
i = 0;
UART_Write('\n');
UART_Write('\r');
while (message[i] != '\0') {
UART_Write(message[i]);
i++;
}
}
unsigned char record = '\0';
void MyusartRead()
{
// while(!PIR1bits.RCIF);
if(RCREG == 'a')
record = 'a';
else if(RCREG == 'b')
record = 'b';
else if(RCREG == 'c')
record = 'c';
if(RCREG == '\r'){
UART_Write('\n');
UART_Write('\r');
if(record == 'a'){
mode = 2;
print_mode('a');
}
else if(record == 'b'){
mode = 1;
print_mode('b');
}
else if(record == 'c'){
mode = 0;
print_mode('c');
}
else
record = '\0';
}
else
UART_Write(RCREG);
return ;
}
char *GetString(){
return mystring;
}
void __interrupt(high_priority) Hi_ISR(void)
{
if(RCIF)
{
if(RCSTAbits.OERR)
{
CREN = 0;
Nop();
CREN = 1;
}
MyusartRead();
}
// process other interrupt sources here, if required
return;
}Editor is loading...
Leave a Comment