MORE RELATED

GROOMIX INFO
Blogs & Articles
MENU
. . .

programming

views : 162

Here is the code to interface 2 serial devices with AVR

There is two method by which we can interface 2 serial devices with AVR microcontroller. One is by polling method and the other is interrupt based. To create baud rate we can use delay method or a timer interrupt method. Copy and paste code to interface 2 serial devices with AVR microcontroller is given below-

Interfacing 2 serial devices using polling method

//START -set baud rate for 1200bps
unsigned char min_bit_length = 83;
void send_byte(char send_byte){
    char i;
    //start bit
    output_port4 &=~(1<<utx);
    for(i=0; i<min_bit_length; i++){_delay_us(10);}//1 bit delay
   
 //1 bit
    if(send_byte & 0b00000001){output_port4 |= (1<<utx);} else{output_port4 &=~(1<<utx);}
    for(i=0; i<min_bit_length; i++){_delay_us(10);}//1 bit delay
//2 bit
    if(send_byte & 0b00000010){output_port4 |= (1<<utx);} else{output_port4 &=~(1<<utx);}
    for(i=0; i<min_bit_length; i++){_delay_us(10);}//1 bit delay
//3 bit
    if(send_byte & 0b00000100){output_port4 |= (1<<utx);} else{output_port4 &=~(1<<utx);}
    for(i=0; i<min_bit_length; i++){_delay_us(10);}//1 bit delay
//4 bit
    if(send_byte & 0b00001000){output_port4 |= (1<<utx);} else{output_port4 &=~(1<<utx);}
    for(i=0; i<min_bit_length; i++){_delay_us(10);}//1 bit delay
//5 bit
    if(send_byte & 0b00010000){output_port4 |= (1<<utx);} else{output_port4 &=~(1<<utx);}
    for(i=0; i<min_bit_length; i++){_delay_us(10);}//1 bit delay
//6 bit
    if(send_byte & 0b00100000){output_port4 |= (1<<utx);} else{output_port4 &=~(1<<utx);}
    for(i=0; i<min_bit_length; i++){_delay_us(10);}//1 bit delay
//7 bit
    if(send_byte & 0b01000000){output_port4 |= (1<<utx);} else{output_port4 &=~(1<<utx);}
    for(i=0; i<min_bit_length; i++){_delay_us(10);}//1 bit delay
//8 bit
    if(send_byte & 0b10000000){output_port4 |= (1<<utx);} else{output_port4 &=~(1<<utx);}
    for(i=0; i<min_bit_length; i++){_delay_us(10);}//1 bit delay
//stop bit
    output_port4 |= (1<<utx);
    for(i=0; i<min_bit_length; i++){_delay_us(10);}//1 bit delay
}
void send_string(char send_string[]){
    unsigned char i=0;      & amp; nbsp;               ; ;     // iterating variable
    unsigned char LENGHT = 0;// length of the word
    for(i=0;i<500; i++){
      & amp; nbsp; if(send_string[i] == NULL){
      & amp; nbsp;     break;
      & amp; nbsp; }
      & amp; nbsp; LENGHT++;
    }
    i=0;
    while(i<LENGHT){send_byte(send_s tring[i]);i++;}
}
//END
#define output_ddr DDRD
#define output_port4 PORTD
//in main()
//initialize this port
output_ddr |= (1<<utx);//as output port
output_port4 |= (1<<utx);//pull up because in idle time uart tx line is high

while(1 ){//main while
send_string("testing transmitter of software uart");// this will send out and you can see it on pc terminal
_delay_ms(1000);   
    }

Above code is made for avr microcontroller and its crystal 12Mhz. for any doubt, ask me in comment below.

Interfacing 2 serial devices using interrupt method

