Bluetooth <-> AVR (8bit) Control!
look at the picture i draw it myself by mouse ;) it's my first ever!
first of all, you should know how it works each part and how connected in AVR unit.
that is very important but i couldn't explain whole part how it's work. it's not easy and need much time, so i simply symbolize and show you the code that i made for works.
my avr module name is jkit-128 that made in korea company: http://jcnet.co.kr/
unfortunately company page didn't support foreigner language only korean.
this company has community site: http://cafe.naver.com/lazydigital/ There are a lot of korean member here! and i also join there! if i could, i will write in english,
this is helpful for my english practices and maybe the others who coming to see my "blogger" and whole world friends!
this is first part, so let's see the whole part at once!
This video made by old phone so sound is so low maybe mute, sorry!
anyway as you see led, clcd(character-lcd), motor is working by smart-phone!
and here is code!
#include <avr/io.h>
#define Kilo 1000UL
#define Mega Kilo*Kilo
#define F_CPU 16*Mega
#include <util/delay.h>
// PORTE->DATA[7~0],PORTF->E,RW,RS[4,2,0]
#define DB(x) (1<<x)
#define W 0
#define R 1
#define I 0
#define D 1
#define E (1<<PF4)
#define RW(x) ((x)<<PF2)
#define RS(x) ((x)<<PF0)
void usart1_init(uint16_t ubrr);
void usart1_transmit(uint8_t data);
uint8_t usart1_receiver();
void usart1_string(uint8_t *p);
void bluetooth_test(); // "key value<ok" display.
void entire_check();
void clcd_init();
void clcd_WRtiming(uint8_t rw,uint8_t rs,uint8_t data);
void clcd_string(uint8_t *p);
void _main_dsp();
#define light_on '1'
#define light_off '2'
#define clcd_on '3'
#define clcd_off '4'
#define motor_on '5'
#define motor_off '6'
#define target_entire_check '7'
#define init_clcd '8'
#define ON 'O'
#define OFF 'X'
struct title
{
uint8_t light[6];
uint8_t clcd[7];
uint8_t motor[9];
uint8_t author[10];
int8_t light_state;
int8_t clcd_state;
int8_t motor_state;
}display=
{
"LED:" ,
"CLCD:" ,
"MOTOR:",
"DynE!",
OFF,
OFF,
OFF
};
struct title *ptr = &display;
// ------ Main loop ------
int main(void)
{
static uint8_t c;
usart1_init(8); //115.2k
while(1)
{
_main_dsp();
c = usart1_receiver();
switch(c)
{
case light_on: DDRA = 0xff; PORTA = 0xff; ptr->light_state = ON; break;
case light_off: DDRA = 0; PORTA = 0; ptr->light_state = OFF;break;
case clcd_on: clcd_WRtiming(W,I,(DB(3)|DB(2)|DB(1)|DB(0))); _delay_us(39); ptr->clcd_state = ON; break;
case clcd_off: clcd_WRtiming(W,I, DB(3)); _delay_us(39); ptr->clcd_state = OFF; break;
case motor_on: DDRB = (1<<DDB6)|(1<<DDB7); PORTB = (0<<PB6)|(1<<PB7); ptr->motor_state = ON;break;
case motor_off: PORTB &= ~((1<<PB6)|(1<<PB7)); ptr->motor_state= OFF; break;
case target_entire_check: entire_check(); break;
case init_clcd: clcd_init(); ptr->clcd_state = ON; break;
default: break;
}
}
}
// ------ USART1 -----
void usart1_init(uint16_t ubrr)
{
UBRR1H = (uint8_t)(ubrr>>8); //set baud H
UBRR1L = (uint8_t)ubrr; //set baud L
UCSR1B = (1<<RXEN1)|(1<<TXEN1); //rx,tx enable
UCSR1C = (0<<USBS1)|(3<<UCSZ10);//1stop,8bit
}
void usart1_transmit(uint8_t data)
{
while(!(UCSR1A & (1<<UDRE1))) ; //wait transmit buffer until empty
UDR1 = data; //put data into buffer then send the data
}
uint8_t usart1_receiver()
{
while(!(UCSR1A & (1<<RXC1))) ; //wait data to be received
return UDR1; //return function value as UDR1
}
void usart1_string(uint8_t *p)
{
while(*p) usart1_transmit(*p++) ;
asm("nop"); // 62.5ns wait (1clock)
}
void bluetooth_test()
{
static uint8_t ok[]="<ok";
static uint8_t u;
u = usart1_receiver();
usart1_transmit(u);
usart1_string(ok);
}
void entire_check()
{
usart1_string(ptr->light);
usart1_transmit(ptr->light_state);
usart1_transmit('\n');
usart1_string(ptr->motor);
usart1_transmit(ptr->motor_state);
usart1_transmit('\n');
usart1_string(ptr->clcd);
usart1_transmit(ptr->clcd_state);
}
// ------- CLCD ------
void clcd_WRtiming(uint8_t rw,uint8_t rs,uint8_t data)
{
PORTF = RS(rs);
PORTF |= RW(rw);
PORTF |= E;
PORTC = data;
PORTF &= ~E;
_delay_us(50);
}
void clcd_init()
{
DDRF = 0xff;
/*DDRE = 0xff;*/
DDRC = 0xff;
//Power on
_delay_ms(30); //8bit initialization
//function set
clcd_WRtiming(W,I,(DB(5)|DB(4)|DB(3)));
_delay_us(39);
/*cursor or display shift move without wr data or rd data
clcd_WRtiming(W,I,(DB(4)|DB(2)));
_delay_us(39);*/
//display on/off control
clcd_WRtiming(W,I,(DB(3)|DB(2)|DB(1)|DB(0)));
_delay_us(39);
//entry mode set move direction cursor or display wr data or rd data
clcd_WRtiming(W,I,(DB(2)|DB(1)));
_delay_ms(39);
//display clear
clcd_WRtiming(W,I,DB(0));
_delay_ms(2);
} // end of initialization
void clcd_string(uint8_t *p)
{
while(*p) clcd_WRtiming(W,D,*p++);
}
void _main_dsp()
{
clcd_WRtiming(W,I,(DB(7)));
_delay_us(39);
clcd_string(ptr->light);
clcd_WRtiming(W,D,ptr->light_state);
clcd_WRtiming(W,I,(DB(7)|8));
_delay_us(39);
clcd_string(ptr->motor);
clcd_WRtiming(W,D,ptr->motor_state);
clcd_WRtiming(W,I,(DB(7)|DB(6)));
_delay_us(39);
clcd_string(ptr->clcd);
clcd_WRtiming(W,D,ptr->clcd_state);
clcd_WRtiming(W,I,(DB(7)|DB(6)|8));
_delay_us(39);
clcd_string(ptr->author);
}
}
Thank you for coming my "blogger" and i hope see you in my next time.
No comments:
Post a Comment