nichengyu1989 发表于 2013-1-7 19:57:47

用avrstudio5.1编译atmega128的程序遇到问题

今天刚下载了avrstudio5.1,感觉很好用,寄存器什么的都有提示,非常爽,随即写了个打开串口的小程序,USART0打开了,但是只能发送,但是不能接收,收不到数,进不了接收中断,希望各位能帮我指点一下中断服务函数的格式是不是应该向我这样写呢,我的寄存器有没有配对?万分感谢,已经纠结一天了。

#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#define LED0 0
#define LED1 1
#define LED2 2
#define LED3 3
#define fosc 7372800
#define baud 9600
#define UART0_DATA_EMPTY_IRQ USART0_UDRE_vect //! define the UART data buffer ready interrupt vector
#define UART0_RX_IRQ USART0_RX_vect //! define the UART data

void uart0_init(void)
{
        UCSR0B = 0x00;                  // shut down UART00
        UCSR0A = 0x00;                      //asynchronous communication and not use double speed
        UCSR0B=0x98;
        UCSR0C =(1<<UCSZ01)|(1<<UCSZ00);    // 8 data bits
        UBRR0L=(fosc/16/(baud+1))%256;      // set the baudrate
        UBRR0H=(fosc/16/(baud+1))/256;
        UCSR0B=(1<<RXCIE0)|(1<<RXEN0)|(1<<TXEN0);      //enable transmission and receive
}
void putchar0(unsigned char *data)
{
while (!(UCSR0A&(1<<UDRE0)));
       UDR0=data;
}


unsigned char getchar0(void)
{
       
    while(!(UCSR0A& (1<<RXC0)));// 我想问题主要出在这里!!不论我怎样发送,RXC0都不置位!
       
        return UDR0;
                        
}

int main(void)
{
        unsigned char data={0xff,0xff};
   cli();
   uart0_init();
   sei();
   putchar0(data);

}

ISR(USART0_RX_vect) // USART接收中断服务
{
    unsigned char status,*data;      
    status = UCSR0A;
    data = UDR0;
    putchar0(data);
}

pangbin4 发表于 2013-1-7 20:58:17

等高手进入,我没有使用过GCC,

richyhuang 发表于 2013-1-7 22:57:50

没消息循环啊,程序跑飞了吧

yklstudent 发表于 2013-1-7 23:20:01

#include <avr/io.h>

#ifndef F_CPU
#define F_CPU 7372800UL
#endif
#include <util/delay.h>

#include <avr/interrupt.h>
#define LED0 0
#define LED1 1
#define LED2 2
#define LED3 3
#define fosc 7372800
#define baud 9600
#define UART0_DATA_EMPTY_IRQ USART0_UDRE_vect //! define the UART data buffer ready interrupt vector
#define UART0_RX_IRQ USART0_RX_vect //! define the UART data

void uart0_init(void)
{
        UCSR0B = 0x00;                  // shut down UART00
    UCSR0A = 0x00;                      //asynchronous communication and not use double speed
    UCSR0B = 0x98;
    UCSR0C = (1<<UCSZ01)|(1<<UCSZ00);    // 8 data bits
    UBRR0L = (fosc/16/(baud+1))%256;      // set the baudrate
    UBRR0H = (fosc/16/(baud+1))/256;
    UCSR0B = (1<<RXCIE0)|(1<<RXEN0)|(1<<TXEN0);      //enable transmission and receive
}

void putchar0(unsigned char *data)
{
        while(!(UCSR0A&(1<<UDRE0)));
    UDR0 = *data;
}


unsigned char getchar0(void)
{
        while(!(UCSR0A& (1<<RXC0)));// 我想问题主要出在这里!!不论我怎样发送,RXC0都不置位!   
        return UDR0;
}

void PutStr(unsigned char *DataStr,unsigned char DataLen)
{
        unsigned char i;
        for(i=0;i<DataLen;i++)
        {
                putchar0(DataStr+i);
        }
}

