BASIC4MCU | 질문게시판 | 답변 : SG90C
페이지 정보
작성자 master 작성일2018-12-13 14:38 조회8,254회 댓글1건본문
// MCU BASIC: https://www.basic4mcu.com// DateTime : 2018-12-13 오후 2:41:16// by Ok-Hyun Park//int angle=0;//void svangle1(){if(angle!=1){ angle=1;OCR1A= 600; delay(500); // 0도OCR1A=1500; delay(500); // 90도(정지)}}//void svangle2(){if(angle!=1){ angle=1;OCR1A=2400; delay(500); // 180도OCR1A=1500; delay(500); // 90도(정지)}}
전체 소스가 아니라서 몇도로 제어하는 함수인지 알 수가 없지만
OCR1A= 600; delay(500); // 0도이 코드가 정회전이라면OCR1A=2400; delay(500); // 180도이 코드는 역회전이 되고OCR1A=1500; delay(500); // 90도(정지)중간 값이 정지 가 됩니다.
큰 글씨로 적은 딜레이 값을 조정해서 원하는 각도로 맞춰보세요
시간으로 각도를 맞춘다는 것이 불가능에 가까우므로 대략적인 흉내만 납니다.
댓글 1
조회수 8,254Jdhe님의 댓글
Jdhe 작성일
이 코딩인데 바꿔주실수 있으신가요..??ㅠㅠㅠ 부탁드립니다.
#define F_CPU 16000000
#include <avr/io.h>
#include <stdio.h>
#include <util/delay.h>
#include <math.h>
#include <avr/interrupt.h>
#define LINE1 0x80 //첫번째 라인의 DDRAM address : 0x00 -> 0x80+0x00
#define LINE2 0xC0 //두번째 라인의 DDRAM address : 0x40 -> 0x80+0x40
#define MAX 4
//PG2=LCD_EN, PG1=RW, PG0=RS 에 연결
#define CMD_WRITE 0x09 //명령어쓰기 E=1, RW=0, RS=0 1
#define CMD_READ 0x0B //명령어읽기 E=1, RW=1, RS=0 1
#define DATA_WRITE 0x0D //데이터쓰기 E=1, RW=0, RS=1 1
#define DATA_READ 0x0F //데이터읽기 E=1, RW=1, RS=1 1
#define LCD_EN 0x01 //PG0에 연결되어 있기 때문에 bit2를 1로 했다.
void LCD_cmd_write(char cmd)
{
PORTG = CMD_WRITE; //PORTG에 RS, RW, E 가 연결되어 있다.
PORTA = cmd; //PORT에 데이터버스가 연결되어 있다.
PORTG = PORTG^LCD_EN; //E 신호를 H->L로 하기 위해
_delay_ms(2); //LCD 내부 동작시간
}
void pwm_init()
{
TCCR1A = 0xA2;
TCCR1B = 0x1b;
ICR1 = 4999;
OCR1A = 0;
OCR1B = 0;
TCNT1 = 0x00;
}
void svangle1(float angle)
{
unsigned int pulse;
pulse = angle*225/90 + 375 + 0.5;
OCR1A = pulse;
}
void svangle2(float angle)
{
unsigned int pulse;
pulse = angle*225/90 + 375 + 0.5;
OCR1B = pulse;
}
unsigned int Echo1(){
unsigned int range;
PORTC = PORTC|0x01;
_delay_us(10);
PORTC = PORTC&0xfe;
while(!((PINC&0x04) == 0x04));
TCNT3 = 0;
TCCR3B = 2;
while((PINC&0x04) == 0x04);
TCCR3B = 8; range = TCNT3/116;
return range;
}
unsigned int Echo2(){
unsigned int range;
PORTC = PORTC|0x02;
_delay_us(10);
PORTC = PORTC&0xfd;
while(!((PINC&0x08) == 0x08));
TCNT3 = 0;
TCCR3B = 2;
while((PINC&0x08) == 0x08);
TCCR3B = 8; range = TCNT3/116;
return range;
}
void init_Echo()
{
TCCR3A = 0;
TCCR3B = 8;
_delay_ms(100);
}
//LCD에 데이터를 쓰기 위한 함수
void LCD_data_write(char data)
{
PORTG = DATA_WRITE; //PORTG에 RS, RW, E 가 연결되어 있다.
PORTA = data; //PORTA에 데이터버스가 연결되어 있다.
PORTG = PORTG^LCD_EN; //E 신호를 H->L로 하기 위해
_delay_ms(2); //LCD 내부 동작시간
}
void init_Rx_Tx(void)
{
DDRD = 0x02;
UCSR1A = 0x00;
UCSR1B |= (1<<RXEN1)|(1<<TXEN1);
UCSR1C = 0x06;
UBRR1H = 0x00;
UBRR1L = 0x67;
}
char rx_char(void)
{
while((UCSR1A&0x80)==0);
return UDR1;
}
void tx_char(char tx_data)
{
while((UCSR1A&0x20) == 0);
UDR1 = tx_data;
}
// ATmega128의 포트 초기화
void USART_SendString(char *str)
{
int i=0;
while (str[i]!=0)
{
tx_char(str[i]);
i++;
}
}
// LCD 초기화, 초기화 강좌의 순서도 참조
void init_LCD(void)
{
_delay_ms(15); //15msec 이상 시간지연
LCD_cmd_write(0x38); //기능셋(데이터버스 8비트, 라인수:2줄)
_delay_ms(5); //4.1msec 이상 시간지연, 생략가능
LCD_cmd_write(0x38); //기능셋, 생략 가능
_delay_us(100); //100usec 이상 시간지연, 생략가능
LCD_cmd_write(0x38); //기능셋, 생략 가능
LCD_cmd_write(0x08); //표시 Off , 생략 가능
LCD_cmd_write(0x01); //화면 지우기
LCD_cmd_write(0x06); //엔트리모드셋
LCD_cmd_write(0x0C); //표시 on
}
// LCD에 문자열을 표시하기 위한 함수
void LCD_wr_string(char d_line, char *lcd_str)
{
LCD_cmd_write(d_line); //문자열을 표시하기 위한 라인 설정
while(*lcd_str != '\0')
{
LCD_data_write(*lcd_str); //한개의 문자씩 LCD에 표시한다.
lcd_str++;
}
}
void init_system(void)
{
DDRA = 0xFF; //LCD 데이터 버스
PORTA = 0xFF;
DDRG = 0x7F; //LCD 컨트롤 신호(PG2=LCD_EN, PG1=RW, PG0=RS)
PORTG = 0x3F;
DDRC = 0x03;
PORTC = 0x00;
DDRB = 0xff;
}
int main(void)
{
init_Rx_Tx();
int count = 0;
char str[16];
int range[2];
init_system();
init_LCD();
pwm_init();
init_Echo();
while(1){
range[0]=Echo1(); //들어가는거 //차단
if(range[0]<30){
if(MAX-count>=1){
PORTB|=1; _delay_ms(1000); PORTB&=~1;
svangle1( 0); _delay_ms(500);
svangle1(90); _delay_ms(500);
count++;
}
}
//
range[1]=Echo2(); //나가는거
if(range[1]<30){
if(count>=1){
PORTB|=4; _delay_ms(1000); PORTB&=~4;
svangle2( 0); _delay_ms(500);
svangle2(90); _delay_ms(500);
count--;
}
}
sprintf(str,"%d ",MAX-count);
LCD_wr_string(LINE1,str);
USART_SendString(str);
USART_SendString("\n");
}
}