parent
d1bbac33f9
commit
1c08382936
@ -1,13 +1,13 @@
|
||||
#ifndef _UART_H
|
||||
#define _UART_H
|
||||
#ifndef _UART_H_
|
||||
#define _UART_H_
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#define BAUD 9600
|
||||
#define MYUBRR F_CPU/16/BAUD-1
|
||||
#define BAUDRATE ((F_CPU)/(BAUD*16UL)-1)
|
||||
|
||||
void USART_Init(unsigned int ubrr);
|
||||
void USART_Transmit(unsigned char data );
|
||||
void uart_puts( char* string);
|
||||
int putchar(int c);
|
||||
void search_i2c_devices(void);
|
||||
void uart_init (void);
|
||||
void read_sync(char buffer[], uint8_t buffersize, uint8_t * bufferindex);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
@ -1,87 +1,45 @@
|
||||
#include <avr/io.h>
|
||||
#include "uart.h"
|
||||
|
||||
static int uart_putchar(char c, FILE *stream);
|
||||
|
||||
FILE uart_output = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE);
|
||||
|
||||
void USART_Init(unsigned int ubrr)
|
||||
void uart_init ()
|
||||
{
|
||||
/*Set baud rate */
|
||||
UBRR0H = ubrr>>8;
|
||||
UBRR0L = ubrr;
|
||||
/*Enable receiver and transmitter */
|
||||
UCSR0B = (1<<TXEN0);
|
||||
//| (1<<RXEN0)
|
||||
//|(1<<RXCIE0);
|
||||
/* Set frame format: 8data, 1 stop bit */
|
||||
UCSR0C = (1<<UCSZ00) | (1 << UCSZ01);
|
||||
}
|
||||
UBRRH = (BAUDRATE>>8); // shift the register right by 8 bits
|
||||
UBRRL = BAUDRATE; // set baud rate
|
||||
UCSRB|= (1<<TXEN)|(1<<RXEN)|(1<<RXCIE); // enable receiver and transmitter
|
||||
UCSRC|= (1<<URSEL)|(1<<UCSZ0)|(1<<UCSZ1); // 8bit data format
|
||||
|
||||
void USART_Transmit(unsigned char data )
|
||||
{
|
||||
/* Wait for empty transmit buffer */
|
||||
while ( !( UCSR0A & (1<<UDRE0)) )
|
||||
;
|
||||
/* Put data into buffer, sends the data */
|
||||
UDR0 = data;
|
||||
stdout = &uart_output;
|
||||
}
|
||||
|
||||
void uart_puts(char * str) {
|
||||
while (*str) {
|
||||
putchar(*str++);
|
||||
static int uart_putchar(char c, FILE *stream) {
|
||||
if (c == '\n') {
|
||||
uart_putchar('\r', stream);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int putchar(int c){
|
||||
USART_Transmit((char) c);
|
||||
//while ( !( UCSR0A & (1<<UDRE0)) )
|
||||
//UDR0 = c;
|
||||
loop_until_bit_is_set(UCSRA, UDRE);
|
||||
UDR = c;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
void search_i2c_devices(){
|
||||
char error, address;
|
||||
int nDevices;
|
||||
|
||||
uart_puts("Scanning...");
|
||||
|
||||
|
||||
nDevices = 0;
|
||||
for (address = 1; address < 127; address++ )
|
||||
{
|
||||
uart_puts("\n");
|
||||
uart_puts("Scanning...");
|
||||
// The i2c_scanner uses the return value of
|
||||
// the Write.endTransmisstion to see if
|
||||
// a device did acknowledge to the address.
|
||||
i2c_start_wait(address);
|
||||
error = i2c_readNak();
|
||||
|
||||
if (error == 0)
|
||||
{
|
||||
uart_puts("I2C device found at address 0x");
|
||||
if (address < 16)
|
||||
uart_puts("0");
|
||||
uart_puts(address);
|
||||
uart_puts(" !");
|
||||
|
||||
nDevices++;
|
||||
void read_sync(char buffer[], uint8_t buffersize, uint8_t * bufferindex){
|
||||
for(int i=0; i < buffersize; i++){
|
||||
buffer[i]=0;
|
||||
}
|
||||
else if (error == 4)
|
||||
{
|
||||
uart_puts("Unknown error at address 0x");
|
||||
if (address < 16)
|
||||
uart_puts("0");
|
||||
uart_puts(address);
|
||||
}
|
||||
}
|
||||
if (nDevices == 0)
|
||||
uart_puts("No I2C devices found\n");
|
||||
else
|
||||
uart_puts("done\n");
|
||||
*bufferindex=0;
|
||||
|
||||
// delay(); // wait 5 seconds for next scan
|
||||
char input;
|
||||
do{
|
||||
while ((UCSRA & (1 << RXC)) == 0);
|
||||
input = UDR;
|
||||
|
||||
putchar(input); //echo
|
||||
buffer[*bufferindex]=input;
|
||||
*bufferindex=*bufferindex+1;
|
||||
}while(!(input == '\n' || input == '\r'));
|
||||
buffer[*bufferindex]=0;
|
||||
putchar('\n');
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
Loading…
Reference in New Issue