Zdravim,
posledni dobou si hraji s attiny2313, a zkousim si naprogramovat vlastni jednoduchy seriovy prenos mezi dvema mikrokontrolery.
Pro prijem pouzivam INT1 externi preruseni. Vypada to zhruba tak, ze zmenou logicke urovne se zavola obsluha preruseni a po predem danych casovych intervalech se odebiraji vzorky na tomto pinu. Postupne je tak sestaven cely prijimany Byte.
Jenze toto preruseni se vola 2x za sebou, i kdyz zadna zmena logicke urovne na tomto pinu nenastala.
Zde je vetsina kodu:
;##### INT1 interrupt routine ########################
INT1_routine:
rcall wait_20ms ;Sycn with transmitter
ldi code, 0b00000001 ;start-bit
ldi loop, 0x07
ldi R25, 0b00000001 ; and make another zero
while:
rcall wait_50ms ;wait for another bit
lsl code ;shift code byte
in temp, PIND
lsr temp ;Our bit is on 4th position
lsr temp ; shift it to 1st
lsr temp
and temp, R25 ; by logical AND
add code, temp ;adding bit to code from the righ
dec loop ;Do the same for rest of the bits
cpi loop, 0
brne while
out PORTB, code ;Light LED diodes on PORTB
rcall wait_1sec ;Wait one second
ldi temp, 0xff ;and turn off the LED dispaly
out PORTB, temp ;before new number can be received
reti ;Jump out of interrupt
;and wait for another transmission
;====================================================
; ===== Wait for interrupt/transmission ============
wait:
rjmp wait
Kdyby nahodou, vysila se 8 bitu, prvni a posledni je vzdy log.1 a kazdy bit trva 50 ms. :(