Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

RS232 communication between two pic18f452

Status
Not open for further replies.

sobia sarwar

Member level 5
Member level 5
Joined
Nov 15, 2012
Messages
81
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Visit site
Activity points
1,883
I have a project named intelligent voting machine and i have completed my control unit and ballot unit code. my control and ballot unit are having RS232 communication.
what i wanted to do is:
when any of the switch connected with ballot unit is pressed the glcd connected with the control unit display that vote is casted and also check no switch is pressed more than once

can anybody please help.
i m attaching my control unit and ballot unit code and schematic and also the final project schematic.

- - - Updated - - -

I m using mikroc pro v5.4 and proteus v7.8

- - - Updated - - -

RS232 communication between both units is interrupt based
 

Attachments

  • final final.rar
    556.4 KB · Views: 116
  • schematic.rar
    17.1 KB · Views: 110

Do you want RS232 communication code? What data are you sending? Are you not using SN75176?

Load pic2pic.hex to master and pic2pic_slave.hex to slave and simulate.

master sends characters 'A' and 'B' with a duration of 1 sec in between. Slave receives it and turns ON led if 'A' is received and turns OFF led if 'B' is received.

What is the problem you are facing in programming?
 

Attachments

  • PIC2PIC.rar
    61.8 KB · Views: 107
  • sim.rar
    118.7 KB · Views: 105
Last edited:

I have changed my project communication from RS485 to Rs232 thats why i m not using SN75176. problem is:

when any of the switch connected with ballot unit is pressed the glcd connected with the control unit display that vote is casted and also check no switch is pressed more than once and its not happening.simple transfer is happening
 

You have used SN75176 in your Proteus file. Why do you need it if you are doing rs232 communication?

Your switch polling code should be like

Code:
if((sw1 == 1) && (sw2 == 0) && (sw3 == 0) && (sw4 == 0) && (sw5 == 0)) {
    Delay_ms(100);
    if((sw1 == 1) && (sw2 == 0) && (sw3 == 0) && (sw4 == 0) && (sw5 == 0)) {
    
	led1 = 1;led2 = 0;led3 = 0;led4 = 0;;led5 = 0;

    }

}
else if((sw1 == 0) && (sw2 == 1) && (sw3 == 0) && (sw4 == 0) && (sw5 == 0)) {
    Delay_ms(100);
    if((sw1 == 1) && (sw2 == 1) && (sw3 == 0) && (sw4 == 0) && (sw5 == 0)) {
    
	led1 = 0;led2 = 1;led3 = 0;led4 = 0;;led5 = 0;

    }

}
else if((sw1 == 0) && (sw2 == 0) && (sw3 == 1) && (sw4 == 0) && (sw5 == 0)) {
    Delay_ms(100);
    if((sw1 == 1) && (sw2 == 0) && (sw3 == 1) && (sw4 == 0) && (sw5 == 0)) {
    
	led1 = 0;led2 = 0;led3 = 1;led4 = 0;;led5 = 0;

    }

}
else if((sw1 == 0) && (sw2 == 0) && (sw3 == 0) && (sw4 == 1) && (sw5 == 0)) {
    Delay_ms(100);
    if((sw1 == 0) && (sw2 == 0) && (sw3 == 0) && (sw4 == 1) && (sw5 == 0)) {
    
	led1 = 0;led2 = 0;led3 = 0;led4 = 1;;led5 = 0;

    }

}
else if((sw1 == 0) && (sw2 == 0) && (sw3 == 0) && (sw4 == 0) && (sw5 == 1)) {
    Delay_ms(100);
    if((sw1 == 0) && (sw2 == 0) && (sw3 == 0) && (sw4 == 0) && (sw5 == 1)) {
    
	led1 = 0;led2 = 0;led3 = 0;led4 = 0;;led5 = 1;

    }

}

also check no switch is pressed more than once

What if after one person casts vote and another wants to cast? Then all the switches have to be enabled. I think you should lock the buttons after casting the vote for 30 sec.

Is the person allows to cast vote after scanning with barcode and verified?

and its not happening

You have to make it happen using code.
 

Attachments

  • PIC2PIC v2.rar
    87.7 KB · Views: 102
  • PIC2PIC RS232.rar
    87.8 KB · Views: 94
Last edited:

I have changed my design for RS232 and removed SN75176. i have attached the new design
 

Attachments

  • new.rar
    16.8 KB · Views: 93

OK. So you need RS232 Communication. You need only one button to be pressed to cast vote and the buttons should not be pressed more than once and mutiple buttons should not be pressed. Am I right?

You can lock buttons after a person casts vote but it also needs to be activated so that another person can use. So, how are you re-enabling the buttons. What is the condition for re-enabling the buttons?

