• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

ATtiny2313 SIRC help please

Status
Not open for further replies.

AtomSoft

Well-Known Member
Hey guys i know not many AVR users here and most are using C perhaps :( but here is my code. Im not sure whats wrong tho. I know my 2313 is running at 8mhz i checked the fuses.

Code:
.include "tn2313def.inc" 
.def MyIr = R14
.def Temp = R15
.def devTmp = R16		
.def cmdTmp = R17
.def mask = R18
	
.def d1 = R20 	
.def d2 = R21 	
.def d3 = R22
.def sPulse = R23
 	
.org 0x0000 			
    rjmp RESET 			

RESET: 	
    ldi sPulse, 0x0C        ;12 * (200us) = 2.4mS
    ldi d1, 0x00
    ldi d2, 0x00
    ldi d3, 0x00
    ldi cmdTmp,0x00
    ldi devTmp,0x00
	ldi mask,0b11111110

	out DDRB,mask ; port B all outputs except PB0
	out PORTB,d1  ; disable pullups and set all low
MainLoop:
    rcall   GetSirc
    cpi     cmdTmp,0x01
	brne	notcorr
correct:
	sbi     PORTB,1
	rjmp    MainLoop
notcorr:
	cbi     PORTB,1
	rjmp    MainLoop
	
GetSirc:
    rcall   WaitLow
GetStart:
    inc     d1
    rcall   Delay200uS
    in      MyIR,PINB
    sbrs    MyIR,0         ;Check if pin hi/lo
    rjmp    GetStart       ;Pin still low add more
    cpi     d1,0x0B        ;The pin is now high
    brlo    GetSirc        ;if the count is less than 11 bad start
    cpi     d1,0x0E        ;if the count is equal to or geater than 14 bad start
    brsh    GetSirc

    ldi     d1, 0x00       ;If it made it here the start pulse was ok
    ldi     d2, 0x07
GetSeven:
    dec     d2
    lsr     cmdTmp
repeatS:
    rcall   WaitLow
    inc     d1
    rcall   Delay200uS
    in      MyIR,PINB
    sbrs    MyIR,0          ;Check if pin hi/lo
    rjmp    repeatS         ;If pin still low repeat

    cpi     d1,0x05
    brlo    DoneOring  
    ori     cmdTmp,$40
DoneOring:
    cpi     d2, 0x00        ;Check if collected 7 bits
    brne    GetSeven
    ldi     d1, 0x00
    ldi     d2, 0x05
GetFive:    
    dec     d2
    lsr     devTmp
repeatF:
    rcall   WaitLow
    inc     d1
    rcall   Delay200uS
    in      MyIR,PINB
    sbrs    MyIR,0          ;Check if pin hi/lo
    rjmp    repeatF         ;If pin still low repeat

    cpi     d1,0x05
    brlo    DoneOring2  
    ori     devTmp,$10
DoneOring2:
    cpi     d2, 0x00        ;Check if collected 7 bits
    brne    GetFive
    ldi     d1, 0x00
    ldi     d2, 0x00
    ret

Delay200uS:                ;delay 202us @ 8mhz 
    ldi     d1, $06
L2:
    ldi     d2, $59
L3:
    dec     d2         
    brne    L3        
    dec     d1         
    brne    L2        
    ret

WaitLow:
    in      MyIR,PINB
    sbrc    MyIR,0
    rjmp    WaitLow
    ret
im i checking the data from a pin ok?
 

AtomSoft

Well-Known Member
I fixed it it had to do with the stack pointer :D
Code:
.include "tn2313def.inc" 
.def devTmp = R16		
.def cmdTmp = R17
.def mask = R18
.def temp = R19
.def d1 = R20 	
.def d2 = R21 	
.def d3 = R22
.def sPulse = R23
.def MyIr = R24
.def d4 = R25
 	
.org 0x0000 			
 
RESET: 
	ldi R26,RAMEND    ; Initial Stack Pointer to SRAM END address (RAMEND)       
    out SPL,R26       ; Stack Pointer (SP) = RAMEND	
    ldi sPulse, 0x0C        ;12 * (200us) = 2.4mS
    ldi d1, 0x00
    ldi d2, 0x00
    ldi d3, 0x00
    ldi cmdTmp,0x00
    ldi devTmp,0x00
	ldi temp,0x01
	ldi mask,0b11111110

	out DDRB,mask ; port B all outputs
	out PORTB,temp ; initialize port B
MainLoop:
	rcall   Delay100mS
	clr		d1
	clr		d2
	clr		d3
    clr 	cmdTmp
    rcall   GetSirc

	sbi     PORTB,1
    cpi     cmdTmp,0x06
	brne	DoneM
	cbi     PORTB,1
DoneM:
	rjmp    MainLoop
try2:
	sbi     PORTB,1
	rjmp    MainLoop
	
GetSirc:
    rcall   WaitLow
GetStart:
    inc     d3
    rcall   Delay200uS
	SBIC 	PINB,0
    rjmp    GetStart       ;Pin still low add more
    cpi     d3,11        ;The pin is now high
    brlo    GetSirc        ;if the count is less than 11 bad start
    cpi     d3,13        ;if the count is equal to or geater than 14 bad start
    brsh    GetSirc

    ldi     d3, 0x00       ;If it made it here the start pulse was ok
    ldi     d4, 0x07

GetSeven:
    dec     d4
    lsr     cmdTmp
repeatS:
    rcall   WaitLow
    inc     d3
    rcall   Delay200uS
	SBIC 	PINB,0
    rjmp    repeatS         ;If pin still low repeat

    cpi     d3,0x05
    brlo    DoneOring  
    ori     cmdTmp,$40
DoneOring:
    cpi     d4, 0x00        ;Check if collected 7 bits
    brne    GetSeven


    ldi     d3, 0x00
    ldi     d4, 0x05
GetFive:    
    dec     d4
    lsr     devTmp
repeatF:
    rcall   WaitLow
    inc     d3
    rcall   Delay200uS
	SBIC 	PINB,0
    rjmp    repeatF         ;If pin still low repeat

    cpi     d3,0x05
    brlo    DoneOring2  
    ori     devTmp,$10
DoneOring2:
    cpi     d4, 0x00        ;Check if collected 7 bits
    brne    GetFive
    ldi     d3, 0x00
    ldi     d4, 0x00
    ret

Delay200uS:                ;delay 202us @ 8mhz 
    ldi     d1, $06
L2:
    ldi     d2, $59
L3:
    dec     d2         
    brne    L3        
    dec     d1         
    brne    L2        
    ret

WaitLow:
	SBIS 	PINB,0
    rjmp    WaitLow
    ret
; ============================= 
;    delay loop generator 
;     800000 cycles: (100mS)
; ----------------------------- 
; delaying 799995 cycles:
Delay100mS:
          ldi  d1, $5F
WGLOOP0:  ldi  d2, $17
WGLOOP1:  ldi  d3, $79
WGLOOP2:  dec  d3
          brne WGLOOP2
          dec  d2
          brne WGLOOP1
          dec  d1
          brne WGLOOP0
; ----------------------------- 
; delaying 3 cycles:
          ldi  d1, $01
WGLOOP3:  dec  d1
          brne WGLOOP3
; ----------------------------- 
; delaying 2 cycles:
          nop
          nop
; ============================= 
		  ret
I got it to work when i press button 7 aka 0x06 . I chose a nice number to test. The issue is it works 1 time. Like when i press the wrong number or button it should turn off the LED but no luck :(
 
Last edited:

Sceadwian

Banned
Have you verified those delay loop lengths? Also if you're going to continue programming in ASM you should probably make a custom delay subroutine/macro you have one for 100ms and one for 200ms when you should just have a single routine that you push a few bytes on the stack to set the length.
Nothing jumped out and bit me right away, but my AVR ASM is rusty.
 

Sceadwian

Banned
NPPC Software for ATMEL MCU programming
Might be someplace you'll want to visit, looks good from a first glance for re-creating a stimulus file so you can simulate what's going on in code with a given input signal.
 

AtomSoft

Well-Known Member
hey i got tired of ASM so i went to C and yay got it working:
Code:
    #define F_CPU 1000000UL                                    /* Clock Frequency = 1Mhz */

    #include <inttypes.h>
    #include <avr/io.h>
    #include <util/delay.h>

	#define irPin (PINB & (1<<PINB0))

    unsigned char ir_add;
    unsigned char ir_cmd;


void GetSIRC(){
    char x;
	char lTime;

StartLook:
    ir_add = ir_cmd = 0;

    while(irPin);               //wait for it to be low
    lTime = 0;                  //reset the counter

    while(irPin == 0){          //while the pin is low which is our pulse count
        lTime++;                //increment every 200uS until pin is high
        _delay_us(200);         //200uS delay
    }

    if(lTime <= 10)             //Start too short
        goto StartLook;         //Restart
    if(lTime >= 14)             //Start too long
        goto StartLook;         //Restart
			
    lTime = 0;
    for(x=0;x<7;x++){           //repeat 7 times for command
        ir_cmd >>= 1;           //if it was skipped or is done ORing then shift over the 1

        while(irPin);           //wait for it to be low
        lTime = 0;              //reset the counter

        while(irPin == 0){      //while the pin is low which is our pulse count
            lTime++;            //increment every 200uS until pin is high
            _delay_us(200);     //200uS delay
        }

        if(lTime >= 6)          //If its high then OR a 1 in else skip
            ir_cmd |= 0x40;     //if its less than 6 its a 0 so dont OR it		
								
    }
    for(x=0;x<5;x++){           //repeat 5 times for address/device
        ir_add >>= 1;           //if it was skipped or is done ORing then shift over the 1

        while(irPin);           //wait for it to be low
        lTime = 0;              //reset the counter

        while(irPin == 0){      //while the pin is low which is our pulse count
            lTime++;            //increment every 200uS until pin is high
            _delay_us(200);     //200uS delay
        }

        if(lTime >= 6)          //If its high then OR a 1 in else skip
            ir_add |= 0x10;     //if its less than 6 its a 0 so dont OR it			
    }

}

int main(){                         // The main function

	DDRB = 0b11111110;                  // Set all the pins of PortB as output (PB0 = input)


    while (1) {                         // Set up an infinite loop
		GetSIRC();
		if(ir_cmd == 0x06)
			PORTB|=(1<<PB1);
		else
			PORTB&=~(1<<PB1);

		_delay_ms(200);
    }
}
 

be80be

Well-Known Member
Jason you no any C18 cheat sheets That code looks a lot easier then on the pic good work. Jason
 

AtomSoft

Well-Known Member
I dont have any cheat sheets sorry. But i might create a cheat sheet come to think of it. I seem to forget alot of codes and there are alot of handy things there so . Ill try to make a cheat sheet for me and of course share it.

C is so cool tho i be a cheat sheet would work across platforms like a C18 would be similar to a SourceBoostC and AVR-GCC cheat sheet.


Hey Burt do me a favor and check it my website link works.. and replay back with its status like if its online or not. Im hosting off my PC for now but not sure if i setup correctly.

EDIT
You know what got me lost first...
The values are inverted lol

Like for a PIC to set a pin as input you would:
TRISA= 0b11111111; // The 1 = Input and 0 = Output

But for a AVR its:
DDRA = 0b00000000; // The 0 = Input and 1 = Output
 
Last edited:

be80be

Well-Known Member
Presumably he's not married?
And no Girlfriend Lol he burned that ATtiny2313 up I was thinking where did he get the time myself. Every time I get some thing going I have to stop and take my kids some where then when I get back. I have to rethink what I was doing. I got me a laptop and take them skating that gets me 4 hour and no beating the door down to go some where.
P.S. that's why I ask about the cheat sheet
 

Nigel Goodwin

Super Moderator
Most Helpful Member
And no Girlfriend Lol he burned that ATtiny2313 up I was thinking where did he get the time myself. Every time I get some thing going I have to stop and take my kids some where then when I get back. I have to rethink what I was doing. I got me a laptop and take them skating that gets me 4 hour and no beating the door down to go some where.
P.S. that's why I ask about the cheat sheet
Don't mention kids!!!.

Mu daughter passed her driving test this week :eek:

I'm currently at work car-less, hoping she remembers to come and pick me up later :rolleyes: Not to mention, adding her to my insurance has quadrupled my insurance costs!,
 

be80be

Well-Known Member
Don't mention kids!!!.

Mu daughter passed her driving test this week

I'm currently at work car-less, hoping she remembers to come and pick me up later Not to mention, adding her to my insurance has quadrupled my insurance costs!,
I don't even want to think about that I have twins Girls. they be driving at the same time.
 

AtomSoft

Well-Known Member
I have a Wife and 1 Son lol hes almost 3 years old.

The wife works from 7am - 4pm (this includes traveling time)
The son is in daycare from 8am - 5pm (includes traveling time)

So i have lots of time to myself. :D
On the weekends they are both here. Right now im telling my son to stop fighting with my nephew :D

Thanks for the complements guys. I made a PDF but its only 1 page so far. Tell me what you think...

I will try to shrink everything to fit into a few pages when its complete ....

I just want to make sure i can see the info and notice any mistakes :D

http://atomsofttech.no-ip.info/tempdl/c18Cheat.pdf
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top