Week 5
Microcontroller Programming

Hello World Echo Board
I got the USB to serial cables to work to program on my laptop... but then realized how slow debugging was and had to come into the lab to program anyway. I wanted to make a program that I could eventually use for my final project, so I decided to have the user set the time through the terminal and then the program would keep time. I implemented this on the hello world echo board, but ran out of memory with the ATtiny45 so it's actually implemented with an ATmega88 (see Week 6).

Here's how it works:


Here's the code:
// Alarm Clock

// Courtney Schmitt
// 10/14/08


#include avr/io.h
#include avr/pgmspace.h
#include util/delay.h
#include stdio.h
#include string.h
#include "serial_mega88.h"

void set_time(void);
void update_time(void);
void convert_time(void);
void display_time(void);
void display_menu(void);

// Time variables
int min = 0;	// 00 --> 59 minutes
int hour = 0;	// 00 --> 23 hour of the day (military time)
int day = 1;	// 1 --> 7 day of the week (sun --> sat)
int am = 1;

int hour12;

char input1;
char input2;

char hour_out[2];
char min_out[2];


int main(void) {
	
	// Infinite loop to update time
	output(tx_pin);
	clear(tx_pin);
	
	set_time();
	
	while (1) 
	{
		convert_time();
		display_time();
		update_time();
	}

	return 0;
}


void set_time(void) {
	print_string("Set Day:\n");
	print_string("   1. Sunday\n");
	print_string("   2. Monday\n");
	print_string("   3. Tuesday\n");
	print_string("   4. Wednesday\n");
	print_string("   5. Thursday\n");
	print_string("   6. Friday\n");
	print_string("   7. Saturday\n");
	
	input1 = get_char();
	put_char(input1);
	put_char('\n');
	
	day = (int)input1 - 48;
	
	print_string("Enter hour:\n");	
	
	char input1 = get_char();
	put_char(input1);
	char_delay();
	char input2 = get_char();
	put_char(input2);
	put_char('\n');
	
	if (input1 == '0')
	{
		hour = (int)input2 - 48;
	}
	else if (input1 == '1')
	{
		hour = (int)input2 - 48 + 10;
	}
	
	
	print_string("Enter minutes:\n");	
	
	input1 = get_char();
	put_char(input1);
	char_delay();
	input2 = get_char();
	put_char(input2);
	put_char('\n');
	
	if (input1 == '0')
	{
		min = (int)input2 - 48;
	}
	if (input1 == '1')
	{
		min = (int)input2 - 48 + 10;
	}
	if (input1 == '2')
	{
		min = (int)input2 - 48 + 20;
	}
	if (input1 == '3')
	{
		min = (int)input2 - 48 + 30;
	}
	if (input1 == '4')
	{
		min = (int)input2 - 48 + 40;
	}
	if (input1 == '5')
	{
		min = (int)input2 - 48 + 50;
	}
	
	
	print_string("AM (Y/N):\n");	
	
	input1 = get_char();
	put_char(input1);
	put_char('\n');
	
	if (input1 == 'y' || input1 == 'Y')
	{
		am = 1;
		if (hour == 12)
		{ hour = 0; }
	}
	else
	{
		am = 0;
		if (hour < 12)
		{ hour = hour + 12; }
	}
	
}


void update_time(void) {

   	min++;
	if (min == 60)
	{
		hour++;
		min = 0;
	}
	if (hour == 24)
	{
		day++;
		hour = 0;
	}
	if (day == 8)
	{
		day = 1;
	}

}


void convert_time(void) {
	
	if (hour < 13)
	{
		hour12 = hour;
		
		if (hour == 0) 
		{ hour12 = 12; }
	}
	else
	{ hour12 = hour - 12; }

}


void display_time(void) {
	
	char_delay();
		
	sprintf(hour_out, "-", hour12);
	sprintf(min_out, "%02d", min);
	
	if (day == 1)
	{ print_string("Sun "); }
	if (day == 2)
	{ print_string("Mon "); }
	if (day == 3)
	{ print_string("Tue "); }
	if (day == 4)
	{ print_string("Wed "); }
	if (day == 5)
	{ print_string("Thu "); }
	if (day == 6)
	{ print_string("Fri "); }
	if (day == 7)
	{ print_string("Sat "); }

	print_string(hour_out);
	put_char(':');
	print_string(min_out);
	put_char(' ');
	
	if (hour < 12)
	{ put_char('A'); }
	else
	{ put_char('P'); }
	put_char('M');
	
	put_char('\n');
}