| CODE |
;*************************************************************************************** ; ; ; Title: RF remote control at 418MHz with roll-code ; ; (RF 418MHz transmitter code) ; ; Device: AT90s2323 at 4MHz crystal ; and TLP-434 RF transmitter (418 MHz) ; Author: Serasidis Vasilis ; Date: 04.06.2004 ; Updated: 15.04.2005 Added roll-code feaure. ; Home: http://www.serasidis.gr ; email: info@serasidis.gr ; ; Based in software UART at 2400bps, 8n1 ; ;*************************************************************************************** .include "2323def.inc" ;Define chip .equ TxD =0 ;Transmition pin is PB0 .equ key1 =2 ;Key No1 is connected to pin PB2 .equ key2 =1 ;Key No2 is connected to pin PB1 .equ Key_1 =01 .equ Key_2 =02 .def temp =R16 ;temporary storage register. .def temp2 =R17 ;temporary storage register. .def Txbyte =R18 ;Data to be transmitted. .def init_byte =R19 ;This byte is sended first and its to recognize the receiver the correct remote control. .def second_byte =R20 ;This byte is sended second and its to recognize the receiver the correct remote control. .def command =R21 ;The command byte mean which key is pressed from the remote. .def bitcnt =R23 ;bit counter .def count =R24 .def dcount =R25 .def dcount2 =R26 .def rollcode =R27 .cseg .org 0 rjmp reset ;Reset handler ret ;Return from sleep and go to "keys" address. reset: ldi temp,RAMEND out SPL,temp ;Init Stack Pointer ldi temp,0b00000001;PB0 output, PB1, PB2 inputs out DDRB,temp ldi temp,0b00000111 out portB,temp ;enable internal pullup resistor on PB1 and disable on PB2... ;...and enable Data-pin TxD (PB0). start: clr second_byte Release_keys: rcall delay sbis PINB,key1 ; rjmp Release_keys rcall delay sbis PINB,key2 ; rjmp Release_keys ;subi second_byte,$20 ldi temp,0x30 ;turn on sleep mode (Power-down mode) out MCUCR,temp ;interrupt on low level. ldi temp,0x40 ;enable external interrupts (INT0) out GIMSK,temp sei ;enable global interrupts ready sleep ;go to sleep mode ;=================================================================================== keys: sbic PINB,key1 ; rjmp next_key ldi command,Key_1 ;load the code for key No1 rjmp Enable_RF next_key: ldi command,Key_2 ;load the code for key No2 Enable_RF: ldi count,4 load: ldi init_byte,$30 ;Load init_byte register with initial byte (recognition byte). mov txbyte,init_byte;1) send initial byte rcall b_transmit mov txbyte,second_byte;2) send second byte rcall b_transmit mov txbyte,command ;3) send the command byte rcall b_transmit eor init_byte,second_byte;4) create the checksum of bytes "init_byte" and "second_byte"... eor init_byte,command mov txbyte,init_byte;...move the resault to transmit byte (txbyte)... rcall b_transmit ;and send it out. dec count ;send 4 times the data brne load inc second_byte rjmp Release_keys; ;*************************************************************************** ;* Transmition routine TxD ;*************************************************************************** b_transmit: ldi bitcnt,10;1+8+sb (sb is # of stop bits) com Txbyte ;Invert everything sec ;Start bit putchar0: brcc putchar1;If carry set cbi PORTB,TxD; send a '0' rjmp putchar2;else putchar1: sbi PORTB,TxD; send a '1' nop putchar2: rcall UART_delay;One bit delay rcall UART_delay lsr Txbyte ;Get next bit dec bitcnt ;If not all bit sent brne putchar0;send next ret ;return ;*************************************************************************** ;* ;* "UART_delay" ;* ;* This delay subroutine generates the required delay between the bits when ;* transmitting and receiving bytes. The total execution time is set by the ;* constant "b": ;* ;* 3xb + 7 cycles (including rcall and ret) ;*************************************************************************** .equ b=135;2400 bps @ 4 MHz crystal, +5V .equ c=2 UART_delay: ldi temp2,c UART_delay2: ldi temp,b UART_delay1: dec temp brne UART_delay1 dec temp2 brne UART_delay2 ret ;*************************************************************************** ;* Delay routine ;*************************************************************************** delay: ldi dcount2,10 delay_: ldi dcount,250 delay1: dec dcount brne delay1 dec dcount2 brne delay_ ret .db "(c) 09.06.2004 by Serasidis Vasilis. www.serasidis.gr" |