ATTINY Port Manipulation problem

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

I'm using ATTINY85 12Mhz. And I use Arduino as ISP to burn MCU.

 

Here are my circuit schematic and Arduino code.

 

 

---------------------------------------------------------------------

#include <avr/io.h>

 

void setup() {
DDRB = B00100110;      //replaces pinMode(, OUTPUT);
PORTB = B00100000;
while(1){
if (PINB == B00001000 )        // if PB3 high
{
    PORTB= B00000100;         // PB2  high
      while( PINB == B00001100)  // while PB3, PB2 high
      {
          if(PINB  == B00011100  )   // if PB4 high
            {
                PORTB = B00000000;     // PB2, PB1 low
            }
      }
  if(PINB  == B00000100 )      // If PB2 is high
    {
      PORTB = B00000110;       //PB2, PB1 high
      //asm("nop"); //1 clock delay
      delayMicroseconds (1);               //delay time
    }
    PORTB = B00000000;         //PB2 PB1 low
}
}
}

void loop() {

}

 

 

------------------------------------------------------------------------

I have two problems with using port manipulation.

 

The first one is MCU does not work as I coded, or I intended. My intention is to keep output1 high until the input signal gets low. But It gets low periodically as the picture below (blue: output1, yellow: original signal).

 

And the second one is MCU is too slow... FWHM of the Input signal is about 4us. But PB2 goes high after about 5us input 1( after passing comparator) get in. 

 

 

Is there any problem in my code? Because calculation time is so critical for my application ( less than 5us), I think port manipulation is the only solution.

hello

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
if (PINB == B00001000 )        // if PB3 high

is not a great way to read one bit. As well as checking PB3 for being high this line is insisting that all the other 7 pins in the port will return 0. That may not be the case. Far better is to use:

if (PINB & (1 << 3) )        // if PB3 high

which will check ONLY bit 3 and ignore all the rest. If you don't understand why read the "Bit Manipulation 101" thread in the Tutorial Forum here.

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

I don't see any hysteresis around your comparator.

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

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

triplehoon95 wrote:

DDRB  = B00100110;      //replaces pinMode(, OUTPUT);
PORTB = B00100000;

 

If they are supposed to be binary literals, don't they need to be 0b ... ?

 

Or do you have 256 macros somewhere ?

 

Please see Tip #1 in my signature (below; may not be visible on mobile) for how to properly post source code.

 

Also, please learn to use the screenshot feature of your scope - it'll give far better results than wonky photos!

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

awneil wrote:
Or do you have 256 macros somewhere ?
It's Arduino so, rather curiously, yes, he does:

D:\arduino-1.8.8>grep B10100011 * -r
hardware/arduino/avr/cores/arduino/binary.h:#define B10100011 163

(don't ask me why someone in Arduino land went to all the trouble of defining 256 macros that actually make the code LESS readable when B10100011 could just be written as 0b10100011 anyway but I guess this is aimed at none avr-gcc targets which don't have Joerg's extension for 0b prefixes?)

Last Edited: Wed. Mar 18, 2020 - 11:05 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
if (PINB & (1 << 3) )        // if PB3 high

 

Thanks for your advice. I already used that line before to check bit 3 only, but I think It is a little bit slow for me. So, I just check all the bit at once to not do operators like &, <<, etc. in code.

 

I think my limit is that I use Arduino to burn MCU. Do you think it will be faster when I use Assembly code with using AtmelStudio?

 

 

 

 

hello

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

triplehoon95 wrote:
I think It is a little bit slow for me ... not do operators like &, <<, etc. in code.

You do realise that the shift in that expression is done at compile time?  The microcontroller doesn't have to do the shifting at run time!

 

Do you think it will be faster when I use Assembly code with using AtmelStudio?

Assembler is not magic; neither is Atmel Studio.

 

The question is not about what language you use, or what IDE - it's down to the skill & experience of the programmer.

 

It is unlikely that code by a novice assembler programmer will out-perform the code generated by a modern, optimising compiler.

 

EDIT

 

for example, see https://www.avrfreaks.net/commen... - where the compiler is smart enough to recognise that the expression represents a single-bit operation, and codes it accordingly.

 

If you write your source code as a full-port read & compare, the compiler probably loses that opportunity ...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Wed. Mar 18, 2020 - 12:57 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

triplehoon95 wrote:
Thanks for your advice. I already used that line before to check bit 3 only, but I think It is a little bit slow for me. So, I just check all the bit at once to not do operators like &, <<, etc. in code.

Did you just pluck the fact that you "think it is slow" from the air or do you actually see evidence of that being the case? Would it surprise you to learn that the AVR micro is actually optimised for exactly this kind of operation and has SBIS and SBIC opcodes specifically to check the state of one bit in an IO register and & (1 << 3) will generate such an opcode. You may think the C syntax looks "more complicated" but in reality the generated code is just about as fast as it possibly could be. When you do the == Bxxxx thing it will be IN, AND, BRANCH which is a LONGER sequence.

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

In your OP schematic, you do not say what the input pins are, also the output of output2 is a line, is that supposed to do something, is the line high or low?

From the description it looks like a logic gate or two would give you the results you want, no real need for an MPU in this, unless I'm missing something.

 

Jim

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

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

Depending upon the situation, thinking about all the ramifications of which pins do what can make coding more efficient.

I might have configured input on PB1 & PB2 with the respective outputs on PB3 and PB4.

Then the code could be:

PORTB = ((PINB & 0b00110) << 2);

or even placing inputs on PB1 & PB3 with the respective outputs on PB2 and PB4, which could code as:

PORTB = ((PINB & 0b01010) << 1);

which generates the following assembly:

    in    r24, 0x03
    andi  r24, 0x0A
    add   r24, r24
    out   0x05, r24

 

 

Of course, both of these ignore any other output signals that might be present on PORTB.

David

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

The OP's schematic isn't the real schematic.

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

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

I assume that you want PB2 to be high when the input is above +1V and PB1 to be high when input is above +3V.

The code should have specific PortB pins cleared and set by using the AND and OR operations:   PortB |= (1<<PB1);  and  PortB &= ~(1<<PB1); for example.  I believe that you are getting "side effects" from setting/clearing all the pins in PortB at the same time: PortB = B00010000;  // sets bit 4 and clears all the other bits of PortB.

 

You should be doing such tight code in assembler.  The C code MAY be adding side effects like stacking registers that you don't need.  Assembler will give you 12 precise instructions per microsecond.

 

General-purpose op-amps can be very slow with the rise and fall times of their output signals.  They are designed for analog use.  However, op-amps specifically designed to be comparators will have fast enough rise and fall times.

 

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

You may find "toggling a pin via the PIN reg" faster then writing values to Ports.....

 

Jim

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

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

Thank you all guys. Your recommendations are very useful to the signal processing get faster. 

 

But I have one mistake that I set clock speed of my MCU as 1MHz...

 

Changing clock speed is the answer to my problem.

hello