Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define F_CPU 1000000UL // 1MHz clock speed
- /* Define CPU clock Frequency e.g. here its 8MHz */
- #include <avr/io.h>
- #include <string.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include <util/delay.h> /* Include AVR std. library file */
- #include <avr/interrupt.h>
- #define BAUD_PRESCALE ((F_CPU / (BAUDRATE * 16UL)) - 1) /* Define prescale value */
- void USART_Init(unsigned long BAUDRATE) /* USART initialize function */
- {
- UCSRA = 0b00000000;
- //UCSRB |= (1 << RXEN) | (1 << TXEN) | (1 << RXCIE); /* Enable USART transmitter and receiver and Interrupt */
- UCSRB |= (1 << RXEN) | (1 << TXEN) ; /* Enable USART transmitter and receiver */
- UCSRC |= (1 << URSEL)| (1 << UCSZ0) | (1 << UCSZ1); /* Write USCRC for 8 bit data and 1 stop bit */
- UBRRL = BAUD_PRESCALE; /* Load UBRRL with lower 8 bit of prescale value */
- UBRRH = (BAUD_PRESCALE >> 8); /* Load UBRRH with upper 8 bit of prescale value */
- //UBRRL = 0;
- //UBRRH = 12; // Assuming 1 Mhz Clock
- }
- void UART_init(){
- UCSRA = 0x02 ;
- UCSRB = 0x18 ;
- UCSRC = 0x86 ;
- UBRRL = 12 ;
- UBRRH = 0 ;
- }
- void UART_send(unsigned char data){
- while((UCSRA&(1<<UDRE))==0) ;
- UDR = data ;
- }
- char USART_RxChar() /* Data receiving function */
- {
- while (!(UCSRA & (1 << RXC))); /* Wait until new data receive */
- return(UDR); /* Get and return received data */
- }
- void USART_TxChar(char data) /* Data transmitting function */
- {
- UDR = data; /* Write data to be transmitting in UDR */
- while (!(UCSRA & (1<<UDRE))); /* Wait until data transmit and buffer get empty */
- }
- int main(void)
- {
- DDRA = 0xFF;
- PORTA |= (1<<PORTA7);
- // USART_Init(9600); /* initialize USART with 9600 baud rate */
- //sei();
- UART_init() ;
- unsigned char data ;
- while (1)
- {
- //data = USART_RxChar();
- while((UCSRA&(1<<RXC))==0) ;
- data = UDR ;
- if(data=='1'){
- PORTA |= (1<<PORTA0);
- UART_send(data) ;
- }
- else if(data=='0'){
- PORTA |= (1<<PORTA1);
- UART_send(data) ;
- }
- else{
- UART_send(data) ;
- }
- PORTA = data;
- //_delay_ms(1000);
- //PORTA= 0x00;
- //_delay_ms(300);
- //PORTA |= (1<<PORTA7);
- }
- }
- ISR (USART_RXC_vect)
- {
- uint8_t oldsrg = SREG;
- cli();
- char received_char = UDR;
- PORTA = received_char;
- _delay_ms(600);
- PORTA= 0x00;
- _delay_ms(50);
- PORTA= 0xFF;
- _delay_ms(100);
- PORTA= 0x00;
- _delay_ms(100);
- SREG = oldsrg;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement