共3条
1/1 1 跳转至页
TAMEGA,128,UART1 一个TAMEGA 128 UART1 问题 (急,急)
问
我用ATMEGA 128 的UART1做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: 发送可以为何接收不行。 我已经将发送调试通过了,但接收不行,只要将接收中断函数一加入整个程序都无法运行,发送也不行了,这是为什么???????
请大侠指点。
//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: 发送可以为何接收不行。 我已经将发送调试通过了,但接收不行,只要将接收中断函数一加入整个程序都无法运行,发送也不行了,这是为什么???????
请大侠指点。
共3条
1/1 1 跳转至页
回复
有奖活动 | |
---|---|
【有奖活动】分享技术经验,兑换京东卡 | |
话不多说,快进群! | |
请大声喊出:我要开发板! | |
【有奖活动】EEPW网站征稿正在进行时,欢迎踊跃投稿啦 | |
奖!发布技术笔记,技术评测贴换取您心仪的礼品 | |
打赏了!打赏了!打赏了! |
打赏帖 | |
---|---|
vscode+cmake搭建雅特力AT32L021开发环境被打赏30分 | |
【换取逻辑分析仪】自制底板并驱动ArduinoNanoRP2040ConnectLCD扩展板被打赏47分 | |
【分享评测,赢取加热台】RISC-V GCC 内嵌汇编使用被打赏38分 | |
【换取逻辑分析仪】-基于ADI单片机MAX78000的简易MP3音乐播放器被打赏48分 | |
我想要一部加热台+树莓派PICO驱动AHT10被打赏38分 | |
【换取逻辑分析仪】-硬件SPI驱动OLED屏幕被打赏36分 | |
换逻辑分析仪+上下拉与多路选择器被打赏29分 | |
Let'sdo第3期任务合集被打赏50分 | |
换逻辑分析仪+Verilog三态门被打赏27分 | |
换逻辑分析仪+Verilog多输出门被打赏24分 |