Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ps2 and bluetooth communication files added #3

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions Communication/USART_128.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#include "USART_128.h"
void USART_Init( unsigned int baud, uint8_t n )
{
sei();
if(n)
{
//Initialize baud Rate(4800)
UBRR1H=(unsigned char)(baud>>8);
UBRR1L=(unsigned char)baud;
//TX RX Enable
UCSR1B|=(1<<RXEN0)|(1<<TXEN0);
//Set 8-bit data, Parity disabled
UCSR1C |=(3<<UCSZ10);

}else
{
//Initialize baud Rate(9600)
UBRR0H=(unsigned char)(baud>>8);
UBRR0L=(unsigned char)baud;
//TX RX Enable
UCSR0B|=(1<<RXEN0)|(1<<TXEN0);//|(1<<UCSZ02);
//Set 8-bit data, Parity disabled
UCSR0C |= (3<<UCSZ00);
}

}

void USART_InterruptEnable(uint8_t n)
{
if(n)
{
UCSR1B|=(1<<RXCIE1);
}
else UCSR0B|=(1<<RXCIE0);
}

void USART_Transmitchar( unsigned char data, uint8_t n )
{
if(n)
{
while ( !( UCSR1A & (1<<UDRE1)) );
UDR1=data;
}else
{
while ( !( UCSR0A & (1<<UDRE0)) );
UDR0=data;
}
}

unsigned char USART_Receive(uint8_t n )
{
if(n)
{
while (! (UCSR1A & (1 << RXC1)) );
return UDR1;
}else
{
while (! (UCSR0A & (1 << RXC0)) );
return UDR0;
}
}



void USART_TransmitString(char *str, uint8_t n)
{
while(*str>0)
{
USART_Transmitchar(*str,n);
//_delay_ms(1);
str++;
}
}

void USART_TransmitNumber(long int num, uint8_t n)
{
if(num<0)
{
USART_Transmitchar('-',n);
num=(-1)*num;
}

if(num >= 10){
USART_TransmitNumber(num/10,n);
num = num%10;
}
USART_Transmitchar(num+'0',n); /* n is between 0 and 9 */

}
/*
void USART0_TransmitBinary(int num)
{
int i=0,j=0;
while(num)
{
USART0_TransmitNumber(num%2);
i++;
num=num/2;
}
if(i!=8)
{
for(j=0;j<(8-i);j++)
USART0_TransmitNumber(0);
}

}*/
21 changes: 21 additions & 0 deletions Communication/USART_128.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef USART_128
#define USART_128

#include <avr/io.h>
#include <avr/interrupt.h>
#define F_CPU 8000000UL
#include <util/delay.h>

#define New_Line(n) USART_Transmitchar(0x0A,n);\
USART_Transmitchar(0x0D,n);
#define Space(n,x) for(int i=0;i<=n;i++)USART_Transmitchar(0x20,x);

void USART_Init(unsigned int, uint8_t);
void USART_InterruptEnable(uint8_t );
void USART_Transmitchar( unsigned char, uint8_t);
unsigned char USART_Receive(uint8_t);
void USART_TransmitString(char *str, uint8_t);
void USART_TransmitNumber(long int, uint8_t);
//void USART0_TransmitBinary(int);

#endif
79 changes: 79 additions & 0 deletions Communication/USART_32.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include "USART_32.h"

void USART_Init( unsigned int baud )
{
/* Set baud rate */
//unsigned int num;
//num=((F_CPU)/(16*baud))-1;
UBRRH = (unsigned char)(baud>>8);
UBRRL = (unsigned char)baud;
/* Enable receiver and transmitter */
UCSRB = (1<<RXEN)|(1<<TXEN); //|(1<<RXCIE);
/* Set frame format: 8data, 2stop bit */
UCSRC = (1<<URSEL)|(1<<USBS)|(3<<UCSZ0);
}

void USART_InterruptEnable()
{
UCSRB|=(1<<RXCIE);
}

void USART_Transmitchar( unsigned char data )
{
/* Wait for empty transmit buffer */
while ( !( UCSRA & (1<<UDRE)) )
;
/* Put data into buffer, sends the data */
UDR = data;
}

