질문게시판 > 답변 : PWM질문입니다.

TODAY44 TOTAL124,247
사이트 이용안내
Login▼/회원가입
포럼 동영상강좌 회원가입


BASIC4MCU | 질문게시판 | 답변 : PWM질문입니다.

페이지 정보

작성자 master 작성일2018-05-16 09:31 조회54회 댓글1건

본문

	

ìëì´ë¸ ëìì ëí ì´ë¯¸ì§ ê²ìê²°ê³¼ 

 

 

// DateTime : 2018-05-16 오전 9:33:26
// by Ok-Hyun Park
//
#define DEBUG
#include <LiquidCrystal.h>
LiquidCrystal lcd(2,3,4,5,6,7,8,9,10,11,12);
//
SteppingMotor Ra ={39,0,true,0};
SteppingMotor Dec={41,0,true,0};
//
void sethz(int num,unsigned int hz){
  PWM->PWM_CH_NUM[num].PWM_CPRD=(3000000/hz);
  PWM->PWM_CH_NUM[num].PWM_CDTY=(1500000/hz);
}
//
struct SteppingMotor{
  int P_CWint P_CCWboolean Dirlong step//CW: true CCW: false
};
//
void ISR1(){ if(Ra.Dir )Ra.step++;  else Ra.step--;  }
void ISR2(){ if(Dec.Dir)Dec.step++; else Dec.step--; }
void ISR3(){ Ra.Dir =!Ra.Dir;  }
void ISR4(){ Dec.Dir=!Dec.Dir; }
//
void PWM_Init(){
  // PWM Set-up on pins PC7 and PA20(Arduino Pins 39(PWMH2): see Datasheet chap.38.5.1 page 973
  PMC->PMC_PCER1|=PMC_PCER1_PID36;               // PWM power ON
  PWM->PWM_DIS=PWM_DIS_CHID2;                    // Disable PWM channel 2
  PWM->PWM_DIS=PWM_DIS_CHID3;                    // Disable PWM channel 3
  // Select Instance=PWM; Signal=PWMH2(channel 2); I/O Line=PC7(P7,Arduino pin 39,see pinout diagram); Peripheral type B
  PIOC->PIO_PDR|=PIO_PDR_P7;                     // Set the pin to the peripheral PWM,not the GPIO
  PIOC->PIO_ABSR|=PIO_PC7B_PWMH2;                // Set PWM pin perhipheral type B
  //  Select Intancd=PWMl Signal=PWMH3(channel 2); I/O LIne=PC9(P9,Arduino pin 41,see pinout diagram); Peripheral type B
  //Added by me for generate another pulse
  PIOC->PIO_PDR|=PIO_PDR_P9;                     // Set the pin to the peripheral PWM,not the GPIO
  PIOC->PIO_ABSR|=PIO_PC9B_PWMH3;                // Set PWM pin perhipheral type B
  PWM->PWM_CLK=PWM_CLK_PREB(0)|PWM_CLK_DIVB(28); // Set the PWM clock rate to 3MHz(84MHz/28).
  PWM->PWM_CH_NUM[2].PWM_CMR=PWM_CMR_CPRE_CLKB;  // clock source as CLKB on channel 2,waveform left aligned
  //Added by me
  PWM->PWM_CH_NUM[3].PWM_CMR=PWM_CMR_CPRE_CLKB;  // clock source as CLKB on channel 3,waveform left aligned
  PWM->PWM_CH_NUM[2].PWM_CPRD=3000;              // Channel 2: Set the PWM frequency 3MHz/CPRD=1 KHz;
  PWM->PWM_CH_NUM[2].PWM_CDTY=1500;              // Channel 2: Set the PWM duty cycle to x%=(CDTY/ CPRD)*100%;
  //Added by me for another pulse out
  PWM->PWM_CH_NUM[3].PWM_CPRD=3000;              // Channel 3: Set the PWM frequency 3MHz/CPRD=1 KHz;
  PWM->PWM_CH_NUM[3].PWM_CDTY=1500;              // Channel 3: Set the PWM duty cycle to x%=(CDTY/ CPRD)*100%;
  PWM->PWM_IER1=PWM_IER1_CHID2;                  // Interrupt on Channel 2 Counter match
  //Added by me for channel 3
  PWM->PWM_IER1=PWM_IER1_CHID3;                  // Interrupt on Channel 3 Counter match
  NVIC_EnableIRQ(PWM_IRQn);                      // Enable interrupt
  PWM->PWM_ENA=PWM_ENA_CHID2;                    // Enable PWM Channel 2
  //Added by me
  PWM->PWM_ENA=PWM_ENA_CHID3;                    // Enable PWM Channel 3
}
//
void setup(){
  lcd.begin(20,4);    lcd.clear();
  lcd.setCursor(0,0); lcd.print("    Solomon  TCS    ");
  lcd.setCursor(0,1); lcd.print("      Ver  000      ");
  lcd.setCursor(0,2); lcd.print("   Void Laboratory  ");
  lcd.setCursor(0,3); lcd.print("   made by Dr.Void  ");
  PWM_Init();
  //lcd.begin(20,4);
  //attachInterrupt(Ra.P_CW,ISR1,RISING);        // 인터럽트 부착(하이 펄스 회수 측정)
  //attachInterrupt(Ra.P_CCW,ISR3,CHANGE);       // 방향체크
  //DEC 축 모터
  //attachInterrupt(Dec.P_CW,ISR2,RISING);       // 인터럽트 부착(하이 펄스 회수 측정)
  //attachInterrupt(Dec.P_CCW,ISR4,CHANGE);      // 방향 체크*/
  //
  //delay(3000);
  lcd.clear();
  // pinMode(33,OUTPUT); pinMode(34,OUTPUT); pinMode(35,OUTPUT); for(int i=36;i<53;i++){ pinMode(i,INPUT_PULLUP); }
  // Serial.begin(9600); pinMode( 2,OUTPUT); pinMode( 3,OUTPUT);
}
//
void loop(){
  /*lcd.clear();
  lcd.setCursor(0,0);
  switch(keycheck(33,34,35)){
    case 0: lcd.print("NoInput");        break;
    case 36: lcd.print("MENU");          break; //5
    case 37: lcd.print("DEC-");          break;
    case 38: lcd.print("Prev");          break;
    case 39: lcd.print("num1");          break;
    case 40: lcd.print("num4");          break;
    case 41: lcd.print("num7");          break;
    case 42: lcd.print("num*");          break;
    case 43: lcd.print("RA+");           break; //6
    case 44: lcd.print("Enter");         break;
    case 45: lcd.print("RA-");           break;
    case 46: lcd.print("num2");          break;
    case 47: lcd.print("num5");          break;
    case 48: lcd.print("num8");          break;
    case 49: lcd.print("num0");          break;
    case 50: lcd.print("LCD BackLight"); break; //7
    case 51: lcd.print("DEC+");          break;
    case 52: lcd.print("Next");          break;
  }
  */
}
//
int keycheck(const int pin1,const int pin2,const int pin3){
  int i;
  digitalWrite(pin1,LOW);
  for(i=pin3+1;i<pin3+9;i++){ if(!digitalRead(i))return i; }
  digitalWrite(pin1,HIGH); digitalWrite(pin2,LOW);
  for(;i<pin3+17;i++){
    #ifdef DEBUG
      Serial.print(i); Serial.print(": "); Serial.println(digitalRead(i));
    #endif
    if(!digitalRead(i))return i;
  }
  digitalWrite(pin2,HIGH); digitalWrite(pin2,LOW);
  for(;i<pin3+25;i++){ if(!digitalRead(i))return i; }
  return 0;
}

 

LCD 핀과 PWM핀에서 핀 중복이 있는 것 아닌지 체크해보세요

PWM핀을 옮기는 것보다 LCD 핀을 옮기는 것이 쉬울겁니다.

 

  • BASIC4MCU 작성글 SNS에 공유하기
  • 페이스북으로 보내기
  • 트위터로 보내기
  • 구글플러스로 보내기

댓글 1

조회수 54

DrVoid님의 댓글

DrVoid 작성일

늦은시간에 올린 글인데도 답변을 달아주셔서 감사합니다.
할당한 PWM이 2번이라서 다른 PWM은 문제없을거라 생각하였는데 역시나..
정말 감사합니다. 도움이 필요할때마다 글을 올릴게요.
정말로 감사합니다.

질문게시판HOME > 질문게시판 목록

MCU, AVR, 아두이노 등 전자공학에 관련된 질문을 무료회원가입 후 작성해주시면 전문가가 답변해드립니다.

게시물 검색

Privacy Policy
                   MCU BASIC ⓒ 2017 모바일버전으로보기