Programming: heading views : 0

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. 



Share this page with your friends
share via Whatsapp

Posted By :
Mahesh Nigam
(Scientist)
2019-09-27 17:13
See Author's other Published Topics

Airtel cheating ther customers by incomplete description of recharge pack of Big companies like Airtel, also cheating their customers by providing incomplete or confusing description of their recharge plans
Big companies like Airtel, also cheating their customers by providing incomplete or confusing description of their recharge plans
साहस और धैर्य का प्रतिक कारगिल विजय दिवस  of साहस और धैर्य का प्रतिक कारगिल विजय दिवस
साहस और धैर्य का प्रतिक कारगिल विजय दिवस
Vaisakhi  of Vaisakhi
Vaisakhi
3 मार्च - विश्व वन्यजीव दिवस of 3 मार्च - विश्व वन्यजीव दिवस
3 मार्च - विश्व वन्यजीव दिवस
1 मार्च- विश्व प्रशंसा दिवस of 1 मार्च- विश्व प्रशंसा दिवस
1 मार्च- विश्व प्रशंसा दिवस
1 मार्च- विश्व नागरिक सुरक्षा दिवस of 1 मार्च- विश्व नागरिक सुरक्षा दिवस
1 मार्च- विश्व नागरिक सुरक्षा दिवस
1 मार्च - शून्य भेदभाव दिवस of 1 march शून्य भेदभाव दिवस
1 march शून्य भेदभाव दिवस
करती रही of पूरा नही बस थोड़ा सा ही समझा होता ....
पूरा नही बस थोड़ा सा ही समझा होता ....
वैलेंटाइन of वैलेंटाइन डे मनाने के पीछे एक रोचक तथ्य व 7 से 14 तक वैलेंटाइन सेलिब्रेट करने की सूची
वैलेंटाइन डे मनाने के पीछे एक रोचक तथ्य व 7 से 14 तक वैलेंटाइन सेलिब्रेट करने की सूची
National Test Agency of UGC NET December 2023 results and download its subject wise cut-off
UGC NET December 2023 results and download its subject wise cut-off
परीक्षा पे चर्चा  of परीक्षा पे चर्चा युवाओं के लिए तनाव मुक्त माहौल बनाने के लिए प्रधान मंत्री नरेंद्र मोदी के नेतृत्व में बड़े आंदोलन - 'एग्जाम वॉरियर्स' का हिस्सा है।
परीक्षा पे चर्चा युवाओं के लिए तनाव मुक्त माहौल बनाने के लिए प्रधान मंत्री नरेंद्र मोदी के नेतृत्व में बड़े आंदोलन - 'एग्जाम वॉरियर्स' का हिस्सा है।

Peoples