新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > AVR M16實(shí)驗(yàn)之十一 片內(nèi)AD

AVR M16實(shí)驗(yàn)之十一 片內(nèi)AD

作者: 時(shí)間:2016-11-25 來(lái)源:網(wǎng)絡(luò) 收藏
/**********************************************************************
* 文件名稱: main.c
* 程序作者: kidcao1987
* 程序版本: V1.0
* 功能描述: 接上串口,打開(kāi)串口調(diào)試助手。在上面循環(huán)顯示:
“當(dāng)前電壓值: 伏
http://bbs.cepark.com
天上掉下一個(gè)大南瓜?。?!
作者:kidcao1987 QQ:289718970

* 編譯器:WinAVR-20090313
* 芯片:ATmega16,外部11.0592MHZ晶振
* 技術(shù)支持:http://bbs.cepark.com
**********************************************************************/
#include
#include

#define BAUD 9600

unsigned int uiResult; //轉(zhuǎn)換結(jié)果
float fVoltage; //
unsigned char const ucStr1[]={"當(dāng)前電壓值:"};
unsigned char const ucStr2[]={"http://bbs.cepark.com"};
unsigned char const ucStr3[]={"天上掉下一個(gè)大南瓜!??!"};
unsigned char const ucStr4[]={"作者:kidcao1987 QQ:289718970"};
unsigned char const ucStr5[]={"伏"};


unsigned int GetADCData(void); //獲取ADC的數(shù)值
void ADCInit(void); //ADC初始化


void USARTInit(void); //初始化
void USARTTransmitData(unsigned char); //發(fā)送數(shù)據(jù)
void USARTTransmitString(unsigned char const *pStr); //發(fā)送字符串
void USARTTransmitString1(unsigned char const *pStr); //發(fā)送字符串


int main(void)
{
PORTD = 0x03;
DDRD = 0x02; //PD0輸入 PD1輸出

PORTA = 0x00;
DDRA = 0xFE;//設(shè)置為輸出,PA0為輸入端,設(shè)置為輸入,高阻態(tài)

USARTInit(); //串口初始化
ADCInit();

while(1)
{
uiResult = GetADCData();
fVoltage = (float)(uiResult)/1024*500;
uiResult = (unsigned int)(fVoltage); //電壓值轉(zhuǎn)換成顯示值

USARTTransmitString1(ucStr1);
USARTTransmitData((unsigned char)(uiResult/100) + 48);
USARTTransmitData(.);
USARTTransmitData((unsigned char)(uiResult%100/10) + 48);
USARTTransmitData((unsigned char)(uiResult%10) + 48);
USARTTransmitString1(ucStr5);
USARTTransmitData(0x0d);
USARTTransmitData(0x0a); //回車換行
USARTTransmitString(ucStr2);
USARTTransmitString(ucStr3);
USARTTransmitString(ucStr4);
USARTTransmitData(0x0d);
USARTTransmitData(0x0a); //回車換行
USARTTransmitData(0x0d);
USARTTransmitData(0x0a); //回車換行
_delay_ms(1000);
}
}

unsigned int GetADCData(void)
{
ADCSRA |= (1return ADC;
}

void ADCInit(void)
{
ADMUX = (1<ADCSRA = (1<}


void USARTInit(void) //初始化
{
UCSRA = 0X00;
UCSRC |= (1<UBRRL = (11059200 / BAUD / 16 - 1) % 256; //波特率設(shè)置為9600
UBRRH = (11059200 / BAUD / 16 - 1) / 256;
UCSRB |= (1 << RXCIE) | (1 << RXEN) | (1 << TXEN); //接收中斷使能,接收使能,發(fā)送使能
}




void USARTTransmitData(unsigned char ucTransmitData)
{
while(!(UCSRA & (1<
UDR = ucTransmitData; //發(fā)送
}



void USARTTransmitString(const unsigned char* pStr)
{
while(*pStr !=