Advertisement
Guest User

SSD1306 TWI AVR Assembly

a guest
Jan 13th, 2019
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;Program is made for the Atmega328p for connecting with the SSD1306 OLED
  2. ;Made by int 21h
  3.  
  4. .def temp1 = r16
  5. .def temp2 = r17
  6. .def temp3 = r18
  7.  
  8. .equ SLAVE_ADDRESS_W = 0b01111010
  9.  
  10. ;******************************
  11. ;           INIT
  12. ;******************************
  13.  
  14.     clr   temp2
  15.     clr   temp3
  16.  
  17.     ;Init Stackpointer
  18.     ldi   temp1, HIGH(RAMEND)
  19.     out   SPH, temp1
  20.     ldi   temp1, LOW(RAMEND)
  21.     out   SPL, temp1
  22.  
  23.     ldi   temp1, 0xFF
  24.     out   DDRD, temp1
  25.     out   DDRC, temp1 ;Don't know if needed or not
  26.  
  27. ;******************************
  28. ;           MAIN
  29. ;******************************
  30.  
  31.     ldi   temp1, 0b00110011
  32.     ;8*8 Dotmatrix
  33.     rcall oled_send_data
  34.     rcall oled_send_data
  35.     rcall oled_send_data
  36.     rcall oled_send_data
  37.     rcall oled_send_data
  38.     rcall oled_send_data
  39.     rcall oled_send_data
  40.     rcall oled_send_data
  41.  
  42. end:
  43.     rjmp  end
  44.  
  45. ;******************************
  46. ;           ROUTINEN
  47. ;******************************
  48.  
  49. oled_send_command:
  50.     ;Not worked on this yet
  51.     ret
  52.  
  53. oled_send_data:
  54.     push  temp1
  55.     rcall oled_start
  56.    
  57.     ;Send Slave Addres
  58.     ldi   temp1, SLAVE_ADDRESS_W
  59.     sts   TWDR, temp1
  60.     ldi   temp1, (1 << TWINT)|(1 << TWEN)
  61.     sts   TWCR, temp1
  62. oled_send_data_0:
  63.     lds   temp1, TWCR
  64.     sbrs  temp1, TWINT
  65.     rjmp  oled_send_data_0
  66.  
  67.     ;Send Control Byte
  68.     ldi   temp1, 0x40
  69.     sts   TWDR, temp1
  70.     ldi   temp1, (1 << TWINT)|(1 << TWEN)
  71.     sts   TWCR, temp1
  72. oled_send_data_1:
  73.     lds   temp1, TWCR
  74.     sbrs  temp1, TWINT
  75.     rjmp  oled_send_data_1
  76.  
  77.     ;Send Byte
  78.     pop   temp1
  79.     push  temp1
  80.     sts   TWDR, temp1
  81.     ldi   temp1, (1 << TWINT)|(1 << TWEN)
  82.     sts   TWCR, temp1
  83. oled_send_data_2:
  84.     lds   temp1, TWCR
  85.     sbrs  temp1, TWINT
  86.     rjmp  oled_send_data_2
  87.  
  88.     rcall oled_stop
  89.     pop   temp1
  90.     ret
  91.  
  92. oled_start:
  93.     push  temp1
  94.     ldi   temp1, (1 << TWINT)|(1 << TWSTA)|(1 << TWEN)
  95.     sts   TWCR, temp1
  96. oled_start_0:
  97.     lds   temp1, TWCR
  98.     sbrs  temp1, TWINT
  99.     rjmp  oled_start_0
  100.     ;Check Status Code
  101.     lds   temp1, TWSR
  102.     andi  temp1, 0x08
  103.     sbrs  temp1, 3
  104.     rcall oled_error
  105.     pop   temp1
  106.     ret
  107.  
  108. oled_stop:
  109.     push  temp1
  110.     ldi   temp1, (1 << TWINT)|(1 << TWSTO)|(1 << TWEN)
  111.     sts   TWCR, temp1
  112. oled_stop_0:
  113.     lds   temp1, TWCR
  114.     sbrs  temp1, TWINT
  115.     rjmp  oled_start_0
  116.     pop   temp1
  117.     ret
  118.  
  119. ;If an error occurs, toggle the LED
  120. oled_error:
  121.     push  temp1
  122.     ldi   temp1, 0x01
  123.     out   PORTD, temp1
  124.     pop   temp1
  125.     ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement