السلام عليكم هذا كود مصمم لل ATmega8A
ياليت احد يقدر يحوله على اي نوع من pic بالميكروسي وجزاكم الله خير
كود:
************* USART initialization function definition ***********
void USART_Init ( uint32_t baud )
{
/ / Calculate UBRR asynchronous mode ( U2X = 0)
uint16_t _ubr = ( F_CPU/16/baud-1 ) ;
/ / Set speed
UBRRH = ( uint8_t ) ( _ubr >> 8 ) ;
UBRRL = ( uint8_t ) _ubr ;
/ / Switch on the transmitter and receiver
UCSRB = (1 << RXEN ) | (1 << TXEN ) ;
/ / Set frame format : 8 data bits, 1 stop bit
UCSRC = (1 << URSEL ) | (3 << UCSZ0 ) ;
}
/ / ************** Definition broadcasting function *********************
void USART_Transmit (unsigned int date)
{
/ / Wait until transmit buffer is empty
while (! ( UCSRA & (1 << UDRE ))) ;
/ / Throw data to the transmit buffer , transmission start
UDR = data ;
}
/ / ******* interruption of completing the processing of the ADC ********************
SIGNAL ( SIG_ADC )
{
/ / calc . the voltage at the input
result = (float ) ( ADCL | ( ADCH << 8) ) / 1024 * 128 ;
counter + + ; / / increase the counter
PORTD & = ~ _BV ( PB2 ) ;
if ( counter == 200 ) / / every 200 measurements
{
sensor = result ;
USART_Transmit (sensor ) ;
if ( sensor > 25 )
{
PORTD & = ~ _BV ( PB3 ) ;
}
if ( sensor < 10 )
{
PORTD | = _BV ( PB3 ) ;
}
counter = 0; / / reset the counter
}
PORTD | = _BV ( PB2 ) ;
}
/ / ********************* ADC INITIALIZATION ************************* **
init_adc void (void )
{
/ / ADEN - activating the ADC, ADIE - Enable interrupt
/ / ADFR - Free Runing (measuring all the time ) , ADSC - start measuring
/ / ADPSX - set prescaler
ADCSRA = _BV ( ADEN ) | _BV ( ADIE ) | _BV ( ADFR ) | _BV ( ADSC ) | _BV ( ADPS0 ) | _BV ( ADPS1 ) | _BV ( ADPS2 ) ;
}
/ / ********************* FUNCTION MAIN ************************* **
int main ( void)
{
counter = 0 ;
/ / Initialize the UART module
USART_Init (19200 ) ;
DDRD = 0xff ;
init_adc () / / activate the ADC
sei (); / / release the global interrupt
for (; ;) / / infinite loop
{
}
return 0;
}