Author Topic: ATMega8 wont current sink?  (Read 6460 times)

0 Members and 1 Guest are viewing this topic.

Offline HDL_CinC_DragonTopic starter

  • Supreme Robot
  • *****
  • Posts: 1,261
  • Helpful? 5
ATMega8 wont current sink?
« on: July 23, 2007, 09:34:35 PM »
Ive just started with programming my AT Mega 8 AVR

Im one of those people that learns best by taking source code, running it, seeing what happens, studying the code, tweaking it, seeing what happens, and then I remember it and can use it whenever. What I find works best is that I simply ask a question top anyone and everyone knowledgeable (Such as posting in forums)  on exactly what it is that I am trying to do at that particular point in time. I hope I will find someone who would be willing to answer my short questions when I have them?

Now that you know the actual logic im using behind my seemingly ridiculous questioning patterns:

What would I need to do to simply just make my LED turn on, wait for lets say 350ms, turn off, and then wait another 350ms, and repeat?


-EDIT-
Oh and just so you know, I did search around a bit and I couldnt find anything for what im looking for... :-\ I searched a few sites including this one and some websites that were referred on this forum and even some on those forums that were originally referred. I just cant seem to find what im looking for so that is also why ive decided to simply ask directly for help...
« Last Edit: July 23, 2007, 09:52:52 PM by HDL_CinC_Dragon »
United States Marine Corps
Infantry
Returns to society: 2014JAN11

Offline dunk

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 1,086
  • Helpful? 21
    • dunk's robot
Re: ATMega8 wont current sink?
« Reply #1 on: July 24, 2007, 03:05:45 AM »

Offline HDL_CinC_DragonTopic starter

  • Supreme Robot
  • *****
  • Posts: 1,261
  • Helpful? 5
Re: ATMega8 wont current sink?
« Reply #2 on: July 24, 2007, 08:51:23 PM »
Thanks dunk

That site was mostly for the hardware and very little explanation on what the software did.

Code: [Select]
void ioinit (void)
{
    //1 = output, 0 = input
    DDRB = 0b11111111; //All outputs
    DDRC = 0b11111111; //All outputs
    DDRD = 0b11111110; //PORTD (RX on PD0)
}
im assuming "DDRB = 0b11111111; DDRC = 0b11111111;" means that anything related to PB0 - PB7 and PC0 - PC7 are going to be ouput functions? And "DDRD = 0b11111110;" means PD0 - PD6 are going to be output but PD7 is going to be input?

Code: [Select]
PORTC = 0xFF;
PORTB = 0xFF;
PORTD = 0xFF;

PORTC = 0x00;
PORTB = 0x00;
PORTD = 0x00;

