//Peter Greczner - pag42@cornell.edu //Matthew Rosoff - msr53@cornell.edu //Include Files #include #include "constants_32.h" #include #include //USART Variables #define USART_BUFFER_SIZE 24 //ring buffer size #define USART_BAUDRATE(br, fosc) (fosc*125000/br-1) //macro to calculate UBRRL //Ring Buffer variables char usart_buffer[USART_BUFFER_SIZE]; //the USART char ring buffer unsigned char head,end,overflow; unsigned char token_start,token_end; unsigned char cmd_done=0; //variable to hold whether a command has been entered via carriage rtrn eeprom unsigned int codes[8] = {0,0,0,0,0,0,0,0};//eeprom codes eeprom unsigned char code_index = 0; //code index of the highest code stored in eeprom unsigned char i; //character variable used for loop indices unsigned char but_id[7] = {0,0,0,0,0,0,0}; unsigned char id_index = 0; unsigned char isSendable = 0; char invalid_code; //flag if an invalid code is entered //DEBOUNCE VARIABLES and KEYPAD VARIABLES**************************************************** #define maxkeys 16 //State machine state names #define NoPush 1 #define MaybePush 2 #define Pushed 3 #define MaybeNoPush 4 unsigned char key, butnum; unsigned int time2 = 30; unsigned int time2_reset = 30; unsigned char PushFlag; //message indicating a button push unsigned char PushState = 1; //state machine //END DEBOUNCE VARIABLES************************************************************************ //key pad scan table flash unsigned char keytbl[16]={0xee, 0xed, 0xeb, 0xe7, 0xde, 0xdd, 0xdb, 0xd7, 0xbe, 0xbd, 0xbb, 0xb7, 0x7e, 0x7d, 0x7b, 0x77}; //array to store the number that was pressed on the num pad, records 100 if a non-valid num was //pressed unsigned int numPad[17] = {100,1,2,3,100,4,5,6,100,7,8,9,100,99,0,100,100}; int multi[4] = {1000,100,10,1}; //multipliers for converting the code char numsEntered = 0; //keeps track of how many nums were entered in a code int currCode = 0; //holds the current code's integer value while being pressed char numWrong = 0; //number of codes wrong in a row //Security State variables #define OPEN 0 //currently not locked out, open #define LOCKDOWN 1 //in a forced lockdown #define LOCKMINUTE 2 //in a 1 minute lockdown caused by 3 incorrect codes in a row char lockState = 0; //holds the current lock down state //Timing variables #define t1 1000 //definition to hold how long to count for 1 second to pass unsigned int time1; unsigned int lock_min_status; unsigned int day, hour, minute, second; //current time variables unsigned int sd, sh, sm; //stored time variables in case of an incorrect data entry on setting the date //Interrupt for timer2 that executes every millisecond, triggers and event in main interrupt [TIM2_COMP] void timer2_compare(void) { if(time2 > 0) --time2; } //Interrupt for timer0 that executes every millisecond, triggers the date event in main interrupt [TIM0_COMP] void timer0_compare(void) { //Decrement the time if not already zero if (time1>0) --time1; } //Method to get which button on the keypad was pressed, used in lab 2 //keypad scanning code to get the button number that was pressed on the keypad void getButtonPress(){ //get lower nibble DDRC = 0x0f; PORTC = 0xf0; delay_us(5); key = PINC; //get upper nibble DDRC = 0xf0; PORTC = 0x0f; delay_us(5); key = key | PINC; //find matching keycode in keytbl if (key != 0xff) { for (butnum=0; butnum= USART_BUFFER_SIZE) end = 0; // Get data from the buffer return usart_buffer[end]; } //helper method which returns the number of unread characters unsigned char usart_unread_data(void) { if (overflow) return USART_BUFFER_SIZE; if (head> end) return head - end; if (head < end) return USART_BUFFER_SIZE-end+head; return 0; } //helper method which returns the length of the last command(token) entered unsigned char usart_token_length(void){ if(overflow)return USART_BUFFER_SIZE; if(token_starttoken_end)return USART_BUFFER_SIZE-token_start+token_end; return 0; } //helper method to write a character via USART void usart_putc(char data) { while (!(UCSRA & 0x20)); // Wait untill USART data register is empty // Transmit data UDR = data; } //testing helper method to print out entire ring buffer void print_usart_buffer(){ char x=0; for(x = 0; x=USART_BUFFER_SIZE)head=0; if(head==end)overflow++; usart_buffer[head]=UDR; if(usart_buffer[head]=='\r'){ cmd_done=1; token_start=token_end+1; token_end=head; } usart_putc(usart_buffer[head]);//Echo characters } //Our main method void main(void){ char buffer[40]; //for debugging char temp; char x=0; char length; //Set up the LEDs on Port A DDRA = 0xff; PORTA = 0xff; //Set up the USART UBRRL = 103; UCSRB|=(1<