go away spammer

Author Topic: New program for the atmega168  (Read 6060 times)

0 Members and 1 Guest are viewing this topic.

Offline gamefreakTopic starter

  • Supreme Robot
  • *****
  • Posts: 543
  • Helpful? 2
  • Robo-Enthusiast
New program for the atmega168
« on: January 29, 2008, 07:44:40 PM »
Alright, for my project I dont really want the photovore program, so im wondering what exactly i need to start the program from scratch, any links to how to write programs for the atmega168?
All hail Rodney, the holy 555 timer
And Steve said: "Let there be lead!"

Offline ed1380

  • Supreme Robot
  • *****
  • Posts: 1,478
  • Helpful? 3
Re: New program for the atmega168
« Reply #1 on: January 29, 2008, 08:56:39 PM »
learn C or any language that atmega168 compilers recognize.

whatever programming knowledge you have would help.
Problems making the $50 robot circuit board?
click here. http://www.societyofrobots.com/robotforum/index.php?topic=3292.msg25198#msg25198

Offline JonHylands

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 562
  • Helpful? 3
  • Robot Builder/ Software Developer
    • Jon's Place
Re: New program for the atmega168
« Reply #2 on: January 29, 2008, 08:58:38 PM »
The easiest way to write a program is to take a copy of an existing one that works, rip out all the stuff you don't want, and then start adding new functionality...

- Jon

Offline gamefreakTopic starter

  • Supreme Robot
  • *****
  • Posts: 543
  • Helpful? 2
  • Robo-Enthusiast
Re: New program for the atmega168
« Reply #3 on: January 29, 2008, 09:00:42 PM »
I know some C and am currently aceing the BASIC class at my school, but there is certain code that applies to the atmega that i need to know.

I dont want to rip the code and change it because i actually need to have an understanding of the code so that i know what im doing. So are there any online tutorials on the atmega168 in particular?
« Last Edit: January 30, 2008, 08:49:39 PM by gamefreak »
All hail Rodney, the holy 555 timer
And Steve said: "Let there be lead!"

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,703
  • Helpful? 173
    • Society of Robots
Re: New program for the atmega168
« Reply #4 on: February 01, 2008, 02:56:03 PM »
gamefreak, its all C, no matter the microcontroller. :P

Just modify my code and see what changes on your robot. Reverse engineering is how I learned to program robots.

Offline gamefreakTopic starter

  • Supreme Robot
  • *****
  • Posts: 543
  • Helpful? 2
  • Robo-Enthusiast
Re: New program for the atmega168
« Reply #5 on: February 02, 2008, 08:48:23 PM »
well i just reverse engineered the code for the servos to work with a pezio buzzer, makes a useful little debug alarm, but whats throwing my for a loop is your PORTS_ALL_ON(port_letter,number) function, does it turn on the number for all 3 ports, or for all numbers of port?
All hail Rodney, the holy 555 timer
And Steve said: "Let there be lead!"

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,703
  • Helpful? 173
    • Society of Robots
Re: New program for the atmega168
« Reply #6 on: February 03, 2008, 06:55:37 PM »
Quote
but whats throwing my for a loop is your PORTS_ALL_ON(port_letter,number) function, does it turn on the number for all 3 ports, or for all numbers of port?
lol . . . hmmmm I have no idea . . . I just copied those functions and don't remember the link . . . I don't think I coped it correctly, either . . . see what happens when you put a port letter, number in it . . .

Offline Maltaeron

  • Full Member
  • ***
  • Posts: 65
  • Helpful? 0
Re: New program for the atmega168
« Reply #7 on: February 04, 2008, 07:59:26 AM »
The only really special things for the atmega168 are the special register/memory items like PORTB, and so on, take a look at the datasheet, it should have everything you need to know.

Offline JonHylands

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 562
  • Helpful? 3
  • Robot Builder/ Software Developer
    • Jon's Place
Re: New program for the atmega168
« Reply #8 on: February 04, 2008, 08:52:42 AM »
The "proper" way to do ports with the AVR line now is apparently (from what I've heard) this: (I'm going to use B3 as an example, substitute letter and number as appropriate for other ports/pins)

To force a pin to be an output:

DDRB |= (1 << 3);

To force a pin to be an input:

DDRB &= ~(1 << 3);

To read an input pin:

if (PINB & (1 << 3))

To set an output pin to high:

PORTB |= (1 << 3);

To set an output pin to low:

PORTB &= ~(1 << 3);

As you can see, there's a lot of similarity in the above code. What I normally do is something like this (here's some examples from my new mini-sumo, Seeker2x):

Code: [Select]
#define BUTTON_DDR DDRB
#define BUTTON_PORT PINB
#define BUTTON_MASK (1 << 3) // the button is connected to B3

#define SENSOR_POWER_DDR DDRA
#define SENSOR_POWER_PORT PORTA
#define SENSOR_POWER_MASK (1 << 2) // the MOSFET is controlled by A2

....

// Make the sensor power MOSFET pin be an output, and set it high
SENSOR_POWER_DDR |= SENSOR_POWER_MASK;
SENSOR_POWER_PORT &= ~SENSOR_POWER_MASK;

// Set up the button to be an input
BUTTON_DDR &= ~BUTTON_MASK;

// Test to see if the button is pressed
if (!(BUTTON_PORT & BUTTON_MASK))
    printf ("Button Pressed\n");

So, I set up symbolic names for the direction port (DDRB), the port or pin involved (PORTB/PINB), and the pin mask. Then by doing ANDs and ORs with either the pin mask, or the inverse of the pin mask, I can set and clear individual bits in the register.

- Jon

 


Get Your Ad Here