Thứ Sáu, 14 tháng 12, 2018

Electronic Voting Machine using Internal EEPROM of AVR


Developed By: 

Akshay Daga
The microcontroller based voting machines made the process of voting and counting the voted lot easier than before. Previously the votes were marked in paper which are then stored safely in a box and inside a well secure room for days. The process of separating the votes and counting them manually may take a lot of days. But after finding the electronic voting machine the votes can be marked without using papers, which makes the voting process eco-friendly. Moreover it makes the counting process faster and the results can be announced in a comparatively shorter period of time. 
There should be a controller inside the Electronic Voting Machine (EVM) which controls the process and there should be storage medium where the details of the vote are stored. There should be a ballet unit which is been used in such a way that the one who came to cast the vote can do only one vote. There should be a keypad also which can be used by those who wish to cast a vote. In this project the AVR microcontrolleris used to demonstrate the working of a simplest voting machine which can communicate with a PC serially and which can store the count of votes in its internal memory.


This project is all about how we can make use of internal EEPROM of the AVR for storing data for future purpose. In this project we are making an Electronic Voting Machine (EVM) using the EEPROM of the AVR. As in the case of normal EVM, there is a control unit and a ballet unit. The ballet unit can be used to cast the vote only after it is enabled by pressing a key in the control unit. After casting a single vote the ballet unit will get disabled again. Each and every time a user press a key the LCD displays which candidate he voted for.
The interesting part is that each and every time a vote being casted, the count will get updated automatically in the internal EEPROM. Hence once the polling is done we can disconnect the unit from power supply and keep it safe till the day of counting. We can power up the board and even after a long period of time we can read out the number of votes casted for each candidate by connecting the EVM to the through hyperterminal. 


Circuit:

Circuit View

Code: 
#define F_CPU 8000000
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <avr/eeprom.h>
#include "lcd.h"
#include "usart.h"
void switch_init ( void );
void led_init ( void );
void evm_init ( void );
void evm_raedy_wait ( void );
void evm_vote ( void );
char read_key_wait ( void );
void update_count ( int candidate_address );
void send_count_out ( void );
void reset_count ( void );
int main ( void )
{
char poll_result_mode = 1;
evm_init ();
MENU:
//-----------------------------------------//
stdout = &lcd_out;
lcd_clear ();
printf ( "A-POLL B-COUNT");
printf ( "\nRESET COUNT-C" );
//-----------------------------------------//
poll_result_mode = read_key_wait ();   // wait till any key is pressed 
                 // and read the current key pad value
switch ( poll_result_mode )
{
case 0:
while ( 1 )
{
evm_raedy_wait ();
evm_vote ();
}
goto MENU;
case 1:
send_count_out ();
goto MENU;
case 2:
reset_count ();
goto MENU;
}
while ( 1 );
}
void switch_init ( void )
{
cli();
DDRC &= 0xE0;
PORTC = 0xFF;
DDRD |= 0x80;
PORTD |= 0x80;
sei();
}
void led_init ( void )
{
cli();
DDRD |= 0x80;
PORTD |= 0x80;
sei();
}
void evm_init ()
{
led_init ();
switch_init ();
usart_init();
lcd_init ();
}
void evm_raedy_wait ( void )
{
lcd_clear ();
printf ( "_______EVM______" );
//============ wait till evm unit enabled by external switch ===============//
while ( ( PINC & 0x10 ) );
_delay_ms ( 50 );
while ( !( PINC & 0x10 ) );
PORTD &= 0x7F;
//============ wait till evm unit enabled by external switch ===============//
lcd_clear ();
printf ( "YOU CAN VOTE NOW" );
}
char read_key_wait ( void )
{
char button;
while ( 0x0F == ( button = ( PINC & 0x0F ) ) );   // wait till any key is pressed 
 // and read the current key pad value
_delay_ms ( 50 );
switch ( button )
{
case 0x0B:
return 0;
case 0x07:
return 1;
case 0x0D:
return 2;
case 0x0E:
return 3;
};
return 0;
}
void evm_vote ( void )
{
char button;
button = read_key_wait ();   // wait till any key is pressed 
 // and read the current key pad value
switch ( button )
{
case 0:
update_count ( 0 );
lcd_clear ();
printf ( "You voted for A" );
break;
case 1:
update_count ( 1 );
lcd_clear ();
printf ( "You voted for B" );
break;
case 2:
update_count ( 2 );
lcd_clear ();
printf ( "You voted for C" );
break;
case 3:
update_count ( 3 );
lcd_clear ();
printf ( "You voted for D" );
break;
};
PORTD |= 0x80;
_delay_ms ( 3000 );
}
void update_count ( int candidate_address )
{
int base_address = 100;
int count = 0;
count = eeprom_read_byte ( ( unsigned char * ) ( base_address + candidate_address ) );
count ++;
eeprom_write_byte ( ( unsigned char * ) ( base_address + candidate_address ), count );
}
void send_count_out ( void )
{
int base_address = 100;
int i = 0;
stdout = &uart_out;
printf ( "\n______________________EVM______________________" );
for ( i = 0; i < 4; i ++ )
printf ( "\nCANDIDATE : %c, NO. VOTES : %d", ( 'A' + i ), eeprom_read_byte ( ( unsigned char * ) ( base_address + i ) ) );
}
void reset_count ( void )
{
int base_address = 100;
int i = 0;
for ( i = 0; i < 4; i ++ )
eeprom_write_byte ( ( unsigned char * ) ( base_address + i ), 0 );
lcd_clear ();
printf ( "RESET [OK]");
_delay_ms ( 2000 );
}
########## LCD ##########
#ifndef _LCD_H
#define _LCD_H
#ifndef F_CPU
#define F_CPU 8000000
#endif
#include<avr/io.h>
#include<util/delay.h>
#include<inttypes.h>
#include <stdio.h>
#include <string.h>
#define rs PA0
#define rw PA1
#define en PA2
void lcd_init();
void dis_cmd(char);
void dis_data(char);
void lcdcmd(char);
void lcddata(char);
void lcd_clear(void);
void lcd_2nd_line(void);
void lcd_1st_line(void);
void lcd_string(const char *data);
int lcd_print(char c, FILE *stream);
int lcd_scroll(const char *data);
FILE lcd_out = FDEV_SETUP_STREAM(lcd_print, NULL,
                                         _FDEV_SETUP_WRITE);
char disp_beg [] = "                ";
int lcd_print(char c, FILE *stream)
{
  if('\n' == c)
  lcd_2nd_line();
  else
    dis_data(c);
  return 0;
}
int lcd_scroll(const char *data)
{
int i;
int j = 0;
strcat(disp_beg, data);
for(i = 0; i < 14; i ++)
strcat(disp_beg, " ");
while(1)
{
for(i = 0;i < 16;i ++)
{
if(!disp_beg[i + j])
return 0;
else;
dis_data(disp_beg [i + j]);
}
j ++;
_delay_ms(500);
lcd_clear ();
}
return 0;
}
void lcd_string(const char *data)
{
for(;*data;data++)
dis_data (*data);
}
void lcd_clear(void)
{
dis_cmd(0x01);
_delay_ms(10);
}
void lcd_2nd_line(void)
{
dis_cmd(0xC0);
_delay_ms(1);
}
void lcd_1st_line(void)
{
dis_cmd(0x80);
_delay_ms(1);
}
void lcd_init() // fuction for intialize 
{
DDRA=0xFF;
dis_cmd(0x02); // to initialize LCD in 4-bit mode.
dis_cmd(0x28); //to initialize LCD in 2 lines, 5X7 dots and 4bit mode.
dis_cmd(0x0C);
dis_cmd(0x06);
dis_cmd(0x80);
dis_cmd(0x01);
_delay_ms(500);
    stdout = &lcd_out;
}
void dis_cmd(char cmd_value)
{
char cmd_value1;
cmd_value1 = cmd_value & 0xF0; //mask lower nibble because PA4-PA7 pins are used. 
lcdcmd(cmd_value1); // send to LCD
cmd_value1 = ((cmd_value<<4) & 0xF0); //shift 4-bit and mask
lcdcmd(cmd_value1); // send to LCD
}
void dis_data(char data_value)
{
char data_value1;
data_value1=data_value&0xF0;
lcddata(data_value1);
data_value1=((data_value<<4)&0xF0);
lcddata(data_value1);
}
void lcdcmd(char cmdout)
{
PORTA=cmdout;
PORTA&=~(1<<rs);
PORTA&=~(1<<rw);
PORTA|=(1<<en);
_delay_ms(1);
PORTA&=~(1<<en);
}
void lcddata(char dataout)
{
PORTA=dataout;
PORTA|=(1<<rs);
PORTA&=~(1<<rw);
PORTA|=(1<<en);
_delay_ms(1);
PORTA&=~(1<<en);
}
#endif

Code 2:


#ifndef _USART_H

#define _USART_H
#ifndef F_CPU
#define F_CPU 8000000
#endif
#define USART_BAUDRATE 9600
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)
#include<avr/io.h>
#include<util/delay.h>
#include <stdio.h>
void usart_init();
void usart_putch(unsigned char send);
unsigned int usart_getch();
void usart_send_string(const char* data);
int uart_print(char c, FILE *stream);
FILE uart_out = FDEV_SETUP_STREAM(uart_print, NULL,
                                         _FDEV_SETUP_WRITE);
int uart_print(char c, FILE *stream)
{
  if (c == '\n')
    uart_print('\r', stream);
  loop_until_bit_is_set(UCSRA, UDRE);
  UDR = c;
  return 0;
}
void usart_init()
{
UCSRB |= (1 << RXEN) | (1 << TXEN);   
// Turn on the transmission and reception circuitry
UCSRC |= (1 << URSEL) | (1<<USBS) | (1 << UCSZ0) | (1 << UCSZ1); 
// Use 8-bit character sizes
UBRRL = BAUD_PRESCALE; // Load lower 8-bits of the baud rate value..
// into the low byte of the UBRR register
UBRRH = (BAUD_PRESCALE >> 8); // Load upper 8-bits of the baud rate value..
// into the high byte of the UBRR register
stdout = &uart_out;
}
void usart_putch(unsigned char send)
{
while ((UCSRA & (1 << UDRE)) == 0); // Do nothing until UDR is ready..
// for more data to be written to it
UDR = send; // Send the byte 
}
unsigned int usart_getch()
{
while ((UCSRA & (1 << RXC)) == 0);
// Do nothing until data have been received and is ready to be read from UDR
return(UDR); // return the byte
}
void usart_send_string(const char* data)
{
for(; *data; data ++)
usart_putch(*data);
}
#endif

Electronic Voting Machine using Internal EEPROM of AVR Circuit Diagram

#define F_CPU 8000000
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <avr/eeprom.h>
#include "lcd.h"
#include "usart.h"
void switch_init ( void );
void led_init ( void );
void evm_init ( void );
void evm_raedy_wait ( void );
void evm_vote ( void );
char read_key_wait ( void );
void update_count ( int candidate_address );
void send_count_out ( void );
void reset_count ( void );
int main ( void )
{
char poll_result_mode = 1;
evm_init ();
MENU:
//-----------------------------------------//
stdout = &lcd_out;
lcd_clear ();
printf ( "A-POLL B-COUNT");
printf ( "\nRESET COUNT-C" );
//-----------------------------------------//
poll_result_mode = read_key_wait ();   // wait till any key is pressed 
                 // and read the current key pad value
switch ( poll_result_mode )
{
case 0:
while ( 1 )
{
evm_raedy_wait ();
evm_vote ();
}
goto MENU;
case 1:
send_count_out ();
goto MENU;
case 2:
reset_count ();
goto MENU;
}
while ( 1 );
}
void switch_init ( void )
{
cli();
DDRC &= 0xE0;
PORTC = 0xFF;
DDRD |= 0x80;
PORTD |= 0x80;
sei();
}
void led_init ( void )
{
cli();
DDRD |= 0x80;
PORTD |= 0x80;
sei();
}
void evm_init ()
{
led_init ();
switch_init ();
usart_init();
lcd_init ();
}
void evm_raedy_wait ( void )
{
lcd_clear ();
printf ( "_______EVM______" );
//============ wait till evm unit enabled by external switch ===============//
while ( ( PINC & 0x10 ) );
_delay_ms ( 50 );
while ( !( PINC & 0x10 ) );
PORTD &= 0x7F;
//============ wait till evm unit enabled by external switch ===============//
lcd_clear ();
printf ( "YOU CAN VOTE NOW" );
}
char read_key_wait ( void )
{
char button;
while ( 0x0F == ( button = ( PINC & 0x0F ) ) );   // wait till any key is pressed 
 // and read the current key pad value
_delay_ms ( 50 );
switch ( button )
{
case 0x0B:
return 0;
case 0x07:
return 1;
case 0x0D:
return 2;
case 0x0E:
return 3;
};
return 0;
}
void evm_vote ( void )
{
char button;
button = read_key_wait ();   // wait till any key is pressed 
 // and read the current key pad value
switch ( button )
{
case 0:
update_count ( 0 );
lcd_clear ();
printf ( "You voted for A" );
break;
case 1:
update_count ( 1 );
lcd_clear ();
printf ( "You voted for B" );
break;
case 2:
update_count ( 2 );
lcd_clear ();
printf ( "You voted for C" );
break;
case 3:
update_count ( 3 );
lcd_clear ();
printf ( "You voted for D" );
break;
};
PORTD |= 0x80;
_delay_ms ( 3000 );
}
void update_count ( int candidate_address )
{
int base_address = 100;
int count = 0;
count = eeprom_read_byte ( ( unsigned char * ) ( base_address + candidate_address ) );
count ++;
eeprom_write_byte ( ( unsigned char * ) ( base_address + candidate_address ), count );
}
void send_count_out ( void )
{
int base_address = 100;
int i = 0;
stdout = &uart_out;
printf ( "\n______________________EVM______________________" );
for ( i = 0; i < 4; i ++ )
printf ( "\nCANDIDATE : %c, NO. VOTES : %d", ( 'A' + i ), eeprom_read_byte ( ( unsigned char * ) ( base_address + i ) ) );
}
void reset_count ( void )
{
int base_address = 100;
int i = 0;
for ( i = 0; i < 4; i ++ )
eeprom_write_byte ( ( unsigned char * ) ( base_address + i ), 0 );
lcd_clear ();
printf ( "RESET [OK]");
_delay_ms ( 2000 );
}
########## LCD ##########
#ifndef _LCD_H
#define _LCD_H
#ifndef F_CPU
#define F_CPU 8000000
#endif
#include<avr/io.h>
#include<util/delay.h>
#include<inttypes.h>
#include <stdio.h>
#include <string.h>
#define rs PA0
#define rw PA1
#define en PA2
void lcd_init();
void dis_cmd(char);
void dis_data(char);
void lcdcmd(char);
void lcddata(char);
void lcd_clear(void);
void lcd_2nd_line(void);
void lcd_1st_line(void);
void lcd_string(const char *data);
int lcd_print(char c, FILE *stream);
int lcd_scroll(const char *data);
FILE lcd_out = FDEV_SETUP_STREAM(lcd_print, NULL,
                                         _FDEV_SETUP_WRITE);