does the "PORTB = 0xFF;" mean to set all output pins in array B(defined by "DDRB = 0b11111111;) to high?

 ??? ??? ???

Help greatly appreciated!!
United States Marine Corps
Infantry
Returns to society: 2014JAN11

Offline dunk

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 1,086
  • Helpful? 21
    • dunk's robot
Re: ATMega8 wont current sink?
« Reply #3 on: July 25, 2007, 03:01:36 AM »
Quote
im assuming "DDRB = 0b11111111; DDRC = 0b11111111;" means that anything related to PB0 - PB7 and PC0 - PC7 are going to be ouput functions? And "DDRD = 0b11111110;" means PD0 - PD6 are going to be output but PD7 is going to be input?
correct.
Quote
does the "PORTB = 0xFF;" mean to set all output pins in array B(defined by "DDRB = 0b11111111;) to high?
correct.
you can easily verify this by setting the value of a pin and testing with a multimeter.

i'm guessing you have worked out that 0xFF is the hex way of writing the binary number 0b11111111.
you can easily convert from one to another using a scientific calculator. (or use http://www.google.com/search?q=0xff+in+binary)

dunk.

Offline HDL_CinC_DragonTopic starter

  • Supreme Robot
  • *****
  • Posts: 1,261
  • Helpful? 5
Re: ATMega8 wont current sink?
« Reply #4 on: July 25, 2007, 11:35:17 AM »
oh ok so 0xFF and 0b11111111 are BOTH set the corresponding pins to high? and by setting them to high means voltage will be flowing out of that pin and anything 0 sets it low meaning data/voltage will be flowing into that pin?

I know im asking a lot of questions and im sorry, but I need to learn some how right?...
United States Marine Corps
Infantry
Returns to society: 2014JAN11

Offline hazzer123

  • Supreme Robot
  • *****
  • Posts: 460
  • Helpful? 3
Re: ATMega8 wont current sink?
« Reply #5 on: July 25, 2007, 01:07:48 PM »
In programming you need to learn to understand different counting systems.

There are 3 main ones - decimal, binary and hexidecimal.

The names refer to the base number of the counting system.

In decimal the base number is 10. This means than there are 10 different symbols for letters.  The right most number has a unit value (or 10^0). Each time you go left a number, the value increases by a power, therefore the second number is 10^1 or 10. This carries on.

In binary the base number is 2 and the same applies. Hexadecimal is base 16.
Imperial College Robotics Society
www.icrobotics.co.uk

Offline HDL_CinC_DragonTopic starter

  • Supreme Robot
  • *****
  • Posts: 1,261
  • Helpful? 5
Re: ATMega8 wont current sink?
« Reply #6 on: July 25, 2007, 01:22:39 PM »
I know them already.
decimal - 0 1 2 3 4 5 6 7 8 9
hex 0 1 2 3 4 5 6 7 8 9 A B C D E F
binary 0 1 10 11 100 101 110 111 1000 1001


Its how they're applied that I need to make sure about.


and second, if "PORTC = 0xFF;" and "DDRC = 0b11111111;" are the same thing, then why would the program that I was pointed to call them twice in 2 separate functions?
« Last Edit: July 25, 2007, 01:43:07 PM by HDL_CinC_Dragon »
United States Marine Corps
Infantry
Returns to society: 2014JAN11

Offline hazzer123

  • Supreme Robot
  • *****
  • Posts: 460
  • Helpful? 3
Re: ATMega8 wont current sink?
« Reply #7 on: July 25, 2007, 01:44:24 PM »
I dont think they are the same thing. One is to do which direction of the port (DDRC - 1 means output) and the other to do with the state of the output (high or low).
Imperial College Robotics Society
www.icrobotics.co.uk

Offline HDL_CinC_DragonTopic starter

  • Supreme Robot
  • *****
  • Posts: 1,261
  • Helpful? 5
Re: ATMega8 wont current sink?
« Reply #8 on: July 25, 2007, 01:56:46 PM »
ohhh ok, so "DDR[C - D] followed by 0b and 0s and 1s determines its role, and PORT[C - D] followed by 0x and up to 2 hex digits sets its high/low status? does the PORT[C - D] have to be followed by 0x[hex] or can it also be 0x[bin]? Basically im asking if they're interchangeable and that C will recognize them if theyre used wherever.


so if I had in my program DDRB 0b00000001, only pin B7 would be output and the rest of the B array would input right? so then could I then say in my program, PORTB 0x00000001 so that B0 - B6 stay low and B7 start outputting high(5v?) or would it have to be 0x1 for that?
United States Marine Corps
Infantry
Returns to society: 2014JAN11

Offline dunk

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 1,086
  • Helpful? 21
    • dunk's robot
Re: ATMega8 wont current sink?
« Reply #9 on: July 25, 2007, 02:27:20 PM »
Quote
ohhh ok, so "DDR[C - D] followed by 0b and 0s and 1s determines its role, and PORT[C - D] followed by 0x and up to 2 hex digits sets its high/low status? does the PORT[C - D] have to be followed by 0x[hex] or can it also be 0x[bin]? Basically im asking if they're interchangeable and that C will recognize them if theyre used wherever.
yup.
you've got it.
you can use hex, decimal or binary numbers interchangeably. your C compiler should be able to use them all.
your C compiler will be looking for the "0x" for hex or "0b" for binary part so it's important to get that right.
i usually use binary numbers when i'm setting individual bits and hex the rest of the time but you can use which ever you find easiest.

Quote
so if I had in my program DDRB 0b00000001, only pin B7 would be output and the rest of the B array would input right? so then could I then say in my program, PORTB 0x00000001 so that B0 - B6 stay low and B7 start outputting high(5v?) or would it have to be 0x1 for that?
as i said above, you have to make sure to label all binary numbers "0b".
you have "0x00000001" there where you should have "0b00000001" but otherwise you are mostly correct.

if i was being fussy i'd say that you wouldn't care what voltage B0 - B6 are as you have set them to be used as inputs...
if you wanted B0 - B6 set low and B7 high you would want:
Code: [Select]
DDRB = 0b11111111;       // all outputs
PORTB = 0b00000001;      // B0 - B6 set low and B7 high

( or this would do exactly the same thing:
Code: [Select]
DDRB = 0xFF;
PORTB = 0x01;
)

from here the easiest way is to try programming a microcontroller and see what happens.
just play with some different combinations and see what voltage the pins are with your multimeter or an LED.

dunk.

Offline HDL_CinC_DragonTopic starter

  • Supreme Robot
  • *****
  • Posts: 1,261
  • Helpful? 5
Re: ATMega8 wont current sink?
« Reply #10 on: July 25, 2007, 11:15:13 PM »
I played around with it a bit =D I discovered that it reads right to left, not left to right.
"PORTD = 0b00000001"(0x01) makes D0 high whereas 0b10000000(0x80) makes D7 high

im proud of that little discovery.... *as the expert roboticists shake there heads and laugh as they solder the last connection on their 13-CPU robots of destruction*

... what? :P


-EDIT-
I just realized that in the very code I quoted a few posts up it says that already...... theres goes my happiness from that little "discovery" lol
« Last Edit: July 25, 2007, 11:17:03 PM by HDL_CinC_Dragon »
United States Marine Corps
Infantry
Returns to society: 2014JAN11

Offline HDL_CinC_DragonTopic starter

  • Supreme Robot
  • *****
  • Posts: 1,261
  • Helpful? 5
Re: ATMega8 wont current sink?
« Reply #11 on: July 25, 2007, 11:50:49 PM »
Question:
Using the $50 robot tutorials set up, to make the LED light up I have to make PD4 low so that voltage flows into the MCU instead of out of it right?
Well when I do that, it doesnt light up... I have DDRD = 0x00 and PORTD = 0x00 in my code but the LED doesnt light up... any suggestions on what I might be doing?

The LED isnt dead either
United States Marine Corps
Infantry
Returns to society: 2014JAN11

Offline hazzer123

  • Supreme Robot
  • *****
  • Posts: 460
  • Helpful? 3
Re: ATMega8 wont current sink?
« Reply #12 on: July 26, 2007, 12:21:39 AM »
Yeah, ive never used Amtel chips at all. But from your code, a 1 represents an output. Therefore your code should be DDRD = 0xFF and PORTD = 0x00. This is the opposite to the chips i use (PICs) and quite non sensical (since the number 0 looks like the O from output etc).

When the pin is set to an input its state it high impedance, meaning it will take the state of anything that is attached to it. In your case it would have become high when the LED was attached and therefore no current flowed.
Imperial College Robotics Society
www.icrobotics.co.uk

Offline HDL_CinC_DragonTopic starter

  • Supreme Robot
  • *****
  • Posts: 1,261
  • Helpful? 5
Re: ATMega8 wont current sink?
« Reply #13 on: July 26, 2007, 12:30:49 AM »
DDRD 0xFF PORTD 0x00 doesnt work :-\ I had already tried it
United States Marine Corps
Infantry
Returns to society: 2014JAN11

Offline HDL_CinC_DragonTopic starter

  • Supreme Robot
  • *****
  • Posts: 1,261
  • Helpful? 5
Re: ATMega8 wont current sink?
« Reply #14 on: October 25, 2007, 04:04:28 PM »
I run this code in my ATMega 8:
DDRD = 0xFF;
PORTD = 0xFF;
and it outputs 4.9v FROM all D Ports TO GND just as it should

now supposedly, when I run
DDRD = 0x00;
PORTD = 0x00;
its supposed to take FROM the 5v regulated line TO all D Ports which apparently should be serving as GND, am I right?

If im correct then something is not working properly. I used my DMM to confirm everything ive said too...... dont suppose anyone can shed some light on what I might be doing wrong?
United States Marine Corps
Infantry
Returns to society: 2014JAN11

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,703
  • Helpful? 173
    • Society of Robots
Re: ATMega8 wont current sink?
« Reply #15 on: October 25, 2007, 07:29:09 PM »
When you do:

DDRD = 0x00;

that means you are configuring the D port as all inputs. This means they dont output, just act as a binary input pin.

What you want is this:

DDRD = 0xFF;  //configure all D ports for output

(I am assuming you are using my SoR_Utils.h, right?)

As a stupid check, if you do
Code: [Select]
DDRD = 0xFF;
PORT_OFF(PORTD, 4);
PORT_ON(PORTD, 0);
does D4 turn off while the others remain on?

Offline HDL_CinC_DragonTopic starter

  • Supreme Robot
  • *****
  • Posts: 1,261
  • Helpful? 5
Re: ATMega8 wont current sink?
« Reply #16 on: October 25, 2007, 09:10:40 PM »
I tried it with the "DDRD = 0xFF; PORTD = 0x00;" combo also... I tried all of em lol. and yeah Im using the Sor_Utils. I havnt tried the PORT_OFF, PORT_ON commands yet though, only the DDR and PORT combos for the D pins. I even used binary instead of hex to isolate a specific pin but no dice.
But technically just for sake of argument, if your current sinking, dont you want the pins to be inputs? or are you simply setting them to outputs but setting them low so the voltage difference forces the current through the MCU? Would setting them as inputs have the current run through sensitive areas of the MCU?


I might have time tomorrow to work on it but tonight I dont have any time... im already about an hour behind on where I need to be >_< Thanks for the help thus far!
United States Marine Corps
Infantry
Returns to society: 2014JAN11

 


Get Your Ad Here

data_list