qhtony 发表于 2005-6-17 13:00:29

我想问问大家达人关于uart(RS232)通信协议编程中要注意的地方。

最近在用mega64的uart0端口于sim100模块的通信中发生了些问题。我用iccavr自动生成程序,然后自己按照mega64的说明书上加了1段发送和1段接受程序,但是在单步调试的过程中发现总是不能正常的进入发送程序。

如果能有程序示例给我看下最好,谢谢。

qhtony 发表于 2005-6-21 17:58:40

//ICC-AVR application builder : 2005-6-21 9:31:40

// Target : M64

// Crystal: 8.0000Mhz



#include <iom64v.h>

#include <macros.h>

#define FRAMING_ERROR (1<<FE1)

#define PARITY_ERROR (1<<UPE1)

#define DATA_OVERRUN (1<<DOR1)

#define DATA_REGISTER_EMPTY (1<<UDRE1)

#define RX_COMPLETE (1<<RXC1)

#define UART_BEGIN_STX      0xBB

#define UART_END_STX      0xEE

#define RX_BUFFER_SIZE1      8

#pragma interrupt_handler uart1_rx_isr:iv_USART1_RXC

void delay_nus(unsigned int n);

void delay_nms(unsigned int n);

void port_init(void);

void uart1_init(void);

void uart1_rx_isr(void);

void init_devices(void);







// USART1 Receiver buffer



char rx_buffer1;

unsigned char rx_counter;

// This flag is set on USART0 Receiver buffer overflow

int Uart_RecvFlag;



void port_init(void)

{

PORTA = 0xFF;

DDRA= 0x00;

PORTB = 0xFF;

DDRB= 0x00;

PORTC = 0xFF; //m103 output only

DDRC= 0x00;

PORTD = 0xFF;

DDRD= 0x00;

PORTE = 0xFF;

DDRE= 0x00;

PORTF = 0xFF;

DDRF= 0x00;

PORTG = 0x1F;

DDRG= 0x00;

}



//UART0 initialisation

// 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 = 0x33; //set baud rate lo

UBRR1H = 0x00; //set baud rate hi

UCSR1B = 0x90;

}





void uart1_rx_isr(void)

{

char status,data;



//uart has received a character in UDR

status=UCSR1A;

data=UDR1;

if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)

   {

       if (!Uart_RecvFlag)

    {   

       rx_buffer1 = data;

    switch (rx_counter)

    {

            case 0:

      if (data == UART_BEGIN_STX)   rx_counter = 1;

      break;

       case 1:

       case 2:

       case 3:

       case 4:

      rx_counter++;

      break;

       case 5:

      rx_counter = 0;

      if (data == UART_END_STX)Uart_RecvFlag = 1;

      break;

    }

   }



}

   else

    rx_counter = 0;



}





//call this routine to initialise all peripherals

void init_devices(void)

{

//stop errant interrupts until set up

CLI(); //disable all interrupts

XDIV= 0x00; //xtal divider

XMCRA = 0x00; //external memory

port_init();

uart1_init();



MCUCR = 0x00;

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

}



void main(void)

{

init_devices();

DDRF=0x07;

PORTF=0x00;

while(1)

{

    if (Uart_RecvFlag)

    {

         PORTF |= BIT(PF0);

         delay_nms(1000);

         PORTF &= ~BIT(PF0);

         delay_nms(1000);//处理收到的数据包

         Uart_RecvFlag = 0;      //允许USART接受新的数据包

   }

          //处理其它任务

}

SEI();

}

void delay_nus(unsigned int n)//n微秒延时函数

{

unsigned int i;

for (i=0;i<n;i++)

{

      asm("nop");

}

}



void delay_nms(unsigned int n)

{

unsigned int i;

for(i=0;i<n;i++)

{

delay_nus(1000);

}

}





上面这段是我自己编的一段程序,但是似乎就是进不了uart1的中断程序,想问问大家大概是哪里有问题?
页: [1]
查看完整版本: 我想问问大家达人关于uart(RS232)通信协议编程中要注意的地方。