/**********************************************************************
* 文件名称: main.c
* 程序作者: kidcao1987
* 程序版别: V1.0
* 功用描绘: 接上串口,翻开串口调试帮手。在上面循环显现:
“当时电压值: 伏
http://bbs.cepark.com
天上掉下一个大南瓜!!!
作者:kidcao1987 QQ:289718970
”
* 编译器:WinAVR-20090313
* 芯片:ATmega16,外部11.0592MHZ晶振
* 技术支持:http://bbs.cepark.com
**********************************************************************/
#include
#include
#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
void ADCInit(void)
{
ADMUX = (1<
void USARTInit(void) //初始化
{
UCSRA = 0X00;
UCSRC |= (1<
UBRRH = (11059200 / BAUD / 16 – 1) / 256;
UCSRB |= (1 << RXCIE) | (1 << RXEN) | (1 << TXEN); //接纳中止使能,接纳使能,发送使能
}
void USARTTransmitData(unsigned char ucTransmitData)
{
while(!(UCSRA & (1<
UDR = ucTransmitData; //发送
}
void USARTTransmitString(const unsigned char* pStr)
{
while(*pStr != )
{
USARTTransmitData(*pStr++);
}
USARTTransmitData(0x0d);
USARTTransmitData(0x0a);
}
void USARTTransmitString1(const unsigned char* pStr)
{
while(*pStr != )
{
USARTTransmitData(*pStr++);
}
}
视频地址: