Beispiel #1
0
// for debugging
void blinkNTimes(int n)
{	int msec = 500;
	SET_BIT(PORTB, 0);

	for(int i = 0; i < n; i++)
	{
		SET_BIT(PORTB, 0);
		wait_avr(msec);
		CLR_BIT(PORTB, 0);
		wait_avr(msec);
	}
	
}
Beispiel #2
0
void ini_lcd(void) {
  SET_BIT(DDR, RS_PIN);
  SET_BIT(DDR, RW_PIN);
  SET_BIT(DDR, EN_PIN);
  wait_avr(16);
  output(0x30, 0);
  wait_avr(5);
  output(0x30, 0);
  wait_avr(1);
  write(0x3c, 0);
  write(0x0c, 0);
  write(0x06, 0);
  write(0x01, 0);
}
Beispiel #3
0
void flashSuccess(void)
{
	SET_BIT(PORTB, 0);
	wait_avr(150);
	CLR_BIT(PORTB, 0);
	wait_avr(150);
	SET_BIT(PORTB, 0);
	wait_avr(150);
	CLR_BIT(PORTB, 0);
	wait_avr(150);
	SET_BIT(PORTB, 0);
	wait_avr(150);
	CLR_BIT(PORTB, 0);
}
void main(){
	SET_BIT(DDRB, 0);
	ini_lcd();
	clr_lcd();
	for(;;){
	pos_lcd(0,0);
	put_str_lcd("Press 1. Saria's...");
	pos_lcd(1,0);
	put_str_lcd("Press 2. Mary...");
		if (get_key() == 16)
		{
			clr_lcd();
			pos_lcd(0,2);
			put_str_lcd("Saria's Song");
			pos_lcd(1, 0);
			put_str_lcd("Press B to stop.");
			play_song(saria, 85);
			clr_lcd();
		}
		else if (get_key() == 15)
		{
			clr_lcd();
			pos_lcd(0,2);
			put_str_lcd("Mary Had A");
			pos_lcd(1, 2);
			put_str_lcd("Little Lamb");
			play_song(mary, 26);
			clr_lcd();
		}	
		wait_avr(50);
	}
}
Beispiel #5
0
int main(void)
{
	//set the direction of port 40 to be an input
	set_input(PIN,BUTTON);
	//set direction of port 1 to be an output
	set_output(DDRD,LED);
	
	while(1)
	{
		//if(GET_BIT(PIN,BUTTON))
		//{
			set_high(PORTD,LED);
			wait_avr(DELAY);
			set_high(PORTD,LED);
			wait_avr(DELAY);
		//}
	}
}
Beispiel #6
0
void LCD(void)
{
	ini_lcd();
	clr_lcd();
	put_str_lcd("hi");
	wait_avr(3000);
	clr_lcd();	
	
	resetLCD();
	setDateTime();	
}
Beispiel #7
0
void 
start_led(void)
{
	int msec = 500;
	SET_BIT(PORTB, 0);

	for(;;)
	{	
		if(GET_BIT(PINB,1)){
			CLR_BIT(PORTB, 0);

		}
		else{
			SET_BIT(PORTB, 0);
			wait_avr(msec);
			CLR_BIT(PORTB, 0);
			wait_avr(msec);
		}
	}

}
unsigned char key_pressed(unsigned char row, unsigned char column)
{
	DDRC = 0;
	PORTC = 0;
	
	SET_BIT(PORTC, row);
	SET_BIT(DDRC, column);
	wait_avr(100);
	
	return !GET_BIT(PINC, row);

}
Beispiel #9
0
unsigned char getSingleButton(unsigned char row,unsigned char column)
{
	//set C3-C0 outputs to 1 except bit column
	//and not C4-C7 are not modified
	DDRC = 0x00;
	DDRC |= 1 << row;
	PORTC |= 1 << column;
	//wait to read the input
	wait_avr(2);
	//now read pin with number row
	//if button is being pressed then the state will change from
	// 1 to 0 
	return ((PINC & (1 << column)) == 0 ? 1:0);
}
Beispiel #10
0
// Blink twice to show error and wait one second
// then blink one time per second to show 1
// or blink two times per second to show 0
void showError(unsigned char err)
{
	SET_BIT(PORTB, RED);
	wait_avr(250);
	CLR_BIT(PORTB, RED);
	wait_avr(250);
	SET_BIT(PORTB, RED);
	wait_avr(250);
	CLR_BIT(PORTB, RED);
	
	wait_avr(1000);
	
	int i;
	for(i = 7; i >= 3; --i)
	{
		if ((err & (1 << i)) != 0)
		{
			SET_BIT(PORTB, RED);
			wait_avr(150);
			CLR_BIT(PORTB, RED);
			wait_avr(1000);
		}
		else
		{
			SET_BIT(PORTB, RED);
			wait_avr(150);
			CLR_BIT(PORTB, RED);
			wait_avr(150);
			SET_BIT(PORTB, RED);
			wait_avr(150);
			CLR_BIT(PORTB, RED);
			wait_avr(1000);
		}			
	}
	
}
void wait_btn() {
	int btn = 0;
	int hold = 0;
	
	while(1) {
		btn = btn << 1;
		if(GET_BIT(PINA, 1)) {
			btn = btn | 1;
		}
		if(btn == 0xff) {
			hold = 1;
		} else if(btn == 0 && hold == 1) {
			return;
		}
		wait_avr(1);
	}
}
void increment()
{	
	sec++;
	if (sec == 60)
	{
		sec = 0;
		min++;
		if (min == 60)
		{
			min = 0;
			hr++;
			if (hr == 24) 
			{ 
				hr = 0;
				day++;
				if ((year%4 == 0 && year%100 != 0) || year%400 == 0)
				{
					if (day > leapyear[month-1])
					{
						day = 1;
						month++;
						if (month > 12)
						{
							month = 1;
							year++;
						}
					}	
				}
				else if (day > normal[month-1])
				{	
					day = 1;
					month++;
					if (month > 12)
					{
						month = 1;
						year++;
					}
				}
			}
		}
	}
	wait_avr(100);
}
int main(void){
	ini_lcd();
	ini_avr();

	for(;;)
	{
		if(get_key() == 13) // SET DATE
			{
				//put_lcd('A');
				row = 0;
				col = 0;
				clr_lcd();
				pos_lcd(0,0);
				put_str_lcd("MMDDYYYY");
				start = 0;
				pos_lcd(0,0);
				set_date = 1;
				set_time = 0;
				index = 0;
			}
			
			else if(get_key() == 9) // SET TIME
			{
				//put_lcd('B');
				row = 1;
				col = 0;
				clr_lcd();
				pos_lcd(1,0);
				put_str_lcd("HHMMSS");
				start = 0;
				pos_lcd(1,0);
				set_time = 1;
				set_date = 0;
				index = 0;
			}
		if (start == 1)
		{
			increment();
			print();
		}
		else 
		{
			if(get_key() == 2)
			{
				//put_lcd('#');
				if (set_date)	{setDate();}
				if (set_time)	{setTime();}
				start = 1;
			}
			else if(get_key() == 3){
				put_lcd('0');
				if(set_date){
					date[index] = 0;
				}
				else if(set_time){
					time[index] = 0;
				}
				index++;
				col++;
			}
			else if(get_key() == 4){
				//put_lcd('*');
				clr_lcd();
				col = 0;
				row = 0;
				set_time = 0;
				set_date = 0;
				start = 1;
				index = 0;
			}
			else if(get_key() == 6){
				put_lcd('9');
				if(set_date){
					date[index] = 9;
				}
				else if(set_time){
					time[index] = 9;
				}
				index++;
				col++;
			}
			else if(get_key() == 7){
				put_lcd('8');
				if(set_date){
					date[index] = 8;
				}
				else if(set_time){
					time[index] = 8;
				}
				index++;
				col++;
			}
			else if(get_key() == 8){
				put_lcd('7');
				if(set_date){
					date[index] = 7;
				}
				else if(set_time){
					time[index] = 7;
				}
				index++;
				col++;
			}
			else if(get_key() == 10){
				put_lcd('6');
				if(set_date){
					date[index] = 6;
				}
				else if(set_time){
					time[index] = 6;
				}
				index++;
				col++;
			}
			else if(get_key() == 11){
				put_lcd('5');
				if(set_date){
					date[index] = 5;
				}
				else if(set_time){
					time[index] = 5;
				}
				index++;
				col++;
			}
			else if(get_key() == 12){
				put_lcd('4');
				if(set_date){
					date[index] = 4;
				}
				else if(set_time){
					time[index] = 4;
				}
				index++;
				col++;
			}
			else if(get_key() == 14){
				put_lcd('3');
				if(set_date){
					date[index] = 3;
				}
				else if(set_time){
					time[index] = 3;
				}
				index++;
				col++;
			}
			else if(get_key() == 15){
				put_lcd('2');
				if(set_date){
					date[index] = 2;
				}
				else if(set_time){
					time[index] = 2;
				}
				index++;
				col++;
			}
			else if(get_key() == 16){
				put_lcd('1');
				if(set_date){
					date[index] = 1;
				}
				else if(set_time){
					time[index] = 1;
				}
				index++;
				col++;
			}
			if (set_time)
			{
				if (col > 5)
				{
					col = 0;
					pos_lcd(row, col);
				}
			}
			else if (set_date)
			{
				if (col > 7)
				{
					col = 0;
					pos_lcd(row, col);
				}
			}
		}
		wait_avr(25);
	}
	return 0;
}
Beispiel #14
0
void setDateTime(void)
{
	// keep track of LCD pos
	int row, col;
	row = 0;
	col = 2;
	// number is the number entered
	int number;
	// counter to keep track of how many digits entered
	int counter = 0;
	// number to keep track of the number so far
	int userInput = 0;
	char buf[1]; 
	do 
	{
		while(col < 10 || row != 1)
		{
			pos_lcd(row, col);
			number = get_key();
			if(isValidKey(number))
			{
				sprintf(buf, "%i", number);
				put_str_lcd(buf);
				wait_avr(400);

				if(col >= 11 && row == 0)
				{
					row++;
					col = 2;
				}
				else if(col <= 11)
				{
					col++;
					if(col == 4 || col == 7)
						col++;
				}
					
				counter++;
				if(counter%2 == 0 && counter != 6)
					userInput += number;
				else if(counter == 5)
					userInput = 1000*number;
				else if(counter == 6)
					userInput += 100*number;
				else if(counter == 7)
					userInput += 10*number;
				else 
					userInput = 10*number;
			
			}
			if(counter == 2)
				Month = userInput;
			else if(counter == 4)
				Day = userInput;
			else if(counter == 8)
				Year = userInput;
			else if(counter == 10)
				Hour = userInput;
			else if(counter == 12)
				Minutes = userInput;
			else if(counter == 14)
				Seconds = userInput;
		}
		if(!isValidDate(Day, Month, Year))
		{
			clr_lcd();
			sprintf(buf, "%s", "invalid date");
			put_str_lcd(buf);
			wait_avr(2000);
			row = 0;
			col = 2;

			// counter to keep track of how many digits entered
			counter = 0;
			// number to keep track of the number so far
			userInput = 0;
			resetLCD();
		}
	}while(!isValidDate(Day, Month, Year));


	while(1)
	{
		wait_avr(1000);
		incrementTime();
		pos_lcd(0, 2);
		sprintf(buf, "%02i/%02i/%02i", Month, Day, Year);
		put_str_lcd(buf);
		pos_lcd(1, 2);
		sprintf(buf, "%02i:%02i:%02i", Hour, Minutes, Seconds);
		put_str_lcd(buf);
	}	
}
int main(void)
{
	ini_lcd();
	clr_lcd();
	int ca = 0;
	int cb = 0;
	int aa=0;
	int ab=0;
	int ma=0;
	int mb=0;
	int mina=0;
	int minb=0;
	//ADMUX |= 71;
	//SET_BIT(ADMUX,6);
	//ADCSRA|=135;
	//SET_BIT(ADCSRA)
	//ADMUX=(1<<REFS0); // For Aref=AVcc;
	SET_BIT(ADMUX,6);
	//ADCSRA=(1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0); //Prescalar div factor =128
	ADCSRA|=135;
	
	//wait_avr(50000);
	//uint8_t ch=ch&0b00000111;
	
	//ADMUX|=ch;
	//ADCSRA|=(1<<ADSC);

	//ADCSRA|=(1<<ADEN);
	
	
    while(1)
    {
        //TODO:: Please write your application code 
		pos_lcd(0,0);
		sprintf(beg,"A:Start Sampling");
		puts_lcd2(beg);
		pos_lcd(1, 0);
		sprintf(beg,"B:reset");
		puts_lcd2(beg);
		if(key_pressed(0, 7))
		{
			while(1)
			{
				if(key_pressed(1, 7))
				{
					result = 1;
					max=0;
					min=9999;
					average=0;
					total=0;
					count=0;
					clr_lcd();
					break;
				}
				get_adc();
				clr_lcd();
				pos_lcd(0, 0);
				//int a,b;
				ca = result / 100;
				cb = result % 100;
				aa=average/100;
				ab=average%100;
				ma=max/100;
				mb=max%100;
				mina=min/100;
				minb=min%100;
				sprintf(beg,"Cr:%01d.%02d Mx:%01d.%02d",ca,cb,ma,mb);//(adc/1023)*5
				puts_lcd2(beg);
				pos_lcd(1, 0);
				sprintf(beg,"Avg:%01d.%02d Mn:%01d.%02d",aa,ab,mina,minb);
				puts_lcd2(beg);
				wait_avr(500);
			}
		}
		

			
    }
}