Vous êtes sur la page 1sur 5

/*

* File: evm.c
* Author: Innovate Logics
*
* Created on May 31, 2015, 1:36 PM
*/

#include
#include
#include "config.h"
#include "appl_head.h"

#define ctrl PORTA


#define vote_exit 0x04
#define vote_ctrl 0x08
#define vote_mode 0x20
#define rslt_mode 0x10
#define vote_panel PORTB
#define party1 0x01
#define party2 0x02
#define party3 0x04
#define party4 0x08
#define party5 0x10
#define party6 0x20
#define party7 0x40
#define party8 0x80

unsigned int vote_cnt_party1;


unsigned int vote_cnt_party2;
unsigned int vote_cnt_party3;
unsigned int vote_cnt_party4;
unsigned int vote_cnt_party5;
unsigned int vote_cnt_party6;
unsigned int vote_cnt_party7;
unsigned int vote_cnt_party8;
/*
*
*/

void delay(unsigned int delay){

unsigned int count=0;


count = (delay*50000);
while (count>0){
count--;
}
}

void retreive_vote_cnt(){
if(nvm_read(0x00)!=0xff){
vote_cnt_party1 = nvm_read(0x00);vote_cnt_party1<<8;
vote_cnt_party1 |= nvm_read(0x01);
}

if(nvm_read(0x02)!=0xff){
vote_cnt_party2 = nvm_read(0x02);vote_cnt_party2<<8;
vote_cnt_party2 |= nvm_read(0x03);
}

if(nvm_read(0x04)!=0xff){
vote_cnt_party3 = nvm_read(0x04);vote_cnt_party3<<8;
vote_cnt_party3 |= nvm_read(0x05);
}

if(nvm_read(0x06)!=0xff){
vote_cnt_party4 = nvm_read(0x06);vote_cnt_party4<<8;
vote_cnt_party4 |= nvm_read(0x07);
}

if(nvm_read(0x08)!=0xff){
vote_cnt_party5 = nvm_read(0x08);vote_cnt_party5<<8;
vote_cnt_party5 |= nvm_read(0x09);
}

if(nvm_read(0x0a)!=0xff){
vote_cnt_party6 = nvm_read(0x0a);vote_cnt_party6<<8;
vote_cnt_party6 |= nvm_read(0x0b);
}

if(nvm_read(0x0c)!=0xff){
vote_cnt_party7 = nvm_read(0x0c);vote_cnt_party7<<8;
vote_cnt_party7 |= nvm_read(0x0d);
}

if(nvm_read(0x0e)!=0xff){
vote_cnt_party8 = nvm_read(0x0e);vote_cnt_party8<<8;
vote_cnt_party8 |= nvm_read(0x0f);
}
}

void voting_mode(){
while(1){
lcd_command(0x01);lcd_display("Wait");
while(!((ctrl & vote_ctrl) || (ctrl & rslt_mode)));

if(ctrl & rslt_mode){


while(!(ctrl == 0x00));
result_mode();
}
else{
lcd_command(0x01);lcd_command(0x84);lcd_display("Press the key");
lcd_command(0xc0);lcd_display(" to enter your vote");

while(vote_panel == 0x00 && ctrl == 0x00);

if(vote_panel & party1){


while(!(vote_panel == 0x00));
vote_cnt_party1++;save_votes(1);
lcd_command(0x01);lcd_display("Vote counted");
}
else if(vote_panel & party2){
while(!(vote_panel == 0x00));
vote_cnt_party2++;save_votes(2);
lcd_command(0x01);lcd_display("Vote counted");
}
else if(vote_panel & party3){
while(!(vote_panel == 0x00));
vote_cnt_party3++;save_votes(3);
lcd_command(0x01);lcd_display("Vote counted");
}
else if(vote_panel & party4){
while(!(vote_panel == 0x00));
vote_cnt_party4++;save_votes(4);
lcd_command(0x01);lcd_display("Vote counted");
}
else if(vote_panel & party5){
while(!(vote_panel == 0x00));
vote_cnt_party5++;save_votes(5);
lcd_command(0x01);lcd_display("Vote counted");
}
else if(vote_panel & party6){
while(!(vote_panel == 0x00));
vote_cnt_party6++;save_votes(6);
lcd_command(0x01);lcd_display("Vote counted");
}
else if(vote_panel & party7){
while(!(vote_panel == 0x00));
vote_cnt_party7++;save_votes(7);
lcd_command(0x01);lcd_display("Vote counted");
}
else if(vote_panel & party8){
while(!(vote_panel == 0x00));
vote_cnt_party8++;save_votes(8);
lcd_command(0x01);lcd_display("Vote counted");
}
}
}
}

