各位大虾,看一下偶的串口通信程序呢,哪边不对啊,多谢指导啊!!
ATmega16与PC串口通信的程序,哪边有错啊,我刚用C编程序。#include <iom16v.h>
#include <macros.h>
//#define fosc 8000000
//#define baud 9600
#define BUFF_SIZE64
unsigned char tmpSend;
unsigned char tempRecv;
unsigned char ll_buffer;//设置数据缓冲区
unsigned char input=0;//缓冲区读指针
unsigned char output=0;//缓冲区写指针
void UART_init (void);
void UART_rx (void);
void UART_tx (void);
//缓冲区使用软件模拟FIFO
void put_llbuff(unsigned char c )//送数据到缓冲区
{
ll_buffer=c;
if(input<(BUFF_SIZE-1))
input++;
else
input=0;
}
unsigned char get_char(void)//从缓冲区取数据
{
unsigned char temp;
if(output==input)
return 0;
else
{
temp=ll_buffer;
if(output<(BUFF_SIZE-1))
output++;
else
output=0;
return temp;
}
}
//用中断方式处理UART收发
#pragma interrupt_handler UART_rx: iv_USART_RX
void UART_rx(void)//UART接收中断
{ tempRecv = UDR;//UDR收到数据
}
#pragma interrupt_handler UART_tx: iv_USART_TX
void UART_tx(void)//UART发送完成中断
{
UDR = tmpSend;//数据传送
}
//UART初始化
void UART_init(void)
{
UCSRB = BIT(RXCIE)| BIT(RXEN) |BIT(TXEN);//允许串口发送和接收,并响应接收完成中断
UBRR = 51;//设置串口波特率为9600
//UBRRL = (fosc/16/(baud+1))%256;
//UBRRH = (fosc/16/(baud+1))/256;
UCSRC = BIT(URSEL)|BIT(UCSZ1)|BIT(UCSZ0);//帧结构为8位数据+1位stop位
}
void main(void)
{
DDRB= 0B11111111;//初始化端口
PORTB = 0B00000000;
DDRC= 0B11111111;
PORTB = 0B00000000;
DDRD= 0B11111111;
PORTB = 0B00000000;
UART_init();
put_llbuff(55);
UART_rx();//发送55,以便示波器检测
UART_tx();
get_char();//接收数据
} 程序没有形成循环呀 {:victory:}
页:
[1]