int main(void)
{
        unsigned char data = {0xff,0xff};
    cli();
    uart0_init();
    sei();
    PutStr(data,2);
        while(1);
        return (0);
}

ISR(USART0_RX_vect) // USART接收中断服务
{
        unsigned char status;
        volatile unsigned char data;      
    status = UCSR0A;
    data = UDR0;
    putchar0(&data);
}

nichengyu1989 发表于 2013-1-8 09:37:40

yklstudent 发表于 2013-1-7 23:20 static/image/common/back.gif
#include

#ifndef F_CPU


你好,我试了下你给的代码,下到我的板子里的情况还是128能发出来数据,但是收不到,刚下到板子里面试了试
ISR(USART0_RX_vect) // USART接收中断服务
{
       
      unsigned char status;
      volatile unsigned char data;      
    status = UCSR0A;
        led_on(1);
    data = UDR0;
    putchar0(&data);
}
上面这个函数,是不是只要是收到数据就应该进入,总中断开了,接收中断使能了,但是我数据过来没反应。

yklstudent 发表于 2013-1-8 10:34:24

nichengyu1989 发表于 2013-1-8 09:37 static/image/common/back.gif
你好,我试了下你给的代码,下到我的板子里的情况还是128能发出来数据,但是收不到,刚下到板子里面试了 ...

这个程序感觉应该没什么问题吧 硬件会不会有问题呢?

nichengyu1989 发表于 2013-1-8 10:54:47

yklstudent 发表于 2013-1-8 10:34 static/image/common/back.gif
这个程序感觉应该没什么问题吧 硬件会不会有问题呢?

硬件没有问题,熔丝位也没问题,可能是编译器的问题?等我换个IAR试试,您用什么编译器?

yklstudent 发表于 2013-1-8 10:59:01

nichengyu1989 发表于 2013-1-8 10:54 static/image/common/back.gif
硬件没有问题,熔丝位也没问题,可能是编译器的问题?等我换个IAR试试,您用什么编译器? ...

AS4.3 + WIN2006
和AS6.0+WIN2010都没有问题啊
唯独AS5.1没用过 直接跳到AS6.0的

nichengyu1989 发表于 2013-1-8 11:18:32

yklstudent 发表于 2013-1-8 10:59 static/image/common/back.gif
AS4.3 + WIN2006
和AS6.0+WIN2010都没有问题啊
唯独AS5.1没用过 直接跳到AS6.0的

能否留下qq向你请教?

nichengyu1989 发表于 2013-1-8 11:29:28

yklstudent 发表于 2013-1-8 10:59 static/image/common/back.gif
AS4.3 + WIN2006
和AS6.0+WIN2010都没有问题啊
唯独AS5.1没用过 直接跳到AS6.0的

avr5和6的区别是不是就是多了arm那块?av5需要设置么?

yklstudent 发表于 2013-1-8 11:57:21

nichengyu1989 发表于 2013-1-8 11:18 static/image/common/back.gif
能否留下qq向你请教?

这边有规矩的不许私下交流吧 有问题就这面说好了帮助你的人也多点

nichengyu1989 发表于 2013-1-8 13:54:26

yklstudent 发表于 2013-1-8 10:59 static/image/common/back.gif
AS4.3 + WIN2006
和AS6.0+WIN2010都没有问题啊
唯独AS5.1没用过 直接跳到AS6.0的

问题找到了,232芯片坏了,还不是全坏了,坏了一半,换了个芯片就好了,坑死我了,非常感谢yklstudent的帮助,希望能成为朋友,625847049

liangws201 发表于 2013-1-8 14:12:04

232还有坏一边的说法啊
的呀?

cdhua_andy 发表于 2013-3-31 11:34:02

我的定时器中断也进不去,一开定时器初始话后,程序总是复位,也根本不进中断函数。
页: [1]
查看完整版本: 用avrstudio5.1编译atmega128的程序遇到问题