뉴티씨



  • HOME
  • 고객지원
  • 질문답변

 

 
시리얼통신 질문드립니다.
 글쓴이 : 조민준
작성일 : 18-03-25 22:27
조회 : 8,853  
인터럽트 방식으로 코드를 해봤는데 작동이 되지 않아서 수정방안에 조언을 부탁드립니다.

문제가 없다면 AVR AS계획도 있습니다.

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

volatile unsigned int motor_speed = 0; // 모터속도
volatile unsigned char dir = 0;
volatile int count = 0;

#define MOTOR_PORT        PORTC
#define PWM1_ON            (MOTOR_PORT|=0x01)
#define PWM1_OFF        (MOTOR_PORT&=0xFE)
#define DIR1_ON            (MOTOR_PORT|=0x02)
#define DIR1_OFF        (MOTOR_PORT&=0xFD)
#define ENABLE1_OFF        (MOTOR_PORT|=0x04)
#define ENABLE1_ON        (MOTOR_PORT&=0xFB)
#define BREAK1_ON        (MOTOR_PORT|=0x08)
#define BREAK1_OFF        (MOTOR_PORT&=0xF7)
#define PWM2_ON            (MOTOR_PORT|=0x10)
#define PWM2_OFF        (MOTOR_PORT&=~0x10)
#define DIR2_ON            (MOTOR_PORT|=0x20)
#define DIR2_OFF        (MOTOR_PORT&=~0x20)
#define ENABLE2_OFF        (MOTOR_PORT|=0x40)
#define ENABLE2_ON        (MOTOR_PORT&=~0x40)
#define BREAK2_ON        (MOTOR_PORT|=0x80)
#define BREAK2_OFF        (MOTOR_PORT&=~0x80)

void port_init(void)
{
PORTA = 0x00;
DDRA  = 0x00;
PORTB = 0x00;
DDRB  = 0x00;
PORTC = 0x00; //m103 output only
DDRC  = 0xff;
PORTD = 0x00;
DDRD  = 0x00;
PORTE = 0x00;
DDRE  = 0x00;
PORTF = 0x00;
DDRF  = 0x00;
PORTG = 0x00;
DDRG  = 0xff;
}


void tx0Char(char message)
{
 while(((UCSR0A>>UDRE0)&0x01)==0);
 UDR0 = message;
}

int Putchar(char c)
{
 tx0Char(c);
 return c;
}

int Getchar()
{
 while((UCSR0A&0x80)==0);
 return UDR0;
}

void uart0_init()
{
 UCSR0A = 0x00;
 UCSR0B = 0x00;
 UCSR0C = 0x06;  //전송할 길이를 8비트로 설정
 UBRR0L = 0x67;  //보레이트를 결정 9600
 UBRR0H = 0x00;
 UCSR0B = 0x18;  // 송신부와 수신부가 동작하도록 설정
}

ISR(TIMER0_OVF_vect)
{
 TCNT0 = 0xfb;
 count++;
 if(count>2)count=0;
 //TCNT0 = motor_speed; //reload counter value
}

void timer0_init(void)
{
 TCCR0 = 0x00; //stop
 TCNT0 = 0xFB; //set count 5
 OCR0  = 0x00; //
 TCCR0 = 0x02; //start timer 0.0000005초
}


void init_devices()
{
 port_init();
 timer0_init();
 uart0_init();
 TIMSK = 0x01;  //overflow 인터럽트 사용
 sei();
 XDIV = 0x00; //XTAL 분주 제어 레지스터
 XMCRA = 0x00;
 port_init();
}

void delay(int delaytime){          //함수선언
int i,j;
for(i=0;i<1000;i++){
for(j=0;j<delaytime;j++){
}
}
}


void main()  //시리얼통신
{
 int a;

  ENABLE1_ON;
  PWM1_ON;
  BREAK1_OFF;
  ENABLE2_ON;
  PWM2_ON;
  BREAK2_OFF;

 init_devices();
 fdevopen(Putchar,Getchar);


 while(1)
 {
  scanf("%d",&a);
  printf("%d",a);

  switch(a)
  {
            case '5':
            DIR1_ON;       
            DIR2_ON;   
            PORTG = 0x01;   
            delay(500);//시계
       
            case '4':
            DIR1_ON;       
            DIR2_ON;   
            PORTG = 0x01;   
            delay(250);//시계
           
            case '3':
              ENABLE1_OFF; // 정지
           
            case '2':
            DIR1_ON;       
            DIR2_ON;   
            PORTG = 0x02;   
            delay(250);//반시계

            case '1':
            DIR1_ON;       
            DIR2_ON;   
            PORTG = 0x02;   
            delay(500);//반시계

            default:
              ENABLE1_OFF; //정지

  }

 }
}

 
   
 

 


1