Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Need Help: Problem with time slicing avr

Status
Not open for further replies.
Not sure what it means, it may be a proteus think but it gets fixed if you make the variable a volatile

- - - Updated - - -

I think it is shown for variables stored in the device register which isn't a problem but proteus can't show their value
 

Ok.

BTW, about the double pulses, you are right. I just found this:
errr.png

Previously, I've did this:
"if(pulse_flag >= 3)"

and it is working for now but NOT so smooth and I bet that double pulse is the main problem. I've debug it several times using AVR studio too but I just didn't found what causing these double pulse yet.
 

Here it is:
 

Attachments

  • Proj.zip
    132.7 KB · Views: 103

What settings I need to apply to reproduce the twin pulse?
 

I dont know, I just wait it for a while and it appears and then lost.

It means double pulse does not always occurs.

Maybe you need to zoom it since the distance of those pulses are very small.
 

So I don't change any LDR values and I just wait?
 

Yes I change it then wait then change it again. Something like that, then i just realize that there is double pulse
 

That is not just waining so which of the three LDS do I have to change?
do you change all of the or a specific one?
 

I choose randomly, the same as what I did in the previous problem(when all pulses gone)
 

Hope you still check this post.
Take a look at this pict.

dbl.png

As I said previously, I change all of the LDRs randomly as shown by that picture. On the oscilloscope, if you zoom it a bit(larger time scale), you will see the double pulses are thicker than the single pulse and zoom it again.
Maybe you want to use the new file in the attachment. Using this, I directly get that double pulses without changing anything. Pause it when you see thicker pulse and zoom.
 

Attachments

  • New_proj.zip
    136.8 KB · Views: 86

OK, I'll check it.
It was hard to check the previous version that had two random parameters, time and pot setting

- - - Updated - - -

This will fix the problem
Code:
// Stop the timer
TCCR0 = 0;

// Reset the timer
TCNT0=0;
 
  • Like
Reactions: ArdyNT

    ArdyNT

    Points: 2
    Helpful Answer Positive Rating
OMG I still get this:

pf.png

I've used this:

Code:
    if(pulse_flag >= 3)
        {
            // Clear flag
            pulse_flag = 0;
            
            // Clear count
            count = 0;
             
            // Stop the timer
            TCCR0 = 0;
            
            TCNT0 = 0;
        }


My Hardware works actually but it just not that smooth.
 

What you show is the address of the variable, I don't see the problem.
The value is in the right side and is 0 (you can right click and select to be shown as integer)

- - - Updated - - -

With the last fix the code stopped giving double pulses so it would be logical to revert the condition back to
Code:
if(pulse_flag == 3)

The problem is that if I start moving LDR1 at some point the pulses stop and this is caused by what I described in post #50.
One way to fix it to make the function atomic (disable interrupts)
Code:
while (1)
      {
        display_lux();
        [COLOR="#FF0000"] #asm("cli")[/COLOR]
        set_delay();  
        [COLOR="#FF0000"] #asm("sei")[/COLOR]
      }

- - - Updated - - -

set_delay() takes just 17us to execute so turning off the interrupts for such a small duration is not a problem
 
  • Like
Reactions: ArdyNT

    ArdyNT

    Points: 2
    Helpful Answer Positive Rating
WOW!!!!
Finally it solved.

Thank You Very Much Alexan, Thank You.

I learn so many things from you through this thread.
Once again, Thank You :)
 

This last problem was caused because the code that modified x,y,z was moved out of the interrupt unlike the original code that didn't have this problem.
Whenever you have variables invoked in both main and interrupt you have to be careful because the code may be interrupted in the wrong time so you have to ensure that these parts are executed atomically
 

Ok, Thank you.

The problem is solved anyway, but maybe these are the last questions:

* previously you say set_delay() takes just 17 us. Is this always 17us or may vary?
* what does "atomically" mean? is there any clear explanation of this?
 

Atomically means that no interrrupts can be executed while the code/process runs.

In avr-gcc you have <util/atomic.h> which will give you some good macro's for atomic code.

You might try timer capture/compare, it is built in the timer hardware, check the datasheet.

- - - Updated - - -

Whenever you have variables invoked in both main and interrupt you have to be careful because the code may be interrupted in the wrong time so you have to ensure that these parts are executed atomically

only if variables occupy more than 8 bits in memory or have complex operations done on them (like shift), other wise it is pretty safe.

the most important thing is to declare them VOLATILE (without caps)
 
Last edited:

alexan_e said:
Whenever you have variables invoked in both main and interrupt you have to be careful because the code may be interrupted in the wrong time so you have to ensure that these parts are executed atomically
only if variables occupy more than 8 bits in memory or have complex operations done on them (like shift), other wise it is pretty safe.

You misinterpreted what I wrote as referring to read/modify/write of a multi byte variable , that was not the case.
You think that what I described in post #50 would be prevented with the use of 8bit variable and atomicity would not be needed?

- - - Updated - - -

* previously you say set_delay() takes just 17 us. Is this always 17us or may vary?

That is what I saw with a few executions in the debugger but it may be slightly less or more in some cases depending on the conditions.

- - - Updated - - -

* what does "atomically" mean? is there any clear explanation of this?

https://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=55347
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top