Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <avr/io.h>
- #include <avr/interrupt.h>
- #define F_CPU 16000000UL
- #define BAUD 9600
- #define BDIV (F_CPU/16/BAUD)-1
- #define tByte unsigned char
- #define tWord unsigned int
- #define LED0 PB0
- #define LED1 PB1
- #define BUF_SIZE 30 // Buffer size
- volatile tByte buf[BUF_SIZE]; // Buffer
- volatile tByte head=0; // Put
- volatile tByte tail=0; // Get
- tByte fBufIn(tByte d) { // Put data into buffer
- if (((head+1)%BUF_SIZE)==tail) return 0xFF;
- buf[head]=d;
- head = ++head % BUF_SIZE;
- UCSR0B |= (1<<UDRIE0); // Data empty intr enabled
- return 0;
- }
- tByte fBufOut(void) { // Get data from buffer
- tByte d;
- if (head==tail) return 0xFF;
- d=buf[tail];
- tail = ++tail % BUF_SIZE;
- return d;
- }
- void fUART_Init(void) {
- //- Set baud rate
- UBRR0H = (tByte)(BDIV>>8);
- UBRR0L = (tByte)BDIV;
- //- Transmitter & receiver enabled
- UCSR0B = (1<<RXEN0)|(1<<TXEN0);
- //- UART, 1 stop, 8-bit data, non-parity
- UCSR0C = (3<<UCSZ00);
- UCSR0B |= (1<<RXCIE0); // Rx intr enabled
- }
- void fPrintf(tByte *s){
- int i=0;
- while (s[i] != "\0"){
- fBufIn(s[i]);
- i++;
- }
- }
- int main(void) {
- fUART_Init();
- DDRB |= (1<<LED1)|(1<<LED0);
- PORTB |= (1<<LED1)|(0<<LED0);
- sei();
- fPrintf("hell");
- while (1) {}
- }
- ISR(USART_UDRE_vect) { // Data empty's ISR
- tByte dat;
- dat=fBufOut(); // Get data from buf
- if (dat!=0xFF) { // Check valid data
- UDR0 = dat; // Transmit data
- } else UCSR0B &= ~(1<<UDRIE0); // Data empty intr disabled
- PORTB ^= (1<<LED1)|(1<<LED0);
- }
- ISR(USART_RX_vect) { // Rx complete's ISR
- tByte dat;
- dat = UDR0; // Data received
- fBufIn(dat); // Put data into buf
- PORTB ^= (1<<LED1)|(1<<LED0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement