Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define F_CPU 16000000UL // 16MHz clock from the debug processor
- #include "avr/io.h"
- #include "util/delay.h"
- #define BAUD 9600 // Define baud
- #define BAUDRATE ((F_CPU)/(BAUD*16UL)-1) // Set baud rate
- void uart_init (void)
- {
- UBRR0H = (BAUDRATE>>8); // shift register right by 8 bits
- UBRR0L = BAUDRATE; // set baud rate
- UCSR0B |= (1<<TXEN0) | (1<<RXEN0); // enable receiver and transmitter
- UCSR0C |= (1<<UCSZ00) | (1<<UCSZ01); // 8bit data format
- }
- void uart_transmit (unsigned char data)
- {
- while (!(UCSR0A & (1<<UDRE0))); // wait while register is free
- UDR0 = data; // load data in the register
- }
- int main(void)
- {
- char Keypad[4][4] = {{'1','2','3','A'},
- {'4','5','6','B'},
- {'7','8','9','C'},
- {'*','0','#','D'}};
- uart_init();
- DDRD |= (1<<DDD4|1<<DDD5|1<<DDD6|1<<DDD7); //sets pin PD4-PD7 as output
- DDRB &= ~(1<<DDB0|1<<DDB1|1<<DDB2|1<<DDB3); //sets pin PB0-PB3 as input
- PORTB |= (1<<PORTB0) | (1<<PORTB1)| (1<<PORTB2)| (1<<PORTB3); //enables pull ups on PB0-PB3
- while (1)
- {
- for(int i=4; i<8; i++)
- {
- PORTD |= (1<<4|1<<5|1<<6|1<<7); //set all rows (output) high
- PORTD &= ~(1<<i); //Set row(i) low - one at a time and check each column
- for(int j=0; j<4; j++)
- {
- if (!(PINB & 1<<j)) //Check each column(j) to see which is pulled Low
- {
- uart_transmit(Keypad[i-4][j]); //Output the corresponding character
- _delay_ms(500);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement