Show Posts
Pages: 1 2 [3] 4 5 ... 19
31  Using Arduino / Motors, Mechanics, and Power / Re: Timing problems with short 2...12 us pulses generation with PWM out on: November 05, 2013, 04:34:39 pm
I think for what you need, the simplest solution is to just use mode 0 (normal mode) and just control the pin output high low in the compare match ISR. You have total control of pulse width and frequency.. I don't think the suggestion to use the wave generation mode and setting com1a1 to 0 is not quite right, hence. might as well use normal mode.

if you div clock by 8, one count is 0.5us. to get a 2us pulse at 10hz,
set pin high at count=0
set pin low at count=4 (4*0.5=2us)
set pin high at count =200,000 (since this is higher than 8 or 16 bit, you have to count the overflows)
for 2000hz pulse
set pin high at count = 1000 (again if you are using 8bit timer, you need to count the overflows)
32  Community / Bar Sport / Re: I just got the fastest delivery from China :) on: November 05, 2013, 03:01:52 pm
got another fast delivery. just 3 business days. This item costs 99cents total!

33  Using Arduino / General Electronics / Re: Stranded Wire + Breadboards on: November 05, 2013, 02:38:40 pm
just use some alligator clip or grabber to make your connection. this is just for breadboard prototype right?
34  Using Arduino / Programming Questions / Re: output format question - XML? on: November 05, 2013, 02:24:04 pm
I don't know if there is any library that will format data to xml string.
I just use brute force to add the xml open and closing tag as I build the string.
35  Using Arduino / Programming Questions / Re: creating an averaging function correctly on: November 05, 2013, 01:47:50 pm
Code:
public int mean(int in){
  count++;
  if(count<numberReadings){
   avg += in;
   avg = avg/count;
  }
  if(count >= numberReadings){
    count = 0;
    avg = in;
  }
  return avg;
}

This seems to be trying to average every N samples, starting again for the next N samples. It is not producing a rolling average. Did you intend it to start again every N samples?

In order to calculate an average over a number of samples you need to record the sum and the count. You don't need to record the previous average. Instead of this:

Code:
   avg += in;
   avg = avg/count;

You want:
Code:
   sum += in;

The result is then sum / count.

If what you want is a rolling average you can do that easily using an exponential decaying average, e.g.:

Code:
average = (0.9 * average) + (0.1 * newValue);

the equivalent of that rolling avg code using int operations is
Code:
sum = sum-avg+newvalue
avg=sum/count; //where count=10.

you can set count to whatever number of sample that makes sense for what you are averaging
36  Using Arduino / Programming Questions / Re: programmatically disconnect a pin on: November 05, 2013, 01:39:12 pm
try setting pin mode to input with no pullup.
37  Using Arduino / Programming Questions / Re: Direct Port Manipulation -- analogWrite? on: November 05, 2013, 10:57:29 am
When the OP said it didn't work, I was assuming it meant there was no pwm output. I suppose that could mean it did not compile. The sbi function is in analogWrite source, in order to use it you need to include wiring.h. Another way is to do

Code:
TCCR1A |= _BV(COM1A1);
38  Using Arduino / Programming Questions / Re: Frequent timer interrupts and reading Serial data on: November 04, 2013, 01:55:17 pm
you can define an array of bytes that is volatile. array size depends on what you want the buffer size to be.
you need a head and tail pointer.
the push will add the new value to [head]
the pop will get the value from [tail]
if head==tail, there is no value
make sure head does not overrun tail
increment head/tail after push/pop access, then % to queue size.
wrap the code in loop that push to buffer between cli() and sei()
39  Using Arduino / Programming Questions / Re: which is more efficient? using -> or . to access array of struct members on: November 04, 2013, 12:18:51 pm
So the lesson is, don't try to outsmart the compiler. It knows all the tricks.

what I learned from this is to always use / (if possible), as it can not do any worse than >>, even if avr gcc fixes the 8bit >> in the future.

I imagine it will make some difference if the code uses a ton of these, hence what the multirotor code developer did to globally replace all >> with / makes sense (or at least the code will not perform any worse than before).
40  Using Arduino / Programming Questions / Re: Direct Port Manipulation -- analogWrite? on: November 04, 2013, 12:00:45 pm
Hi there -- I'd like to know how I could do an analogWrite directly.

The simplest answer is to look at the source for analogWrite(), take out whatever functionality you think you don't need, and reproduce whatever's left in your code.

Is there any particular reason you want to do this?

I tried this -- didn't work:

