## PORTA , PINA , DDRA ?

18 posts / 0 new
Author
Message

Ok, we have PORTA to control what voltage levels for the pins. (i.e 0 volts or 5 volts <-- 0 , 1)

DDRA to control what pins are input and what pins are output. (i.e 1 for output , 0 for input )

PINA to read which voltage levels are low or high.

But how can we determine from PINA what "A pins" are
input and what are output.

Is the only way to do it is to reading DDRA into a varible and using That?

I know this is a stupid question but I am wondering if their was an all in one varible that told you both the voltage and weather the pin was an input or output pin all in one.

Also I am using the delay function to do PWM is their a better or more efficient way?

Curious to know how fast the fastest frequency for PWM can be with the atmega32 avr chip?

Like if I do
PORTA = 0x01;
PORTA = 0x00;
what would the frequency of the square wave be for the pin0. I am assuming this is the fastest it can be since their is no delay's in between?

2. For PWM, just download the data sheet and ctrl-F for PWM.

3. If you like time-wasting delays, compile your program and run in the Studio4 simulator. This will report exactly how long you have wasted.

David.

Quote:

Like if I do
PORTA = 0x01;
PORTA = 0x00;
what would the frequency of the square wave be for the pin0. I am assuming this is the fastest it can be since their is no delay's in between?

That would depend on the machine code generated by the compiler, and o the fequency the AVR is running at.

I presume you want a loop around that, so that it actually generates a repeating square wave rather than just (maybe) one positive flank and one negative flank.

The fastest such loop is a SBI,CBI,RJMP sequence that will take six AVR clocks. It will not generate a square wave with 50% dudty cycle though, due to the fact that the low state will be held during the jump the duty cycle will be a 33.333..% . Anyhow, at the default clock fequency all new AVR runs at frpm factory, 1 MHz, that would be a square wave of circa 166 KHz . Running your AVR at eg 8 MHz would get you roughly 1.33 MHz.

Such a loop is of-course not practical for real PWM, where you want to vary the duty cycle and keep the frequency stable. The scheme above has no ability to vary the duty cycle (Oh, OK, with some more code it can take on four values: 0% 33%, 66%, 100%). You will have to lower the frequency for that. How much lower depends on the resolution you want on the PWM signal.

If you need a wee bit more speed than that, or if you aactually need 50% duty cycle, or if you just want to get the PWM job done with something that was constructed with PWM especially in mind, then look at timers in PWM mode.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

Quote:
I know this is a stupid question but I am wondering if their was an all in one varible that told you both the voltage and weather the pin was an input or output pin all in one.
Think about this a little more, how should a single '0' or '1' value be used to descrive these 4 possible options.

/Jesper
http://www.yampp.com
The quick black AVR jumped over the lazy PIC.
What boots up, must come down.

Well now, Jesper. Hes saying "variable" so it could well be something a wee more elaborathe than a single bit... :wink:

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

Just to note that if this is a competition to see what's the fastest square wave you can generate from an AVR then setting the CKOUT fuse will output F_CPU on the CKOUT pin - you simply cannot do better than that. Next up (I think!) is a timer programmed into CTC mode with no prescaler and the OCR set to 0. Both these methods will generate square waves faster than software loops. In software, again on a modern AVR, writing 1 to a PINA register will toggle a pin state so OUT, RJMP maybe the quickest s/w loop though an unrolled OUT, OUT, OUT, OUT,.... would be even quicker (until it hits the buffers!)

Quote:

you simply cannot do better than that

Some AVRs (ATtimy28?) have "high speed PWM" where the clock driving the timer is the system clock multiplied by four. Maybe you can do better than F_CPU on one of those?

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

Quote:

Maybe you can do better than F_CPU on one of those?

LOL, I didn't think of that. Good catch.

Hmmm, now you've got me thinking. A very recent thread, and others over the years, have discussions about the fastest >>input<< signal frequency that can be measured. Some digging is needed, but for PSC