You need this code for button press.
Code:
if(keypressed == 0) {
	
	if((sw1 == 1) && (sw2 == 0) && (sw3 == 0) && (sw4 == 0) && (sw5 == 0)) {
    		Delay_ms(100);
    		if((sw1 == 1) && (sw2 == 0) && (sw3 == 0) && (sw4 == 0) && (sw5 == 0)) {
    
			led1 = 1;led2 = 0;led3 = 0;led4 = 0;;led5 = 0;
			keypressed = 1;

    		}
	
	}
	else if((sw1 == 0) && (sw2 == 1) && (sw3 == 0) && (sw4 == 0) && (sw5 == 0)) {
    		Delay_ms(100);
    		if((sw1 == 1) && (sw2 == 1) && (sw3 == 0) && (sw4 == 0) && (sw5 == 0)) {
    
			led1 = 0;led2 = 1;led3 = 0;led4 = 0;;led5 = 0;
			keypressed = 1;

    		}

	}
	else if((sw1 == 0) && (sw2 == 0) && (sw3 == 1) && (sw4 == 0) && (sw5 == 0)) {
    		Delay_ms(100);
    		if((sw1 == 1) && (sw2 == 0) && (sw3 == 1) && (sw4 == 0) && (sw5 == 0)) {
    
			led1 = 0;led2 = 0;led3 = 1;led4 = 0;;led5 = 0;
			keypressed = 1;

    		}

	}
	else if((sw1 == 0) && (sw2 == 0) && (sw3 == 0) && (sw4 == 1) && (sw5 == 0)) {
    		Delay_ms(100);
    		if((sw1 == 0) && (sw2 == 0) && (sw3 == 0) && (sw4 == 1) && (sw5 == 0)) {
    
			led1 = 0;led2 = 0;led3 = 0;led4 = 1;;led5 = 0;
			keypressed = 1;

    		}

	}
	else if((sw1 == 0) && (sw2 == 0) && (sw3 == 0) && (sw4 == 0) && (sw5 == 1)) {
    		Delay_ms(100);
    		if((sw1 == 0) && (sw2 == 0) && (sw3 == 0) && (sw4 == 0) && (sw5 == 1)) {
    		
			led1 = 0;led2 = 0;led3 = 0;led4 = 0;;led5 = 1;
			keypressed = 1;

    		}

	}

}

After you are allowed to cast vote, the person can press any one button one time and also cannot press multiple buttons.

?You have to use a condition like if cnic is matched and not repeated then the person is allowed to cast and at the same time keypressed is made 0.
 
Last edited:

yes its right. you got it. i have done the switch check which you said.

now if a switch 1 is pressed connected with one micro controller then how can i get that graphics lcd connected with other micro controller display that switch 1 is pressed.
 

You have to send a charcater like 'A' for switch1 and 'B' for Switch2... You have to send these characters to another PIC using UART. If the other PIC reads 'A' from UART then it displays "Switch 1 pressed" on GLCD. The GLCD code will be in the PIC connected to GLCD.

Another way is the PIC with buttons sends the text like "Switch A pressed!" from UART with a delimiter '!'. and PIC with GLCD reads this text and saves it in a string variable and that variable is used as an argument for the GLCD text display function.

Code:
PIC with buttons code


unsigned char key_char;

void main() {
	UART_Init(9600);




	while(1) {
		if(keypressed == 0) {
	
		if((sw1 == 1) && (sw2 == 0) && (sw3 == 0) && (sw4 == 0) && (sw5 == 0)) {
    			Delay_ms(100);
    			if((sw1 == 1) && (sw2 == 0) && (sw3 == 0) && (sw4 == 0) && (sw5 == 0)) {
    				led1 = 1;led2 = 0;led3 = 0;led4 = 0;;led5 = 0;
				keypressed = 1;
				key_char = 1;

    			}
	
		}
		else if((sw1 == 0) && (sw2 == 1) && (sw3 == 0) && (sw4 == 0) && (sw5 == 0)) {
    			Delay_ms(100);
    			if((sw1 == 1) && (sw2 == 1) && (sw3 == 0) && (sw4 == 0) && (sw5 == 0)) {
    				led1 = 0;led2 = 1;led3 = 0;led4 = 0;;led5 = 0;
				keypressed = 1;
				key_char = 2;

    			}

		}
		else if((sw1 == 0) && (sw2 == 0) && (sw3 == 1) && (sw4 == 0) && (sw5 == 0)) {
    			Delay_ms(100);
    			if((sw1 == 1) && (sw2 == 0) && (sw3 == 1) && (sw4 == 0) && (sw5 == 0)) {
    				led1 = 0;led2 = 0;led3 = 1;led4 = 0;;led5 = 0;
				keypressed = 1;
				key_char = 3;

    			}

		}
		else if((sw1 == 0) && (sw2 == 0) && (sw3 == 0) && (sw4 == 1) && (sw5 == 0)) {
    			Delay_ms(100);
    			if((sw1 == 0) && (sw2 == 0) && (sw3 == 0) && (sw4 == 1) && (sw5 == 0)) {
    				led1 = 0;led2 = 0;led3 = 0;led4 = 1;;led5 = 0;
				keypressed = 1;
				key_char = 4;

    			}

		}
		else if((sw1 == 0) && (sw2 == 0) && (sw3 == 0) && (sw4 == 0) && (sw5 == 1)) {
    			Delay_ms(100);
    			if((sw1 == 0) && (sw2 == 0) && (sw3 == 0) && (sw4 == 0) && (sw5 == 1)) {
    				led1 = 0;led2 = 0;led3 = 0;led4 = 0;;led5 = 1;
				keypressed = 1;
				key_char = 5;

    			}

		}

	}



	UART1_Write(key_char);
	
		
	}

}


PIC with GLCD code

unsigned char key_char;

Void main() {


	while(1) {

		// If data is ready, read it:
		if (UART_Data_Ready() == 1) { 
 			key_char = UART_Read();
 		}

		switch(key_char) {

			case '1':
				GLCD_Write_Text("Switch 1 Pressed", 3, 0, 1);
				break;

			case '2'

				.
				.
				.
				
			default:
				break;


		}


	}

}
 
Last edited:

I have tried what you said earlier but it is displaying default value case whether i press switch or not. i m attaching what i implement.

- - - Updated - - -

my attachments
 

Attachments

  • final 1.rar
    3.5 MB · Views: 94
  • new.rar
    16.8 KB · Views: 94

did i have to define extra register. actually i kept default case empty but still i guess when i press switch uart is not transmitting

- - - Updated - - -

did i have to define extra register. actually i kept default case empty but still i guess when i press switch uart is not transmitting

- - - Updated - - -

i mean how can the data saved in key_char will be sent through uart to pic connected with glcd
 

