Beispiel #1
0
int main(void) {
	char buf[36];
	uint8_t ee_timer = 0;
	
	PORTB = (1<<PB7)|(1<<PB4);
	DDRB = (1<<PB7)|(1<<PB4);

	PORTD = 0;
	DDRD = 0;
	
	UBRRH=0;
	UBRRL = 12; // 9,6 kBaud
	
	UCSRA = (1<<U2X);
	UCSRB = (1<<TXEN);
	UCSRC = (3<<UCSZ0); // 8 bit, 1 stopbit
	
	TCCR0A = (1<<WGM01)|(1<<WGM00);
	TCCR0B = (1<<WGM02)|(7<<CS00);
	OCR0A = 49;
	
	TCCR1A = 0;
	TCCR1B = (5<<CS10);
	TCCR1C = 0;
	
	TIMSK = (1<<TOIE1) | (1<<TOIE0);
	
	MCUCR = (1<<ISC01);
	GIMSK = (1<<INT0);
	
	set_sleep_mode(SLEEP_MODE_IDLE);
	
	buf[0]='*';
	buf[1+8] = buf[1+8+1+8] = buf[1+8+1+8+1+8] = ',';
	buf[1+8+1+8+1+8+2] = ',';

	buf[30]='#';
	buf[31]='\r';
	buf[32]='\n';
	buf[33]=0;
	
	load_eeprom();
	
	while(1) {
		
		cli();
		
		if(events & EVT_TIMER) {
			uint32_t cnt_copy 	= cnt;
			uint32_t mark_copy 	= mark_cnt;
			uint8_t evts_copy 	= events;
			uint32_t echo;
			
			events = 0;
			
			sei();
			
			PORTB &= ~(1<<PB7);	// LED on

			echo = ping();
			
			u32tostr((uint8_t*)buf+1, cnt_copy);
			u32tostr((uint8_t*)buf+1+8+1, mark_copy);
			u32tostr((uint8_t*)buf+1+8+1+8+1, echo);
			
			// error indicator - inverted input
			buf[28] = (PINB & (1<<PB0)) ? '0' : '1';
			if(buf[28]&1) {
				PORTB &= ~(1<<PB4);	// ERROR-LED on
			}
			else {
				PORTB |= (1<<PB4);	// ERROR-LED off
			}
			
			buf[30]=0;
			cksum((uint8_t *)buf);
			
			uart_send(buf);
			
			/* eeprom write pending? */
			if(ee_timer) {
				ee_timer--;
				
				if(!ee_timer)
					write_eeprom(cnt_copy);
			}
			else if(evts_copy & EVT_CNTR)
				ee_timer = EE_WRDIV; 	// counter changed -> schedule eeprom write
			
			PORTB |= (1<<PB7);	// LED off
		}
		else
			sei();
		
		sleep_mode();
	}
	
	return 0;
}
Beispiel #2
0
/*****************************************************************************
** 函数名称: USART_Put_Num
** 功能描述: 串口发送数值
				函数中会将数值转为相应的字符串,发送出去。比如 4567 转为 "4567" 
** 作   者: Dream
** 日   期: 2010年12月17日
*****************************************************************************/	
void USART_Put_Num(u32 dat)
{
 	char temp[20];
 	u32tostr(dat,temp);
 	USART_Send_Str(temp);
}