char disp_beg [] = "                ";
int lcd_print(char c, FILE *stream)
{
  if('\n' == c)
  lcd_2nd_line();
  else
    dis_data(c);
  return 0;
}
int lcd_scroll(const char *data)
{
int i;
int j = 0;
strcat(disp_beg, data);
for(i = 0; i < 14; i ++)
strcat(disp_beg, " ");
while(1)
{
for(i = 0;i < 16;i ++)
{
if(!disp_beg[i + j])
return 0;
else;
dis_data(disp_beg [i + j]);
}
j ++;
_delay_ms(500);
lcd_clear ();
}
return 0;
}
void lcd_string(const char *data)
{
for(;*data;data++)
dis_data (*data);
}
void lcd_clear(void)
{
dis_cmd(0x01);
_delay_ms(10);
}
void lcd_2nd_line(void)
{
dis_cmd(0xC0);
_delay_ms(1);
}
void lcd_1st_line(void)
{
dis_cmd(0x80);
_delay_ms(1);
}
void lcd_init() // fuction for intialize 
{
DDRA=0xFF;
dis_cmd(0x02); // to initialize LCD in 4-bit mode.
dis_cmd(0x28); //to initialize LCD in 2 lines, 5X7 dots and 4bit mode.
dis_cmd(0x0C);
dis_cmd(0x06);
dis_cmd(0x80);
dis_cmd(0x01);
_delay_ms(500);
    stdout = &lcd_out;
}
void dis_cmd(char cmd_value)
{
char cmd_value1;
cmd_value1 = cmd_value & 0xF0; //mask lower nibble because PA4-PA7 pins are used. 
lcdcmd(cmd_value1); // send to LCD
cmd_value1 = ((cmd_value<<4) & 0xF0); //shift 4-bit and mask
lcdcmd(cmd_value1); // send to LCD
}
void dis_data(char data_value)
{
char data_value1;
data_value1=data_value&0xF0;
lcddata(data_value1);
data_value1=((data_value<<4)&0xF0);
lcddata(data_value1);
}
void lcdcmd(char cmdout)
{
PORTA=cmdout;
PORTA&=~(1<<rs);
PORTA&=~(1<<rw);
PORTA|=(1<<en);
_delay_ms(1);
PORTA&=~(1<<en);
}
void lcddata(char dataout)
{
PORTA=dataout;
PORTA|=(1<<rs);
PORTA&=~(1<<rw);
PORTA|=(1<<en);
_delay_ms(1);
PORTA&=~(1<<en);
}
#endif


#ifndef _USART_H
#define _USART_H
#ifndef F_CPU
#define F_CPU 8000000
#endif
#define USART_BAUDRATE 9600
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)
#include<avr/io.h>
#include<util/delay.h>
#include <stdio.h>
void usart_init();
void usart_putch(unsigned char send);
unsigned int usart_getch();
void usart_send_string(const char* data);
int uart_print(char c, FILE *stream);
FILE uart_out = FDEV_SETUP_STREAM(uart_print, NULL,
                                         _FDEV_SETUP_WRITE);
int uart_print(char c, FILE *stream)
{
  if (c == '\n')
    uart_print('\r', stream);
  loop_until_bit_is_set(UCSRA, UDRE);
  UDR = c;
  return 0;
}
void usart_init()
{
UCSRB |= (1 << RXEN) | (1 << TXEN);   
// Turn on the transmission and reception circuitry
UCSRC |= (1 << URSEL) | (1<<USBS) | (1 << UCSZ0) | (1 << UCSZ1); 
// Use 8-bit character sizes
UBRRL = BAUD_PRESCALE; // Load lower 8-bits of the baud rate value..
// into the low byte of the UBRR register
UBRRH = (BAUD_PRESCALE >> 8); // Load upper 8-bits of the baud rate value..
// into the high byte of the UBRR register
stdout = &uart_out;
}
void usart_putch(unsigned char send)
{
while ((UCSRA & (1 << UDRE)) == 0); // Do nothing until UDR is ready..
// for more data to be written to it
UDR = send; // Send the byte 
}
unsigned int usart_getch()
{
while ((UCSRA & (1 << RXC)) == 0);
// Do nothing until data have been received and is ready to be read from UDR
return(UDR); // return the byte
}
void usart_send_string(const char* data)
{
for(; *data; data ++)
usart_putch(*data);
}
#endif

16 x 2 LCD | 16x2 Character LCD Module
LCD

LCD (Liquid Crystal Display) screen is an electronic display module and find a wide range of applications. A 16x2 LCD display is very basic module and is very commonly used in various devices and circuits. These modules are...
ATmega16 AVR Microcontroller
ATmega16
ATmega16 is an 8-bit high performance microcontroller of Atmel’s Mega ...
Resistor Image
Resistor
Resistor is a passive component used to control current in a circuit. Its resistance is given by the ratio of voltage applied across its terminals to the current passing through it. Thus a particular value of resistor, for fixed voltage, limits the current through it. They are omnipresent in...

Không có nhận xét nào:

Đăng nhận xét

Bài đăng mới nhất

Hướng dẫn sử dụng Cân điện tử Fujihatsu FTC-01

Hướng dẫn sử dụng Cân điện tử Fujihatsu FTC-01 # candientu ,  # fujihatsu ,  # candientufujihatsu  #candientu,  # candientufujhatsu , #fuji...

Bài đăng phổ biến