post the code of PIC with GLCD and PIC with buttons here. Don't post the project files.

See the above code posted by me. It clearly shows that UART1_Write(key_char) is transmitting key_char to PIC with GLCD.
 

PIC code with GLCD:

Code:
#include"image.h"
 #define max_character 18           // length of barcode
/*************************SDCARD************************/
sbit Mmc_Chip_Select           at RC0_bit;
sbit Mmc_Chip_Select_Direction at TRISC0_bit;

 /**************************glcd***************************/
char Glcd_DataPort at PORTD;

sbit Glcd_CS1 at RB5_bit;                   // GLCD chip select 1
sbit Glcd_CS2 at RB6_bit;                   // GLCD chip select 2
sbit Glcd_RS  at RB2_bit;                   // GLCD data/instruction
sbit Glcd_RW  at RB3_bit;                   // GLCD read/write
sbit Glcd_EN  at RB4_bit;                   // GLCD enable
sbit Glcd_RST at RB7_bit;                   // GLCD reset

sbit Glcd_CS1_Direction at TRISB5_bit;
sbit Glcd_CS2_Direction at TRISB6_bit;
sbit Glcd_RS_Direction  at TRISB2_bit;
sbit Glcd_RW_Direction  at TRISB3_bit;
sbit Glcd_EN_Direction  at TRISB4_bit;
sbit Glcd_RST_Direction at TRISB7_bit;

/*******************BARCODE****************************/
 sbit PS2_Data at RA3_bit;
sbit PS2_Clock at RA4_bit;
sbit PS2_Data_Direction at TRISA3_bit;
sbit PS2_Clock_Direction at TRISA4_bit;
/*********************************************************/
void UART_Write_2DArray(char _2DArray[10][16], unsigned int _row, unsigned int _col);

unsigned short keydata = 0, special = 0, down = 0;
char fat_txt[20] = "FAT16 not found";
char filename[11] = "voter1.txt"; // File names
char character='l';
unsigned long size;
char Buffer[512];
char txt[] = "34101-9764655-9";
 //char txt1[] = "34101-9764655-9";
char cnics[16];
char tmp2[16];
char cnics_array[10][16];
int res;
unsigned int count, row, col, _row, _col = 0;
unsigned char TwoDArray[10][16];
unsigned int k = 0;
unsigned int match_counter = 0, read_counter = 0;
int b_duplicate = 0;
int counter = 0;         // counter of barcode characters
char barcode [max_character];       // barcode string
char repeat[10]={0,0,0,0,0,0,0,0,0,0};
int ctr=-1;
char ctrtxt[10];
int row1;
//unsigned char received_text[50];
unsigned char key_char;
int di;
///////////////////////////////////barcode scan///////////////////////////////

void Barcodescan()
{    memset(barcode, 0, max_character);      // all character set to 0


       if (Ps2_Key_Read(&keydata, &special, &down))
        {
            if (down && (keydata == 13))
            {                 // Enter
                // Glcd_Fill(0);
                //Glcd_Write_Text(barcode, 0, 0, 1);
                memset(barcode, 0, max_character);
                counter = 0;
                // UART1_Write(text);

            }
            else if (down && (keydata == 16))
            {
            counter--;
                barcode[counter] = 0;
                               // set previous charater to 0
            }
            else if (down && !special && keydata)
           {        // other keys
               barcode[counter++] = keydata;


           }
        }

}
 ///////////////////////////file read & compare//////////////////////////////////
void M_Open_File_Read()
{

     Mmc_Fat_Assign(&filename, 0);
     Mmc_Fat_Reset(&size);
     row = 0;
     j = 0;
     do {



        ctr++;
        Mmc_Fat_Read(&character);

        while(character!= ' ') {
             cnics[count] = character;
             count++;
             Mmc_Fat_Read(&character);
        }
        if(character == ' ') {
               while(character != '\n') {
                       Mmc_Fat_Read(&character);
               }
        }
        cnics[count] = '\0';
        UART1_Write_Text("cnics = ");
        UART1_Write_Text(cnics);
        UART1_Write(10);
        UART1_Write(13);
        i = 0;
        col = 0;
        for(i=0;i<count;i++) {
          /*
          if(cnics[i] == '\0')
          {
                cnics_array[row][col]='\0';
          }
          else {
          */      cnics_array[row][col]= cnics[i];
                  col++;
          //}
        }
        count = 0;
        //UART1_Write_Text("cnics array = ");
        //UART_Write_2DArray(cnics_array, row, col);
        UART1_Write(10);
        UART1_Write(13);

        for (k=0;k<15;k++) {
            tmp2[k] =  cnics_array[row][k];
        }
        //tmp2[15] = '\0';
        //UART1_Write_Text("tmp2 = ");
        //UART1_Write_Text(tmp2);
        UART1_Write(10);
        UART1_Write(13);
        //UART1_Write_Text("txt = ");
        //UART1_Write_Text(txt);
        UART1_Write(10);
        UART1_Write(13);
        row++;
        j++;
        k = 0;
        col = 0;
        for(i=0;i<15;i++) {
             if(txt[i] == tmp2[i]) {
                    match_counter++;

                     //  repeat[i]=1;

             }

        }
        i = 0;
         /*     UART1_Write_Text("ctr = ");



              IntToStr(ctr, ctrtxt);  // txt is " -4220" (one blank here)

              UART1_Write_Text(ctrtxt);
           */

        if(match_counter == 15 && repeat[ctr] ==0) {
        
               repeat[ctr]=1;
            UART1_Write_Text("txt[] matches with tmp2[]");


            // UART1_Write_Text("repeat = ");
            /*for(row1=0;row1<10;row1++)
            {
            IntToStr(repeat[row1], repeattxt);
               UART1_Write_Text(repeattxt);
                UART1_Write_Text(" ");
             }*/
              UART1_Write_Text(13);
             Glcd_Write_Text("Welcome To Voting Machine", 3, 3, 0);
            b_duplicate = 1;
            break;

        }


        else
        {

            UART1_Write_Text("txt[] doesn't match with tmp2[]");
            repeat[ctr]=0;
             Glcd_Write_Text("Oops! can't cast vote", 3, 3, 0);
            b_duplicate = 0;
        }
        UART1_Write(10);
        UART1_Write(13);
        UART1_Write(10);
        UART1_Write(13);

        match_counter = 0;


     } while(row < 10);

}