```16.24 Interrupts
This section describes the specifics of the interrupt handling as performed in AT90PWM2/2B/3/3B.
16.24.1 List of Interrupt Vector
Each PSC provides 2 interrupt vectors
â€¢ PSCn EC (End of Cycle): When enabled and when a match with OCRnRB occurs
â€¢ PSCn CAPT (Capture Event): When enabled and one of the two following events occurs :
retrigger, capture of the PSC counter or Synchro Error.
16.26.216.26.2See PSCn Interrupt Mask Register page 170 and PSCn Interrupt Flag Register page 171.```

which would certainly imply that interrupts could be generated with finer resolution than the AVR clock. Now, to >>count<< these there would need to be a Tn pin or equivalent.

Xmega has the PLL, but it still has the same AVR clock limitations I think.

Lee

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

Ok thanks for your reply's but I have one more question is their away to view the PWM waves on my computer.

What I want to do is when I run the avr chip to view the waves. I don't have an ossiliscope so I was wondering if their is any software or maybe AVR studio can do it. So I can see the results of the wave out a certain output pin?

Also under projects--> configuration options

I can set the frequency Hz . It is blank now but what values are valid to set in this box (i.e lowest and highest values)

Sam111 wrote:
Ok thanks for your reply's but I have one more question is their away to view the PWM waves on my computer.

You can buy a cheap USB logic analyzer.
Saleae Logic seems very popular and it's very cheap for a logic analyzer (USD 149): http://www.saleae.com
Sold dirtectly from the above link but also sold here: http://www.sparkfun.com/commerce...

Not very high spec but a \$49 scope...

http://www.nkcelectronics.com/di...

It's only \$31 if you are willing to build it:

http://www.nkcelectronics.com/di...

## Attachment(s):

Quote:

I can set the frequency Hz . It is blank now but what values are valid to set in this box (i.e lowest and highest values)

In that box you should enter the frequency that your AVR is actually running at. So, this is not a way to tell the AVR how fast it should run - that is acheived by seting the clock selection fuses, and when applicable select a crystal or oscillator and atach that to the AVR. The field in AVR Studio is a way to tell the compiler how fast your AVR runs (this is needed by the compiler, or rather by avrlibc, to get the timed delay loops offered by eg delay_ms() right).

The min and max values that your AVR can run at is specified in the data sheet.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

Ok , but I am kind of look for a free way to view the square waves.
Free software?

I got a free software ossilscope that works with the computers audio port unfortunatly I cann't find one that works with the avr studio's ?

Maybe avr studio's itself has a way to view the output waves graphically anybody know?

Thanks for clearing up the frequency question.

And if I was going to by an ossiliscope I would probably go for a really good quality one but right know I am not willing to pay for one.

Sam111 wrote:
I got a free software ossilscope that works with the computers audio port

The limitation with using a sound card as scope input is that it only works in the audio frequency range. So you can't use it for frequnecies above ~20 kHz.

If your PC has a parallel port it would probably be better to make a simple parallel port logic analyzer. Just try to google for this and you will find many examples.
Here's one that also works under Vista: http://tfla-01.berlios.de
More examples here: https://www.avrfreaks.net/index.p...

Or you can make an AVR based logic analyzer interfacing to the PC vi RS232 (or USB through an USB to RS232 converter IC). Again try to google for this and you will find examples.

Quote:

I got a free software ossilscope that works with the computers audio port unfortunatly I cann't find one that works with the avr studio's ?

Ah but the joy of Microsoft Windows is that it has this clever trick that allows you to apparently run two programs at once. So your Soundcard Scope program doesn't need to have ANYTHING to do with AVR Studio. You can simply run the two programs at once.

Cliff! Maybe he wants an oscilloscope that shows the waveforms generated from a simulation session in Studio. That is not possible, AFAIK. If I had a parallell life or two, then that would be an interesting Studio plug-in project to try...

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]