////////////////// ///////////////////////////////////////// /////////////////////////////
////////////////////////////// START SOFTWARE UART /////////////////////////////////////
///////////////////////////////////// ///////////////////////////////////////// //////////
unsigned char baud_delay_one=216;//this variable are only used for tune baudrate
unsigned char baud_delay_one_and_half=184;//this variable are only used for tune baudrate
char bit_count=0;
char uart_rx_byte=0;
char uart_status=0;
#define idle 0
#define transmitting 1
#define receiving 2
#define received_data_pending 3
#define start_bit 4
void init_timer0(){         //TIMER0 initialize for software uart
    TIMSK |= (1<<TOIE0);    //ENABLE 0-TIMER0 OVERFLOW INTERRUPT
    TCCR0 = (0x04);         //NORMAL MODE,INTERNAL CLK,PRESCALER=1:256
    TCNT0=baud_delay_one;        //1 bit length for 9600bps (104us) at 12Mhz crystal
    sei();                    // Interrupt Enabled GLOBALLY
}
ISR(TIMER0_OVF_vect) {
    unsigned char sreg = SREG;
    cli();                        // Disable Global interrupt
    TCNT0=baud_delay_one;                    //1 bit length for 9600bps (104us) at 12Mhz crystal
    bit_count++;
    SREG = sreg;
}
void uart_send_byte(char send_byte){
    TCNT0=baud_delay_one;
    bit_count=0;
    while(1){
        if(bit_count==0){output_port4 &=~(1<<utx);}
        if((bit_count>0) && (bit_count<9)){
            if(send_byte & (1<<(bit_count-1))){output_port4 |= (1<<utx);} else{output_port4 &=~(1<<utx);}
        }
        if(bit_count==9){output_port4 |= (1<<utx);}
        if(bit_count>9){break;}
    }
}

void uart_sent_string(char send_string[]){
    unsigned char i=0;                         // iterating variable
    unsigned char LENGHT = 0;// length of the word
    for(i=0;i<500; i++){
        if(send_string[i] == ''){
            break;
        }
        LENGHT++;
    }
    i=0;
    while(i<LENGHT){uart_send_byte(s end_string[i]);i++;}
}

#define output_ddr DDRD
#define output_port4 PORTD
#define utx 3
//in main()
//initialize this port


init_timer0();//this to create baud rate by overflow interrupt
output_ddr |= (1<<utx);//as output port
output_port4 |= (1<<utx);//pull up because in idle time uart tx line is high

while(1 ){//main while
uart_sent_string("testing transmitter of software uart");// this will send out and you can see it on pc terminal
_delay_ms(1000);   
    }
///////////////////////////////////// ///////////////////////////////////////// //////////
//////////////////////////////// END SOFTWARE UART /////////////////////////////////////
///////////////////////////////////// ///////////////////////////////////////// //////////

Above code is made for avr microcontroller and its crystal 12Mhz. for any doubt, ask me in comment below. 

Posted By :
Mr. Unique

2017-08-16/00:16:49

Share this Page

Comments...

More :programming

programming
Arduino UNO tachometer coding with variable resistor and DC motor speed control
views :183

p

programming
Here is the code to interface 2 serial devices with AVR
views :162

p

programming
Software Uart for AVR microcontroller tested on 1200 baud rate
views :611

p

programming
ATMEL STUDIO-While loop conditioning issues
views :229

p

programming
ATMEL STUDIO-While loop is not working using interrupt incremented conditioning
views :293

p

programming
Copy and paste code for facebook share button in your websites
views :352
programming
How to program atmel 8051 family microcontroller? (step by step)
views :244
programming
Embedded program code in C language for Digital Voltmeter for AVR microcontroller
views :315
programming
Assemby program code for Automatic Door Opening System (8051 family microcontroller)
views :228
programming
Very simple and accurate Seven Segment Display program Code in Embedded C language
views :260

p

programming
Highway vehicle speed detector or checker Embedded C language code in MikroC AVR Framework also applicable for 8051 Microcontroller
views :152

p

programming
Assembly program for accurate 1 second delay function
views :182
programming
What is Embedded system programming?
views :157

Latest Updates

kya transformer dc pe work kr skta h..?
views :0

kya transformer dc pe work kr skta h..?
views :137

kya transformer dc pe work kr skta h..?
views :0
Breaking News
Best internet Recharge Plans than jio..!
views :214
Engineering Project Ideas
Vehicle Theft Protection Using Gsm
views :191
Engineering Project Ideas
DTMF Controlled Robot or Mobile Controlled Robot
views :138
Engineering Project Ideas
Digital Clock With Home Appliances Control Using Microcontroller
views :100
Engineering Project Ideas
Cooking Monitor
views :195
Engineering Project Ideas
Boundary Security System(Wireless)
views :145
Engineering Project Ideas
Automatic Garage Parking System
views :138

---More To Do---

This pages is developed to give users a genuine web materials of all types throughout the world. Many articles, by experts are publishing here, every hours. You can publish your topics as well as you can comment on all posted topics and learn more. Many types of topics are published here like Technology based ( Electronics, Electricals, Mechanicals, civil engineering, computer science, programming and software development, Assemply language codings), Health like medicine, healthy diets, Homeopathy etc) and many types of latest news updates.


All content, design, and layout are Copyright © 2016 - Digital Photography Review All Rights Reserved. Reproduction in whole or part in any form or medium without specific written permission is prohibited.

MORE TO DO