///////////////////////////////////verification/////////////////////////////////

 int VerifyDuplicateVoter()

 {        M_Open_File_Read();


 }

  void UART_Write_2DArray(char _2DArray[][16], unsigned int _row, unsigned int _col) {
           for(i=0;i<=_col;i++) {
                 TXREG = _2DArray[_row][i];
                 while(!PIR1.TXIF); //(while(!TXSTA.TRMT); TXSTA>TRMT = 0;
                 PIR1.TXIF = 0;
                 PIE1.TXIE = 1;
           }
}
//-------------- Main. Uncomment the function(s) to test the desired operation(s)

void main() 
{
    PORTB = 0x00;
     TRISB = 0X00;
     PORTA = 1;
     TRISA = 1;
     TRISD=0;
     TRISC =0;
     PORTC = 0x02;
     TRISC.F6 = 0;
     TRISC.F7 = 1;
     PORTC = 0x02;
     UART1_Init(9600);
     Ps2_Config();
     Glcd_Init();

     Glcd_Fill(0xff);

      Glcd_Set_Font(Font_Glcd_Character8x7, 8, 7, 32);

    Ps2_Config();
    delay_ms(1000);



     SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV64, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);


          if (Mmc_Fat_Init ()==0)
       {
         // reinitialize spi at higher speed

        SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV4, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);

         //M_Open_File_Read();
         res =  VerifyDuplicateVoter();
         while(1) {

                // If data is ready, read it:
                if (UART1_Data_Ready() == 1) {
                         key_char = UART1_Read();
                 }

                switch(key_char) {

                        case '1':
                                GLCD_Write_Text("Switch 1 Pressed", 3, 0, 1);
                                break;

                        case '2':

                                GLCD_Write_Text("Switch 1 Pressed", 3, 0, 1);
                                break;
                                
                        case '3':

                                GLCD_Write_Text("Switch 1 Pressed", 3, 0, 1);
                                break;
                                
                         case '4':

                                GLCD_Write_Text("Switch 1 Pressed", 3, 0, 1);
                                break;
                                
                         case '5':

                                GLCD_Write_Text("Switch 1 Pressed", 3, 0, 1);
                                break;

                        default:
                                break;


                }


        }


        }

        else if (Mmc_Fat_Init()== 1)
        {
         UART1_Write('y');
        }
     
        else
       {
         UART1_Write('z');

        }

      }

PIC code with Buttons:
Code:
#define sw1 PORTD.F0
#define sw2 PORTD.F1
#define sw3 PORTD.F2
#define sw4 PORTD.F3
#define sw5 PORTD.F4
#define led1 PORTC.F0
#define led2 PORTC.F1
#define led3 PORTC.F2
#define led4 PORTC.F3
#define led5 PORTC.F4


int keypressed =0;
unsigned char key_char;
void voteCount();
int count=0;
void main()
{
TRISD=0xFF;
//TRISC=0x00;
 TRISB = 0x00;
 PORTB = 0x00;
 TRISC.F6 = 1;
     TRISC.F7 = 0;
 UART1_Init(9600);
sw1=sw2=sw3=sw4=sw5=0;
voteCount();

}

void votecount()
{
     while(1) {
                  if(keypressed == 0) {

                  if((sw1 == 1) && (sw2 == 0) && (sw3 == 0) && (sw4 == 0) && (sw5 == 0)) {
                            Delay_ms(100);
                            if((sw1 == 1) && (sw2 == 0) && (sw3 == 0) && (sw4 == 0) && (sw5 == 0)) {
                                    led1 = 1;led2 = 0;led3 = 0;led4 = 0;;led5 = 0;
                                keypressed = 1;
                                key_char = 1;
                                //UART1_Write_Text("switch 1 pressed!");

                            }

                }
                else if((sw1 == 0) && (sw2 == 1) && (sw3 == 0) && (sw4 == 0) && (sw5 == 0)) {
                            Delay_ms(100);
                            if((sw1 == 1) && (sw2 == 1) && (sw3 == 0) && (sw4 == 0) && (sw5 == 0)) {
                                    led1 = 0;led2 = 1;led3 = 0;led4 = 0;;led5 = 0;
                                keypressed = 1;
                                key_char = 2;
                                //UART1_Write_Text("switch 2 pressed!");

                            }

                }
                else if((sw1 == 0) && (sw2 == 0) && (sw3 == 1) && (sw4 == 0) && (sw5 == 0)) {
                            Delay_ms(100);
                            if((sw1 == 1) && (sw2 == 0) && (sw3 == 1) && (sw4 == 0) && (sw5 == 0)) {
                                    led1 = 0;led2 = 0;led3 = 1;led4 = 0;;led5 = 0;
                                keypressed = 1;
                                key_char = 3;
                           // UART1_Write_Text("switch 3 pressed!");

                            }

                }
                else if((sw1 == 0) && (sw2 == 0) && (sw3 == 0) && (sw4 == 1) && (sw5 == 0)) {
                            Delay_ms(100);
                            if((sw1 == 0) && (sw2 == 0) && (sw3 == 0) && (sw4 == 1) && (sw5 == 0)) {
                                    led1 = 0;led2 = 0;led3 = 0;led4 = 1;;led5 = 0;
                                keypressed = 1;
                                key_char = 4;
                                 //UART1_Write_Text("switch 4 pressed!");

                            }

                }
                else if((sw1 == 0) && (sw2 == 0) && (sw3 == 0) && (sw4 == 0) && (sw5 == 1)) {
                            Delay_ms(100);
                            if((sw1 == 0) && (sw2 == 0) && (sw3 == 0) && (sw4 == 0) && (sw5 == 1)) {
                                    led1 = 0;led2 = 0;led3 = 0;led4 = 0;;led5 = 1;
                                keypressed = 1;
                                key_char = 5;
                                 //UART1_Write_Text("switch 5 pressed!");

                            }

                }

        }



        UART1_Write(key_char);


        }

}

