Squirrels have fuzzy tails.
0 Members and 1 Guest are viewing this topic.
#define F_CPU 1000000UL#include <avr/io.h>#include <util/delay.h>// LED is connected to PB4 (pin 3 on the chip)#define LEDPIN 4int main(void){ DDRB = 0x1F; // PB0-PB4 output PORTB = 0x00; // Set all pins low // Turn LED on for 1 sec and then off for 1 sec forever while(1) { PORTB |= _BV(LEDPIN); // Turn LED on _delay_ms(1000); // Wait 1000 ms (1 sec) PORTB &= ~(_BV(LEDPIN)); // Turn LED off _delay_ms(1000); // Wait 1000 ms (1 sec) } return 0;
So far, every tutorial I've found (just like the $50 robot) doesn't actually teach you anything. They just say "copy this into your code and watch it work"
The better C tutorials, those that carefully step you through the language, are most often those that are PC-centric. Those explain the language concept without being mired up in the "stuff" of embedded systems. I would say start there (with a PC primer) until you have read enough to be comfortable with the construct of the language.Once you have a feel for the language elements, then you will start to identify the things that are different about using C with an embedded system.You might find it best to look at the Arduino development system. It is C with helpful embedded libraries. The book "Getting Started with the Arduino" might be a lot of help to you:http://www.circuitgizmos.com/products/cgbook/cgbook.shtml
#define PORT_ON( port_letter, number ) port_letter |= (1<<number)#define PORT_OFF( port_letter, number ) port_letter &= ~(1<<number)#define PORT_ALL_ON( port_letter, number ) port_letter |= (number)#define PORT_ALL_OFF( port_letter, number ) port_letter &= ~(number)#define FLIP_PORT( port_letter, number ) port_letter ^= (1<<number)#define PORT_IS_ON( port_letter, number ) ( port_letter & (1<<number) )#define PORT_IS_OFF( port_letter, number ) !( port_letter & (1<<number) )
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') }