Week Eleven – Interfaces and Application Programming

Continuing with the Knockdown Indicator

Write an Application that interfaces with an input or output device.



My final project is a device that uses two inputs (apparent wind speed and angle of heel) to calculate whether a sailing vessel is at risk of a knockdown within a given gust ranges, based on the squall curves of the vessel.  All sailing vessels have squall curves as part of their stability booklets, but few mariners know how to read them – this device will do the job for them.


I continued this week with the existing board (since I knew it would take code) and the ATMega 328 microprocessor.  This chip utilizes the USART protocol (Universal Serial Asynchronous Receive Transmit).  There is a good tutorial here.  The ATMega datasheet also has a good overview of USART as well as the specifics (including sample assembly and C code), but assumes a higher level of familiarity with serial interfaces than the tutorial.


I cobbled (cut/pasted/guessed) the code:



//  knockdown


//  Created by Simon Colley on 11/26/12.


//This is meant to run on an ATMEL ATMega328


#define F_CPU 20000000UL

#include <avr/io.h>

#include <util/delay.h>

#include <avr/pgmspace.h>


//outputs = rgb led

#define led_port PORTD

#define led_direction DDRD

#define thirty (1 << PD2)

#define fortyfive (1 << PD3)

#define sixty (1 << PD4)


#define led_delay() _delay_ms(100) // LED delay

#define PWM_delay() _delay_ms(1000) // PWM delay


#define serial_port PORTD

#define serial_direction DDRD

#define serial_pins PIND

#define serial_pin_in (1 << PD0)

#define serial_pin_out (1 << PD1)


//USART setup

void USART_init(void);

unsigned char USART_receive(void);

void USART_send( unsigned char data);

void USART_putstring(char* StringPtr);


int main (void){ // set clock divider to /1


CLKPR = (1 << CLKPCE);

CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0);


clear(led_port, thirty | fortyfive | sixty); // | is bitwise operation, || is logical OR

output(led_direction, thirty | fortyfive | sixty);

set(led_port, thirty | fortyfive | sixty);


void USART_init(void){


    UBRR0H = (uint8_t)(BAUD_PRESCALLER>>8);

    UBRR0L = (uint8_t)(BAUD_PRESCALLER);

    UCSR0B = (1<<RXEN0)|(1<<TXEN0);

    UCSR0C = (3<<UCSZ00);




// get values from RX


unsigned char USART_receive(void){


while(!(UCSR0A & (1<<RXC0)));


return UDR0;



while (1){ //meaning, all the time


    while (UDR0==3) {

    clear(led_port,thirty); //otherwise turn it on!


    set(led_port,thirty); //turn off



    while (UDR0==4) {

    clear(led_port,fortyfive); //otherwise turn it on!


    set(led_port,fortyfive); //turn off



    while (UDR0==6) {

    clear(led_port,sixty); //otherwise turn it on!


    set(led_port,sixty); //turn off




I think this will work, but still working on the python end of it.  Installed py.serial OK, still figuring out python in general.


Spent probably too much time trying to learn how to edit in XCode (great c tutorials on Lynda.com, but a lot of emphasis on using XCode)