示例#1
0
int main(){

	ADMUX |= ((1 << REFS1) | (1 << REFS0));
	ADCSRA |= ((1 << ADEN) | (1 << ADPS0) | (1 << ADPS1) | (1 << ADPS2));

	USART_Init( MYUBRR );

	double result;
	int d1;
	double f2;
	int d2;
	char temp[45];

	while(1){

		result = measure()*0.0025*4.58;

		d1 = result;
		f2 = result - d1;
		d2 = trunc(f2*10000);

		USART_ClearTerminal();

		USART_ForegroundColor( "BLUE" );

		USART_CursorPosition(5,20);

		USART_TransmitString( "--==POWER SUPPLY ATMEGA8 by MiCkl==--\r\n");

		USART_ForegroundColor( "WHITE" );

		USART_CursorPosition(10,21);

		USART_TransmitString( "Aktualne napiecie: " );

		if ( result <= 4 && result >= 0) USART_ForegroundColor( "GREEN" );
		else if ( result <= 8 && result >= 4) USART_ForegroundColor( "YELLOW" );
		else if ( result <= 12 && result >= 8) USART_ForegroundColor( "RED" );

		sprintf(temp, "%d.%04d [+-0.2] V \r\n", d1, d2);
		USART_TransmitString( temp );
		USART_TransmitString("\r\n");

		_delay_ms(200);
	}


}
示例#2
0
int main(void)
{
// Input/Output Ports initialization
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=Out Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=0 State2=T State1=T State0=T
PORTB=0x10;
DDRB=0x08;

// Port C initialization
// Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;

// Port D initialization
// Func7=In Func6=In Func5=In Func4=Out Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=0 State3=T State2=T State1=T State0=P
PORTD=0x01;
DDRD=0x10;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
TCCR0=0x00;
TCNT0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 3276,800 kHz
// Mode: Normal top=0xFFFF
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: On
// Input Capture Interrupt: Off
// Compare A Match Interrupt: On
// Compare B Match Interrupt: On
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer2 Stopped
// Mode: Normal top=0xFF
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// External Interrupt(s) initialization
// INT0: On
// INT0 Mode: Rising Edge
// INT1: Off
GICR|=0x40;
MCUCR=0x03;
GIFR=0x40;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x1C;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=(1<<OCIE2) | (1<<TOIE2) | (0<<TICIE1) | (1<<OCIE1A) | (1<<OCIE1B) | (1<<TOIE1) | (0<<TOIE0);


// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 9600
//UCSRA=0x00;
//UCSRB=0x18;
//UCSRC=0x86;
//UBRRH=0x00;
//UBRRL=0x14;
	// USART settings: 9600 baud 8-n-1
	// WARNING: real baud = 9752: err = 1,58333333333334%
/*	UBRRH = 0;
	UBRRL = 20;
	UCSRB = (1<<RXCIE) | (1<<RXEN) | (1<<TXEN);
	UCSRC = (1<<URSEL) | (1<<UCSZ1) | (1<<UCSZ0);
	SREG |= (1<<7); // enable interrupts
	USART_TransmitString("dsgfgsdg\r\n");
// */


#ifdef URT_EN

// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: Off
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 9600
UCSRA=0;//(0<<RXC) | (0<<TXC) | (0<<UDRE) | (0<<FE) | (0<<DOR) | (0<<UPE) | (0<<U2X) | (0<<MPCM);
UCSRB=(0<<RXCIE) | (0<<TXCIE) | (0<<UDRIE) | (0<<RXEN) | (1<<TXEN) | (0<<UCSZ2) | (0<<RXB8) | (0<<TXB8);
UCSRC=(1<<URSEL) | (0<<UMSEL) | (0<<UPM1) | (0<<UPM0) | (0<<USBS) | (1<<UCSZ1) | (1<<UCSZ0) | (0<<UCPOL);
UBRRH=0x00;
UBRRL=0x33;
	USART_TransmitString("dsgfgsdg\r\n");

#endif


// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

// ADC initialization
// ADC disabled
ADCSRA=0x00;

// SPI initialization
// SPI disabled
SPCR=0x00;

// TWI initialization
// TWI disabled
TWCR=0x00;

	
//	PORTD|=(1<<4);
/*MSCS_init();
unsigned char transmit_buf[17],resiv_buf[17];
unsigned int fractional;
float fractional_part;*/
asm("sei");	


	while(1)
	{
		/*transmit_buf[12]='g';
		transmit_buf[13]='e';
		transmit_buf[14]='r';
		transmit_buf[15]='b';
		transmit_buf[0]=OCR1AH;
		transmit_buf[1]=OCR1AL;
		transmit_buf[2]=koof;
		fractional_part=(koof-transmit_buf[2])*1000.0;
		fractional=fractional_part;
		transmit_buf[3]=fractional/100;
		transmit_buf[4]=fractional%100;
		MSCS_com(transmit_buf,0,resiv_buf);
		*/
 if (!( (1 << PB4) & PINB))
 		{
 	TCCR2=0x02;
 	TCNT2=TF;
 	OCR2=PTF;		
 	_delay_ms(400);
 	TCCR2=0x00;
	 PORTD&=~(1<<4);
 	_delay_ms(1000);
	}
		//USART_Transmit(USART_Receive());
		

	}
}