- - - Updated - - -

i tried what you said but glcd is displaying any result

- - - Updated - - -

glcd not displaying any result
 

Try these codes

Code:
#include"image.h"
 #define max_character 18           // length of barcode
/*************************SDCARD************************/
sbit Mmc_Chip_Select           at RC0_bit;
sbit Mmc_Chip_Select_Direction at TRISC0_bit;

 /**************************glcd***************************/
char Glcd_DataPort at PORTD;

sbit Glcd_CS1 at RB5_bit;                   // GLCD chip select 1
sbit Glcd_CS2 at RB6_bit;                   // GLCD chip select 2
sbit Glcd_RS  at RB2_bit;                   // GLCD data/instruction
sbit Glcd_RW  at RB3_bit;                   // GLCD read/write
sbit Glcd_EN  at RB4_bit;                   // GLCD enable
sbit Glcd_RST at RB7_bit;                   // GLCD reset

sbit Glcd_CS1_Direction at TRISB5_bit;
sbit Glcd_CS2_Direction at TRISB6_bit;
sbit Glcd_RS_Direction  at TRISB2_bit;
sbit Glcd_RW_Direction  at TRISB3_bit;
sbit Glcd_EN_Direction  at TRISB4_bit;
sbit Glcd_RST_Direction at TRISB7_bit;

/*******************BARCODE****************************/
 sbit PS2_Data at RA3_bit;
sbit PS2_Clock at RA4_bit;
sbit PS2_Data_Direction at TRISA3_bit;
sbit PS2_Clock_Direction at TRISA4_bit;
/*********************************************************/
void UART_Write_2DArray(char _2DArray[10][16], unsigned int _row, unsigned int _col);

unsigned short keydata = 0, special = 0, down = 0;
char fat_txt[20] = "FAT16 not found";
char filename[11] = "voter1.txt"; // File names
char character='l';
unsigned long size;
char Buffer[512];
char txt[] = "34101-9764655-9";
 //char txt1[] = "34101-9764655-9";
char cnics[16];
char tmp2[16];
char cnics_array[10][16];
int res;
unsigned int count, row, col, _row, _col = 0;
unsigned char TwoDArray[10][16];
unsigned int k = 0;
unsigned int match_counter = 0, read_counter = 0;
int b_duplicate = 0;
int counter = 0;         // counter of barcode characters
char barcode [max_character];       // barcode string
char repeat[10]={0,0,0,0,0,0,0,0,0,0};
int ctr=-1;
char ctrtxt[10];
int row1;
//unsigned char received_text[50];
unsigned char key_char;
int di;
///////////////////////////////////barcode scan///////////////////////////////

void Barcodescan()
{    memset(barcode, 0, max_character);      // all character set to 0


       if (Ps2_Key_Read(&keydata, &special, &down))
        {
            if (down && (keydata == 13))
            {                 // Enter
                // Glcd_Fill(0);
                //Glcd_Write_Text(barcode, 0, 0, 1);
                memset(barcode, 0, max_character);
                counter = 0;
                // UART1_Write(text);

            }
            else if (down && (keydata == 16))
            {
            counter--;
                barcode[counter] = 0;
                               // set previous charater to 0
            }
            else if (down && !special && keydata)
           {        // other keys
               barcode[counter++] = keydata;


           }
        }

}
 ///////////////////////////file read & compare//////////////////////////////////
