Guest User

Untitled

a guest
Jun 28th, 2018
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. .include    "m8def.inc"
  2.  
  3. .def temp = r16
  4. .def shiftcount = r17
  5. .def letter = r18
  6. .def row = r19
  7. .equ row1 = 0x80
  8. .equ row2 = 0xC0
  9. .equ RS_HI_E_HI = 0x03
  10. .equ RS_LO_E_HI = 0x02
  11. .equ RS_HI_E_LO = 0x01
  12. .equ RS_LO_E_LO = 0x00
  13.  
  14. .org 0x00
  15.     rjmp main
  16.  
  17. .org 0x20
  18. main:
  19.     ldi temp, low(RAMEND)
  20.     out spl, temp
  21.     ldi temp, high(RAMEND)
  22.     out sph, temp
  23.  
  24.     ldi temp, 0xFF
  25.     out ddrb, temp
  26.  
  27.     ldi temp, 0x03
  28.     out ddrd, temp
  29.  
  30.     rcall init
  31.     rcall TWI
  32.     rcall GET
  33.     rjmp inf
  34.  
  35. TWI:
  36.     ldi   r16, 0x02                     ; My adress = 1, I don't respond to generic calls
  37.     out   TWAR, r16                     ; Set address in TWAR
  38.     ldi   r16, (1<<TWEA)|(1<<TWEN)
  39.                                         ; Enable, respond to address calls
  40.     out   TWCR, r16                     ; Write to TWI Control Register
  41.    
  42.     WAIT1:
  43.     in    r16, TWCR
  44.     sbrs  r16, TWINT
  45.     rjmp  WAIT1                         ; Wait for TWINT to be set
  46.    
  47.     in    r16, TWSR                     ; Check TWI status
  48.     andi  r16, 0xF0
  49.     cpi   r16, 0x60
  50.     brne  ERROR                         ; Check if SLA+W is received
  51.     ret
  52.  
  53. GET:
  54.     ldi   r16, (1<<TWINT)|(1<<TWEN)|(1<<TWEA)
  55.     out   TWCR, r16                     ; Clear TWINT and enable again
  56.  
  57.     WAIT2:
  58.     in    r16, TWCR
  59.     sbrs  r16, TWINT
  60.     rjmp  WAIT2
  61.        
  62.  
  63.     in    r16, TWSR
  64.     andi  r16, 0xF0
  65.     cpi   r16, 0x80                     ; Om ingen stopbit hittats;
  66.     brne  inf                           ; Fortsätt hämta data
  67.  
  68.     in    letter, TWDR                  ; Wait for data send
  69.     rcall write
  70.  
  71.     rjmp  GET
  72.  
  73. init:
  74.     ldi temp, 0x30 
  75.     out portb, temp
  76.  
  77.     rcall enable
  78.  
  79.     ldi temp, 0x30
  80.     out portb, temp
  81.    
  82.     rcall enable
  83.  
  84.     ldi temp, 0x30
  85.     out portb, temp
  86.    
  87.     rcall enable
  88.  
  89.     ldi temp, 0x3C ; Function Set
  90.     out portb, temp
  91.  
  92.     rcall enable
  93.     rcall clear
  94.  
  95.     ldi temp, 0x06 ; Entry Mode set
  96.     out portb, temp
  97.  
  98.     rcall enable
  99.  
  100.     ldi temp, 0x0C ; Display on
  101.     out portb, temp
  102.  
  103.     rcall enable
  104.     ret
  105.  
  106. inf:
  107.     rjmp inf
  108.  
  109. ERROR:
  110.     rjmp  inf
  111.  
  112. clear:
  113.     ldi temp, 0x01 ; Clear Disp
  114.     out portb, temp
  115.     rcall enable
  116. enable:
  117.     rcall s_wait
  118.     ldi temp, RS_LO_E_HI
  119.     out portd,temp
  120.     rcall s_wait
  121.     ldi temp, RS_LO_E_LO
  122.     out portd,temp
  123.     rcall s_wait
  124.     ret
  125. s_wait:
  126.     ldi zh, high(350)
  127.     ldi zl, low(350)
  128. s_loop:
  129.     sbiw zl,1
  130.     brne s_loop
  131.     ret
  132.  
  133.  
  134. write:
  135.     ldi temp, RS_HI_E_LO
  136.     out portd, temp
  137.  
  138.     rcall s_wait
  139.  
  140.     ldi temp, RS_HI_E_HI
  141.     out portd,temp
  142.  
  143.     rcall s_wait
  144.  
  145.     out portb, letter
  146.  
  147.     rcall s_wait
  148.  
  149.     ldi temp, RS_HI_E_LO
  150.     out portd, temp
  151.  
  152.     rcall s_wait
  153.  
  154.     ldi temp, RS_LO_E_LO
  155.     out portd, temp
  156.  
  157.     ret
  158.  
  159. shift:
  160.     mov temp, shiftcount
  161.     add temp, row
  162.     out portb, temp
  163.  
  164.     rcall enable
  165.  
  166.     ret
Add Comment
Please, Sign In to add comment