Welcome to my blog here again.....Enjoy it....Gambateh...
Week 8 ~ Do the program for the project.
For the week 8 8/11 ( Friday).C language program is my main program for the project and i using MicroC Pro as the software to create my program. Refer the program below.
#include <built_in.h>
sbit LCD_RS at RD2_bit;
sbit LCD_EN at RD3_bit;
sbit LCD_D4 at RD4_bit;
sbit LCD_D5 at RD5_bit;
sbit LCD_D6 at RD6_bit;
sbit LCD_D7 at RD7_bit;
sbit LCD_RS_Direction at TRISD2_bit;
sbit LCD_EN_Direction at TRISD3_bit;
sbit LCD_D4_Direction at TRISD4_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D7_Direction at TRISD7_bit;
sbit OUT at RB1_bit;
sbit RDY_CLK at RB2_bit;
sbit SEN_MOV at RB3_bit;
sbit SHD at RB4_bit;
sbit MOD at RB5_bit;
sbit OUT_Direction at TRISB1_bit;
sbit RDY_CLK_Direction at TRISB2_bit;
sbit SEN_MOV_Direction at TRISB3_bit;
sbit SHD_Direction at TRISB4_bit;
sbit MOD_Direction at TRISB5_bit;
//char txt1[] = "RFID";
//char txt2[] = "MASUK";
//char txt3[] = "TUNGGU";char txt4[] = "SAVE";
unsigned short sync_flag,
one_seq, // counts the number of 'logic one' in series
data_in, // gets data bit depending on data_in_1st and data_in_2nd
cnt, k, M,
tmp, b, v, Save_Mem,
cnt1, cnt2; // auxiliary counters
unsigned short data_index; // marks position in data arrey
char i;
char _data[256], test[8];
char data_valid[64];
char bad_synch;
char seconds, minutes, hours, day, month, year;
void Interrupt() {
if (INT2IF_bit && INT2IE_bit) {
cnt++; // count interrupts on INT1 pin (RB1)
INT2IF_bit = 0;
}
else if (INT1IE_bit && INT1IF_bit) {
cnt = 0;
sync_flag = 1;
INT1IF_bit = 0;
INT1IE_bit = 0;
INT2IF_bit = 0;
INT2IE_bit = 1;
}
else if (INT3IE_bit && INT3IF_bit) {
Save_Mem = 1;
TMR0IE_bit = 0;
TMR0IF_bit = 0;
INT3IE_bit = 0;
INT3IF_bit= 0;
}
else if (TMR0IF_bit && TMR0IE_bit) {
Save_Mem = 1 ;
TMR0IE_bit = 0;
TMR0IF_bit = 0;
INT3IE_bit = 0;
INT3IF_bit= 0;
}
}
char CRC_Check(char *bit_array) {
char row_count, row_bit, column_count;
char row_sum, column_sum;
char row_check[5];
char column_check[11];
// row parity check:
row_count = 9; // count rows
while (row_count < 59) {
column_count = 0; // count columns
while (column_count < 5) {
row_check[column_count] = bit_array[row_count+column_count];
column_count++;
}
row_bit = 0; // count row bits
row_sum = 0;
while (row_bit < 4) {
row_sum = row_sum + row_check[row_bit];
row_bit++;
}
if (row_sum.B0 != row_check[4].B0) {
return 0;
}
row_count = row_count + 5;
}
// end row parity check
// column parity check
column_count = 9; // count columns
while (column_count < 13) {
row_bit = 0; // count column bits
row_count = 0; // count rows
while (row_bit < 11) {
column_check[row_bit] = bit_array[column_count+row_count];
row_bit++;
row_count = row_count + 5;
}
row_bit = 0; // count column bits
column_sum = 0;
while (row_bit < 10) {
column_sum = column_sum + column_check[row_bit];
row_bit++;
}
if (column_sum.B0 != column_check[10].B0) {
return 0;
}
column_count++;
}
// end column parity check
if (bit_array[63] == 1) {
return 0;
}
return 1;
}
void Transform_RTC() {
seconds = ((seconds & 0xF0) >> 4)*10 + (seconds & 0x0F);
minutes = ((minutes & 0xF0) >> 4)*10 + (minutes & 0x0F);
hours = ((hours & 0xF0) >> 4)*10 + (hours & 0x0F);
year = (day & 0xC0) >> 6;
day = ((day & 0x30) >> 4)*10 + (day & 0x0F);
month = ((month & 0x10) >> 4)*10 + (month & 0x0F);
}
void Read_RTC() {
I2C1_Start(); // Issue start signal
I2C1_Wr(0xA0); // Address PCF8583, see PCF8583 datasheet
I2C1_Wr(2); // Start from address 2
I2C1_Repeated_Start(); // Issue repeated start signal
I2C1_Wr(0xA1); // Address PCF8583 for reading R/W=1
seconds = I2C1_Rd(1);
minutes = I2C1_Rd(1);
hours = I2C1_Rd(1);
day = I2C1_Rd(1);
month = I2C1_Rd(0);
I2C1_Stop(); // Issue stop signal
day = day & 0x3F; // Re-format day data
}
//-------------------- Output values to LCD
void Display_RTC() {
Lcd_Chr(1, 6, (day / 10) + 48); // Print tens digit of day variable
Lcd_Chr(1, 7, (day % 10) + 48); // Print oness digit of day variable
Lcd_Chr(1, 9, (month / 10) + 48);
Lcd_Chr(1,10, (month % 10) + 48);
Lcd_Chr(1,15, year + 49); // Print year variable + 8 (start from year 2008)
Lcd_Chr(2, 6, (hours / 10) + 48);
Lcd_Chr(2, 7, (hours % 10) + 48);
Lcd_Chr(2, 9, (minutes / 10) + 48);
Lcd_Chr(2,10, (minutes % 10) + 48);
Lcd_Chr(2,12, (seconds / 10) + 48);
Lcd_Chr(2,13, (seconds % 10) + 48);
}
//------------------ Performs project-wide init
void Init_RTC() {
I2C1_Init(100000); // Initialize Soft I2C communication
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear LCD display
Lcd_Cmd(_LCD_CURSOR_OFF); // Turn cursor off
Lcd_Out(1,1,"Date:"); // Prepare and output static text on LCD
Lcd_Chr(1,8,':');
Lcd_Chr(1,11,':');
Lcd_Out(2,1,"Time:");
Lcd_Chr(2,8,':');
Lcd_Chr(2,11,':');
Lcd_Out(1,12,"201");
}
//void Display_Intro(){
//Lcd_Init();
//Lcd_Cmd(_LCD_CLEAR);
//Lcd_Cmd(_LCD_CURSOR_OFF);
//Lcd_Out(1,6,txt3);
//Lcd_Out(2,6,txt4);
// Delay_ms(2000);
// Lcd_Cmd(_LCD_CLEAR);
// Lcd_Out(1,1,txt1);
// Lcd_Out(2,5,txt2);
// Delay_ms(2000);
//}
// main program
void main() {
ADCON1 = 0x0F; // AD converter off
CMCON = 7;
k = 0;
OUT_Direction = 1; // RFID TX
RDY_CLK_Direction = 1; // RFID RX
SEN_MOV_Direction = 1; // motion sensor
SHD_Direction = 0; // RFID
MOD_Direction = 0; // RFID
//TrisB0_bit = 1;
Init_RTC(); // Initialize Clock
SHD = 0;
MOD = 0;
Lcd_Init ();
//Display_Intro() ;
UART1_Init(19200); // Initialise USART communication
Delay_ms(100);
UART1_Init(19200);
sync_flag = 0; // sync_flag is set when falling edge on RB0 is detected
one_seq = 0; // counts the number of 'logic one' in series
data_in = 0; // gets data bit
data_index = 0; // marks position in data arrey
cnt = 0; // interrupt counter
cnt1 = 0; // auxiliary counter
cnt2 = 0; // auxiliary counter
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Out(1,6,"RFID CLOCK IN"); // Write text in first row
delay_ms(2000);
Lcd_Cmd(_LCD_CLEAR); // Clear display
// setup interrupts
INTEDG1_bit = 0; // Interrupt on falling edge on RB0
INTEDG2_bit = 1; // Interrupt on rising edge on RB1
INT1IF_bit = 0; // Clear INT0IF
INT2IF_bit = 0; // Clear INT1IF
// INT1IP_bit = 1;
// INT2IP_bit = 1;
INT1IE_bit = 0; // turn OFF interrupt on INT0
INT2IE_bit = 0; // turn OFF interrupt on INT1
//Int0IE_bit = 1;
GIE_bit = 1; // enable GIE
M = 0;
while (1) {
bad_synch = 0; // set bad synchronization variable to zero
cnt = 0; // reseting interrupt counter
sync_flag = 0; // reseting sync flag
INT2IF_bit = 0;
INT2IE_bit = 0; // disable external interrupt on RB1 (for sync and sample)
INT1IF_bit = 0;
INT1IE_bit = 1; // enable external interrupt on RB0 (start sync procedure)
//GIE_bit = 1;
T0CON = 0b10000101; // Timer0 Control Register
while (sync_flag == 0) { // waiting for falling edge on RB0
Read_RTC(); // Read time from RTC(PCF8583)
Transform_RTC(); // Format date and time
Display_RTC(); // waiting for card reader. while waiting
asm nop // display shows actual time
}
while (cnt != 16) { // waiting 16 clocks on RB1 (positioning for sampling)
asm nop // waiting for 16 bit data after card reader being touched
}
cnt = 0;
_data[0] = OUT & 1;
for (data_index = 1; data_index != 0; data_index++) { // getting 128 bits of data from RB0
while (cnt != 32) { // getting bit from RB0 every 32 clocks on RB1
asm nop
}
cnt = 0; // reseting interrupt counter
_data[data_index] = OUT & 1; // geting bit
if(data_index & 1)
if (!(_data[data_index] ^ _data[data_index-1]))
{
bad_synch = 1;
break; //bad synchronisation
}
}
INT2IE_bit = 0; // disable external interrupt on RB1 (for sync and sample)
if (bad_synch)
continue; // try again
cnt1 = 0;
one_seq = 0;
for(cnt1 = 0; cnt1 <= 127; cnt1++) { // we are counting 'logic one' in the data array
if (_data[cnt1 << 1] == 1) {
one_seq++;
}
else {
one_seq = 0;
}
if (one_seq == 9) { // if we get 9 'logic one' we break from the loop
break;
}
} // (the position of the last 'logic one' is in the cnt1)
if ((one_seq == 9) && (cnt1 < 73)) { // if we got 9 'logic one' before cnt1 position 73
// we write that data into data_valid array
data_valid[0] = 1; // (it has to be before cnt1 position 73 in order
data_valid[1] = 1; // to have all 64 bits available in data array)
data_valid[2] = 1;
data_valid[3] = 1;
data_valid[4] = 1;
data_valid[5] = 1;
data_valid[6] = 1;
data_valid[7] = 1;
data_valid[8] = 1;
for(cnt2 = 9; cnt2 <= 63; cnt2++) { // copying the rest of data from the data array into data_valid array
cnt1++;
data_valid[cnt2] = _data[cnt1 << 1];
}
if (CRC_Check(data_valid) == 1) { // if data in data_valid array pass the CRC check
UART1_Write_Text("CRC CHECK OK!"); // Writing of the CRC Check confirmation through USART communication
M = M + 1;
UART1_Write(13); // Cariage return (view ASCII chart)
UART1_Write(10); // Line Feed (view ASCII chart)
for (i = 0; i <= 64; i++){ // This part of the code
k = k + 1; // dislays the number of the specific RfID CARD
if (data_valid[i] == 0) {
Uart1_Write('0');
}
else {
Uart1_Write('1'); // at the end of this for loop you will get a string of "0" and "1"
}
}
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Out(1,6,"WAIT"); // wait for motion sensor timer
//delay_ms (2000);
// Clear display
b = 0; v = 0;
for (v = 0; v < 8; v++) { // save code and time in memory
for (i = 0; i < 8; i++){ // MMC not ready.
// this part will be used to
tmp = tmp ^ data_valid[i + 8*v]<<i; // slot memory sub-routine
}
test[v] = tmp;
// b = b + 1;
TMR0IF_bit = 0; // activate Timer interupt
TMR0IE_bit = 1;
INT3IE_bit = 1;
INT3IF_bit= 0; // activate motion sensor
while (Save_Mem == 0) { // waiting signal from timer or motion
// if student did not pass thru the motion sensor
asm nop // in 3 min the timer int is activated
}
Lcd_Cmd(_LCD_CLEAR); // clear display and restart the new card reader
Save_Mem = 0;
}
// specific to a single RfID CARD
UART1_Write(13); // Cariage return (view ASCII chart)
UART1_Write(10); // Line Feed (view ASCII chart)
Delay_ms(500);
}
}
}
}
This is my complete program for my project that is student attendance system, but have to download to PIC 18F87k22 at controller hardware. PIC 18F87K22 is more faster when compile and download the program into PIC. After that, i will going to test the program and the hardware on week 10.
Thank you to read my blog here....
Remember read week 9 and 10, because have any difference....h
ahaha.....
Have a nice day.....