Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. #define F_CPU 16000000UL // 16MHz clock from the debug processor
  2. #include "avr/io.h"
  3. #include "util/delay.h"
  4. #define BAUD 9600 // Define baud
  5. #define BAUDRATE ((F_CPU)/(BAUD*16UL)-1) // Set baud rate
  6.  
  7. void uart_init (void)
  8. {
  9. UBRR0H = (BAUDRATE>>8); // shift register right by 8 bits
  10. UBRR0L = BAUDRATE; // set baud rate
  11. UCSR0B |= (1<<TXEN0) | (1<<RXEN0); // enable receiver and transmitter
  12. UCSR0C |= (1<<UCSZ00) | (1<<UCSZ01); // 8bit data format
  13. }
  14.  
  15. void uart_transmit (unsigned char data)
  16. {
  17.  
  18. while (!(UCSR0A & (1<<UDRE0))); // wait while register is free
  19. UDR0 = data; // load data in the register
  20.  
  21. }
  22.  
  23. int main(void)
  24. {
  25. char Keypad[4][4] = {{'1','2','3','A'},
  26. {'4','5','6','B'},
  27. {'7','8','9','C'},
  28. {'*','0','#','D'}};
  29.  
  30. uart_init();
  31. DDRD |= (1<<DDD4|1<<DDD5|1<<DDD6|1<<DDD7); //sets pin PD4-PD7 as output
  32. DDRB &= ~(1<<DDB0|1<<DDB1|1<<DDB2|1<<DDB3); //sets pin PB0-PB3 as input
  33. PORTB |= (1<<PORTB0) | (1<<PORTB1)| (1<<PORTB2)| (1<<PORTB3); //enables pull ups on PB0-PB3
  34.  
  35. while (1)
  36. {
  37. for(int i=4; i<8; i++)
  38. {
  39. PORTD |= (1<<4|1<<5|1<<6|1<<7); //set all rows (output) high
  40. PORTD &= ~(1<<i); //Set row(i) low - one at a time and check each column
  41.  
  42. for(int j=0; j<4; j++)
  43. {
  44. if (!(PINB & 1<<j)) //Check each column(j) to see which is pulled Low
  45. {
  46. uart_transmit(Keypad[i-4][j]); //Output the corresponding character
  47. _delay_ms(500);
  48. }
  49. }
  50. }
  51. }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement