|
发表于 2005-4-8 15:08:24
|
显示全部楼层
今天上午来学习马老师的这个程序
发现了一个问题
我的程序为
//ICC-AVR application builder : 2005-4-8 9:28:47
// Target : M128
// Crystal: 16.000Mhz
#include <iom128v.h>
#include <macros.h>
#define _Crystal_ 8000
void port_init(void)
{
PORTA = 0x00;
DDRA = 0x00;
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
PORTE = 0x00;
DDRE = 0x00;
PORTF = 0x00;
DDRF = 0x00;
PORTG = 0x00;
DDRG = 0x00;
}
//UART1 initialize
// desired baud rate:9600
// actual baud rate:9615 (0.2%)
// char size: 8 bit
// parity: Disabled
void uart1_init(void)
{
UCSR1B = 0x00; //disable while setting baud rate
UCSR1A = 0x00;
UCSR1C = 0x06;
UBRR1L = 0x67; //set baud rate lo
UBRR1H = 0x00; //set baud rate hi
UCSR1B = 0xD8;
}
char rx_buffer1[RX_BUFFER_SIZE1];
unsigned char rx_wr_index1,rx_rd_index1,rx_counter1;
//This flag is set on USART1 Receiver buffer overflow
unsigned char rx_buffer_overflow1;
#pragma interrupt_handler uart1_rx_isr:31
void uart1_rx_isr(void)
{
//uart has received a character in UDR
char status,data;
status=UCSR1A;
data=UDR1;
if((status&((1<<FE1)|(1<<DOR1)|(1<<UPE1)))==0)
{
rx_buffer1[rx_rd_index1]=data;
if(++rx_wr_index1==RX_BUFFER_SIZE1) rx_wr_index1=0;
if(++rx_counter1==RX_BUFFER_SIZE1)
{
rx_counter1=0;
rx_buffer_overflow1=1;
}
}
}
char Uart1_Getchar(void)
{
char data;
while(rx_counter1==0);
data=rx_buffer1[rx_rd_index1];
if((++rx_rd_index1==RX_BUFFER_SIZE1))
rx_rd_index1=0;
CLI();
--rx_counter1;
SEI();
return data;
}
char tx_buffer1[TX_BUFFER_SIZE1];
unsigned char tx_wr_index1,tx_rd_index1,tx_counter1;
#pragma interrupt_handler uart1_tx_isr:33
void uart1_tx_isr(void)
{
//character has been transmitted
if(tx_counter1)
{
--tx_counter1;
UDR1=tx_buffer1[tx_rd_index1];
if(++tx_rd_index1==TX_BUFFER_SIZE1)
tx_rd_index1=0;
}
}
void Uart1_Putchar(unsigned char data)
{
while(tx_counter1==TX_BUFFER_SIZE1);
CLI();
if((tx_counter1||((UCSR1A&(1<<UDRE1))==0)))
{
tx_buffer1[tx_wr_index1]=data;
if(++tx_wr_index1==TX_BUFFER_SIZE1)
{
tx_wr_index1=0;
++tx_counter1;
}
}
else
UDR1=data;
SEI();
}
void Uart1_Sendstring(unsigned char *string)
{
while(*string)
Uart1_Putchar(*string++);
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
XDIV = 0x00; //xtal divider
XMCRA = 0x40; //external memory
port_init();
uart1_init();
MCUCR = 0x80;
EICRA = 0x00; //extended ext ints
EICRB = 0x00; //extended ext ints
EIMSK = 0x00;
TIMSK = 0x00; //timer interrupt sources
ETIMSK = 0x00; //extended timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
void Delay(unsigned char time)
{
unsigned int i;
for(; time > 0; time--)
for(i = 0; i < (_Crystal_>>3); i++) // about one millisecond
_NOP();
}
void main(void)
{
unsigned char data;
SPH = 0x0F;
SPL = 0xFF;
init_devices();
while(1)
{
Uart1_Putchar('a');
Uart1_Sendstring("asdf");
Delay(50);
data=Uart1_Getchar();
Uart1_Putchar(data);
}
}
几乎是一样的
我加了一个字符串的输出
问题就出在这一条上Uart1_Sendstring("asdf");
程序执行时,字符串输出不全,有时好象这条语句被跳过一样
void Uart1_Sendstring(unsigned char *string)
{
while(*string)
Uart1_Putchar(*string++);
}
这个是子程序
我用轮训方法的时候这样用过的
哪位帮忙看一下 |
|