
안녕하세요.
ex_Motor_VR_Speed_control_WINAVR_AVRSTUDIO4
뉴티씨 자료실에 있는 펌웨어 컴파일하여 PC 터미널로 테스트 해보았읍니다.
터미널에서 " printf("\r\n\a 현재 모터속도(50~255)\r\n");
프린트 되어야 하는데 데이터가 깨져서 나옵니다. 데이터 속도는 9600으로 맞추었읍니다.
왜 그럴까요?
//UART 통신제어실습
#include <avr/io.h>
#include <util/delay.h>
#include <stdio.h>
#define MOTOR_PORT PORTB
#define MOTOR_PORT_DDR DDRB
#define SW_PORT PING
#define SW_PORT_DDR DDRG
#define CW 0
#define CCW 1
#define MAX 255
#define MIN 50
#define PWM2_ON (MOTOR_PORT|=0x10)
#define PWM2_OFF (MOTOR_PORT&=0xEF)
#define DIR2_CW (MOTOR_PORT|=0x20)
#define DIR2_CCW (MOTOR_PORT&=0xDF)
#define ENABLE2_OFF (MOTOR_PORT|=0x40)
#define ENABLE2_ON (MOTOR_PORT&=0xBF)
#define BREAK2_ON (MOTOR_PORT|=0x80)
#define BREAK2_OFF (MOTOR_PORT&=0x7F)
volatile unsigned char temp=60, input;
// ADC initialize
// Conversion time: 75uS
void adc_init(void)
{
ADCSRA = 0x00; //disable adc
ADMUX = 0x00; //select adc input 0
ADCSRA = 0xC6;
}
// 입력으로 들어오는 채널의 ADC를 스타트 시킨다.
void startConvertion(unsigned char ch)
{
ADCSRA = ADCSRA & 0x3f;
ADMUX = 0x60 | (ch & 0x0f);
ADCSRA = ADCSRA | 0xc0;
}
// startConvertion() 후에 수행되며 컨터팅 된 값을 리턴한다.
unsigned char readConvertData(void)
{
volatile unsigned char temp;
while((ADCSRA & 0x10)==0);
ADCSRA = ADCSRA | 0x10;
temp = ADCL;
temp = ADCH;
ADCSRA = ADCSRA | 0x10;
return temp;
}
void init_UART(void)
{
UCSR0A=0x00;
UCSR0B=0x18;
UCSR0C=0X06;
UBRR0H=0x00;
UBRR0L=0x67;
}
int SerialPutChar(char ch, FILE *fp)
{
while(((UCSR0A>>UDRE0)&0x01)==0);
UDR0 = ch;
return 0;
}
int SerialGetChar(FILE *fp)
{
while((UCSR0A & 0x80)==0);
return UDR0;
}
void Initialize_Timer(void)
{
TCCR0=0x00;
TCCR0=(0<<CS02)|(0<<CS01)|(1<<CS00)|(1<<WGM00)|(1<<WGM01)|(1<<COM01);
}
void Initialize_Motor(void)
{
MOTOR_PORT_DDR=0xff;
MOTOR_PORT=0x00;
}
void motor_init(void){
ENABLE2_ON;
PWM2_ON;
BREAK2_OFF;
DIR2_CW;
}
void Direction_Control(void){
input = (SW_PORT&0x0c)>>2;
if(input == 0x01){
DIR2_CW;
}
if(input == 0x02){
DIR2_CCW;
}
}
void Status_to_UART(void){
startConvertion(1); // VARIABLE RESISTOR Converting
temp = readConvertData();
printf("\r\n\a 현재 모터속도(50~255)\r\n");
if(temp >= MAX) printf("Now MAX speed ");
if(temp <= MIN) {
printf(" Now MIN speed ");
temp = MIN;
}
printf(" Motor Speed : %d\n\r",temp);
_delay_ms(100);
}
int main(void)
{
SREG = 0x00;
UCSR0C = 0x06; // 8 bit, 비동기 통신, parity none, stop bit 0
UBRR0L = 0x67; // 16000000/(9600*16) -1; //set baud rate lo 9,600bps
UBRR0H = 0; // set baud rate hi
UCSR0B = 0x18; // RX enable, TX enable
SREG = 0x80; // Global interrupt enable
init_UART(); // UART 0 초기화
Initialize_Timer();
Initialize_Motor();
adc_init();
motor_init();
SW_PORT_DDR = 0x00;
fdevopen(SerialPutChar,SerialGetChar);
printf("\r\n\a모터속도 Encoder테스트 \r\n");
while(1){
Status_to_UART();
OCR0=temp; // Speed Control
Direction_Control();
}
while(0){
//scanf("%s", str);
//SerialPutChar(ch);
//SerialGetChar( );
//printf("Input:%s",str);
}
return 0;
}
|