Important :
- The Manchester receive routines are blocking calls
(
Man_Receive_Init
andMan_Synchro
). This means that MCU will wait until the task has been performed (e.g. byte is received, synchronization achieved, etc). - Manchester code library implements time-based activities, so interrupts need to be disabled when using it.
External dependencies of Manchester Code Library
The following variables must be defined in all projects using Manchester Code Library: | Description: | Example: |
---|---|---|
extern sfr sbit MANRXPIN; |
Receive line. | sbit MANRXPIN at PINB0_bit; |
extern sfr sbit MANTXPIN; |
Transmit line. | sbit MANTXPIN at PORTB1_bit; |
extern sfr sbit MANRXPIN_Direction; |
Direction of the Receive pin. | sbit MANRXPIN_Direction at
DDB0_bit; |
extern sfr sbit MANTXPIN_Direction; |
Direction of the Transmit pin. | sbit MANTXPIN_Direction at
DDB1_bit; |
Library Routines
- Man_Receive_Init
- Man_Receive
- Man_Send_Init
- Man_Send
- Man_Synchro
- Man_Break
- Manchester_0
- Manchester_1
- Manchester_Out
Man_Receive_Init
Prototype | unsigned int Man_Receive_Init(); |
---|---|
Returns |
|
Description |
The function configures Receiver pin and performs synchronization procedure
in order to retrieve baud rate out of the incoming signal.
Note : In case of multiple
persistent errors on reception, the user should call this routine once again or
Man_Synchro routine to enable synchronization.
|
Requires | Global variables :
|
Example | // Initialize Receiver sbit MANRXPIN at PORTB0_bit; sbit MANRXPIN_Direction at DDB0_bit; ... Man_Receive_Init(); |
Man_Receive
Prototype | unsigned char Man_Receive(unsigned char *error); |
---|---|
Returns | A byte read from the incoming signal. |
Description |
The function extracts one byte from incoming signal. Parameters :
|
Requires | To use this function, the user must prepare the MCU for receiving. |
Example | unsigned char data = 0, error = 0; ... data = Man_Receive(&error); if (error) { /* error handling */ } |
Man_Send_Init
Prototype | void Man_Send_Init(); |
---|---|
Returns | Nothing. |
Description | The function configures Transmitter pin. |
Requires | Global variables :
|
Example | // Initialize Transmitter: sbit MANTXPIN at PORTB1_bit; sbit MANTXPIN_Direction at DDB1_bit; ... Man_Send_Init(); |
Man_Send
Prototype | void Man_Send(unsigned char tr_data); |
---|---|
Returns | Nothing. |
Description |
Sends one byte. Parameters :
Note : Baud rate used is 500 bps.
|
Requires | To use this function, the user must prepare the MCU for sending. |
Example | unsigned char msg; ... Man_Send(msg); |
Man_Synchro
Prototype | unsigned char Man_Synchro(); |
---|---|
Returns |
|
Description | Measures half of the manchester bit length with 10us resolution. |
Requires | To use this function, you must first prepare the MCU for receiving. |
Example | unsigned int man__half_bit_len; ... man__half_bit_len = Man_Synchro(); |
Man_Break
Prototype | void Man_Break(); |
---|---|
Returns | Nothing. |
Description |
Man_Receive is blocking routine and it can block
the program flow. Call this routine from interrupt to unblock the program
execution. This mechanism is similar to WDT.
Note : Interrupts should be disabled
before using Manchester routines again (see note at the top of this page).
|
Requires | Nothing. |
Example | char data1, error, counter = 0;
void Timer0Overflow_ISR() org 0x12 {
if (counter >= 20) {
Man_Break();
counter = 0; // reset counter
}
else
counter++; // increment counter
}
void main() {
TOIE0_bit = 1; // Timer0 overflow interrupt enable
TCCR0_bit = 5; // Start timer with 1024 prescaler
SREG_I_bit = 0; // Interrupt disable
...
Man_Receive_Init();
...
// try Man_Receive with blocking prevention mechanism
SREG_I_bit = 1; // Interrupt enable
data1 = Man_Receive(&error);
SREG_I_bit = 0; // Interrupt disable
...
}
|
Code Example
The following code is code for the Manchester receiver, it shows how to use the Manchester Library for receiving data:The following code is code for the Manchester transmitter, it shows how to use the Manchester Library for transmitting data:// LCD module connectionssbit LCD_RS at PORTD2_bit;sbit LCD_EN at PORTD3_bit;sbit LCD_D4 at PORTD4_bit;sbit LCD_D5 at PORTD5_bit;sbit LCD_D6 at PORTD6_bit;sbit LCD_D7 at PORTD7_bit;sbit LCD_RS_Direction at DDD2_bit;sbit LCD_EN_Direction at DDD3_bit;sbit LCD_D4_Direction at DDD4_bit;sbit LCD_D5_Direction at DDD5_bit;sbit LCD_D6_Direction at DDD6_bit;sbit LCD_D7_Direction at DDD7_bit;// End LCD module connections// Manchester module connectionssbit MANRXPIN at PINB0_bit;sbit MANRXPIN_Direction at DDB0_bit;sbit MANTXPIN at PORTB1_bit;sbit MANTXPIN_Direction at DDB1_bit;// End Manchester module connectionschar error, ErrorCount, temp;void main() {ErrorCount = 0;Lcd_Init(); // Initialize LCDLcd_Cmd(_LCD_CLEAR); // Clear LCD displayMan_Receive_Init(); // Initialize Receiverwhile (1) { // Endless loopLcd_Cmd(_LCD_FIRST_ROW); // Move cursor to the 1st rowwhile (1) { // Wait for the "start" bytetemp = Man_Receive(&error); // Attempt byte receiveif (temp == 0x0B) // "Start" byte, see Transmitter examplebreak; // We got the starting sequenceif (error) // Exit so we do not loop foreverbreak;}do {temp = Man_Receive(&error); // Attempt byte receiveif (error) { // If error occuredLcd_Chr_CP('?'); // Write question mark on LCDErrorCount++; // Update error counterif (ErrorCount > 20) { // In case of multiple errorstemp = Man_Synchro(); // Try to synchronize again//Man_Receive_Init(); // Alternative, try to Initialize Receiver againErrorCount = 0; // Reset error counter}}else { // No error occuredif (temp != 0x0E) // If "End" byte was received(see Transmitter example)Lcd_Chr_CP(temp); // do not write received byte on LCD}Delay_ms(25);}while (temp != 0x0E) ; // If "End" byte was received exit do loop}}
// Manchester module connectionssbit MANRXPIN at PINB0_bit;sbit MANRXPIN_Direction at DDB0_bit;sbit MANTXPIN at PORTB1_bit;sbit MANTXPIN_Direction at DDB1_bit;// End Manchester module connectionschar index, character;char s1[] = "mikroElektronika";void main() {Man_Send_Init(); // Initialize transmitterwhile (1) { // Endless loopMan_Send(0x0B); // Send "start" byteDelay_ms(100); // Wait for a whilecharacter = s1[0]; // Take first char from stringindex = 0; // Initialize index variablewhile (character) { // String ends with zeroMan_Send(character); // Send characterDelay_ms(90); // Wait for a whileindex++; // Increment index variablecharacter = s1[index]; // Take next char from string}Man_Send(0x0E); // Send "end" byteDelay_ms(1000);}}
No comments:
Post a Comment