void M_Open_File_Read()
{

     Mmc_Fat_Assign(&filename, 0);
     Mmc_Fat_Reset(&size);
     row = 0;
     j = 0;
     do {



        ctr++;
        Mmc_Fat_Read(&character);

        while(character!= ' ') {
             cnics[count] = character;
             count++;
             Mmc_Fat_Read(&character);
        }
        if(character == ' ') {
               while(character != '\n') {
                       Mmc_Fat_Read(&character);
               }
        }
        cnics[count] = '\0';
        UART1_Write_Text("cnics = ");
        UART1_Write_Text(cnics);
        UART1_Write(10);
        UART1_Write(13);
        i = 0;
        col = 0;
        for(i=0;i<count;i++) {
          /*
          if(cnics[i] == '\0')
          {
                cnics_array[row][col]='\0';
          }
          else {
          */      cnics_array[row][col]= cnics[i];
                  col++;
          //}
        }
        count = 0;
        //UART1_Write_Text("cnics array = ");
        //UART_Write_2DArray(cnics_array, row, col);
        UART1_Write(10);
        UART1_Write(13);

        for (k=0;k<15;k++) {
            tmp2[k] =  cnics_array[row][k];
        }
        //tmp2[15] = '\0';
        //UART1_Write_Text("tmp2 = ");
        //UART1_Write_Text(tmp2);
        UART1_Write(10);
        UART1_Write(13);
        //UART1_Write_Text("txt = ");
        //UART1_Write_Text(txt);
        UART1_Write(10);
        UART1_Write(13);
        row++;
        j++;
        k = 0;
        col = 0;
        for(i=0;i<15;i++) {
             if(txt[i] == tmp2[i]) {
                    match_counter++;

                     //  repeat[i]=1;

             }

        }
        i = 0;
         /*     UART1_Write_Text("ctr = ");



              IntToStr(ctr, ctrtxt);  // txt is " -4220" (one blank here)

              UART1_Write_Text(ctrtxt);
           */

        if(match_counter == 15 && repeat[ctr] ==0) {
        
               repeat[ctr]=1;
            UART1_Write_Text("txt[] matches with tmp2[]");


            // UART1_Write_Text("repeat = ");
            /*for(row1=0;row1<10;row1++)
            {
            IntToStr(repeat[row1], repeattxt);
               UART1_Write_Text(repeattxt);
                UART1_Write_Text(" ");
             }*/
              UART1_Write_Text(13);
             Glcd_Write_Text("Welcome To Voting Machine", 3, 3, 0);
            b_duplicate = 1;
		UART1_Write('1');
            break;

        }


        else
        {

            UART1_Write_Text("txt[] doesn't match with tmp2[]");
            repeat[ctr]=0;
             Glcd_Write_Text("Oops! can't cast vote", 3, 3, 0);
            b_duplicate = 0;
		UART1_Write('0');
        }
        UART1_Write(10);
        UART1_Write(13);
        UART1_Write(10);
        UART1_Write(13);

        match_counter = 0;


     } while(row < 10);

}

///////////////////////////////////verification/////////////////////////////////

 int VerifyDuplicateVoter()

 {        M_Open_File_Read();


 }

  void UART_Write_2DArray(char _2DArray[][16], unsigned int _row, unsigned int _col) {
           for(i=0;i<=_col;i++) {
                 TXREG = _2DArray[_row][i];
                 while(!PIR1.TXIF); //(while(!TXSTA.TRMT); TXSTA>TRMT = 0;
                 PIR1.TXIF = 0;
                 PIE1.TXIE = 1;
           }
}
//-------------- Main. Uncomment the function(s) to test the desired operation(s)

void main() 
{
    PORTB = 0x00;
     TRISB = 0X00;
     PORTA = 1;
     TRISA = 1;
     TRISD=0;
     TRISC =0;
     PORTC = 0x02;
     TRISC.F6 = 0;
     TRISC.F7 = 1;
     PORTC = 0x02;
     UART1_Init(9600);
     Ps2_Config();
     Glcd_Init();

     Glcd_Fill(0xff);

      Glcd_Set_Font(Font_Glcd_Character8x7, 8, 7, 32);

    Ps2_Config();
    delay_ms(1000);



     SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV64, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);


          if (Mmc_Fat_Init ()==0)
       {
         // reinitialize spi at higher speed

        SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV4, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);

         //M_Open_File_Read();
         res =  VerifyDuplicateVoter();
         while(1) {

                // If data is ready, read it:
                if (UART1_Data_Ready() == 1) {
                         key_char = UART1_Read();
                 }

                switch(key_char) {

                        case '1':
                                GLCD_Write_Text("Switch 1 Pressed", 3, 0, 1);
                                break;

                        case '2':

                                GLCD_Write_Text("Switch 2 Pressed", 3, 0, 1);
                                break;
                                
                        case '3':

                                GLCD_Write_Text("Switch 3 Pressed", 3, 0, 1);
                                break;
                                
                         case '4':

                                GLCD_Write_Text("Switch 4 Pressed", 3, 0, 1);
                                break;
                                
                         case '5':

                                GLCD_Write_Text("Switch 5 Pressed", 3, 0, 1);
                                break;

                        default:
                                break;


                };


        }


        }

        else if (Mmc_Fat_Init()== 1)
        {
         UART1_Write('y');
        }
     
        else
       {
         UART1_Write('z');

        }

      }
Code:
#define sw1 PORTD.F0
#define sw2 PORTD.F1
#define sw3 PORTD.F2
#define sw4 PORTD.F3
#define sw5 PORTD.F4
#define led1 PORTC.F0
#define led2 PORTC.F1
#define led3 PORTC.F2
#define led4 PORTC.F3
#define led5 PORTC.F4


int keypressed =0;
unsigned char key_char, received_char;
void voteCount();
int count=0;
void main()
{
TRISD=0xFF;
//TRISC=0x00;
 TRISB = 0x00;
 PORTB = 0x00;
 TRISC.F6 = 1;
     TRISC.F7 = 0;
 UART1_Init(9600);
sw1=sw2=sw3=sw4=sw5=0;
voteCount();

}

void votecount()
{
     while(1) {
		
		// If data is ready, read it:
                if (UART1_Data_Ready() == 1) {
                         received_char = UART1_Read();
                 }
	
		if(received_char == '0') {
			keypressed = 1;
		}
		else if(received_char == '1') {
			keypressed = 0;
		}	
			
                  if(keypressed == 0) {

                  if((sw1 == 1) && (sw2 == 0) && (sw3 == 0) && (sw4 == 0) && (sw5 == 0)) {
                            Delay_ms(100);
                            if((sw1 == 1) && (sw2 == 0) && (sw3 == 0) && (sw4 == 0) && (sw5 == 0)) {
                                    led1 = 1;led2 = 0;led3 = 0;led4 = 0;;led5 = 0;
                                keypressed = 1;
                                key_char = '1';
                                //UART1_Write_Text("switch 1 pressed!");

                            }

                }
                else if((sw1 == 0) && (sw2 == 1) && (sw3 == 0) && (sw4 == 0) && (sw5 == 0)) {
                            Delay_ms(100);
                            if((sw1 == 0) && (sw2 == 1) && (sw3 == 0) && (sw4 == 0) && (sw5 == 0)) {
                                    led1 = 0;led2 = 1;led3 = 0;led4 = 0;;led5 = 0;
                                keypressed = 1;
                                key_char = '2';
                                //UART1_Write_Text("switch 2 pressed!");

                            }

                }
                else if((sw1 == 0) && (sw2 == 0) && (sw3 == 1) && (sw4 == 0) && (sw5 == 0)) {
                            Delay_ms(100);
                            if((sw1 == 0) && (sw2 == 0) && (sw3 == 1) && (sw4 == 0) && (sw5 == 0)) {
                                    led1 = 0;led2 = 0;led3 = 1;led4 = 0;;led5 = 0;
                                keypressed = 1;
                                key_char = '3';
                           // UART1_Write_Text("switch 3 pressed!");

                            }

                }
                else if((sw1 == 0) && (sw2 == 0) && (sw3 == 0) && (sw4 == 1) && (sw5 == 0)) {
                            Delay_ms(100);
                            if((sw1 == 0) && (sw2 == 0) && (sw3 == 0) && (sw4 == 1) && (sw5 == 0)) {
                                    led1 = 0;led2 = 0;led3 = 0;led4 = 1;;led5 = 0;
                                keypressed = 1;
                                key_char = '4';
                                 //UART1_Write_Text("switch 4 pressed!");

                            }

                }
                else if((sw1 == 0) && (sw2 == 0) && (sw3 == 0) && (sw4 == 0) && (sw5 == 1)) {
                            Delay_ms(100);
                            if((sw1 == 0) && (sw2 == 0) && (sw3 == 0) && (sw4 == 0) && (sw5 == 1)) {
                                    led1 = 0;led2 = 0;led3 = 0;led4 = 0;;led5 = 1;
                                keypressed = 1;
                                key_char = '5';
                                 //UART1_Write_Text("switch 5 pressed!");

                            }

                }

        }



        UART1_Write(key_char);


        }

}
 
Last edited:

thanks alot.its works exactly what i wanted.

their is question that can i clear glcd previous data displayed to make next to be displayed
 

Yes. You can add
Code:
 GLCD_Write_Text("                                ", 3,0,1);

You have to do like this

Code:
// If data is ready, read it:
                if (UART1_Data_Ready() == 1) {
                         key_char = UART1_Read();
                         GLCD_Write_Text("                                ", 3,0,1);
                 }
 

i tried glcd but doesn't work. if the switch is pressed then can i save in some variable or array so that when i need it i can use it after wards.
 

my code for control unit:
Code:
 #define max_character 18           // length of barcode
/*************************SDCARD************************/
sbit Mmc_Chip_Select           at RC0_bit;
sbit Mmc_Chip_Select_Direction at TRISC0_bit;

 /**************************glcd***************************/
char Glcd_DataPort at PORTD;

sbit Glcd_CS1 at RB5_bit;                   // GLCD chip select 1
sbit Glcd_CS2 at RB6_bit;                   // GLCD chip select 2
sbit Glcd_RS  at RB2_bit;                   // GLCD data/instruction
sbit Glcd_RW  at RB3_bit;                   // GLCD read/write
sbit Glcd_EN  at RB4_bit;                   // GLCD enable
sbit Glcd_RST at RB7_bit;                   // GLCD reset

sbit Glcd_CS1_Direction at TRISB5_bit;
sbit Glcd_CS2_Direction at TRISB6_bit;
sbit Glcd_RS_Direction  at TRISB2_bit;
sbit Glcd_RW_Direction  at TRISB3_bit;
sbit Glcd_EN_Direction  at TRISB4_bit;
sbit Glcd_RST_Direction at TRISB7_bit;

/*******************BARCODE****************************/
 sbit PS2_Data at RA3_bit;
sbit PS2_Clock at RA4_bit;
sbit PS2_Data_Direction at TRISA3_bit;
sbit PS2_Clock_Direction at TRISA4_bit;
/*********************************************************/
void UART_Write_2DArray(char _2DArray[10][16], unsigned int _row, unsigned int _col);

unsigned short keydata = 0, special = 0, down = 0;
char fat_txt[20] = "FAT16 not found";
char filename[11] = "voter1.txt"; // File names
char character='l';
unsigned long size;
char Buffer[512];
char txt[] = "34101-9764655-9";
 //char txt1[] = "34101-9764655-9";
char cnics[16];
char tmp2[16];
char cnics_array[10][16];
int res;
unsigned int count, row, col, _row, _col = 0;
unsigned char TwoDArray[10][16];
unsigned int j,i,k = 0;
unsigned int match_counter = 0, read_counter = 0;
int b_duplicate = 0;
int counter = 0;         // counter of barcode characters
char barcode [max_character];       // barcode string
char repeat[10]={0,0,0,0,0,0,0,0,0,0};
int ctr=-1;
char ctrtxt[10];
int row1;
//unsigned char received_text[50];
unsigned char key_char;
int di;
///////////////////////////////////barcode scan///////////////////////////////

void Barcodescan()
{    memset(barcode, 0, max_character);      // all character set to 0


       if (Ps2_Key_Read(&keydata, &special, &down))
        {
            if (down && (keydata == 13))
            {                 // Enter
                // Glcd_Fill(0);
                //Glcd_Write_Text(barcode, 0, 0, 1);
                memset(barcode, 0, max_character);
                counter = 0;
                // UART1_Write(text);

            }
            else if (down && (keydata == 16))
            {
            counter--;
                barcode[counter] = 0;
                               // set previous charater to 0
            }
            else if (down && !special && keydata)
           {        // other keys
               barcode[counter++] = keydata;


           }
        }

}
 ///////////////////////////file read & compare//////////////////////////////////
