Author Topic: Ports on ATMEGA8  (Read 7638 times)

0 Members and 1 Guest are viewing this topic.

Offline MilesTopic starter

  • Full Member
  • ***
  • Posts: 49
  • Helpful? 1
Ports on ATMEGA8
« on: April 20, 2009, 11:22:45 PM »
Hello, i am using the ATMEGA8 microcontroller to light up some LEDS. I am currently using Ports/pins PD0 to PD4 as circled in Red. This gives me the possibility to light up 5 LED's however i need to controll 10. One option is i just use two microcontrollers which is fine but i want to know what the other pins on the ATMEGA8 do incase i can controll more. What do the pins cirlced in orange do on the ATMEGA8 and can they be used to turn LED's on and off as in turn the port on and off?

Offline SmAsH

  • Supreme Robot
  • *****
  • Posts: 3,959
  • Helpful? 75
  • SoR's Locale Electronics Nut.
Re: Ports on ATMEGA8
« Reply #1 on: April 20, 2009, 11:30:10 PM »
owned! but seriously, the atmega8 has approx 20 I/O 14 of them digital, 6 analogue. all of them can be used to turn on and off an led.
NOTE: atmega8 and atmega168 are the same except for some more memory in the 168.
the only pins you cant use are xtal1 and xtal2 (9&10)
gnd(8 & 22), vcc(7), aref(21), avcc(20) and reset(1) the rest are I/O meaning you can control 20 leds but that's with no inputs of coarse.
« Last Edit: April 20, 2009, 11:49:56 PM by SmAsH »
Howdy

Offline MilesTopic starter

  • Full Member
  • ***
  • Posts: 49
  • Helpful? 1
Re: Ports on ATMEGA8
« Reply #2 on: April 21, 2009, 12:05:56 AM »
OK awesome, so what i might do is use these Pins in the picture, are they ok? Also when setting up the ports in AVR Studio, i go to the header file (SoR_Utils.h) and i scroll down the bottom to where it has the simplified functions. I have just put this down:

void LED_1_on(void)
     {
     PORT_OFF(PORTD, 0); //turn LED 1 on
     }

and repeated this for the first 5 LED's (whilst changing the port number from 0-4)
and aslo made simplified functions for turning each LED off.

Do i just do the same for pin number 15 which is the PB1 and have iit like:       PORT_OFF(PORTB, 1)  ?


Offline SmAsH

  • Supreme Robot
  • *****
  • Posts: 3,959
  • Helpful? 75
  • SoR's Locale Electronics Nut.
Re: Ports on ATMEGA8
« Reply #3 on: April 21, 2009, 12:18:42 AM »
yep, those pins are all good, i don't know much on the programming side of things but i would think so.
Howdy

Offline chelmi

  • Supreme Robot
  • *****
  • Posts: 496
  • Helpful? 15
    • Current projects
Re: Ports on ATMEGA8
« Reply #4 on: April 21, 2009, 12:11:14 PM »
owned! but seriously, the atmega8 has approx 20 I/O 14 of them digital, 6 analogue. all of them can be used to turn on and off an led.
NOTE: atmega8 and atmega168 are the same except for some more memory in the 168.
the only pins you cant use are xtal1 and xtal2 (9&10)
gnd(8 & 22), vcc(7), aref(21), avcc(20) and reset(1) the rest are I/O meaning you can control 20 leds but that's with no inputs of coarse.

Just to precise, the atmega8 and the 168 are not exactly the same (small differences beside the memory, like more PWM channel and higher frequency for the 168).
There are 23 GPIO, all of them can be used as digital I/O, some can be configured in a "specialized" mode like analog/digital conversion, PWM, I2C, SPI, etc.
The best source of information is the datasheet on atmel site. This can be intimidating, but take a look at it and ask questions here if it's unclear.

Chelmi.

Offline MilesTopic starter

  • Full Member
  • ***
  • Posts: 49
  • Helpful? 1
Re: Ports on ATMEGA8
« Reply #5 on: April 27, 2009, 01:02:18 AM »
OK so i got my project working and i'm controlling 10 LED's. Is it possible to use some of the pins as digital inputs? as in i could have a switch turning on and off suppling 5v to one of the pins and having the ATMEGA8 reading it as on or off? I don't want to use the ADC pins because i don't want to let them go to waste.

Offline SmAsH

  • Supreme Robot
  • *****
  • Posts: 3,959
  • Helpful? 75
  • SoR's Locale Electronics Nut.
Re: Ports on ATMEGA8
« Reply #6 on: April 27, 2009, 01:12:36 AM »
yep, sure is possible ;) the attached image pulls the pin "high" (5V) when pushed, to pull the pin "low" (0V) when pushed just switch the low and high connections around like the second image attached ;D
« Last Edit: April 27, 2009, 01:15:43 AM by SmAsH »
Howdy

Offline hazzer123

  • Supreme Robot
  • *****
  • Posts: 460
  • Helpful? 3
Re: Ports on ATMEGA8
« Reply #7 on: April 27, 2009, 01:22:16 AM »
In SoR_Utils.h, the ports are configured like this -

Code: [Select]
//************CONFIGURE PORTS************

//configure ports for input or output - specific to ATmega8

void configure_ports(void)

{

DDRC = 0x00;  //configure all C ports for input

PORTC = 0x00; //make sure pull-up resistors are turned off

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

DDRB = 0xC7;  //configure B ports 0, 1, 2, 6, 7 for output (google search '0b11000111 to hex')

}

//***************************************

Change these values to your requirements. I guess to read a digital input, you can just read PORTx, and apply a bitmask.

e.g. to read bit 4 of portb it is probably
Code: [Select]
int reading = (PORTB | (1<<4)) >>4;
reading would then be 0 or 1
« Last Edit: April 27, 2009, 01:27:56 AM by hazzer123 »
Imperial College Robotics Society
www.icrobotics.co.uk

Offline MilesTopic starter

  • Full Member
  • ***
  • Posts: 49
  • Helpful? 1
Re: Ports on ATMEGA8
« Reply #8 on: April 27, 2009, 01:43:56 AM »
I don't understand. How do you set up a port so it gets read instead of giving something? Just say i wanted to set up PB5 (or pin19) on ATMEGA8 how/where would i write in the code to set this up? and also in the C. code how would i read that value e.g (if digital_sensor == 255) or (if digital_sensor == 0). or would it be something else like if the port is high or low?

Offline hazzer123

  • Supreme Robot
  • *****
  • Posts: 460
  • Helpful? 3
Re: Ports on ATMEGA8
« Reply #9 on: April 27, 2009, 04:03:50 AM »
When configuring an 8 bit port on an AVR you must write a byte to the DDRx register. Each bit in the byte determines the function of the corresponding pin. On AVRs '1' means output, and '0' means input.
So to set PB0-PB4 to outputs and PB5-PB7 to inputs you would use this

Code: [Select]
DDRB = 0x1F; //0x1F is hexadecimal for binary 00011111
Now... if PB6 had a low voltage applied to it (ie logical 0) then the 6th bit of the variable PORTB would be 0. If it had a high voltage (logical 1) the 6th bit would be 1. To extract this bit from the byte you need to use bit-masking so that you can ignore all except the 6th bit.

Hmmmm there must be tutorials on SoR for this.... if not, i'll write one.

Have fun :)
Imperial College Robotics Society
www.icrobotics.co.uk

 


Get Your Ad Here

data_list