代码;
hardware.c
#include "hardware.h"
#include
void IO_init()
{
}
void UART_init()
{
unsigned int UBRRREG;
UBRRREG = F_CPU / ( 8 * BAUDRATE ) - 1;
UBRRH = UBRRREG / 256;
UBRRL = UBRRREG % 256;
UCSRA = ( 1 << U2X );
UCSRB = ( 1 << TXEN );
UCSRC = (1 << URSEL ) | ( 1 << UCSZ1 ) | ( 1 << UCSZ0 );
}
void UART_puts( char *s )
{
while( *s )
{
UART_write( *s );
s++;
while( !( UCSRA & (1 << TXC )) );
UCSRA |= ( 1 << TXC );
}
}
void MCU_init()
{
}
void ADC_init()
{
ADMUX = 0;
ADCSRA = (1 << ADEN)|(1 << ADPS1)|(1 << ADPS0);
}
unsigned int getADC(unsigned char chn)
{
ADMUX = (chn % 16);
ADCSRA = (1 << ADEN)|(1 << ADPS1)|(1 << ADPS0)|(1 << ADSC);
while(ADCSRA & (1 << ADSC));
return ADC;
}
main.c
#include "hardware.h"
#include
#include
char s[30];
unsigned int Vd;
int main()
{
UART_init();
ADC_init();
for(;;)
{
_delay_ms(500);
Vd = getADC(chnTMP);
sprintf(s, "ADC = %d, Vcc = %ld\n\r", Vd, 1024L*VBG/Vd);
UART_puts(s);
}
return 0;
}
仿真效果图:
文章评论(0条评论)
登录后参与讨论