Em có đoạn code dùng để dùng để test 2 cai cổng nối tiếp của con ATmega 128 nhưng khi demo(dung proteus) thi không thấy cổng F hiện ra gi cả. Cấ bác chỉ giùm em chỗ sai với
Đoạn code của em như sau:
#define FCLK 4000000
#define BAUD 9600
#define UBRR ((long) FCLK/(16*BAUD)-1)
#define RXB8 1
#define TXB8 0
#define UPE 2
#define OVR 3
#define FE 4
#define UDRE 5
#define RXC 7
#define FRAMING_ERROR (1<<FE)
#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<OVR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)
#include<mega128.h>
#include <stdio.h>
void put(int c){
while ((UCSR0A & DATA_REGISTER_EMPTY)==0);
UDR0=c;
}
int get(){
while ((UCSR0A & RX_COMPLETE) == 0) {};
return UDR1;
}
void main ()
{
int i,j;
int temp,temp1;
DDRF=0xFF;
DDRB=0x00;
PORTB=0x00;
UCSR0A=0x00;
UCSR0B=0x08;
UCSR0C=0x06;
UBRR0H=UBRR >> 8;
UBRR0L=UBRR & 0xFF;
UCSR1A=0x00;
UCSR1B=0x08;
UCSR1C=0x06;
UBRR1H=UBRR >> 8;
UBRR1L=UBRR & 0xFF;
temp1=0x00;
while(1){
temp=PINB;
put(temp);
temp1=get();
for(i=0;i<300;i++)for(j=0;j<300;j++);
PORTF=temp1+0x09;
for(i=0;i<300;i++)for(j=0;j<300;j++);
}
}
Đoạn code của em như sau:
#define FCLK 4000000
#define BAUD 9600
#define UBRR ((long) FCLK/(16*BAUD)-1)
#define RXB8 1
#define TXB8 0
#define UPE 2
#define OVR 3
#define FE 4
#define UDRE 5
#define RXC 7
#define FRAMING_ERROR (1<<FE)
#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<OVR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)
#include<mega128.h>
#include <stdio.h>
void put(int c){
while ((UCSR0A & DATA_REGISTER_EMPTY)==0);
UDR0=c;
}
int get(){
while ((UCSR0A & RX_COMPLETE) == 0) {};
return UDR1;
}
void main ()
{
int i,j;
int temp,temp1;
DDRF=0xFF;
DDRB=0x00;
PORTB=0x00;
UCSR0A=0x00;
UCSR0B=0x08;
UCSR0C=0x06;
UBRR0H=UBRR >> 8;
UBRR0L=UBRR & 0xFF;
UCSR1A=0x00;
UCSR1B=0x08;
UCSR1C=0x06;
UBRR1H=UBRR >> 8;
UBRR1L=UBRR & 0xFF;
temp1=0x00;
while(1){
temp=PINB;
put(temp);
temp1=get();
for(i=0;i<300;i++)for(j=0;j<300;j++);
PORTF=temp1+0x09;
for(i=0;i<300;i++)for(j=0;j<300;j++);
}
}
Comment