Code: Select all
// Lcd pinout config
sbit LCD_RS at LATA5_bit;
sbit LCD_EN at LATA4_bit;
sbit LCD_D7 at LATA0_bit;
sbit LCD_D6 at LATA1_bit;
sbit LCD_D5 at LATA2_bit;
sbit LCD_D4 at LATA3_bit;
// Pin direction
sbit LCD_RS_Direction at TRISA5_bit;
sbit LCD_EN_Direction at TRISA4_bit;
sbit LCD_D7_Direction at TRISA0_bit;
sbit LCD_D6_Direction at TRISA1_bit;
sbit LCD_D5_Direction at TRISA2_bit;
sbit LCD_D4_Direction at TRISA3_bit;
void main() {
char rfid_no[13], i, column;
OSCCON.OSTS = 0;
OSCCON.IRCF0 = 1;
OSCCON.IRCF1 = 1;
OSCCON.IRCF2 = 1;
ANSELC = 0X00;
ANSELB = 0X00;
TRISB = 0;
TRISB.RB6 = 0; //Tx pin
TRISB.RB7 = 1; //Rx pin
TRISC = 0;
TRISC.RC6 = 0; //Tx pin
TRISC.RC7 = 1; //Rx pin
LCD_Init();
LCD_Cmd(_LCD_CLEAR);
LCD_OUT(1,1, "WELCOME");
UART2_Init(9600);
delay_ms(500);
UART1_Init(9600);
delay_ms(500);
UART_Set_Active(&UART2_Read, &UART2_Write, &UART2_Data_Ready, &UART2_Tx_Idle); // set UART2 active
rfid_no[12] = '\0'; //indicate end of tag number (terminate string)
while(1) {
if(UART2_Data_Ready()) { //if data is ready to be read
for (i=0; i<12;) { //read the tag number one after another
if (UART2_DATA_READY()) {
rfid_no[i] = UART2_Read(); //store the tag number
i++;
}
}
LCD_CMD(_LCD_CLEAR);
LCD_OUT(1,1, rfid_no);
UART_Set_Active(&UART1_Read, &UART1_Write, &UART1_Data_Ready, &UART1_Tx_Idle); //SWITCH UART TO PC
UART1_Write_Text(rfid_no); //write tag number to PC
UART1_WRITE(13);
delay_ms(500);
}
else if (UART1_DATA_READY()==1) { //read response from PC
res = UART1_Read();
switch (res) {
case '0': //if tag is not registered, PC will return 0
LCD_Cmd(_LCD_CLEAR);
LCD_Out(1,1, "Unregistered");
LCD_Out(2,5, "Tag");
UART_Set_Active(&UART2_Read, &UART2_Write, &UART2_Data_Ready, &UART2_Tx_Idle);
case '1': //if not enough credit, PC returns 1
LCD_Cmd(_LCD_CLEAR);
LCD_Out(1,5, "Low");
LCD_Out(2,1, "Balance");
break;
case '2': //IF enough credit, display remaining balance
UART1_Write_Text("1"); //send request to get balance
UART1_Write(13);
delay_ms(100); //wait a while to send
LCD_Out(1,2, "Balance");
break;
case 'e': //PC finished sending balance
UART_Set_Active(&UART2_Read, &UART2_Write, &UART2_Data_Ready, &UART2_Tx_Idle);
default:
balance = UART1_Read();
LCD_chr(2,column, balance);
column++;
}
}
}
}