void M_Open_File_Read()
{

     Mmc_Fat_Assign(&filename, 0);
     Mmc_Fat_Reset(&size);
     row = 0;
     j = 0;
     do {



        ctr++;
        Mmc_Fat_Read(&character);

        while(character!= ' ') {
             cnics[count] = character;
             count++;
             Mmc_Fat_Read(&character);
        }
        if(character == ' ') {
               while(character != '\n') {
                       Mmc_Fat_Read(&character);
               }
        }
        cnics[count] = '\0';
        UART1_Write_Text("cnics = ");
        UART1_Write_Text(cnics);
        UART1_Write(10);
        UART1_Write(13);
        i = 0;
        col = 0;
        for(i=0;i<count;i++) {
          /*
          if(cnics[i] == '\0')
          {
                cnics_array[row][col]='\0';
          }
          else {
          */      cnics_array[row][col]= cnics[i];
                  col++;
          //}
        }
        count = 0;
        //UART1_Write_Text("cnics array = ");
        //UART_Write_2DArray(cnics_array, row, col);
        UART1_Write(10);
        UART1_Write(13);

        for (k=0;k<15;k++) {
            tmp2[k] =  cnics_array[row][k];
        }
        //tmp2[15] = '\0';
        //UART1_Write_Text("tmp2 = ");
        //UART1_Write_Text(tmp2);
        UART1_Write(10);
        UART1_Write(13);
        //UART1_Write_Text("txt = ");
        //UART1_Write_Text(txt);
        UART1_Write(10);
        UART1_Write(13);
        row++;
        j++;
        k = 0;
        col = 0;
        for(i=0;i<15;i++) {
             if(txt[i] == tmp2[i]) {
                    match_counter++;

                     //  repeat[i]=1;

             }

        }
        i = 0;
         /*     UART1_Write_Text("ctr = ");



              IntToStr(ctr, ctrtxt);  // txt is " -4220" (one blank here)

              UART1_Write_Text(ctrtxt);
           */

        if(match_counter == 15 && repeat[ctr] ==0) {

               repeat[ctr]=1;
            UART1_Write_Text("txt[] matches with tmp2[]");


            // UART1_Write_Text("repeat = ");
            /*for(row1=0;row1<10;row1++)
            {
            IntToStr(repeat[row1], repeattxt);
               UART1_Write_Text(repeattxt);
                UART1_Write_Text(" ");
             }*/
              UART1_Write_Text(13);
               GLCD_Write_Text("                                    ", 2, 3, 0);
             Glcd_Write_Text("Welcome To Voting Machine", 3, 3, 0);

            b_duplicate = 1;
                UART1_Write('1');
            break;

        }


        else
        {

            UART1_Write_Text("txt[] doesn't match with tmp2[]");
            repeat[ctr]=0;
             Glcd_Write_Text("Oops! can't cast vote", 3, 3, 0);
            b_duplicate = 0;
                UART1_Write('0');
        }
        UART1_Write(10);
        UART1_Write(13);
        UART1_Write(10);
        UART1_Write(13);

        match_counter = 0;


     } while(row < 10);

}

///////////////////////////////////verification/////////////////////////////////

 int VerifyDuplicateVoter()

 {        M_Open_File_Read();


 }

  void UART_Write_2DArray(char _2DArray[][16], unsigned int _row, unsigned int _col) {
           for(i=0;i<=_col;i++) {
                 TXREG = _2DArray[_row][i];
                 while(!PIR1.TXIF); //(while(!TXSTA.TRMT); TXSTA>TRMT = 0;
                 PIR1.TXIF = 0;
                 PIE1.TXIE = 1;
           }
}
//-------------- Main. Uncomment the function(s) to test the desired operation(s)

void main() {
   PORTB = 0x00;
     TRISB = 0X00;
     PORTA = 1;
     TRISA = 1;
     TRISD=0;
     TRISC =0;
     PORTC = 0x02;
     TRISC.F6 = 0;
     TRISC.F7 = 1;
     PORTC = 0x02;
     UART1_Init(9600);
     Ps2_Config();
     Glcd_Init();

     Glcd_Fill(0xff);

      Glcd_Set_Font(Font_Glcd_Character8x7, 8, 7, 32);

    Ps2_Config();
    delay_ms(1000);



     SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV64, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);


          if (Mmc_Fat_Init ()==0)
       {
         // reinitialize spi at higher speed

        SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV4, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);

         //M_Open_File_Read();
         res =  VerifyDuplicateVoter();
         while(1) {

                // If data is ready, read it:
                if (UART1_Data_Ready() == 1) {
                         key_char = UART1_Read();
                         GLCD_Write_Text("                                    ", 3, 5, 1);
                 }

                switch(key_char) {

                        case '1':
                                GLCD_Write_Text("Switch 1 Pressed", 3, 0, 1);
                                break;

                        case '2':

                                GLCD_Write_Text("Switch 2 Pressed", 3, 0, 1);
                                break;

                        case '3':

                                GLCD_Write_Text("Switch 3 Pressed", 3, 0, 1);
                                break;

                         case '4':

                                GLCD_Write_Text("Switch 4 Pressed", 3, 0, 1);
                                break;

                         case '5':

                                GLCD_Write_Text("Switch 5 Pressed", 3, 0, 1);
                                break;

                        default:
                                break;


                };


        }


        }

        else if (Mmc_Fat_Init()== 1)
        {
         UART1_Write('y');
        }

        else
       {
         UART1_Write('z');

        }
}

- - - Updated - - -

yes glcd clearing. and i want to save that when switch is pressed and vote is casted then save the result in some array so that when i needed that after wards then i can use it
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top