DDRB |= (1<<PB5);   //OUTPUT;
PORTB |= (1<<PB5);
// connect pwm to pin on timer 1, channel A
sbi(TCCR1A, COM1A1);
OCR1A = 50; // set pam duty

This is for PIN 11 on the ATMEGA 1280.

try skipping the portb|= line. all you need is set the pin to output, no need to set it to high.
I you do not have any other code besides this, then it should work.
41  Using Arduino / Motors, Mechanics, and Power / Re: Timing problems with short 2...12 us pulses generation with PWM out on: November 04, 2013, 10:56:12 am
Right, I realized that hence I said I misread the original post.
Anyway, the OP posted in another thread and only knew to use analogWrite. I replied with instructions to use timer registers. Instead of following up in the same thread, he started this new thread and made it appear he knows how to use timer rrlegisters. Anyways, the answer was given in the other thread and one just need to apply a little creativity to get the right waveform.
42  Using Arduino / Motors, Mechanics, and Power / Re: Timing problems with short 2...12 us pulses generation with PWM out on: November 03, 2013, 08:40:12 pm
I just realized there is no div 16. so don't follow the numbers I gave in the previous post.

you will need to use div 64 to get 10hz using ICR1 of 12500, 2000hz using 62 (62.5 is the exact value, so there will be some error)., but since each count is 4us, the smallest pulse you can get is 8us.

So you will need to switch both clock divider and ICR1 and OCR1A values to change from 10hz to 2000hz at various pulse width from 2us to 12us.
43  Using Arduino / Programming Questions / Re: which is more efficient? using -> or . to access array of struct members on: November 03, 2013, 08:24:50 pm
how about

using uint16_t?
how will >>1 compare to /2
how about >>4 vs /16?

they will end up the same?

if / can get shorter code without extra casting, then / would be better to use than >>
44  Using Arduino / Motors, Mechanics, and Power / Re: Timing problems with short 2...12 us pulses generation with PWM out on: November 03, 2013, 08:00:06 pm
ah yes, its mode 10 not mode 11.

in order to change frequency, just change ICR register value. But in order to get the 10hz - 2000hz range,
use div 16, which gives 1us per count
 to get 10 hz, you need to count to 50,000 (fits 16 bit) (set ICR1 to 50000)
to get 2000 hz, count to 250 (set ICR1 to 250)

it can be done.
all you need to do to modify freq is change ICR1 value, and to change the duty cycle, change the OCR1A value.
45  Using Arduino / Programming Questions / Re: which is more efficient? using -> or . to access array of struct members on: November 03, 2013, 07:49:42 pm
ok, my conclusion is it confirms /2 is more efficient than >>1

I've always thought >> is faster than / (just as dc42 mentioned). I guess not.
I think this is as far as the hair splits.

Nick, what program did you use to disassemble? I get this using object dump for a=b/2 program

Code:
C:\Temp\build6136053095908538649.tmp>C:\arduino-1.0.5\hardware\tools\avr\bin\avr
-objdump.exe -d sketch_nov03b.cpp.o

sketch_nov03b.cpp.o:     file format elf32-avr


Disassembly of section .text.setup:

00000000 <setup>:
   0:   25 9a           sbi     0x04, 5 ; 4
   2:   a0 e8           ldi     r26, 0x80       ; 128
   4:   b0 e0           ldi     r27, 0x00       ; 0
   6:   1c 92           st      X, r1
   8:   e1 e8           ldi     r30, 0x81       ; 129
   a:   f0 e0           ldi     r31, 0x00       ; 0
   c:   10 82           st      Z, r1
   e:   8c 91           ld      r24, X
  10:   82 60           ori     r24, 0x02       ; 2
  12:   8c 93           st      X, r24
  14:   80 81           ld      r24, Z
  16:   81 61           ori     r24, 0x11       ; 17
  18:   80 83           st      Z, r24
  1a:   82 e0           ldi     r24, 0x02       ; 2
  1c:   90 e0           ldi     r25, 0x00       ; 0
  1e:   90 93 89 00     sts     0x0089, r25
  22:   80 93 88 00     sts     0x0088, r24
  26:   84 e0           ldi     r24, 0x04       ; 4
  28:   90 e0           ldi     r25, 0x00       ; 0
  2a:   90 93 87 00     sts     0x0087, r25
  2e:   80 93 86 00     sts     0x0086, r24
  32:   8c 91           ld      r24, X
  34:   80 68           ori     r24, 0x80       ; 128
  36:   8c 93           st      X, r24
  38:   08 95           ret

Disassembly of section .text.loop:

00000000 <loop>:
   0:   08 95           ret

Pages: 1 2 [3] 4 5 ... 19