bác nào co kinh nghiệm về module thu phát này thì xin giúp em với.em loay hoay 2 ngày rồi mà vẫn không chạy được.
đây là code được viết trong mikroC , nguồn từ 4room nay:
http://www.mikroe.com/forum/viewtopi...23831&start=15
code bên phát:
// Manchester module connections
//sbit MANRXPIN at RC0_bit;
//sbit MANRXPIN_Direction at TRISC0_bit;
sbit MANTXPIN at RC1_bit;
sbit MANTXPIN_Direction at TRISC1_bit;
// End Manchester module connections
char index, character;
char s1[] = "This is the test Data from Master Tx unit";
char noise = 0;
void main() {
ANSEL = 0; // Configure AN pins as digital I/O
ANSELH = 0;
C1ON_bit = 0; // Disable comparators
C2ON_bit = 0;
TRISC.F2 = 0;
TRISC.F3 = 0;
PORTC.F2 = 0;
PORTC.F3 = 0;
Man_Send_Init(); // Initialize transmitter
while (1) { // Endless loop
for (noise = 0; noise <= 4; noise++){
Man_Send(0x80);
Delay_ms(100);
Man_Send(0xCA);
Delay_ms(100);
}
PORTC.F2 = 1; // Start byte sent indicator
Man_Send(0xC3); // Send "start" byte
Delay_ms(500); // Wait for a while
character = s1[0]; // Take first char from string
index = 0; // Initialize index variable
while (character) { // String ends with zero
PORTC.F3 = 1; // LCD data sending indicator
Man_Send(character); // Send character
Delay_ms(90); // Wait for a while
index++; // Increment index variable
character = s1[index]; // Take next char from string
}
Man_Send(0xCF); // Send "end" byte
PORTC.F2 = 0; // Start byte sent indicator
PORTC.F3 = 0; // LCD data sending indicator
Delay_ms(2000);
}
}
Đây là code bên nhận:
// Manchester module connections Start
sbit MANRXPIN at RC0_bit;
sbit MANRXPIN_Direction at TRISC0_bit;
sbit MANTXPIN at RC1_bit;
sbit MANTXPIN_Direction at TRISC1_bit;
// End Manchester module connections End
unsigned char error = 0, ErrorCount = 0, temp = 0;
void main() {
ANSEL = 0; // Configure AN pins as digital I/O
ANSELH = 0;
C1ON_bit = 0; // Disable comparators
C2ON_bit = 0;
PORTC.F2 = 0; // Clear Start bit found indicator
PORTC.F3 = 0; // Clear Update UART indicator
TRISC.F2 = 0; // Set as output used for Start bit found indicator
TRISC.F3 = 0; // Set as output used for Update UART indicator
PORTC.F5 = 0;
TRISC.F5 = 0;
UART1_Init(19200);
Man_Receive_Init(); // Initialize Receiver
do{ // Rx Routine forever loop
GET_BYTE:
temp = Man_Receive(&error); // Attempt byte receive
if (error) goto ERROR_BYTE; // if comm's error goto error handler
goto START_BYTE; // if good byte Received goto start handler
ERROR_BYTE:
PORTC.F2 = 0; // Clear Start bit found indicator
PORTC.F3 = 0; // Clear Update UART indicator
UART1_Write_Text("ErrorByte count = "); // Write error and count to UART
ErrorCount++; // Update error counter
UART1_Write(ErrorCount+48);
UART1_Write_Text("\n\r");
if(ErrorCount > 5) goto RE_SYNC;
goto GET_BYTE;
START_BYTE:
if (temp == 0xC3){ // "Start" byte value expected is 0xC3
PORTC.F2 = 1; // Set Start bit found indicator
// a valid Start Byte has been found
goto DISPLAY_BYTE; // goto the Display routine
}// Exit if
goto NOISE_BYTE; // Valid data byte found is not a start byte
// go to noise reporting loop
DISPLAY_BYTE:
if(temp == 0xCF) goto STOP_BYTE; // Valid Stop Byte found Exit
UART1_Write_Text("Valid Data Found\n\r"); // Send Activity to UART
while (1){ // loop to collect all valid data
temp = Man_Receive(&error); // Attempt byte receive
if (error) goto ERROR_BYTE; // if comm's error goto error handler
if (temp == 0xCF)break; // Stop condition found Exit while loop
PORTC.F3 = 1; // Set Update UART indicator
UART1_Write(temp); // write received byte on UART
}
UART1_Write_Text("\n\r"); // new line + Carriage Return
PORTC.F2 = 0; // Clear Start bit found indicator
PORTC.F3 = 0; // Clear Update UART indicator
goto STOP_BYTE; // Exit UART write loop
NOISE_BYTE:
UART1_Write_Text("Synchronized But Data Found is Noise = "); // Send Activity to UART
UART1_Write(temp);
UART1_Write_Text("\n\r");
goto GET_BYTE; // Exit noise detect loop
RE_SYNC:
UART1_Write_Text("Synchronizing\n\r"); // Display Activity on 2nd Row
delay_ms(50);
temp = Man_Synchro(); // Try to synchronize again
//Man_Receive_Init(); // Alternative, try to Initialize Receiver again
ErrorCount = 0; // Reset error counter
if (temp == 0xFF) goto RE_SYNC; // Sync failed try again
goto GET_BYTE; // Sync complete start over again
STOP_BYTE:
if (temp == 0xCF){ // If "End" byte was received
UART1_Write_Text("Valid Stop Found\n\r"); // Send Activity to UART
delay_ms(100);
}
goto GET_BYTE; // start over again
}while(1); // Rx Routine
} // main
code viết rất dễ hiểu.nhung không hiếu sao em chỉ nhận được dữ liệu đúng trong lần phát đầu tiên.cho dù em phát 1000 kí tự đi nữa tjì trong lần phát đầu tiên em cung nhận đúng 1000 kí tự,nhưng tói lần phát 2 thì không đúng nữa.
và em không hiểu đoạn code này để làm gì
for (noise = 0; noise <= 4; noise++){
Man_Send(0x80);
Delay_ms(100);
Man_Send(0xCA);
Delay_ms(100);
}
xin giúp em.thanks
đây là code được viết trong mikroC , nguồn từ 4room nay:
http://www.mikroe.com/forum/viewtopi...23831&start=15
code bên phát:
// Manchester module connections
//sbit MANRXPIN at RC0_bit;
//sbit MANRXPIN_Direction at TRISC0_bit;
sbit MANTXPIN at RC1_bit;
sbit MANTXPIN_Direction at TRISC1_bit;
// End Manchester module connections
char index, character;
char s1[] = "This is the test Data from Master Tx unit";
char noise = 0;
void main() {
ANSEL = 0; // Configure AN pins as digital I/O
ANSELH = 0;
C1ON_bit = 0; // Disable comparators
C2ON_bit = 0;
TRISC.F2 = 0;
TRISC.F3 = 0;
PORTC.F2 = 0;
PORTC.F3 = 0;
Man_Send_Init(); // Initialize transmitter
while (1) { // Endless loop
for (noise = 0; noise <= 4; noise++){
Man_Send(0x80);
Delay_ms(100);
Man_Send(0xCA);
Delay_ms(100);
}
PORTC.F2 = 1; // Start byte sent indicator
Man_Send(0xC3); // Send "start" byte
Delay_ms(500); // Wait for a while
character = s1[0]; // Take first char from string
index = 0; // Initialize index variable
while (character) { // String ends with zero
PORTC.F3 = 1; // LCD data sending indicator
Man_Send(character); // Send character
Delay_ms(90); // Wait for a while
index++; // Increment index variable
character = s1[index]; // Take next char from string
}
Man_Send(0xCF); // Send "end" byte
PORTC.F2 = 0; // Start byte sent indicator
PORTC.F3 = 0; // LCD data sending indicator
Delay_ms(2000);
}
}
Đây là code bên nhận:
// Manchester module connections Start
sbit MANRXPIN at RC0_bit;
sbit MANRXPIN_Direction at TRISC0_bit;
sbit MANTXPIN at RC1_bit;
sbit MANTXPIN_Direction at TRISC1_bit;
// End Manchester module connections End
unsigned char error = 0, ErrorCount = 0, temp = 0;
void main() {
ANSEL = 0; // Configure AN pins as digital I/O
ANSELH = 0;
C1ON_bit = 0; // Disable comparators
C2ON_bit = 0;
PORTC.F2 = 0; // Clear Start bit found indicator
PORTC.F3 = 0; // Clear Update UART indicator
TRISC.F2 = 0; // Set as output used for Start bit found indicator
TRISC.F3 = 0; // Set as output used for Update UART indicator
PORTC.F5 = 0;
TRISC.F5 = 0;
UART1_Init(19200);
Man_Receive_Init(); // Initialize Receiver
do{ // Rx Routine forever loop
GET_BYTE:
temp = Man_Receive(&error); // Attempt byte receive
if (error) goto ERROR_BYTE; // if comm's error goto error handler
goto START_BYTE; // if good byte Received goto start handler
ERROR_BYTE:
PORTC.F2 = 0; // Clear Start bit found indicator
PORTC.F3 = 0; // Clear Update UART indicator
UART1_Write_Text("ErrorByte count = "); // Write error and count to UART
ErrorCount++; // Update error counter
UART1_Write(ErrorCount+48);
UART1_Write_Text("\n\r");
if(ErrorCount > 5) goto RE_SYNC;
goto GET_BYTE;
START_BYTE:
if (temp == 0xC3){ // "Start" byte value expected is 0xC3
PORTC.F2 = 1; // Set Start bit found indicator
// a valid Start Byte has been found
goto DISPLAY_BYTE; // goto the Display routine
}// Exit if
goto NOISE_BYTE; // Valid data byte found is not a start byte
// go to noise reporting loop
DISPLAY_BYTE:
if(temp == 0xCF) goto STOP_BYTE; // Valid Stop Byte found Exit
UART1_Write_Text("Valid Data Found\n\r"); // Send Activity to UART
while (1){ // loop to collect all valid data
temp = Man_Receive(&error); // Attempt byte receive
if (error) goto ERROR_BYTE; // if comm's error goto error handler
if (temp == 0xCF)break; // Stop condition found Exit while loop
PORTC.F3 = 1; // Set Update UART indicator
UART1_Write(temp); // write received byte on UART
}
UART1_Write_Text("\n\r"); // new line + Carriage Return
PORTC.F2 = 0; // Clear Start bit found indicator
PORTC.F3 = 0; // Clear Update UART indicator
goto STOP_BYTE; // Exit UART write loop
NOISE_BYTE:
UART1_Write_Text("Synchronized But Data Found is Noise = "); // Send Activity to UART
UART1_Write(temp);
UART1_Write_Text("\n\r");
goto GET_BYTE; // Exit noise detect loop
RE_SYNC:
UART1_Write_Text("Synchronizing\n\r"); // Display Activity on 2nd Row
delay_ms(50);
temp = Man_Synchro(); // Try to synchronize again
//Man_Receive_Init(); // Alternative, try to Initialize Receiver again
ErrorCount = 0; // Reset error counter
if (temp == 0xFF) goto RE_SYNC; // Sync failed try again
goto GET_BYTE; // Sync complete start over again
STOP_BYTE:
if (temp == 0xCF){ // If "End" byte was received
UART1_Write_Text("Valid Stop Found\n\r"); // Send Activity to UART
delay_ms(100);
}
goto GET_BYTE; // start over again
}while(1); // Rx Routine
} // main
code viết rất dễ hiểu.nhung không hiếu sao em chỉ nhận được dữ liệu đúng trong lần phát đầu tiên.cho dù em phát 1000 kí tự đi nữa tjì trong lần phát đầu tiên em cung nhận đúng 1000 kí tự,nhưng tói lần phát 2 thì không đúng nữa.
và em không hiểu đoạn code này để làm gì
for (noise = 0; noise <= 4; noise++){
Man_Send(0x80);
Delay_ms(100);
Man_Send(0xCA);
Delay_ms(100);
}
xin giúp em.thanks
Comment