go_away

Author Topic: Beginner Need Programming Help.  (Read 1463 times)

0 Members and 1 Guest are viewing this topic.

Offline DerutyTopic starter

  • Jr. Member
  • **
  • Posts: 7
  • Helpful? 0
Beginner Need Programming Help.
« on: July 28, 2009, 02:08:11 PM »
I'm using the basis of the $50 Robot and revising it to my own project.
 Currently I'm trying to build a timer of sorts, but I'm having a heck of time doing it.
I tried using a loop of 30 w/ a delay_cycles(65000) in order to make a 2.5 minute timer, but it doesn't appear to work.
Frankly nothing "delay_cycles()" based seems to be working. I may just simply not understand how it is supposed to operate.

Furthermore I tried an alternative method counting the overflow from timer0 but it doesn't appear to be changing from 0 at all.
Code: [Select]
/****************INITIALIZATIONS*******************/
//other stuff Im experimenting with for SoR
//uartInit();  // initialize the UART (serial port)
//uartSetBaudRate(9600);// set the baud rate of the UART for our debug/reporting output
//rprintfInit(uartSendByte);// initialize rprintf system

timerInit(); // initialize the timer system

configure_ports(); // configure which ports are analog, digital, etc.
a2dInit(); // initialize analog to digital converter (ADC)
a2dSetPrescaler(ADC_PRESCALE_DIV32); // configure ADC scaling
a2dSetReference(ADC_REFERENCE_AVCC); // configure ADC reference voltage

//rprintf("Initialization Complete\r\n");
/**************************************************/


/*********ADD YOUR CODE BELOW THIS LINE **********/
LED_on();//turn LED on
Beep_off();

timer0Init();

while(1)
{
timecycles=timer0GetOverflowCount();
if(timecycles>= 1)
{
Beep_on();
}
else
{
delay_cycles(200);
}
}
return 0;
}

I really don't know what I'm doing when it comes to using timer and I swear the delay cycles don't work! I even tried the method demonstrated in the cycles and timing section on the main page (the LED w/ delay cycles between on and off port switching)

So if someone could explain how to do this and clarify how delay_cycles works/what I'm doing wrong w/ it that would be great.

Offline wil.hamilton

  • Robot Overlord
  • ****
  • Posts: 207
  • Helpful? 6
  • rtfm
Re: Beginner Need Programming Help.
« Reply #1 on: July 28, 2009, 02:52:48 PM »
try including the delay header file
Code: [Select]
#include <delay.h>

then you can use the delay_ms() function. it takes input in milliseconds

exampe, if you wanted to delay 10 seconds:
Code: [Select]
delay_ms(10000);

i found this here: http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=43054
but, i've used delay ms before, you can google for more info about delay_ms and avr
use the google.  it's your friend.

Offline DerutyTopic starter

  • Jr. Member
  • **
  • Posts: 7
  • Helpful? 0
Re: Beginner Need Programming Help.
« Reply #2 on: July 28, 2009, 05:03:30 PM »
Ahh thanks. I didn't know there was a difference. That really helped  :)

Offline wil.hamilton

  • Robot Overlord
  • ****
  • Posts: 207
  • Helpful? 6
  • rtfm
Re: Beginner Need Programming Help.
« Reply #3 on: July 28, 2009, 06:25:57 PM »
no problem, generally when i can't figure out something programming wise i just type it into google with some keywords

for example, in this case, i would type "avr time delay" or something like that
use the google.  it's your friend.

Offline DerutyTopic starter

  • Jr. Member
  • **
  • Posts: 7
  • Helpful? 0
Re: Beginner Need Programming Help.
« Reply #4 on: July 29, 2009, 05:04:33 AM »
Well I did do that, but I had just assumed delay_ms was synonymous w/ delay_cycles and delay_us. Then I searched more and got stuff w/ Timer0, Timer1, and Timer2 <_< made it all more confusing than it really was.

Anyway thanks again. I'll be sure to check everything more indepth than I did this time.


Offline wil.hamilton

  • Robot Overlord
  • ****
  • Posts: 207
  • Helpful? 6
  • rtfm
Re: Beginner Need Programming Help.
« Reply #5 on: July 29, 2009, 08:41:39 AM »
That's fine, you had a legitimate question and you were confused by the answers you had found online.  We are here to answer you questions, don't mean to deter them :P

Glad I could help though.
use the google.  it's your friend.

Offline DerutyTopic starter

  • Jr. Member
  • **
  • Posts: 7
  • Helpful? 0
Re: Beginner Need Programming Help.
« Reply #6 on: July 29, 2009, 04:05:26 PM »
Could you help w/ 1 more problem  :)?

I want to change PORTC pins from the $50 robot tutorial to digital input versus analog input. I know it has something to do w/
Code: [Select]
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')

}
but frankly I can't make heads or tails of how to alter that. I know I need to make sure the pull-up resistors are turned on, but... I don't know how to alter that command. Also I've seen code like this
Code: [Select]
#define F_CPU 8000000UL         /* 8MHz crystal oscillator */
 
#define BUTTON_PORT PORTD       /* PORTx - register for button output */
#define BUTTON_PIN PIND         /* PINx - register for button input */
#define BUTTON_BIT PD2          /* bit for button input/output */
 
which I can only deduce is a different method of altering the it. The question of the hour now is, how do I turn on the pull-up resistor, can I just comment out the PORTC=0x00 and put PORTC|=_BV(#of pin), albeit I'll have to do that several times. Another way I see is finding the hex number that would do that, but it seems rather ambiguous and I have no idea what to search for that. Also is there a step I'm missing?

Also a link to a tutorial would also be nice, one that specifically deals w/ this since I've found several but they only mention and show how to do it once, w/o much explanation.

EDIT: NVM I think I figured it out, but a nice link to a tutorial site would be nice. A recommended one, since I can't tell which ones are good atm >_>
« Last Edit: July 30, 2009, 05:38:48 AM by Deruty »

Offline wil.hamilton

  • Robot Overlord
  • ****
  • Posts: 207
  • Helpful? 6
  • rtfm
Re: Beginner Need Programming Help.
« Reply #7 on: July 30, 2009, 09:08:27 AM »
if you have any tutorials you looked at you could post them and i could tell you which one i thought was better?
use the google.  it's your friend.

Offline DerutyTopic starter

  • Jr. Member
  • **
  • Posts: 7
  • Helpful? 0
Re: Beginner Need Programming Help.
« Reply #8 on: August 02, 2009, 02:39:21 PM »
I've mainly used this guy's tutorial so far. It seems good. http://www.micahcarrick.com/05-15-2006/avr-tutorial-switch-debounce.html

Offline wil.hamilton

  • Robot Overlord
  • ****
  • Posts: 207
  • Helpful? 6
  • rtfm
Re: Beginner Need Programming Help.
« Reply #9 on: August 02, 2009, 08:59:04 PM »
a quick browse i'd agree with you, he seems to go into detail and thoroughly explain things (which is good for you)
use the google.  it's your friend.

 


Get Your Ad Here