为什么cvavr串口只能接受两位数据
本帖最后由 wensheng18 于 2013-1-4 16:07 编辑请教个问题。我用cvavr编译软件自动生成的程序,只能接受两位数据,我那里出错了吗?琢磨了很久,还没找到为题。在自制的仿真器上仿真只有两位数据进入。。程序如下:
/*****************************************************
This program was produced by the
CodeWizardAVR V1.25.7a Standard
Automatic Program Generator
?Copyright 1998-2007 Pavel Haiduc, HP InfoTech s.r.l.
Project :
Version :
Date : 2012-11-29
Author: F4CG
Company : F4CG
Comments:
Chip type : ATmega16L
Program type : Application
Clock frequency : 8.000000 MHz
Memory model : Small
External SRAM size: 0
Data Stack size : 256
*****************************************************/
#include <mega16.h>
#define uchar unsigned char
#define uintunsigned int
#define RXB8 1
#define TXB8 0
#define UPE 2
#define OVR 3
#define FE 4
#define UDRE 5
#define RXC 7
#define FRAMING_ERROR (1<<FE)
#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<OVR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)
uint num;
bit b,b0;//,b3,b4
bit t0,t1,t2,t3,t4,t5,t6,t7,t8,t9;
uchar time;
//uchar n;
// USART Receiver buffer
#define RX_BUFFER_SIZE 4
char rx_buffer;
#if RX_BUFFER_SIZE<256
unsigned char rx_wr_index,rx_rd_index,rx_counter;
#else
unsigned int rx_wr_index,rx_rd_index,rx_counter;
#endif
// This flag is set on USART Receiver buffer overflow
bit rx_buffer_overflow;
// USART Receiver interrupt service routine
interrupt void usart_rx_isr(void)
{
char status,data;
status=UCSRA;
data=UDR;
if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
{
rx_buffer=data;
if (++rx_wr_index == RX_BUFFER_SIZE) rx_wr_index=0;
if (++rx_counter == RX_BUFFER_SIZE)
{
rx_counter=0;
rx_buffer_overflow=1;
};
};
}
#ifndef _DEBUG_TERMINAL_IO_
// Get a character from the USART Receiver buffer
#define _ALTERNATE_GETCHAR_
#pragma used+
char getchar(void)
{
signed int data;
if (rx_counter == 0)
{
data = -1;
}
else
{
data=rx_buffer; //读取缓冲队列中的数据
if (++rx_rd_index == RX_BUFFER_SIZE) rx_rd_index=0;//读取指针指向下一个未读的数据,如果指到了队列尾部,则指回到队列头步
#asm("cli") // 关中断!非常重要
--rx_counter; //队列中未读数据个数减1。因为该变量在接收中断中要改变的,为了防止冲突,所以改动前临时关闭中断。程序相当可靠了。
#asm("sei") // 开中断
}
return data;
}
#pragma used-
#endif
// USART Transmitter buffer
#define TX_BUFFER_SIZE 8
char tx_buffer;
#if TX_BUFFER_SIZE<256
unsigned char tx_wr_index,tx_rd_index,tx_counter;
#else
unsigned int tx_wr_index,tx_rd_index,tx_counter;
#endif
// USART Transmitter interrupt service routine
interrupt void usart_tx_isr(void)
{
if (tx_counter)
{
--tx_counter;
UDR=tx_buffer;
if (++tx_rd_index == TX_BUFFER_SIZE) tx_rd_index=0;
};
}
#ifndef _DEBUG_TERMINAL_IO_
// Write a character to the USART Transmitter buffer
#define _ALTERNATE_PUTCHAR_
#pragma used+
void putchar(char c)
{
while (tx_counter == TX_BUFFER_SIZE);
#asm("cli")
if (tx_counter || ((UCSRA & DATA_REGISTER_EMPTY)==0))
{
tx_buffer=c;
if (++tx_wr_index == TX_BUFFER_SIZE) tx_wr_index=0;
++tx_counter;
}
else
UDR=c;
#asm("sei")
}
#pragma used-
#endif
// Standard Input/Output functions
#include <stdio.h>
// Timer 0 overflow interrupt service routine
interrupt void timer0_ovf_isr(void)
{
// Reinitialize Timer 0 value
TCNT0=0x83;
// Place your code here
if(1000==num++)
{
PORTD.7=1;
num=0;
}
else if(num==50)
PORTD.7=0;
if(t0) time++; if(t1) time++;
if(t2) time++; if(t3) time++;
if(t4) time++; if(t5) time++;
if(t6) time++; if(t7) time++;
if(t8) time++; if(t9) time++;
}
// Declare your global variables here
void main(void)
{
// Declare your local variables here
// Input/Output Ports initialization
// Port A initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
PORTA=0x00;
DDRA=0xFF;
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=P State5=T State4=P State3=T State2=P State1=T State0=P
PORTB=0x55;
DDRB=0x00;
// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0xC3;
// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x80;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 125.000 kHz
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x03;
TCNT0=0x83;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x01;
// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 9600
UCSRA=0x00;
UCSRB=0xD8;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x33;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
// Global enable interrupts
#asm("sei")
while (1)
{
// Place your code here
if(!PINB.0)
{
t0=PORTC.7=1;
if(time>=10&&!b)
{
t0=time=0;
b=1;
putchar('B');
}
}
else
t0=time=PORTC.7=b=0;
if(!PINB.2)
{
t1=1;
if(time>=10&!b0)
{
t1=time=0;
PORTC.6=b0=1;
putchar('A');
}
}
else
t1=time=PORTC.6=b0=0;
if(rx_buffer==0x01)
if(rx_buffer==0x02)
if(rx_buffer==0x03)
if(rx_buffer==0x04)
if(rx_buffer==0x05)
{
PORTA.0=~PORTA.0;
rx_rd_index=rx_buffer=rx_buffer=rx_buffer=rx_buffer=rx_buffer=0;
}
/* else
{
//rx_rd_index=rx_buffer=rx_buffer=rx_buffer=rx_buffer=rx_buffer=rx_buffer=rx_buffer=rx_buffer=0;
}
*/
/*if(PINA.0)
{
t3=1;
if(time>=10&&!b3)
{
b3=1;
putchar('A');
putchar('0');
putchar('O');
putchar('F');
putchar('F');
b4=0;
}
}
else
{
t3=time=b3=0;
if(!b4)
{
b4=1;
putchar('A');
putchar('0');
putchar('N');
putchar('O');
}
}*/
};
}
程序太长,感觉很乱。
先查清楚是发送端有无问题,确定没问题后再查是否接收端有无接收端到。
是CVAVR编译器向导生成的程序。
页:
[1]