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
Không có nhận xét nào:
Đăng nhận xét