Jump to content

Archived

This topic is now archived and is closed to further replies.

H@L0_F00

[Version 1] My Collective Post Of Questions

Recommended Posts

I confess... I was given a Teensy, and have done absolutely nothing with it... I would like to start and contribute what little I can, but I want to understand what all is going on. I HATE not understanding things... I want to work in C because I've dabbled in C, but never really got into it because I wasn't good enough to code anything useful... With the Teensy though, I will have good reason to practice C (which is how I'll begin to remember the different things and advance in general). I've been looking over various source code files, header files, and even makefiles in an attempt to understand it better, but I've not really gotten anywhere. I KNOW I will have many questions while developing, so I've made this post where I can keep all my questions as they arise. For now, I would appreciate it if somebody could explain to me, line-by-line where possible, what this snippet does:

// Teensy 2.0: LED is active high
#if defined(__AVR_ATmega32U4__) || defined(__AVR_AT90USB1286__)
#define LED_ON        (PORTD |= (1<<6))
#define LED_OFF        (PORTD &= ~(1<<6))

// Teensy 1.0: LED is active low
#else
#define LED_ON    (PORTD &= ~(1<<6))
#define LED_OFF    (PORTD |= (1<<6))
#endif

#define LED_CONFIG    (DDRD |= (1<<6))
#define CPU_PRESCALE(n)    (CLKPR = 0x80, CLKPR = (n))
#define DIT 80        /* unit time for morse code */

void morse_character(char c);
void morse_P(const char *s);
const unsigned char morse_code_table[];


int main(void)
{
    unsigned char i;

    // set for 16 MHz clock, and make sure the LED is off
    CPU_PRESCALE(0);
    LED_CONFIG;
    LED_OFF;

I don't understand the definitions and how they work exactly, for one, and I also don't understand how a definition can be called as a... declaration, I guess?

I also read somewhere about a dev channel on IRC, but I can no longer find it. If somebody could point me to it, I'd appreciate it.

Thanks.

Share this post


Link to post
Share on other sites

Hi,

I'm a beginner with programming in C/C++ as well. :) Here are some of the sites I found that are helpful to me in understanding C++ codes... I tried to define what the code says, so I hope it helps you somewhat :\

http://www.cppreference.com/wiki/preprocessor/define (C++ reference)

http://msdn.microsoft.com/en-us/library/ty67wk28(VS.80).aspx

http://www.cs.umd.edu/class/spring2003/cms...p/bitshift.html

http://www.somacon.com/p125.php (bitwise operations)

http://linux.die.net/man/3/uint16_t (Definition of terms used)

http://www.pjrc.com/teensy/usb_keyboard.html (where you could find info about Teensy & codes)

The #define is used to simplify your code. For example, if you have a certain method that you have to use again and again throughout your code and you don't want to spend all that time pasting the same code. Thus, the #define will tell the compiler that if this keyword is in the code, substitute it with this definition. Or, just Defining a variable to hold an expression or a value.

Here's an example from a site I found:

#define absolute_value(x) (((x)<0) ? (x) : (x) )

int num = -1;

while ( absolute_value(num)){...} --Instead of having to write out the entire expression, I can just type in the keyword I defined earlier

The conditional operator (?) returns one of two values depending on the value of a Boolean expression

condition ? first_expression : second_expression;

The PORTD |= (1<<6)

|= is the binary bitwise inclusive Or, the << tells you that the bits are shifted to the left 6 times

// Teensy 2.0: LED is active high

//If its either Teensy 2.0 or Teensy ++ 2.0, then the LED light is defined by these definitions (PORTD ...)

#if defined(__AVR_ATmega32U4__) || defined(__AVR_AT90USB1286__)

#define LED_ON (PORTD |= (1<<6))

#define LED_OFF (PORTD &= ~(1<<6))

// Teensy 1.0: LED is active low

#else

#define LED_ON (PORTD &= ~(1<<6))

#define LED_OFF (PORTD |= (1<<6))

#endif

#define LED_CONFIG (DDRD |= (1<<6))

#define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))

#define DIT 80 /* unit time for morse code */

void morse_character(char c);

void morse_P(const char *s);

const unsigned char morse_code_table[];

int main(void)

{

unsigned char i;

// set for 16 MHz clock, and make sure the LED is off

CPU_PRESCALE(0);

LED_CONFIG; //this was defined earlier too

LED_OFF;[/code] //the LED_OFF was defined earlier, so compiler will fill in its value based on whether its Teensy 1.0 or Teensy 2.0

Share this post


Link to post
Share on other sites

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...