۳۱-خرداد-۱۳۸۸, ۲۰:۲۱:۳۷
با عرض سلام خدمت دوستان
من یک پروژه آماده کرده ام که بصورت زیر است
یک سیستم ورود و خروج که دارای یک صفحه کلید ماتریسی برای گرفتن اطلاعات از کاربر و دارای یک ساعت که از DS1307 استفاده کرده ام و از max232 هم برای ارتباط با نرم افزاری که نوشته ام و از یک LCD2*16 برای نمایش اطلاعات
کل پروژه هم از لحاظ نرم افزاری و سخت افزاری آماده شده اما حالا یک مشکل دارم می خوام سیستم دو منظوره بشه یعنی سیستم همیشه ساعت را برای کاربر نشان بده و آن را خودکار Refresh کنه تا زمانی که بخواهیم کاربر اطلاعات ورود را وارد کنه یا بخواهد ساعت را تنظیم کنه در حال حاضر من برنامه را به گونه ای نوشته ام که اگر کاربر بخواهد ساعت را ببیند باید یک کلید را بزند و اگر بخواهد وارد شود باید کلید مربوطه به آن را بزند بطور کلی چگونه م تونم هم ساعت را نشان بدم به صورت اینکه 1 ثانیه یک بار refresh بشه و هم از کاربر اطلا عات بگیرم یا اینکه ساعت را بصورت refresh شده نمایش بدم تا زمانی که کاربر کلید های مربوطه به دیگر توابع را فشار بده
من یک پروژه آماده کرده ام که بصورت زیر است
یک سیستم ورود و خروج که دارای یک صفحه کلید ماتریسی برای گرفتن اطلاعات از کاربر و دارای یک ساعت که از DS1307 استفاده کرده ام و از max232 هم برای ارتباط با نرم افزاری که نوشته ام و از یک LCD2*16 برای نمایش اطلاعات
کل پروژه هم از لحاظ نرم افزاری و سخت افزاری آماده شده اما حالا یک مشکل دارم می خوام سیستم دو منظوره بشه یعنی سیستم همیشه ساعت را برای کاربر نشان بده و آن را خودکار Refresh کنه تا زمانی که بخواهیم کاربر اطلاعات ورود را وارد کنه یا بخواهد ساعت را تنظیم کنه در حال حاضر من برنامه را به گونه ای نوشته ام که اگر کاربر بخواهد ساعت را ببیند باید یک کلید را بزند و اگر بخواهد وارد شود باید کلید مربوطه به آن را بزند بطور کلی چگونه م تونم هم ساعت را نشان بدم به صورت اینکه 1 ثانیه یک بار refresh بشه و هم از کاربر اطلا عات بگیرم یا اینکه ساعت را بصورت refresh شده نمایش بدم تا زمانی که کاربر کلید های مربوطه به دیگر توابع را فشار بده
/*****************************************************
This program was produced by the
CodeWizardAVR V2.03.4 Standard
Automatic Program Generator
© Copyright 1998-2008 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com
Project :
Version :
Date : 2009/02/24
Author :
Company :
Comments:
Chip type : ATmega16
Program type : Application
Clock frequency : 4.000000 MHz
Memory model : Small
External RAM size : 0
Data Stack size : 256
*****************************************************/
#include <mega16.h>
#include <delay.h>
#include <i2c.h>
#include <ds1307.h> // DS1307 Real Time Clock
#include <lcd.h> // Alphanumeric LCD
#include <bcd.h>
#include <string.h>
#include <stdlib.h>
// Standard Input/Output functions
#include <stdio.h>
/**********************************************************************/
#define RXB8 1
#define TXB8 0
#define UPE 2
#define OVR 3
#define FE 4
#define UDRE 5
#define RXC 7
#define FRAMING_ERROR (1<<FE)
#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<OVR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)
#define TX_NEWLINE
{transmitByte(0x0d); transmitByte(0x0a);}
#define xtal 4000000
#define baud 9600
/**********************************************************************/
unsigned char receiveByte(void);
void TimeSet();
void transmitByte(unsigned char);
void transmitString_F(const unsigned char*);
void transmitString(unsigned char*);
void Reset();
void Clk();
int GetKey();
char IntToChar(int IntVal);
int CheckSameArray(char * a,char * b,int len);
int LoginSession();
int ChangePasswordSession();
void GetTime(char * time);
void SendResetCmd();
void beep(int offon);
/**********************************************************************/
unsigned char hour,minute,second;
char lcd1[16],lcd2[16];
unsigned char year,month,day;
int State;
/**********************************************************************/
// I2C Bus functions
#asm
.equ __i2c_port=0x15 ;PORTC
.equ __sda_bit=0
.equ __scl_bit=1
#endasm
// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0x1B ;PORTA
#endasm
// External Interrupt 0 service routine
interrupt [EXT_INT0] void ext_int0_isr(void)
{
}
// External Interrupt 1 service routine
interrupt [EXT_INT1] void ext_int1_isr(void)
{
}
// External Interrupt 2 service routine
interrupt [EXT_INT2] void ext_int2_isr(void)
{
}
// Timer 0 overflow interrupt service routine
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
}
// Declare your global variables here
void main(void)
{
// Declare your local variables here
int key;
int i;
char testStr;
char time[8];
// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTA=0x00;
DDRA=0x00;
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=Out Func0=Out
// State7=T State6=T State5=T State4=T State3=T State2=T State1=0 State0=0
PORTB=0x00;
DDRB=0x03;
// Port C initialization
// Func7=Out Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=0 State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x80;
// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s) initialization
// INT0: On
// INT0 Mode: Low level
// INT1: On
// INT1 Mode: Low level
// INT2: On
// INT2 Mode: Falling Edge
GICR|=0xE0;
MCUCR=0x00;
MCUCSR=0x00;
GIFR=0xE0;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x01;
// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 9600
UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x86;
//UBRRH=0x00;
//UBRRL=0x19;
UBRRH=(xtal/16/baud-1) >> 8;
UBRRL=(xtal/16/baud-1) & 0xFF;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
// I2C Bus initialization
i2c_init();
// DS1307 Real Time Clock initialization
// Square wave output on pin SQW/OUT: On
// Square wave frequency: 32768Hz
rtc_init(3,1,0);
// LCD module initialization
lcd_init(16);
// Global enable interrupts
#asm("sei")
lcd_clear();
/**********************************************************************/
lcd_putsf("com port is not active");
putchar('3');
putchar('D');
testStr=getchar();
if(testStr=='1'){
lcd_clear();
SendResetCmd();
}
/**********************************************************************/
while (1)
{
// Place your code here
key=GetKey();
switch(key){
case (12):{ //F1 neshan dadane sa@t
State=12;
delay_ms(200);
lcd_clear();
GetTime(time);
lcd_putchar(time[0]);
lcd_putchar(time[1]);
lcd_putchar(time[2]);
lcd_putchar(time[3]);
lcd_putchar(time[4]);
lcd_putchar(time[5]);
lcd_putchar(time[6]);
lcd_putchar(time[7]);
break;
}
case (13):{ //F2 vorood
State=13;
LoginSession();
break;
}
case (14):{ //F3 // taghyire password
State=14;
ChangePasswordSession();
break;
}
case (15):{ //F4 //tanzime zaman va tarikh
State=14;
lcd_clear();
TimeSet();
break;
}
}//end switch key
};
}
void Reset()
{
PORTB.1=1;
delay_us(10);
PORTB.1=0;
}
void Clk()
{PORTB.0=0;
delay_us(10);
PORTB.0=1;
}
int GetKey(){
int k,i;
while(1){
Reset();
for (i=1;i<28;i++){
PORTB.2=1;
Clk();
delay_ms(100);
Reset();
if (PINB.2==0){
return(1);
}
for (k=1;k<28;k++){
PORTB.2=1;
Clk();
if (PINB.2==0){
switch(k){
case(1): {
return(4);
}
case(2): {
return(7);
}
case(3): {
return(10);
}
case(8): {
return(2);
}
case(9): {
return(5);
}
case(10): {
return(8);
}
case(11): {
return(0);
}
case(16): {
return(3);
}
case(17): {
return(6);
}
case(18): {
return(9);
}
case(19): {
return(11);
}
case(24): {
return(12);
}
case(25): {
return(13);
}
case(26): {
return(14);
}
case(27): {
return(15);
}
}//switch(k){
}//if(PINB.2==0)
}//for (k=1;k<20;k++)
}// for (i=1;i<20;i++)
}// while(1)
}
char IntToChar(int IntVal){
return(IntVal+48);
}
//**************************************************
//Function to receive a single byte
//*************************************************
unsigned char receiveByte( void )
{
unsigned char data, status;
while(!(UCSRA & (1<<RXC)));
// Wait for incomming data
status = UCSRA;
data = UDR;
return(data);
}
//***************************************************
//Function to transmit a single byte
//***************************************************
void transmitByte( unsigned char data )
{
while ( !(UCSRA & (1<<UDRE)) )
;
/* Wait for empty transmit buffer */
UDR = data;
/* Start transmition */
}
//***************************************************
//Function to transmit a string in Flash
//***************************************************
void transmitString_F(const unsigned char* string)
{
while (*string)
transmitByte(*string++);
}
//***************************************************
//Function to transmit a string in RAM
//***************************************************
void transmitString(unsigned char* string)
{
while (*string)
transmitByte(*string++);
}
int LoginSession(){
int j,i,key,passlen,len;
char pl[1],u[5],p[8],s[20],sw[1],time[8];
unsigned char r;
lcd_clear();
lcd_putsf("username (5 char(1..9)): ");
s[0]='1';//login
for (i=1;i<6;i++){
key=GetKey();
if (key==12) goto exitl; //exit for time
if((key>=0) && (key<=9)){
s[i]=IntToChar(key);
lcd_gotoxy(7+i,1);
lcd_putchar(s[i]);
delay_ms(100);
}else{
i--;
continue;
}//end if key
}//end for u[i]
lcd_clear();
lcd_putsf("password:");
passlen=0;
for (i=7;i<15;i++){ // for for get password
key=GetKey();
if (key==12) goto exitl;//exit
if((key==11)&&(passlen>0)) break;
if((key>=0) && (key<=9)){
s[i]=IntToChar(key);
lcd_gotoxy(2+i,0);
lcd_putchar('*');
passlen++;
delay_ms(100);
}else{ /* loging state=1 username passlen password ex:1 12345 3 123 */
i--;
continue;
}//end if key
}//end for get password
s[6]=IntToChar(passlen);
rtc_get_time(&hour,&minute,&second);
GetTime(time);
i=6+passlen+1;
j=0; // for gharar dadane time
for (j=0;j<5;j++)
{
s[i]=time[j];
i++;
} //End for gharar dadane time
len=6+passlen+1+5;//login state+username=(6 char)+ passlen(1 char)+pass+time(5 char) //dar inja time bedoone sanie
s[0]='1';//login state ==1
s[1]='1'; //user name az 10000 shoroo mishavand
for (i=0;i<len;i++) // ferstadane dastoor be computer
putchar(s[i]);
putchar('D'); // ferestadane entehaye dastoor
lcd_clear();
lcd_putsf("please wait");
r=getchar();
if (r=='1'){
lcd_clear();
lcd_putsf("correct U and P.");
beep(1);
delay_ms(500);
beep(0);
delay_ms(100);
beep(1);
delay_ms(50);
beep(0);
}
else
if (r=='0'){
lcd_clear();
lcd_putsf("U or P is not valid");
beep(1);
delay_ms(1000);
beep(0);
delay_ms(500);
beep(1);
delay_ms(200);
beep(0);
}
else if (r=='2'){
lcd_clear();
lcd_putsf("Access denied.Time Error.");
beep(1);
delay_ms(1000);
beep(0);
delay_ms(500);
beep(1);
delay_ms(200);
beep(0);
}
exitl:
SendResetCmd();
}
int ChangePasswordSession(){
int j,i,key,passlen,len,newpasslen1,newpasslen2,swnewpass;
char s[24],newp1[8],newp2[8];
unsigned char r;
lcd_clear();
lcd_putsf("username (5 char(1..9)): ");
s[0]='2';//change password
for (i=1;i<6;i++){
key=GetKey();
if (key==12) goto exitc; //exit
if((key>=0) && (key<=9)){
s[i]=IntToChar(key);
lcd_gotoxy(7+i,1);
lcd_putchar(s[i]);
delay_ms(100);
}else{
i--;
continue;
}//end if key
}//end for u[i]
lcd_clear();
lcd_putsf("password:");
passlen=0;
for (i=7;i<15;i++){
key=GetKey();
if (key==12) goto exitc; //exit
if((key==11)&&(passlen>0)) break;
if((key>=0) && (key<=9)){
s[i]=IntToChar(key);
lcd_gotoxy(2+i,0);
lcd_putchar(IntToChar(key));
passlen++;
delay_ms(100);
}else{
i--;
continue;
}//end if
}//end for
s[6]=IntToChar(passlen);
swnewpass=0;
while(swnewpass!=1){//while for get new pass
lcd_clear();
lcd_putsf("new password:");
newpasslen1=0;
for (i=0;i<8;i++){
newp1[i]='a';
newp2[i]='a';
}
for (i=0;i<8;i++){
key=GetKey();
if (key==12) goto exitc; //exit
if((key==11)&&(newpasslen1>0)) break;
if((key>=0) && (key<=9)){
newp1[i]=IntToChar(key);
lcd_gotoxy(14+i,0);
lcd_putchar(IntToChar(key));
newpasslen1++;
delay_ms(100);
}else{
i--;
continue;
}//end if
}//end for
lcd_clear();
lcd_putsf("new password repeat:");
newpasslen2=0;
for (i=0;i<8;i++){
key=GetKey();
if (key==12) goto exitc; //exit
if((key==11)&&(newpasslen2>0)) break;
if((key>=0) && (key<=9)){
newp2[i]=IntToChar(key);
lcd_gotoxy(5+i,1);
lcd_putchar(IntToChar(key));
newpasslen2++;
delay_ms(100);
}
else{
i--;
continue;
}//end if
}//end for
if((newpasslen1!=newpasslen2)||(CheckSameArray(newp1,newp2,8)==0)) {
lcd_clear();
if (newpasslen1!=newpasslen2) {
lcd_putsf("Error Len P and Re.P.press f4");
beep(1);
delay_ms(1000);
beep(0);
delay_ms(500);
beep(1);
delay_ms(200);
beep(0);
}
else
{
lcd_putsf("Error same P and Re.P.press f4");
beep(1);
delay_ms(1000);
beep(0);
delay_ms(500);
beep(1);
delay_ms(200);
beep(0);
}
key=GetKey();
if (key==12) goto exitc; //exit
while((key!=15)&&(key!=12)) {
key=GetKey();
if (key==12) goto exitc; //exit
}
}
else swnewpass=1;
} //end while new pass
j=0;
for(i=(6+passlen+2);i<(((6+passlen+2))+newpasslen1);i++){
s[i]=newp1[j];
j++;
}
s[6+passlen+1]=IntToChar(newpasslen1);
len=1+5+1+passlen+1+newpasslen1; //login state+username=(6 char)+ passlen(1 char)+pass+newpasslen+newpass
lcd_clear();
s[0]='2';
s[1]='1';
for (i=0;i<len;i++)
putchar(s[i]);
putchar('D');
lcd_clear();
lcd_putsf("please wait");
r=getchar();
if (r=='1'){
lcd_clear();
lcd_putsf("Done");
beep(1);
delay_ms(500);
beep(0);
delay_ms(100);
beep(1);
delay_ms(50);
beep(0);
}
else
if (r=='0'){
lcd_clear();
lcd_putsf("Error.U or P is not valid");
beep(1);
delay_ms(1000);
beep(0);
delay_ms(500);
beep(1);
delay_ms(200);
beep(0);
}
exitc:
SendResetCmd();
}
int CheckSameArray(char * a,char * b,int len){
int i;
for (i=0;i<len;i++)
{
if(a[i]!=b[i]){
return 0 ;
}
}
return 1;
}
void TimeSet(){
int key;
int tmpyear,tmpmonth,tmpday,tmphour,tmpminute,tmpsecond;
rtc_get_time(&hour,&minute,&second);
rtc_get_date(&day,&month,&year);
tmpyear=year;
tmpmonth=month;
tmpday=day;
tmphour=hour;
tmpminute=minute;
tmpsecond=second;
sprintf(lcd1,"%02u:%02u:%02u",hour,minute,second);
sprintf(lcd2,"%02u/%02u/%02u",year,month,day);
while(1) {
sprintf(lcd1,"%02u:%02u:%02u",tmphour,tmpminute,tmpsecond);
sprintf(lcd2,"%02u/%02u/%02u",tmpyear,tmpmonth,tmpday);
lcd_clear();
lcd_gotoxy(0,0);
lcd_puts(lcd1);
lcd_gotoxy(0,1);
lcd_puts(lcd2);
delay_ms(100);
key=GetKey();
if (key==12) goto exitts;
if( key==11) {
lcd_clear();
break;
}
switch(key){
case (1):{
tmpyear++;
if (tmpyear>=100) tmpyear=0;
break;
}
case (4):{
tmpmonth++;
if (tmpmonth>=13) tmpmonth=1;
break;
}
case (7):{
tmpday++;
if (tmpday>=32) tmpday=1;
break;
}
case (2):{
tmphour++;
if (tmphour>=24) tmphour=0;
break;
}
case (5):{
tmpminute++;
if (tmpminute>=60) tmpminute=0;
break;
}
case (8):{
tmpsecond++;
if (tmpsecond>=60) tmpsecond=0;
break;
}
}
}
year=tmpyear;
month=tmpmonth;
day=tmpday;
hour=tmphour;
minute=tmpminute;
second=tmpsecond;
rtc_set_time(hour,minute,second);
rtc_set_date(day,month,year);
exitts:
}
void GetTime(char * time)
{
unsigned char tmphour,tmpminute,tmpsecond;
int h1,h2,m1,m2,s1,s2;
rtc_get_time(&tmphour,&tmpminute,&tmpsecond);
h1=tmphour/10;
h2=tmphour-(h1*10);
m1=tmpminute/10;
m2=tmpminute-(m1*10);
s1=tmpsecond/10;
s2=tmpsecond-(s1*10);
time[0]=IntToChar(h1);
time[1]=IntToChar(h2);
time[2]=':';
time[3]=IntToChar(m1);
time[4]=IntToChar(m2);
time[5]=':';
time[6]=IntToChar(s1);
time[7]=IntToChar(s2);
}
void SendResetCmd(){
char *s="reset";
puts(s);
}
void beep(int offon){
PORTC.7=offon;
}
This program was produced by the
CodeWizardAVR V2.03.4 Standard
Automatic Program Generator
© Copyright 1998-2008 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com
Project :
Version :
Date : 2009/02/24
Author :
Company :
Comments:
Chip type : ATmega16
Program type : Application
Clock frequency : 4.000000 MHz
Memory model : Small
External RAM size : 0
Data Stack size : 256
*****************************************************/
#include <mega16.h>
#include <delay.h>
#include <i2c.h>
#include <ds1307.h> // DS1307 Real Time Clock
#include <lcd.h> // Alphanumeric LCD
#include <bcd.h>
#include <string.h>
#include <stdlib.h>
// Standard Input/Output functions
#include <stdio.h>
/**********************************************************************/
#define RXB8 1
#define TXB8 0
#define UPE 2
#define OVR 3
#define FE 4
#define UDRE 5
#define RXC 7
#define FRAMING_ERROR (1<<FE)
#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<OVR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)
#define TX_NEWLINE
{transmitByte(0x0d); transmitByte(0x0a);}
#define xtal 4000000
#define baud 9600
/**********************************************************************/
unsigned char receiveByte(void);
void TimeSet();
void transmitByte(unsigned char);
void transmitString_F(const unsigned char*);
void transmitString(unsigned char*);
void Reset();
void Clk();
int GetKey();
char IntToChar(int IntVal);
int CheckSameArray(char * a,char * b,int len);
int LoginSession();
int ChangePasswordSession();
void GetTime(char * time);
void SendResetCmd();
void beep(int offon);
/**********************************************************************/
unsigned char hour,minute,second;
char lcd1[16],lcd2[16];
unsigned char year,month,day;
int State;
/**********************************************************************/
// I2C Bus functions
#asm
.equ __i2c_port=0x15 ;PORTC
.equ __sda_bit=0
.equ __scl_bit=1
#endasm
// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0x1B ;PORTA
#endasm
// External Interrupt 0 service routine
interrupt [EXT_INT0] void ext_int0_isr(void)
{
}
// External Interrupt 1 service routine
interrupt [EXT_INT1] void ext_int1_isr(void)
{
}
// External Interrupt 2 service routine
interrupt [EXT_INT2] void ext_int2_isr(void)
{
}
// Timer 0 overflow interrupt service routine
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
}
// Declare your global variables here
void main(void)
{
// Declare your local variables here
int key;
int i;
char testStr;
char time[8];
// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTA=0x00;
DDRA=0x00;
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=Out Func0=Out
// State7=T State6=T State5=T State4=T State3=T State2=T State1=0 State0=0
PORTB=0x00;
DDRB=0x03;
// Port C initialization
// Func7=Out Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=0 State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x80;
// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s) initialization
// INT0: On
// INT0 Mode: Low level
// INT1: On
// INT1 Mode: Low level
// INT2: On
// INT2 Mode: Falling Edge
GICR|=0xE0;
MCUCR=0x00;
MCUCSR=0x00;
GIFR=0xE0;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x01;
// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 9600
UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x86;
//UBRRH=0x00;
//UBRRL=0x19;
UBRRH=(xtal/16/baud-1) >> 8;
UBRRL=(xtal/16/baud-1) & 0xFF;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
// I2C Bus initialization
i2c_init();
// DS1307 Real Time Clock initialization
// Square wave output on pin SQW/OUT: On
// Square wave frequency: 32768Hz
rtc_init(3,1,0);
// LCD module initialization
lcd_init(16);
// Global enable interrupts
#asm("sei")
lcd_clear();
/**********************************************************************/
lcd_putsf("com port is not active");
putchar('3');
putchar('D');
testStr=getchar();
if(testStr=='1'){
lcd_clear();
SendResetCmd();
}
/**********************************************************************/
while (1)
{
// Place your code here
key=GetKey();
switch(key){
case (12):{ //F1 neshan dadane sa@t
State=12;
delay_ms(200);
lcd_clear();
GetTime(time);
lcd_putchar(time[0]);
lcd_putchar(time[1]);
lcd_putchar(time[2]);
lcd_putchar(time[3]);
lcd_putchar(time[4]);
lcd_putchar(time[5]);
lcd_putchar(time[6]);
lcd_putchar(time[7]);
break;
}
case (13):{ //F2 vorood
State=13;
LoginSession();
break;
}
case (14):{ //F3 // taghyire password
State=14;
ChangePasswordSession();
break;
}
case (15):{ //F4 //tanzime zaman va tarikh
State=14;
lcd_clear();
TimeSet();
break;
}
}//end switch key
};
}
void Reset()
{
PORTB.1=1;
delay_us(10);
PORTB.1=0;
}
void Clk()
{PORTB.0=0;
delay_us(10);
PORTB.0=1;
}
int GetKey(){
int k,i;
while(1){
Reset();
for (i=1;i<28;i++){
PORTB.2=1;
Clk();
delay_ms(100);
Reset();
if (PINB.2==0){
return(1);
}
for (k=1;k<28;k++){
PORTB.2=1;
Clk();
if (PINB.2==0){
switch(k){
case(1): {
return(4);
}
case(2): {
return(7);
}
case(3): {
return(10);
}
case(8): {
return(2);
}
case(9): {
return(5);
}
case(10): {
return(8);
}
case(11): {
return(0);
}
case(16): {
return(3);
}
case(17): {
return(6);
}
case(18): {
return(9);
}
case(19): {
return(11);
}
case(24): {
return(12);
}
case(25): {
return(13);
}
case(26): {
return(14);
}
case(27): {
return(15);
}
}//switch(k){
}//if(PINB.2==0)
}//for (k=1;k<20;k++)
}// for (i=1;i<20;i++)
}// while(1)
}
char IntToChar(int IntVal){
return(IntVal+48);
}
//**************************************************
//Function to receive a single byte
//*************************************************
unsigned char receiveByte( void )
{
unsigned char data, status;
while(!(UCSRA & (1<<RXC)));
// Wait for incomming data
status = UCSRA;
data = UDR;
return(data);
}
//***************************************************
//Function to transmit a single byte
//***************************************************
void transmitByte( unsigned char data )
{
while ( !(UCSRA & (1<<UDRE)) )
;
/* Wait for empty transmit buffer */
UDR = data;
/* Start transmition */
}
//***************************************************
//Function to transmit a string in Flash
//***************************************************
void transmitString_F(const unsigned char* string)
{
while (*string)
transmitByte(*string++);
}
//***************************************************
//Function to transmit a string in RAM
//***************************************************
void transmitString(unsigned char* string)
{
while (*string)
transmitByte(*string++);
}
int LoginSession(){
int j,i,key,passlen,len;
char pl[1],u[5],p[8],s[20],sw[1],time[8];
unsigned char r;
lcd_clear();
lcd_putsf("username (5 char(1..9)): ");
s[0]='1';//login
for (i=1;i<6;i++){
key=GetKey();
if (key==12) goto exitl; //exit for time
if((key>=0) && (key<=9)){
s[i]=IntToChar(key);
lcd_gotoxy(7+i,1);
lcd_putchar(s[i]);
delay_ms(100);
}else{
i--;
continue;
}//end if key
}//end for u[i]
lcd_clear();
lcd_putsf("password:");
passlen=0;
for (i=7;i<15;i++){ // for for get password
key=GetKey();
if (key==12) goto exitl;//exit
if((key==11)&&(passlen>0)) break;
if((key>=0) && (key<=9)){
s[i]=IntToChar(key);
lcd_gotoxy(2+i,0);
lcd_putchar('*');
passlen++;
delay_ms(100);
}else{ /* loging state=1 username passlen password ex:1 12345 3 123 */
i--;
continue;
}//end if key
}//end for get password
s[6]=IntToChar(passlen);
rtc_get_time(&hour,&minute,&second);
GetTime(time);
i=6+passlen+1;
j=0; // for gharar dadane time
for (j=0;j<5;j++)
{
s[i]=time[j];
i++;
} //End for gharar dadane time
len=6+passlen+1+5;//login state+username=(6 char)+ passlen(1 char)+pass+time(5 char) //dar inja time bedoone sanie
s[0]='1';//login state ==1
s[1]='1'; //user name az 10000 shoroo mishavand
for (i=0;i<len;i++) // ferstadane dastoor be computer
putchar(s[i]);
putchar('D'); // ferestadane entehaye dastoor
lcd_clear();
lcd_putsf("please wait");
r=getchar();
if (r=='1'){
lcd_clear();
lcd_putsf("correct U and P.");
beep(1);
delay_ms(500);
beep(0);
delay_ms(100);
beep(1);
delay_ms(50);
beep(0);
}
else
if (r=='0'){
lcd_clear();
lcd_putsf("U or P is not valid");
beep(1);
delay_ms(1000);
beep(0);
delay_ms(500);
beep(1);
delay_ms(200);
beep(0);
}
else if (r=='2'){
lcd_clear();
lcd_putsf("Access denied.Time Error.");
beep(1);
delay_ms(1000);
beep(0);
delay_ms(500);
beep(1);
delay_ms(200);
beep(0);
}
exitl:
SendResetCmd();
}
int ChangePasswordSession(){
int j,i,key,passlen,len,newpasslen1,newpasslen2,swnewpass;
char s[24],newp1[8],newp2[8];
unsigned char r;
lcd_clear();
lcd_putsf("username (5 char(1..9)): ");
s[0]='2';//change password
for (i=1;i<6;i++){
key=GetKey();
if (key==12) goto exitc; //exit
if((key>=0) && (key<=9)){
s[i]=IntToChar(key);
lcd_gotoxy(7+i,1);
lcd_putchar(s[i]);
delay_ms(100);
}else{
i--;
continue;
}//end if key
}//end for u[i]
lcd_clear();
lcd_putsf("password:");
passlen=0;
for (i=7;i<15;i++){
key=GetKey();
if (key==12) goto exitc; //exit
if((key==11)&&(passlen>0)) break;
if((key>=0) && (key<=9)){
s[i]=IntToChar(key);
lcd_gotoxy(2+i,0);
lcd_putchar(IntToChar(key));
passlen++;
delay_ms(100);
}else{
i--;
continue;
}//end if
}//end for
s[6]=IntToChar(passlen);
swnewpass=0;
while(swnewpass!=1){//while for get new pass
lcd_clear();
lcd_putsf("new password:");
newpasslen1=0;
for (i=0;i<8;i++){
newp1[i]='a';
newp2[i]='a';
}
for (i=0;i<8;i++){
key=GetKey();
if (key==12) goto exitc; //exit
if((key==11)&&(newpasslen1>0)) break;
if((key>=0) && (key<=9)){
newp1[i]=IntToChar(key);
lcd_gotoxy(14+i,0);
lcd_putchar(IntToChar(key));
newpasslen1++;
delay_ms(100);
}else{
i--;
continue;
}//end if
}//end for
lcd_clear();
lcd_putsf("new password repeat:");
newpasslen2=0;
for (i=0;i<8;i++){
key=GetKey();
if (key==12) goto exitc; //exit
if((key==11)&&(newpasslen2>0)) break;
if((key>=0) && (key<=9)){
newp2[i]=IntToChar(key);
lcd_gotoxy(5+i,1);
lcd_putchar(IntToChar(key));
newpasslen2++;
delay_ms(100);
}
else{
i--;
continue;
}//end if
}//end for
if((newpasslen1!=newpasslen2)||(CheckSameArray(newp1,newp2,8)==0)) {
lcd_clear();
if (newpasslen1!=newpasslen2) {
lcd_putsf("Error Len P and Re.P.press f4");
beep(1);
delay_ms(1000);
beep(0);
delay_ms(500);
beep(1);
delay_ms(200);
beep(0);
}
else
{
lcd_putsf("Error same P and Re.P.press f4");
beep(1);
delay_ms(1000);
beep(0);
delay_ms(500);
beep(1);
delay_ms(200);
beep(0);
}
key=GetKey();
if (key==12) goto exitc; //exit
while((key!=15)&&(key!=12)) {
key=GetKey();
if (key==12) goto exitc; //exit
}
}
else swnewpass=1;
} //end while new pass
j=0;
for(i=(6+passlen+2);i<(((6+passlen+2))+newpasslen1);i++){
s[i]=newp1[j];
j++;
}
s[6+passlen+1]=IntToChar(newpasslen1);
len=1+5+1+passlen+1+newpasslen1; //login state+username=(6 char)+ passlen(1 char)+pass+newpasslen+newpass
lcd_clear();
s[0]='2';
s[1]='1';
for (i=0;i<len;i++)
putchar(s[i]);
putchar('D');
lcd_clear();
lcd_putsf("please wait");
r=getchar();
if (r=='1'){
lcd_clear();
lcd_putsf("Done");
beep(1);
delay_ms(500);
beep(0);
delay_ms(100);
beep(1);
delay_ms(50);
beep(0);
}
else
if (r=='0'){
lcd_clear();
lcd_putsf("Error.U or P is not valid");
beep(1);
delay_ms(1000);
beep(0);
delay_ms(500);
beep(1);
delay_ms(200);
beep(0);
}
exitc:
SendResetCmd();
}
int CheckSameArray(char * a,char * b,int len){
int i;
for (i=0;i<len;i++)
{
if(a[i]!=b[i]){
return 0 ;
}
}
return 1;
}
void TimeSet(){
int key;
int tmpyear,tmpmonth,tmpday,tmphour,tmpminute,tmpsecond;
rtc_get_time(&hour,&minute,&second);
rtc_get_date(&day,&month,&year);
tmpyear=year;
tmpmonth=month;
tmpday=day;
tmphour=hour;
tmpminute=minute;
tmpsecond=second;
sprintf(lcd1,"%02u:%02u:%02u",hour,minute,second);
sprintf(lcd2,"%02u/%02u/%02u",year,month,day);
while(1) {
sprintf(lcd1,"%02u:%02u:%02u",tmphour,tmpminute,tmpsecond);
sprintf(lcd2,"%02u/%02u/%02u",tmpyear,tmpmonth,tmpday);
lcd_clear();
lcd_gotoxy(0,0);
lcd_puts(lcd1);
lcd_gotoxy(0,1);
lcd_puts(lcd2);
delay_ms(100);
key=GetKey();
if (key==12) goto exitts;
if( key==11) {
lcd_clear();
break;
}
switch(key){
case (1):{
tmpyear++;
if (tmpyear>=100) tmpyear=0;
break;
}
case (4):{
tmpmonth++;
if (tmpmonth>=13) tmpmonth=1;
break;
}
case (7):{
tmpday++;
if (tmpday>=32) tmpday=1;
break;
}
case (2):{
tmphour++;
if (tmphour>=24) tmphour=0;
break;
}
case (5):{
tmpminute++;
if (tmpminute>=60) tmpminute=0;
break;
}
case (8):{
tmpsecond++;
if (tmpsecond>=60) tmpsecond=0;
break;
}
}
}
year=tmpyear;
month=tmpmonth;
day=tmpday;
hour=tmphour;
minute=tmpminute;
second=tmpsecond;
rtc_set_time(hour,minute,second);
rtc_set_date(day,month,year);
exitts:
}
void GetTime(char * time)
{
unsigned char tmphour,tmpminute,tmpsecond;
int h1,h2,m1,m2,s1,s2;
rtc_get_time(&tmphour,&tmpminute,&tmpsecond);
h1=tmphour/10;
h2=tmphour-(h1*10);
m1=tmpminute/10;
m2=tmpminute-(m1*10);
s1=tmpsecond/10;
s2=tmpsecond-(s1*10);
time[0]=IntToChar(h1);
time[1]=IntToChar(h2);
time[2]=':';
time[3]=IntToChar(m1);
time[4]=IntToChar(m2);
time[5]=':';
time[6]=IntToChar(s1);
time[7]=IntToChar(s2);
}
void SendResetCmd(){
char *s="reset";
puts(s);
}
void beep(int offon){
PORTC.7=offon;
}