/* * Introduction to Atmel microcontrollers demo program. * * This program demonstrates simple I/O (input-output) using switches and LEDs, * as well as how to do character output through the UART to HyperTerminal on a * PC. * * Last modified 2010-09-02 22:26 */ #include // for PORTx, PINx, DDRx, etc. #include // for printf() #include "uart.h" // for uart_init() (implementation(s) in uart.c) /* forward declarations (functions used before they are defined) */ void printf_example(void); /* * All one-time initializatons should be put here in init() to keep main() * manageable/readable. */ void init(void) { DDRA = 0x00; // configure all 8 bits of PORTA as inputs DDRB = 0xFF; // configure all 8 bits of PORTD as outputs uart_init(); } int main(void) { init(); // do the one-time initializations printf_example(); while (1) { PORTB = PINA; // have PORTB outputs reflect PINA inputs } return 0; // never executed... } /* * The statements in this function provide a few examples on how to use printf() for output * formatting with various data types. For more information about how to use avr-libc's * version of printf(), see: * http://www.nongnu.org/avr-libc/user-manual/group__avr__stdio.html * * In short, for each datum to be displayed, a format specifier (a % element) * must be provided in the format string (the first parameter), along with the datum * itself in the parameter list. */ void printf_example(void) { char aChar = 'Z', aCharInASCII=65; char aString[] = "Mr. String."; uint8_t the_answer = 42; uint16_t days_in_year = 365; uint32_t jenny = 8675309; double pi = 3.14159265358979323846; printf("The character '%c' occurs two times in the string \"%s\",\r\nwhereas '%c' doesn't.\r\n", aChar , aString, aCharInASCII); printf("An 8bit unsigned int in decimal format: %u\r\n", the_answer); printf("An 8bit unsigned int in hexadecimal format: %X\r\n", the_answer); printf("A 16bit unsigned int: %u\r\n", days_in_year); printf("A 32bit unsigned int: %lu\r\n", jenny); printf("A 32bit floating point number: %f, and less accurately: +%6.2f: \r\n", pi, pi); }