/* password_P.c */ /* String constants are stored and accessed from program memory. */ #include // register names/addresses and bit names/numbers #include // for the nop instruction #include // macros for strings in program memory #define MAX_STRING 12 void init_uart( void ); unsigned char getchar_uart( void ); void putchar_uart( unsigned char ); void decimal_char_out_uart( unsigned char); void string_out_uart_P( const unsigned char * ); // uses string constants stored in program memory unsigned char check_password_P( unsigned char * input_string, unsigned char * candidate_string ); unsigned char check_all_passwords_P( unsigned char * input_string ); int main(void) { unsigned char text_input[ MAX_STRING ]; unsigned char num = 0; init_uart(); while(1) { string_out_uart_P(PSTR( "Your password: " )); string_in_uart( text_input ); num = check_all_passwords_P( text_input ); if(num){ string_out_uart_P( PSTR( "Number: " )); decimal_char_out_uart( num ); string_out_uart_P( PSTR( " you may pass!\r\n" ) ); } else string_out_uart_P( PSTR( "You unknown, stay where you are!\r\n" ) ); } return 0; /* never reached */ } /* *********************************** */ /* FUNCTIONS */ /* *********************************** */ void init_uart( void ) { // Bits in UCSR0B register: "RXCIE0 TXCIE0 UDRIE0 RXEN0 TXEN0 UCSZ02 RXB80 TXB80" UCSR0B |= (1<<(RXEN0)) | (1<<(TXEN0)); // "----1---" // Bits in UCSR0C register: "UMSEL01 UMSEL00 UPM01 UPM00 USBS0 UCSZ01/UCPHA0 UCSZ01/UCPHA0 UCPOL0" UCSR0C |= (1<<(UCSZ01)) | (1<<(UCSZ00)); // "-----11-" UBRR0 = 103; // Baud prescaler 103 -> 9600 Baud } unsigned char getchar_uart( void ) { unsigned char ch; // Bits in UCSR0A register: "RXC0 TXC0 UDRE0 FE0 DOR0 UPE0 U2X0 MPCM0" // wait until a byte is ready to read while( ( UCSR0A & ( 1<<(RXC0) ) ) == 0 ){} // grab the byte from the serial port ch = UDR0; return ch; } void putchar_uart( unsigned char ch ) { // Bits in UCSR0A register: "RXC0 TXC0 UDRE0 FE0 DOR0 UPE0 U2X0 MPCM0" // wait until the port is ready to be written to while( ( UCSR0A & ( 1<<(UDRE0) ) ) == 0 ){} // write the byte to the serial port UDR0 = ch; } void decimal_char_out_uart( unsigned char number ) { putchar_uart('0'+ number/100); // print 100's number = number%100; putchar_uart( '0' + number/10 ); // print 10's number = number%10; putchar_uart( '0' + number ); // print 1's } void string_in_uart( char * string ) // Expects '\r' + '\n' to end input. Windows style. { unsigned char charCount, c; for( charCount = 0; ; charCount++ ) { c = getchar_uart( ); /* input 1 character */ string[charCount] = c; /* store the character */ putchar_uart( c ); /* echo the character */ if(c=='\r' ) charCount-- ; /* skip and wait for '\n' to end input */ if( c=='\n'){ string[charCount] = '\0'; return; } /* end of string and end of input */ if( charCount == (MAX_STRING-1)) charCount-- ; /* prevent buffer overflow */ } } unsigned char check_password_P( unsigned char * input_string, unsigned char * candidate_string ) { unsigned char i; for(i=0; ; i++) { if( (pgm_read_byte(&candidate_string[i])) != input_string[i] ) return 0; /* no match */ if( (pgm_read_byte(&candidate_string[i])) == 0x00 ) return 1; /* exact match up to length of candidate */ } } unsigned char check_all_passwords_P( unsigned char * input_string ) { if( check_password_P(input_string, PSTR("kalle") )) return 1; else if( check_password_P(input_string, PSTR("olle") )) return 2; else if( check_password_P(input_string, PSTR("nisse") )) return 3; else return 0; } /* *********************************** */ /* HARDWARE */ /* *********************************** */ /* Chip ATMega328 Arduino Uno R3 stackable header _______ Digital: _____/ \__ Analog: ______________ ______________ txd ->-|D00 >RXD A5|- | \/ | rxd -<-|D01 -(D01)-|03 PD1/TXD PC3 26|-(A3)- -|D04 A1|- -(D02)-|04 PD2/INT0 PC2 25|-(A2)- -|D05~ A0|- -(D03)-|05 PD3/INT1/PWM PC1 24|-(A1)- -|D06~ | Power: -(D04)-|06 PD4 PC0 23|-(A0)- -|D07 Vin|- +5V ---|07 VCC GND 22|--- Gnd | GND|- Gnd ---|08 GND AREF 21|--- Vin -|D08 GND|- Xtal |X|--|09 PB6/OSC1 AVCC 20|--- +5V -|D09~ +5V|- 16MHz |X|--|10 PB7/OSC2 SCK/PB5 19|-(D13)- -|D10~ +3.3V|- -(D05)-|11 PD5/PWM MISO/PB4 18|-(D12)- -|D11~ Res|- -(D06)-|12 PD6/PWM PWM/MOSI/PB3 17|-(D11)- -|D12 IOREF|- -(D07)-|13 PD7 PWM/SS'/PB2 16|-(D10)- -|D13 LED --- | -(D08)-|14 PB0 PWM/PB1 15|-(D09)- -|GND | |______________________________| -|AREF | -|SCL | -|SDA | |________________| */