unsigned char USART_Receive( void )
{
/* Wait for data to be received */
while ( !(UCSRA & (1<<RXC)) )
;
/* Get and return received data from buffer */
return UDR;
}

unsigned char USART_ReceiveNonBlocking(void)
{
if((UCSRA & (1<<RXC)))
return UDR;
else return 0;
}

void USART_TransmitString(char *str)
{
while(*str>0)
{
USART_Transmitchar(*str);
//_delay_ms(1);
str++;
}
}

void USART_TransmitNumber(unsigned long n){
if(n >= 10){
USART_TransmitNumber(n/10);
n = n%10;
}
USART_Transmitchar(n+'0'); /* n is between 0 and 9 */
}

void USART_TransmitBinary(int num)
{
int i=0,j=0;
while(num)
{
USART_TransmitNumber(num%2);
i++;
num=num/2;
}
if(i!=8)
{
for(j=0;j<(8-i);j++)
USART_TransmitNumber(0);
}

}
21 changes: 21 additions & 0 deletions Communication/USART_32.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef USART_32
#define USART_32

#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>

#define New_Line() USART_Transmitchar(0x0A);\
USART_Transmitchar(0x0D);
#define Space(n) for(int i=0;i<=n;i++)USART_Transmitchar(0x20);

void USART_Init(unsigned int);
void USART_Transmitchar( unsigned char);
unsigned char USART_ReceiveNonBlocking();
unsigned char USART_Receive(void);
void USART_InterruptEnable(void);
void USART_TransmitString(char *str);
void USART_TransmitNumber(unsigned long);
void USART_TransmitBinary(int num);

#endif
104 changes: 104 additions & 0 deletions Communication/bluetoothmain.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* bluetooth.c
*
* Created: 08-08-2017 06:55:42 PM
* Author : drishti
*/

#include <avr/io.h>
#include <avr/interrupt.h>
#include "USART_128.h"

#define F_CPU 8000000UL

#include <util/delay.h>
int main(void)
{
DDRB |= 0xFF; //set pin of register as output

USART_Init(103,1);
sei();

USART_InterruptEnable(1); //use 1st usart out of two
while (1)
{
USART_Transmitchar('Z',1);
PORTB =0x00;
}
}
ISR(USART1_RX_vect)
{
unsigned char r_data;
r_data=USART_Receive(1);
if (r_data=='U')
{
USART_Transmitchar('U',1);
//PORTB|=0b00001000;
PORTB |= 1<< PINB0;
_delay_ms(50);
//PORTB &= ~(1<< PINB0);

}
else if(r_data=='R')
{
USART_Transmitchar('R',1);
//PORTB=0b00000100;
PORTB |= 1<< PINB2;
_delay_ms(50);
//PORTB &= ~(1<<PINB1);
}
else if(r_data=='L')
{
USART_Transmitchar('L',1);
//PORTB |=0b00000010;
PORTB |= 1<< PINB3;
_delay_ms(50);
//PORTB &= ~(1<<PINB1);
}
else if(r_data=='D')
{
USART_Transmitchar('D',1);
//PORTB=0b00000001;
PORTB |= 1<< PINB1;
_delay_ms(50);
//PORTB &= ~(1<<PINB1);
}
else if(r_data=='T')
{
USART_Transmitchar('T',1);
//PORTB=0b00010000;
PORTB |= 1<< PINB4;
_delay_ms(50);
//PORTB &= ~(1<<PINB1);
}
else if(r_data=='C')
{
USART_Transmitchar('C',1);
//PORTB=0b00100000;
PORTB |= 1<< PINB6;
_delay_ms(50);
//PORTB &= ~(1<<PINB1);
}
else if(r_data=='X')
{
USART_Transmitchar('X',1);
//PORTB=0b01000000;
PORTB |= 1<< PINB5;
_delay_ms(50);
//PORTB &= ~(1<<PINB1);
}
else if(r_data=='S')
{
USART_Transmitchar('S',1);
//PORTB=0b10000000;
PORTB |= 1<< PINB7;
_delay_ms(50);
//PORTB &= ~(1<<PINB1);
}

else
{
USART_Transmitchar('N',1);
_delay_ms(50);
}
}
Loading