Attiny2313 input problem 0V [solved]

Go To Last Post
21 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Can someone help me, I got some problems with a simple input and output on Attiny2313: the outputs got 2v value, and some huge delays = strange behavior

1. If I want to use the inputs with a default 0v and do something when 5v detected on that inputs, a strange behavior happen. (2v on output, delays, it just not working)

2. If I put inputs with 5v default and do something when 0v detected on that inputs, all goes perfect.

EX1: 
ldi temp 0b11110000
out DDRB, temp

ldi temp 0b00000000
out DPORTB, temp
........
in temp,PINB

///if "temp" has a specific value , then do:///

sbi PORTB, 1
cbi PORTB, 2

THAT CODE IS NOT WORKING
----------------------------------------------------------

EX1: 
ldi temp 0b11110000
out DDRB, temp

ldi temp 0b00001111
out DPORTB, temp
........
in temp,PINB

///if "temp" has a specific value (different because is works on 0 logic), then do: ///

sbi PORTB, 1
cbi PORTB, 2

THAT CODE IS WORKING
______________________________

I read that is something with pull-up resistors, and from what i ve tested, if the pull-up rezistors are disabled, the output got huge delays, 2V on some outputs...just strange. (i ve disable it with bit 7 on MCUR and also i read that when INPUT got 0V default, the pull up are disabled)

Anyone got an idea ?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Choose which pins you want to read as inputs.
It looks as if you want to read PB4, PB5, PB6, PB7.

For reading a switch: use the internal pull-up resistors or use real external resistors. The switch reads 5V when open. 0V when closed to GND.

For reading the output from some other device with a defined 0V / 5V output, you do not need pull-ups.

When you read the whole port e.g. 'ldi temp,PINB' you need to look at the specific bits. Normally by 'andi temp,(1<<PB2)' or similar.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

When you read the whole port e.g. 'ldi temp,PINB' you need to look at the specific bits. Normally by 'andi temp,(1<<PB2)' or similar.

But note that PINB (and all the lower IO port registers) are in range of the direct bit access op codes SBI, CBI, SBIC, SBIS. So to check bit 2 as in David's ANDI example use:

wait_bit_set:
  SBIS PINB, 2
  RJMP wait_bit_set
; here when bit 2 in PINB is set

This uses less code space and less cycles than an IN, ANDI, branch sequence.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

to be more specific I'll post the exact code:

EX1:
ldi temp 0b11110000
out DDRB, temp
ldi temp 0b00000000
out DPORTB, temp

read:
 in temp,PINB
 andi temp, 0b00000011 ; extract the state of first  two inputs

nothing:
  cpi temp,0
  brne next
  cbi PORTB,5
  cbi PORTB,4
  rjmp read

next:
  cpi temp,2
  brne back
  sbi PORTB,5
  cbi PORTB,4

back:
  cpi temp,2
  brne read
  cbi PORTB,5
  sbi PORTB,4
  rjmp read

What i m trying to do, it to duplicate 2 inputs with 2 outputs. So exactly what happened to the inputs PB0 and PB1, should replicate to outputs PB4,PB5 (EXCLUDING THE CASE WHEN BOTH INPUTS ARE 5V)

This above code is not working. I got that strange behavior of outputs.

If i do like this, with default 5v and 0V at change, it works:

EX2:
ldi temp 0b11110000
out DDRB, temp
ldi temp 0b00001111
out DPORTB, temp

read:
 in temp,PINB
 andi temp, 0b00000011 ; extract the state of first  two inputs

nothing:
  cpi temp,3
  brne next
  cbi PORTB,5
  cbi PORTB,4
  rjmp read

next:
  cpi temp,1
  brne back
  sbi PORTB,5
  cbi PORTB,4

back:
  cpi temp,2
  brne read
  cbi PORTB,5
  sbi PORTB,4
  rjmp read

I don t get why.
Tnks for the replies before. hope u can give an advice why it's not working with default 0v, or what should i have to do for it.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

oo, i get it now with SBIS op, better and faster (tnks clawson), but still not resolved my problem.

My problem is not the reading the pins and manipulate it, is that when the pull-up if disable (1 way: input with default value 0V, or just disable it on bit7 of MCUR register.) it start to have 2v around the outputs and others strange behavior

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I presume that you are reading a switch. You must use a pull-up, because otherwise the pin may read as anything !

EX1:
   ldi temp 0b11110000    ; PB4..7 outputs
   out DDRB, temp
   ldi temp 0b00001111    ; PB0..3 inputs w pull-ups
   out DPORTB, temp

I find active-high logic easier to understand:

read:
   in temp,PINB
   eori temp, 0b00001111 ; read switches in English
   andi temp, 0b00000011 ; extract the state of first  two inputs
   cpi temp,0b0001       ; PB0 grounded
   brne next
   ...                   ;
   cpi temp,0b0010       ; PB1 grounded
   brne next1
   ...                   ;

Remember that when you use ANDI that you could have two switches pressed at the same time.

Yes, you can use the SBIS or SBIC instruction. They are very useful with:

    sbis  PINB,0
    rcall PB0_down_job
    sbis  PINB,1
    rcall PB1_down_job
    ...

There are many ways to skin a cat. Choose the method that appeals to you. I prefer ANDI with one bit at a time:

read:
   in   temp,PINB
   eori temp, 0b00001111 ; read switches in English
   andi temp, 0b00000001 ; test PB0 pressed
   brne next1
   rcall PB0_pressed_func
