avr128 串口1的程序,晶振16M,主要功能是ADC采集的电压值变化就用串口将其输出,串口助手收不到预期结果
#define F_CPU 16000000UL#include <avr/io.h>
#include <util/delay.h>
#define uchar unsigned char
#define uint unsigned int
unsigned char SendBuff[10];
uint PRE_ADC_ResultX = 0,PRE_ADC_ResultY = 0;
void Init_USART1(void)
{
UCSR1B = 0X00;
UCSR1A = 0x00;
UCSR1C = 0X06;//8位数据位,1位停止位
UBRR1L = 103 % 256;//波特率:9600
UBRR1H = 103 / 256;
//UBRR = 103 对应波特率为9600
UCSR1B = _BV(RXEN1) | _BV(TXEN1);//允许接收和发送
//{ | _BV(RXCIE0);}//{允许接收中断}
DDRD |= 0X80;
}
void putchar1(uchar c)
{
if(c == '\n') putchar1('\r');
while(!(UCSR1A & 0X20));
UDR1 = c;
}
void ADC_Init(void)
{
DDRF = 0X00;PORTF = 0X00;//配置AD转换端口
ADCSRA = 0XE6;
//ADC转换置位,启动转换,连续转化模式,关闭中断,128分频
}
uint ADC_Convert(uint CH)
{
uint Result;
ADMUX = CH;// ADC通道选择
//读取转换结果,并转换为电压
Result = (uint)(ADC*500.0/1023.0);//读取转换结果
SendBuff[CH*3+0] = Result / 100 + '0';
SendBuff[CH*3+1] = Result % 100 / 10 + '0';
SendBuff[CH*3+2] = Result % 100 % 10 + '0';
return Result;
}
void KEYSCAN()
{
uint x,y;
char *Show_x = "X轴坐标:\n";
char *Show_y = "Y轴坐标:\n";
x = ADC_Convert(0);
_delay_ms(100);
y = ADC_Convert(1);
_delay_ms(100);
if(x != PRE_ADC_ResultX || y != PRE_ADC_ResultY)//如果模数转换结果发生变化
{
unsigned char i = 0,j=0;
PRE_ADC_ResultX = x;
PRE_ADC_ResultY = y;
while(Show_x != '\0')
putchar1(Show_x[i++]);
for(i = 0;i < 3;i++)
putchar1(SendBuff);
putchar1('\n');
while(Show_y[j] != '\0')
putchar1(Show_y[j++]);
for(j = 3;j < 6;j++)
putchar1(SendBuff[j]);
putchar1('\n');
}
}
int main()
{
char *s = "AVR128 MCU TEST:\n";
uchar t = 0;
Init_USART1();
ADC_Init();
while(s[t] != '\0')
putchar1(s[t++]);
while(1)
{
KEYSCAN();
}
}
熔丝位设置如图,求大神帮忙看看,万分感激
|