void nvm_write(unsigned char address, unsigned char value){


EECON1 = 0x04;
EEADR = address;
EEDATA = value;
EECON2 = 0x55;
EECON2 = 0xaa;
WR = 1;
// Wait for the byte to be written in EEPROM
while(EEIF == 0);
EEIF = 0;
// Disabling write cycles
EECON1 = 0x00;
}

unsigned int nvm_read(unsigned char rd_address){


EECON1 = 0x00;
EEADR = rd_address;
RD = 1;
while(RD == 1);
return EEDATA;
}

void save_votes(unsigned char party){


EECON1 = 0x04;
switch(party){
case 1:
nvm_write(0x00,vote_cnt_party1>>8);
nvm_write(0x01,vote_cnt_party1);
break;
case 2:
nvm_write(0x02,vote_cnt_party2>>8);
nvm_write(0x03,vote_cnt_party2);
break;
case 3:
nvm_write(0x04,vote_cnt_party3>>8);
nvm_write(0x05,vote_cnt_party3);
break;
case 4:
nvm_write(0x06,vote_cnt_party4>>8);
nvm_write(0x07,vote_cnt_party4);
break;
case 5:
nvm_write(0x08,vote_cnt_party5>>8);
nvm_write(0x09,vote_cnt_party5);
break;
case 6:
nvm_write(0x0a,vote_cnt_party6>>8);
nvm_write(0x0b,vote_cnt_party6);
break;
case 7:
nvm_write(0x0c,vote_cnt_party7>>8);
nvm_write(0x0d,vote_cnt_party7);
break;
case 8:
nvm_write(0x0e,vote_cnt_party8>>8);
nvm_write(0x0f,vote_cnt_party8);
break;
}
}

void result_mode(){

retreive_vote_cnt();
lcd_command(0x01);
lcd_display("Party 1 = ");lcd_data((vote_cnt_party1%10)+48);
lcd_command(0xc0);
lcd_display("Party 2 = ");lcd_data((vote_cnt_party2%10)+48);
lcd_command(0x94);
lcd_display("Party 3 = ");lcd_data((vote_cnt_party3%10)+48);
lcd_command(0xd4);
lcd_display("Party 4 = ");lcd_data((vote_cnt_party4%10)+48);
delay(3000);
lcd_command(0x01);
lcd_display("Party 5 = ");lcd_data((vote_cnt_party5%10)+48);
lcd_command(0xc0);
lcd_display("Party 6 = ");lcd_data((vote_cnt_party6%10)+48);
lcd_command(0x94);
lcd_display("Party 7 = ");lcd_data((vote_cnt_party7%10)+48);
lcd_command(0xd4);
lcd_display("Party 8 = ");lcd_data((vote_cnt_party8%10)+48);
delay(3000);
return;
}

void main(){

TRISD = 0x00;
TRISA = 0xff;
TRISB = 0xff;
ADCON1 = 0x0f;
lcd_initialize();

lcd_display(" Welcome to");


lcd_command(0x99);lcd_display("ELECTRONIC");
lcd_command(0xd7);lcd_display("VOTING MACHINE");
delay(3000);
lcd_command(0x01);lcd_display("Please wait");
lcd_command(0x94);lcd_display("Retrieving previous");
lcd_command(0xda);lcd_display("votes");
retreive_vote_cnt();delay(3000);

voting_mode();
}