next1:
   andi temp, 0b00000010 ; test PB1 pressed
   brne next2
   rcall PB1_pressed_func
next2:
   ...                   ;

It is not the most efficient, but it is easy to read and modify.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Really nice stuff with SBIS and CBIS.

But from what I can understand the answer is:

"I CAN'T USE AN INPUT WITH DEFAULT VALUE 0 LOGIC,AND DO SOMETHING WHEN I GOT 5V THERE, BECAUSE IN THIS CASE THE PULL-UP ARE DISABLED AND THE INPUTS CAN READ ANYTHING = strange behavior"

Is that right?????????

ALL that switch ,inputs with default 0v it is because i got an RF receiver that got some LEDs for receiving commands, and i wanted that instead of LED , to connect the input of Attiny2313.
So When the receiver give me 5V (for led) to do something in the connected MCU. It seems that is NOT possible. So i have to put some mini 5v relay, so when the receiver give 5V for led, then the relay got activated and give me ground at one of the MCU inputs. Like hell :(((((

I can see also from your code, u did:

 sbis  PINB,0
    rcall PB0_down_job
    sbis  PINB,1
    rcall PB1_down_job
    ... 

instead of:

 Cbis  PINB,0
    rcall PB0_down_job
    Cbis  PINB,1
    rcall PB1_down_job
    ... 

It's just so pathetic to don't have the possibility to read 5v to an input. Damn..... :(

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Pull-ups are used with active low inputs.
Pull-downs are used with active high inputs.
The AVR has built in pull-ups you can use.
It often makes more sense to use active-low in this case.
If you are to use active-high then use external resistors as pull-downs.
So of course you can have +5V (active high) inputs if you want.

EDIT: corrected, thanks Greg.

Last Edited: Mon. Feb 21, 2011 - 02:01 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
The AVR has built in pull-downs you can use.
? :shock: Pull-up's surely?

[indeed - corrected above]

--greg
Still learning, don't shout at me, educate me.
Starting the fire is easy; the hardest part is learning how to keep the flame!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
So When the receiver give me 5V (for led) to do something in the connected MCU.

i.e. your AVR has a stable input from the receiver.

Quote:
For reading the output from some other device with a defined 0V / 5V output, you do not need pull-ups.

nor do you need pull-downs or anything. i.e. they can source 5V or sink to 0V.

Different devices are easier to connect one way than another. e.g. a switch to GND.

An external device often has defined 0V or 5V outputs. e.g. your receiver.

Some external devices have 'open collector' type of output. i.e. they can sink to 0V but need a pull-up to 5V.

It is quite simple really.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I think I understand it.

So i put normal default 5V at input, and 0v for change, and i'll do the next schema:

From theory, when i don t receive nothing from RF ( 0V from RF output) there should be no changes for MCU because the will be 5v to 5v.
When i'll get +5v from RF receiver, my transistor will saturate and make the connection, and the MCU input will go to GND.

I'll try it when i ll arrive at home.

TNKS David, tnks all for advices and explications. Also "SBIS" is really cool.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Costash,

That looks very wrong to me.

Put the resistor to GND and get rid of the transistor. (Or replace with a diode).

When the RF is zero so will the mcu pin. When the RF is 5v so will the mcu pin.

--greg
Still learning, don't shout at me, educate me.
Starting the fire is easy; the hardest part is learning how to keep the flame!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Exactly, but i want to be switched.

When 0v from RF, it have to be 5V at MCU. (do nothing)
When 5v from RF, it have to be 0V at MCU -> do something.

Isn't it?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Why do you have to change the voltage at the MCU - why can't the software take into account that 0v at MCU = do nothing and 5v at MCU = do something??

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

@ MartinM57 - pls look at the first post and reply nr 7, from "clawson"

but i'll explain u also:
- if u put an attiny2313 with input and value of it 0V, then automatically the pull-up resistors will be disable and the MCU will start to behave strange: huge delay, blinking output leds, 2v on output and so on.

So u need to have only 5v default for inputs and 0V at changes.

Take a look at my posted code EX1 and EX2. U can see the difference in it. If the Input is default 0V,it's just behaving like hell

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

if u put an attiny2313 with input and value of it 0V, then automatically the pull-up resistors will be disable and the MCU will start to behave strange: huge delay, blinking output leds, 2v on output and so on.

What are you talking about? The only thing that enables/disables the pull-up resistors in an AVR is when you set the relevant bit in PORTx to 1/0 when the same bit in DDR is 0 (input).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

:)) i ve said the same:
I've said:

INPUT (DDR 0b00000000) + 0V default (PORT 0b00000000) = pull-up disabled

Same like MCUR bit 7 = 1 logic

Isn't it?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Or just push the signal through an inverter. I guess that's what you were trying with the transistor?

--greg
Still learning, don't shout at me, educate me.
Starting the fire is easy; the hardest part is learning how to keep the flame!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If you have 2v on outputs, huuuge delays etc then you have a hardware problem. Missing ground, oscillating power supplies. lack of decoupling capacitors etc.

There is absolutely no reason to have to jump through hoops with inverters/transistors etc. to get a 0v/5v signal into an AVR and do something when the value is 0v and nothing when the value is 5v.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I got my solutions. It worked.

Tnks to all for help.

Topic can be closed! [it's up to you to do this - you go back to the first post and hit the edit button then add [solved] into the Subject: box - I'll do that now]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

MartinM57 wrote:
Missing ground
And problem is suddenly solved.
Once more grounds of two devices not connected.
Am I right?