Advertisement
abdullahkahraman

AVR - 7seg with button

Apr 18th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. #include <avr/io.h>
  2. #include <avr/pgmspace.h>
  3. #include <util/delay.h>
  4. #include <util/twi.h>
  5.  
  6. #define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
  7. #define CPU_16MHz 0x00
  8. #define CPU_8MHz 0x01
  9. #define CPU_4MHz 0x02
  10. #define CPU_2MHz 0x03
  11. #define CPU_1MHz 0x04
  12. #define CPU_500kHz 0x05
  13. #define CPU_250kHz 0x06
  14. #define CPU_125kHz 0x07
  15. #define CPU_62kHz 0x08
  16.  
  17. #define SUCCESS 1
  18. #define ERROR 0
  19.  
  20. void TWI_init(void)
  21. {
  22. //set SCL frequency
  23. TWSR = 0x01;
  24. TWBR = 0x0C;
  25. //enable TWI
  26. TWCR = (1<<TWEN);
  27. }
  28.  
  29. void TWI_start(void)
  30. {
  31. TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
  32. while ((TWCR & (1<<TWINT)) == 0);
  33. }
  34.  
  35. void TWI_stop(void)
  36. {
  37. TWCR = (1<<TWINT)|(1<<TWSTO)|(1<<TWEN);
  38. }
  39.  
  40. void TWI_put_char(uint8_t u8data)
  41. {
  42. TWDR = u8data;
  43. TWCR = (1<<TWINT)|(1<<TWEN);
  44. while ((TWCR & (1<<TWINT)) == 0);
  45. }
  46.  
  47. uint8_t TWI_get_status(void)
  48. {
  49. uint8_t status;
  50. //mask status
  51. status = TWSR & 0xF8;
  52. return status;
  53. }
  54.  
  55. uint8_t serial_7seg_write_char(uint8_t data)
  56. {
  57. TWI_start();
  58. if (TWI_get_status() != 0x08)
  59. return ERROR;
  60. // send the address
  61. TWI_put_char(0xE2);
  62. if (TWI_get_status() != 0x28)
  63. return ERROR;
  64. // write one bye
  65. TWI_put_char(data);
  66. if (TWI_get_status() != 0x28)
  67. return ERROR;
  68. TWI_stop();
  69. return SUCCESS;
  70. }
  71.  
  72. uint8_t serial_7seg_move_cursor()
  73. {
  74. return serial_7seg_write_char(0x79);
  75. }
  76.  
  77. uint8_t serial_7seg_clear_display()
  78. {
  79. return serial_7seg_write_char(0x76);
  80. }
  81.  
  82. int main(void)
  83. {
  84. // set for 16 MHz clock
  85. CPU_PRESCALE(CPU_16MHz);
  86.  
  87. TWI_init();
  88.  
  89. DDRB &= ~(1 << PB7);
  90.  
  91. while (1)
  92. {
  93. if (PINB & (1<<PB7))
  94. {
  95. _delay_ms(100);
  96. if (PINB & (1<<PB7))
  97. {
  98. serial_7seg_write_char('s');
  99. serial_7seg_write_char('e');
  100. serial_7seg_write_char('n');
  101. serial_7seg_write_char('d');
  102. }
  103. }
  104. }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement