原创 Cepark电子园 AVR M16实验之十一 片内AD

2010-3-31 15:20 2281 4 4 分类: EDA/ IP/ 设计与制造

/**********************************************************************
* 文件名称: main.c
* 程序作者: kidcao1987
* 程序版本: V1.0
* 功能描述: 接上串口,打开串口调试助手。在上面循环显示:
“当前电压值: 伏
http://bbs.cepark.com
天上掉下一个大南瓜!!!
作者:kidcao1987 QQ:289718970

* 编译器:WinAVR-20090313
* 芯片:ATmega16,外部11.0592MHZ晶振
* 技术支持:http://bbs.cepark.com
**********************************************************************/
#include <avr/io.h>
#include <util/delay.h>

#define BAUD 9600

unsigned int uiResult; //转换结果
float fVoltage; //
unsigned char const ucStr1[]={"当前电压值:"};
unsigned char const ucStr2[]={"http://bbs.cepark.com"};
unsigned char const ucStr3[]={"天上掉下一个大南瓜!!!"};
unsigned char const ucStr4[]={"作者:kidcao1987 QQ:289718970"};
unsigned char const ucStr5[]={"伏"};


unsigned int GetADCData(void); //获取ADC的数值
void ADCInit(void); //ADC初始化


void USARTInit(void); //初始化
void USARTTransmitData(unsigned char); //发送数据
void USARTTransmitString(unsigned char const *pStr); //发送字符串
void USARTTransmitString1(unsigned char const *pStr); //发送字符串


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

PORTA = 0x00;
DDRA = 0xFE;//设置为输出,PA0为输入端,设置为输入,高阻态

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

while(1)
{
uiResult = GetADCData();
fVoltage = (float)(uiResult)/1024*500;
uiResult = (unsigned int)(fVoltage); //电压值转换成显示值

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 |= (1<<ADSC); //开始转换
while(!(ADCSRA & (1<<ADIF))); //等待转换结束
ADCSRA |= (1<<ADIF); //清零标志位

return ADC;
}

void ADCInit(void)
{
ADMUX = (1<<REFS0) ;//参考电源AVCC,ADC1为转换通道
ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1); //使能ADC,64分频,时钟115.2KHZ,7.3728MHZ晶振
}


void USARTInit(void) //初始化
{
UCSRA = 0X00;
UCSRC |= (1<<URSEL) | (1 << UCSZ1) | (1 << UCSZ0); //异步通信,8位数据,1位停止位,无校验
UBRRL = (11059200 / BAUD / 16 - 1) % 256; //波特率设置为9600
UBRRH = (11059200 / BAUD / 16 - 1) / 256;
UCSRB |= (1 << RXCIE) | (1 << RXEN) | (1 << TXEN); //接收中断使能,接收使能,发送使能
}




void USARTTransmitData(unsigned char ucTransmitData)
{
while(!(UCSRA & (1<<UDRE)));//等待发送缓冲器为空

UDR = ucTransmitData; //发送
}



void USARTTransmitString(const unsigned char* pStr)
{
while(*pStr != '\0')
{
USARTTransmitData(*pStr++);
}
USARTTransmitData(0x0d);
USARTTransmitData(0x0a);
}



void USARTTransmitString1(const unsigned char* pStr)
{
while(*pStr != '\0')
{
USARTTransmitData(*pStr++);
}

}

视频地址:

http://v.youku.com/v_show/id_XMTYxNTE2NTA4.html

https://static.assets-stash.eet-china.com/album/old-resources/2010/3/31/5d697c01-c7a0-4ec4-a4e5-7ac100364335.zip


PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
EE直播间
更多
我要评论
0
4
关闭 站长推荐上一条 /3 下一条