这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 综合技术 » 基础知识 » TAMEGA,128,UART1 一个TAMEGA 128 UART1 问题 (

共3条 1/1 1 跳转至

TAMEGA,128,UART1 一个TAMEGA 128 UART1 问题 (急,急)

院士
2006-09-17 18:14:16     打赏
TAMEGA,128,UART1 一个TAMEGA 128 UART1 问题 (急,急)



关键词: TAMEGA     UART1     一个     问题    

院士
2006-12-22 22:43:00     打赏
2楼
问  我用ATMEGA 128UART1做232串口通信,但无法实现,用AVRSTUDIO 4.0调试发送时数无法送入UDR1,数据接收用中断,源程序如下。



//ICC-AVR application builder : 2003-8-8 22:18:48
// Target : M128
// Crystal: 3.6864Mhz

#include <iom128v.h>
#include <macros.h>
unsigned char rx_buff[10];
unsigned char rx_sign,itemsum = 0;
void port_init(void)
{
PORTA = 0xFF;
DDRA  = 0x00;
PORTB = 0xFF;
DDRB  = 0xF0;
PORTC = 0xFF;
DDRC  = 0x00;
PORTD = 0x00;
DDRD  = 0xFF;
PORTE = 0xFF;
DDRE  = 0x00;
PORTF = 0xFF;
DDRF  = 0x00;
PORTG = 0x1F;
DDRG  = 0x00;
}

//UART1 initialisation
// desired baud rate:9600
// actual baud rate:9600 (0.0%)
// char size: 8 bits
// parity: Disabled
void uart1_init(void)
{

UCSR1B = 0x00; //disable while setting baud rate
UCSR1C = 0x06;
UCSR1A &= 0x20;
UBRR1H = 0x00; //set baud rate hi
UBRR1L = 0x17; //set baud rate lo
UCSR1B = 0x08;
}

//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
//all peripherals are now initialised
}



void send_pc(void)
{
   unsigned char  send_buff[]= "Hellow\n";
   unsigned char  tvi=0;
   while(send_buff[tvi] != '\n')
    {
          UDR1 = send_buff[tvi];
          tvi++;
          while(!(UCSR1A & 0X20));
    }
}



#pragma interrupt_handler uarx1_rx:31
  void uarx1_rx(void)
       {
        if(itemsum < 9)
          {
            rx_buff[itemsum] = UDR0;
            itemsum++;
            }
            else
            {itemsum = 1;
             rx_sign = 1;
             }                                                                                            
        }
        
        
void delay(unsigned char second)
     {
       unsigned int wait1,wait2;
       while(second != 0)
       {
        for(wait1 = 1; wait1 <  20000;wait1++);
        for(wait2 = 1; wait2 <  30000;wait2++);
        second--;
        }
        }
        
        
        
void main(void)
     {

      unsigned char outsum;
      init_devices();
      SEI();
      for(;;)
      {
       send_pc();
       UCSR1B  |= 0x90;
       NOP();
       while(rx_sign != 1);
       rx_sign = 0;
       for(outsum = 1;outsum <= 4;outsum++)
          if(rx_buff[outsum] == 1)
              PORTB |= 1 << (8-outsum);
              else PORTB &= 0 << (8-outsum);
       for(outsum = 5;outsum <= 8;outsum++)
           if(rx_buff[outsum] == 1)
               PORTD |=1 << (12-outsum);
               else PORTD &= 0 <<(12-outsum);
        delay(2);
        }
        }          
        
1: 无UCSR1C = 0x06设的不对,
UCSR1A &= 0x20可以不设
2: 回复赵先生谢谢你的回贴。
   UCSR1C = 0x06设的不对,我是用ASYNCHRONOUS 方式。8位数据,没错啊。
3: MEGA128出厂为MEGA103兼容模式,需要去除。内外晶振要设对 4: 回复 双龙 怎么去掉MEGA103兼容模式?????
   给我一个源程序吧。
  内外晶振要设对。
   我用的是3。6864的晶体。 5: 在熔丝位里去掉MEGA103兼容选项即可! 6: 发送可以为何接收不行。 我已经将发送调试通过了,但接收不行,只要将接收中断函数一加入整个程序都无法运行,发送也不行了,这是为什么???????
  请大侠指点。

专家
2022-08-20 15:42:03     打赏
3楼

感谢分享


共3条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]