// // // helloworld.c // Hello World // Morse code // .... . .-.. .-.. --- / .-- --- .-. .-.. -.. // // Says "HELLO WORLD" in morse code by blinking an LED at // the touch of a button. // // // Alex Berke // Based on code supplied by Neil Gershenfeld // http://academy.cba.mit.edu/classes/embedded_programming/hello.ftdi.44.echo.c // // (c) Massachusetts Institute of Technology 2010 // This work may be reproduced, modified, distributed, // performed, and displayed for any purpose. Copyright is // retained and must be preserved. The work is provided // as is; no warranty is provided, and users accept all // liability. // #include #include #define output(directions, pin) (directions |= pin) // set port direction for output #define input(directions,pin) (directions &= (~pin)) // set port direction for input #define set(port, pin) (port |= pin) // set port pin #define clear(port, pin) (port &= (~pin)) // clear port pin #define pin_test(pins, pin) (pins & pin) // test for port pin #define bit_test(byte, bit) (byte & (1 << bit)) // test for bit set #define led_delay() _delay_ms(100) // Set up morse code constants: // The length of a "dot" is 1 unit. // The length of a "dash" is 3 units. // The space between parts of 1 letter is 1 unit. // The space between letters is 3 units. // The space between words is 7 units. #define DOT_MS 100 // a dot is the length of one unit in morse code #define dot_delay() _delay_ms(DOT_MS) #define dash_delay() _delay_ms(3*DOT_MS) // Define the pins to be used as the input (button) and output (LED). #define LED_PORT PORTA #define LED_DIRECTION DDRA #define LED_PIN (1 << PA7) #define BUTTON_PINS PINB #define BUTTON_PORT PORTB #define BUTTON_DIRECTION DDRB #define BUTTON_PIN (1 << PB2) int main (void) { // // main // // set clock divider to /1 (Make it faster) // CLKPR = (1 << CLKPCE); CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0); // Initialize LED pin (output). clear(LED_PORT, LED_PIN); output(LED_DIRECTION, LED_PIN); // Initialize the button pin (input). set(BUTTON_PORT, BUTTON_PIN); // Turn on pull-up -- I don't understand this. input(BUTTON_DIRECTION, BUTTON_PIN); // main loop while (1) { if (pin_test(BUTTON_PINS, BUTTON_PIN)) clear(LED_PORT, LED_PIN); else say_hello_world(); } } void say_hello_world() { /* Says "Hello World" in morse code, i.e.: * .... . .-.. .-.. --- / .-- --- .-. .-.. -.. * * A dot is expressed as a short blink of the LED. * A dash is expressed as a long blink of the LED. */ // H dot(); dot(); dot(); dot(); letter_space(); // E dot(); letter_space(); // L dot(); dash(); dot(); dot(); letter_space(); // L dot(); dash(); dot(); dot(); letter_space(); // O dash(); dash(); dash(); letter_space(); word_space(); // W dot(); dash(); dash(); letter_space(); // O dash(); dash(); dash(); letter_space(); // R dot(); dash(); dot(); letter_space(); // L dot(); dash(); dot(); dot(); letter_space(); // D dash(); dot(); dot(); letter_space(); } void dot() { /* * Express a dot in morse code. */ set(LED_PORT, LED_PIN); dot_delay(); clear(LED_PORT, LED_PIN); dot_delay(); } void dash() { /* * Express a dash in morse code. */ set(LED_PORT, LED_PIN); dash_delay(); clear(LED_PORT, LED_PIN); dot_delay(); } void letter_space() { /* * Express the space between letters in morse code. * 2 extra units of no signal. * Assumes that it follows a LOW write to the LED for 1 unit of time. */ _delay_ms(2*DOT_MS); } void word_space() { /* * Express the space between letters in morse code. * 6 extra units of no signal. * Assumes that it follows a LOW write to the LED for 1 unit of time. */ _delay_ms(6*DOT_MS); }