Showing posts with label AVR. Show all posts
Showing posts with label AVR. Show all posts
2015-02-15
#if defined (ARDUINO) && ARDUINO >= 100 ? 어디서 비교하는지 궁금합니다.
아두이노 호환 구문 관련 질문을 정리한 글입니다.
출처 : cafe.naver.com/laydigital/
공개용 코드들을 참조하기 위해 살펴보다가 유독 아래와 같은 구문들이 자주 보여서 검색해보니 버젼 호환과 관련된 내용이라는 것을 알게 되었습니다.
http://forum.arduino.cc/index.php?topic=61764.5;wap2
if (ARDUINO < 100)
#include <WProgram.h>
#else
#include <Arduino.h>
#endif
------------------------------------------
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
그런데 여기서 한가지 궁금한 점이 있어 질문드립니다.
ARDUINO 를 100 이란 상수값과 비교한다는건 ARDUINO가 numeric value라는 얘기인데
이값이 정의된 곳이 어디인지 찾아보기가 힘들어서 질문드립니다 현재 ARDUINO는 사용하고 있지 않고 있어서
전체 lib파일들을 전부 흝어 볼수가 없고 지금 참고용으로 살펴보고 있는 코드와 관련된 공개용 소스만 보고 있다가
궁금해서 질문 드립니다.
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
C:\Program Files\Arduino\hardware\tools\avr\bin\avr-gcc -c -g -Os -Wall -ffunction-sections -fdata-sections -mmcu=atmega328p -DF_CPU=16000000L -MMD -DUSB_VID=null -DUSB_PID=null -DARDUINO=106 -IC
컴파일될때 -DARDUINO=106
이렇게 전달되네요.
IDE 내부에서 전달할 수도 있을듯 합니다.
8bit LED control
hi, here is second, let's see how it works LED on avr module on last video and some sample code.
case light_on: DDRA = 0xff; PORTA = 0xff; ptr->light_state = ON; break;
case light_off: DDRA = 0; PORTA = 0; ptr->light_state = OFF;break;
as you know in my code this is " switch ~ case " this is "C" language.
normally you should know 'C' first :) and DDRA means Data Direction Register Address
DDRA is 8bit register 0000 0000 -> high 4bit low 4bit.
DDRA = 0xff -> put the data in register like this " 1111 1111 " .
PORTA is General I/O port pin register you may check Ateml-128 Avr 8bit datasheet, it's great for understand the every terms.
ptr->light_state = ON; is struct in "C" so light_on is make the ddra register as 1111 1111. DDRA = 0xff and PORTA = 0xff it result each 8 pins is signally 1 (2.7~3V).
2015-02-14
Bluetooth & module control code and video
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.
Subscribe to:
Posts (Atom)
