Author Topic: C programming  (Read 1364 times)

0 Members and 1 Guest are viewing this topic.

Offline robotmaniacTopic starter

  • Jr. Member
  • **
  • Posts: 40
  • Helpful? 0
C programming
« on: October 14, 2012, 02:23:36 PM »
Hello guys. I need help on learning C programming for mirocontrollers I know basic statements and all that stuff but i want to learn more for example how do i tell the computer that port PD0 has a servo or a LED and I want to learn how to CREAT this source files not copy and paste I want to understand them.
#define F_CPU 20000000    // AVR clock frequency in Hz, used by util/delay.h
#include <avr/io.h><------what is this????
#include <util/delay.h>
int main() {
    DDRD |= (1<<DDD1);          // set LED pin PD1 to output
    while (1) {
        PORTD |= (1<<PORTD1);   // drive PD1 high
        _delay_ms(100);         // delay 100 ms
        PORTD &= ~(1<<PORTD1);  // drive PD1 low
        _delay_ms(900);         // delay 900 ms
What is DDRD and all tahat stuff i know what while is and the curly braces and the basic stuff I could make a calculator using a compiler but I dont know how to control a servo or light please help tell me the name of a book a pdf a link a video what ever you think that could explain to me all of this awesome things :) THANks in advance
Learning from what I can...

Offline waltr

  • Supreme Robot
  • *****
  • Posts: 1,944
  • Helpful? 99
Re: C programming
« Reply #1 on: October 14, 2012, 05:42:33 PM »
You define port pin as digital output, digital input not what is connected (LED, servo, etc). This is at the lowest level. One can abstract this at a higher level (function) that calls lower level functions to setup the pin's functions.

Another learning tool is the simulator in the IDE. With this you can step through the code and observe what is happening in each register and pin. A little knowledge of the under lining assembler code is useful to understand the hardware of the processor and what a 'C' statement is really doing.

The code you posted does have some 'C' constructs that are not the easiest to understand. One also needs to know the definitions of the names used (like 'DDD1'). So take the code one line at a time and learn what it does then study the processor's data sheet to learn what the code is doing in the processor's hardware.
This will take some time and effort but it is learn-able.
For standard C I recommend the K&R C programming book (I've have my copy for 25 years and still refer to it).
This is the bible for the C programming language and written by the guys that invented C. It is a complete reference and has some very good tutorials.

When you really get stuck on understanding a line of code then post a question about it for help.

I'll take the one line of code and explain what it does:
DDRD |= (1<<DDD1);
DDD1 is a whole number
DDRD is an AVR register
The part: 1<<DDD1
shifts a binary '1' left DDD1 times. If DDD1 = 1 then it shifts the '1' one binary place left to equal 0b00000010 or 0x02. (I know this only from the comment that it sets pin PD1 to an output.
the |= is reading the value in DDRD then logic OR with the shifted bit from the '1<<DDD1' part. What this does is set bit 1 in register DDRD to a '1' without change the value of the other bits in that register.

So to fully know what the line of code does one needs to know the value of DDRD and DDD1 as well as what the DDRD AVR register does in the hardware.

The defines for DDRD and DDD1 should be in a header file, possibly avr/io.h.

LEDs are current driven devices (require a series resistor to limit the current when driven from a logic output. Google and the Robot Tutorials (look at the top, right of this page) will give you the details.

Servos (RC hobby servos) are controlled by a variably width pulse. The processor drives an output pin high for 1 to 2ms and of for about 19ms. Typically this is done either with software timing loops or with some hardware (PWM module) built into the processor. Again use google the the Robot Tutorials.

Offline jkerns

  • Robot Overlord
  • ****
  • Posts: 270
  • Helpful? 12
Re: C programming
« Reply #2 on: October 14, 2012, 06:02:37 PM »
Some of that stuff like DDRD is a name for a register that controls a set of pins and is found in the 165 (guess) page data sheet for the microcontroller.

I thought that had a nice tutorial for the AVR microcontrolers - he has a set of videos along with written explanations and sample code so you can play along.

I get paid to play with robots - can't beat that with a stick.


Get Your Ad Here