2016年4月29日 星期五

義守大學電機系陳慶瀚 VHDL PPT

義守大學電機系 VHDL 

授課教師:陳慶瀚


組合邏輯與VHDL 基本語法

組合邏輯與VHDL 基本語法




2016年4月27日 星期三

28BYJ-48 步進馬達實驗 (一相激磁)

28BYJ-48 步進馬達實驗 (一相激磁)

步進馬達依照靜子線圈的相數多寡可分為單相、雙相、三相、四相和五相等。一般小型的步進馬達以四相式為準,這類的馬達在定字上有四組相對應的線圈,分別提供相差90度相位的電力。當馬達單相激磁時,這四組線圈可在各相的對應處停住轉子,當下一個脈衝來到時,轉子轉動一個角度,這種角度稱為步進角。步進角的計算公式如下:

        步進角=360度/ 寸動數

              = 360度 / (相數 X 轉子齒數)

寸動數:步進馬達每週所轉動的步數,就是相數和轉子齒數的乘積。










//程式
int Pin0 = 6;
int Pin1 = 7;
int Pin2 = 8;
int Pin3 = 9;
int _step = 0;
int count=0 ;
boolean dir = true;
// gre
void setup()
{
 pinMode(Pin0, OUTPUT);
 pinMode(Pin1, OUTPUT);
 pinMode(Pin2, OUTPUT);
 pinMode(Pin3, OUTPUT);
}
 void loop()
{
 switch(_step){
   case 0:
     digitalWrite(Pin0, HIGH);
     digitalWrite(Pin1, LOW);
     digitalWrite(Pin2, LOW);
     digitalWrite(Pin3, LOW);
   break;
   case 1:
     digitalWrite(Pin0, LOW);
     digitalWrite(Pin1, HIGH);
     digitalWrite(Pin2, LOW);
     digitalWrite(Pin3, LOW);
   break;
   case 2:
     digitalWrite(Pin0, LOW);
     digitalWrite(Pin1, LOW);
     digitalWrite(Pin2, HIGH);
     digitalWrite(Pin3, LOW);
   break;
   case 3:
     digitalWrite(Pin0, LOW);
     digitalWrite(Pin1, LOW);
     digitalWrite(Pin2, LOW);
     digitalWrite(Pin3, HIGH);
   break;
     default:
     digitalWrite(Pin0, LOW);
     digitalWrite(Pin1, LOW);
     digitalWrite(Pin2, LOW);
     digitalWrite(Pin3, LOW);
   break;
 }

 count++;
 if (count>4096) {   // 28BYJ-48 5V DC  360 / (5.625 / 64) = 4096 step
  count=0;
  dir=!dir;
  delay(1000);
 }

 if(dir){
   _step++;
 }else{
   _step--;
 }

 if(_step>7){
   _step=0;
 }
 if(_step<0){
   _step=7;
 }
 delay(4);
}

28BYJ-48 步進馬達實驗 (二相激磁)

28BYJ-48 步進馬達實驗 (二相激磁)


留意 順序 1-3-2-4  對應 接腳 6, 8, 7, 9
// here in the sequence 1-3-2-4 for proper sequencing
Stepper small_stepper(STEPS_PER_MOTOR_REVOLUTION, 6, 8, 7, 9);


5V Stepper Motors
You will need a stepper library, but there is one built into Arduino, so no installation should be needed.

These steppers normally have 4096 steps per revolution, but the built in stepper library works in
4 step sequence vs the 8 step sequence that it would expect, so we need to consider it as 2048, though you might find this is not completely accurate either and you may need to tweak code.

The code expects this pinout - you may modify as necessary:
IN1 --> 8
​IN2 --> 9
​IN3 --> 10
​IN4 --> 11
5-12V+ -->5V
​Gnd- --> Gnd





28BYJ-48 步進馬達實驗 (二相激磁)

/*-----( Import needed libraries )-----*/
#include <Stepper.h>

/*-----( Declare Constants, Pin Numbers )-----*/
//---( Number of steps per revolution of INTERNAL motor in 4-step mode )---
#define STEPS_PER_MOTOR_REVOLUTION 32

//---( Steps per OUTPUT SHAFT of gear reduction )---
#define STEPS_PER_OUTPUT_REVOLUTION 32 * 64  //2048


/*-----( Declare objects )-----*/
// create an instance of the stepper class, specifying
// the number of steps of the motor and the pins it's
// attached to

//The pin connections need to be 4 pins connected
// to Motor Driver In1, In2, In3, In4  and then the pins entered
// here in the sequence 1-3-2-4 for proper sequencing
Stepper small_stepper(STEPS_PER_MOTOR_REVOLUTION, 6, 8, 7, 9);  // 1-3-2-4 for proper sequencing

/*-----( Declare Variables )-----*/
int  Steps2Take;

void setup()   /*----( SETUP: RUNS ONCE )----*/
{
// Nothing  (Stepper Library sets pins as outputs)
}/*--(end setup )---*/

void loop()   /*----( LOOP: RUNS CONSTANTLY )----*/
{
  Steps2Take  =  STEPS_PER_OUTPUT_REVOLUTION ;  // Rotate CW 1 turn
  small_stepper.setSpeed(250);
  small_stepper.step(Steps2Take);
  delay(1000);

  Steps2Take  =  - STEPS_PER_OUTPUT_REVOLUTION;  // Rotate CCW 1 turn
  small_stepper.setSpeed(500);  // 700 a good max speed??
  small_stepper.step(Steps2Take);
  delay(2000);

}/* --(end main loop )-- */

/* ( THE END ) */

28BYJ-48 步進馬達實驗 (二相激磁)

// here in the sequence 1-3-2-4 for proper sequencing
Stepper small_stepper(STEPS_PER_MOTOR_REVOLUTION, 6, 8, 7, 9);

留意 順序 1-3-2-4  對應 接腳 6, 8, 7, 9



5V Stepper Motors
You will need a stepper library, but there is one built into Arduino, so no installation should be needed.

These steppers normally have 4096 steps per revolution, but the built in stepper library works in
4 step sequence vs the 8 step sequence that it would expect, so we need to consider it as 2048, though you might find this is not completely accurate either and you may need to tweak code.

The code expects this pinout - you may modify as necessary:
IN1 --> 6
​IN2 --> 7
​IN3 --> 8
​IN4 --> 9

5-12V+ -->5V ----要連接
​Gnd- --> Gnd
----要連接


//========程式=============
//2 phase step motor control
/*-----( Import needed libraries )-----*/
#include <Stepper.h>

/*-----( Declare Constants, Pin Numbers )-----*/
//---( Number of steps per revolution of INTERNAL motor in 4-step mode )---
#define STEPS_PER_MOTOR_REVOLUTION 32

//---( Steps per OUTPUT SHAFT of gear reduction )---
#define STEPS_PER_OUTPUT_REVOLUTION 32 * 128  //4096

/*-----( Declare objects )-----*/
// create an instance of the stepper class, specifying
// the number of steps of the motor and the pins it's
// attached to

//The pin connections need to be 4 pins connected
// to Motor Driver In1, In2, In3, In4  and then the pins entered
// here in the sequence 1-3-2-4 for proper sequencing
Stepper small_stepper(STEPS_PER_MOTOR_REVOLUTION, 6, 8, 7, 9);


/*-----( Declare Variables )-----*/
int  Steps2Take;

void setup()   /*----( SETUP: RUNS ONCE )----*/
{
// Nothing  (Stepper Library sets pins as outputs)
}/*--(end setup )---*/

void loop()   /*----( LOOP: RUNS CONSTANTLY )----*/
{
  small_stepper.setSpeed(1);   // SLOWLY Show the 4 step sequence
  Steps2Take  =  4;  // Rotate CW
  small_stepper.step(Steps2Take);
  delay(2000);

  Steps2Take  =  STEPS_PER_OUTPUT_REVOLUTION / 2;  // Rotate CW 1/2 turn
  small_stepper.setSpeed(100);
  small_stepper.step(Steps2Take);
  delay(1000);

  Steps2Take  =  - STEPS_PER_OUTPUT_REVOLUTION / 2;  // Rotate CCW 1/2 turn
  small_stepper.setSpeed(700);  // 700 a good max speed??
  small_stepper.step(Steps2Take);
  delay(2000);

}/* --(end main loop )-- */

/* ( THE END ) */

28BYJ-48 步進馬達實驗 (一/二相激磁)







 28BYJ-48 5V DC 的話,查詢到的規格上寫著,步進角為 `5.625 / 64`,因此這馬達轉一圈需要的步數是 `360 / (5.625 / 64)`,就是 `4096` 步  (一/二相激磁才會有的步數)


程式 : 一/二相激磁
int Pin0 = 6;
int Pin1 = 7;
int Pin2 = 8;
int Pin3 = 9;
int _step = 0;
int count=0 ;
boolean dir = true;
// gre
void setup()
{
 pinMode(Pin0, OUTPUT);
 pinMode(Pin1, OUTPUT);
 pinMode(Pin2, OUTPUT);
 pinMode(Pin3, OUTPUT);
}
 void loop()
{
 switch(_step){
   case 0:
     digitalWrite(Pin0, LOW);
     digitalWrite(Pin1, LOW);
     digitalWrite(Pin2, LOW);
     digitalWrite(Pin3, HIGH);
   break;
   case 1:
     digitalWrite(Pin0, LOW);
     digitalWrite(Pin1, LOW);
     digitalWrite(Pin2, HIGH);
     digitalWrite(Pin3, HIGH);
   break;
   case 2:
     digitalWrite(Pin0, LOW);
     digitalWrite(Pin1, LOW);
     digitalWrite(Pin2, HIGH);
     digitalWrite(Pin3, LOW);
   break;
   case 3:
     digitalWrite(Pin0, LOW);
     digitalWrite(Pin1, HIGH);
     digitalWrite(Pin2, HIGH);
     digitalWrite(Pin3, LOW);
   break;
   case 4:
     digitalWrite(Pin0, LOW);
     digitalWrite(Pin1, HIGH);
     digitalWrite(Pin2, LOW);
     digitalWrite(Pin3, LOW);
   break;
   case 5:
     digitalWrite(Pin0, HIGH);
     digitalWrite(Pin1, HIGH);
     digitalWrite(Pin2, LOW);
     digitalWrite(Pin3, LOW);
   break;
     case 6:
     digitalWrite(Pin0, HIGH);
     digitalWrite(Pin1, LOW);
     digitalWrite(Pin2, LOW);
     digitalWrite(Pin3, LOW);
   break;
   case 7:
     digitalWrite(Pin0, HIGH);
     digitalWrite(Pin1, LOW);
     digitalWrite(Pin2, LOW);
     digitalWrite(Pin3, HIGH);
   break;
   default:
     digitalWrite(Pin0, LOW);
     digitalWrite(Pin1, LOW);
     digitalWrite(Pin2, LOW);
     digitalWrite(Pin3, LOW);
   break;
 }

 count++;
 if (count>4096) {   // 28BYJ-48 5V DC  360 / (5.625 / 64) = 4096 step
  count=0;
  dir=!dir;
  delay(1000);
 }

 if(dir){
   _step++;
 }else{
   _step--;
 }

 if(_step>7){
   _step=0;
 }
 if(_step<0){
   _step=7;
 }
 delay(1);
}

Arduino Yún


Arduino Yún




2016年4月15日 星期五

手機與Arduino 互動入門 (3/3) 勤益科大 上課 講義

手機與Arduino 互動入門 (3/3)   勤益科大 上課 講義

下載點

Android 藍牙控制 4個 LED 並回傳 I/O狀態 (EX3-3)

Android 藍牙控制 4個 LED 並回傳 I/O狀態
手機畫面 : 

 控制元件畫面 :    TimerInterval  100 

初始化 : 

藍芽連接 與 斷線 : 



Timer1 控制 : 



控制LED 按鈕 : 


 Build App (QR code For .apk)




// Arduino 程式

#include <SoftwareSerial.h>
#include <Wire.h>//引用二個函式庫SoftwareSerial及Wire
SoftwareSerial BT(11,10); //define serial BT  RX=10, TX=11
int time = millis(); //millisceond
void setup()
{
  Serial.begin(9600);
  BT.begin(9600);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  delay(20);
  digitalWrite(7, LOW); 
  digitalWrite(6, LOW); 
  digitalWrite(5, LOW); 
  digitalWrite(4, LOW); 
  
}

void loop()
{
      
  if(BT.available()) //received BT data
  {
    int LED = BT.read();
    Serial.print("Received BT cmd: ");
    Serial.println(LED);
    if(LED == 1)                     //turn on LED if BT cmd is 1
      digitalWrite(7, HIGH);
    else if(LED == 0)                //turn off LED if BT cmd is 0
      digitalWrite(7, LOW); 
     
    if(LED == 3)                     //turn on LED if BT cmd is 1
      digitalWrite(6, HIGH);
    else if(LED == 2)                //turn off LED if BT cmd is 0
      digitalWrite(6, LOW); 
   
    if(LED == 5)                     //turn on LED if BT cmd is 1
      digitalWrite(5, HIGH);
    else if(LED == 4)                //turn off LED if BT cmd is 0
      digitalWrite(5, LOW); 
   
    if(LED == 7)                     //turn on LED if BT cmd is 1
      digitalWrite(4, HIGH);
    else if(LED == 6)                //turn off LED if BT cmd is 0
      digitalWrite(4, LOW); 
   
  }
   
  int now_time = millis();
  if((now_time - time) >= 500)     //actives every 100 milliseconds
  {
    for (int i=0 ;i<=3 ; i++) {
    
    switch (i) {
    case 0:  
            { int LED1_status = digitalRead(7);  //send the status of LED to smartphone over BT
            BT.write(0x10+LED1_status); 
            break; }
    case 1:
           { int LED2_status = digitalRead(6);  //send the status of LED to smartphone over BT
            BT.write(0x20+LED2_status);
            break; }
    case 2 :         
            { int LED3_status = digitalRead(5);  //send the status of LED to smartphone over BT
            BT.write(0x30+LED3_status); 
            break; }
    default:        
            { int LED4_status = digitalRead(4);  //send the status of LED to smartphone over BT
            BT.write(0x40+LED4_status); 
            break;
            }
    }  //switch
    
    } //for
    
    //Serial.print("Send LED_status: ");
    //Serial.print(LED1_status);
    //Serial.print("  ");
    //Serial.print(LED2_status);
    //Serial.print("  ");
    //Serial.print(LED3_status);
    //Serial.print("  ");
    //Serial.println(LED4_status);
    
    time = now_time;
  }
   
}


Android 藍牙控制 1個 LED 閃爍 (EX3-1)

源自於  http://blog.cavedu.com/programming-language/appinventor/paperduino-%E8%97%8D%E7%89%99%E6%8E%A7%E5%88%B6/

手機畫面

1) 選擇藍牙裝置;

2)控制 LED 的按鈕


3) 斷線用的按鈕。







手機程式
初始化:

藍芽清單設定:

  


 LED控制按鈕:



藍芽斷線按鈕:




Build APP (QB code for .apk)








// Arduino 程式

#include <SoftwareSerial.h>   //引用SoftwareSerial函式庫

int led = 2;  //LED在DI1
SoftwareSerial BT(11, 10); 

//設定藍牙在Paperduino上TX、RX的腳位

void setup() {
  BT.begin(9600); 
              //設定與藍芽通訊的鮑率
  pinMode(led, OUTPUT);
}

void loop() {
  int insize;
  byte cmmd[20];
  if ((insize = BT.available() > 0)) //判斷是否有接收到藍牙訊息
  {
    for (int i = 0; i < insize; i++)
      cmmd[i] = BT.read();            //讀取藍牙資訊
    switch (cmmd[0])
    {
      case 'a':                            //若接受到字元'a'時,則讓LED亮
        digitalWrite(led, HIGH);
        break;
      case 'b':                           //若接受到字元'b'時,則讓LED亮
        digitalWrite(led, LOW);
        break;
    }
  }
}



2016年4月12日 星期二

App Inventor 2 的第一支程式 - Hello Purr

App Inventor 2 的第一支程式 - Hello Purr


Hello Purr 的功能為進入 APP 後,顯示一張可愛的貓咪圖

片,使用者點選圖片後即發出一聲喵叫聲。





  1)  新增一個名稱為「CAT_Hello_Purr」的專案,按下 OK 後,即可進入 App Inventor 的  
       Designer 視窗。

  2) 新增一個Button 
  在視窗左側的元件面板(Palette)中,點選 User Interface 中的按鈕(Button)元件,按住滑

  鼠不放將其拖曳至中間的畫面預覽(Viewer)視窗,即可為 APP 程式新增一個按鈕元件。



3) 視窗右側的屬性(Properties)面板上,點選 Image 屬性,上傳一張圖片當作按鈕上的顯示

圖片,點選後會出現過去曾上傳的檔案清單,也可以直接點選上傳檔案(Upload File)按鈕,

選擇電腦中的圖片上傳。





上傳圖片按 OK 後,即可看到中間的畫面預覽視窗出現貓咪的圖片,但此時圖片上會顯示 

Text for Button1 的文字,這原本是顯示在按鈕上的提示語,

4)  可在視窗右側的屬性(Properties)面板上,找到 Width 屬性,設定為 Fill Parent 將貓咪 
      的圖片填滿

5)   可在視窗右側的屬性(Properties)面板上,找到 Text 屬性,將方框中的文字清除




6) 在視窗左側的元件面板(Palette)中,點選媒體(Media)選單中的聲音(Sound)元件,按住

滑鼠不放將其拖曳至中間的畫面預覽(Viewer)視窗,即可為 APP 程式新增一個聲音元件。

聲音元件置於視窗的底部。


7) 在視窗右側的屬性(Properties)面板上,點選 Sound 屬性,再按下檔案來源(Source)按

鈕,準備上傳喵叫聲的檔案。接著,點選上傳檔案(Upload File)按鈕,選擇電腦中的貓叫聲

音檔(meow.mp3)上傳。



8) 修改APP 的 Title 



9) 完成 APP 的視窗畫面佈局(Layout)後,開始使用 App Inventor 來寫程式,請先從視窗

右上角的 Designer 視窗切換至 Blocks 視窗。



10) 在左側 Blocks 選單中點選 Screen1 下的 Button1,在彈出的拼圖式程式區塊中選取 

when Button1 Click do 的事件區塊(event handler),置於預覽視窗中。接著在點選 

Screen1 下的 Sound1,在彈出的拼圖式程式區塊中選取 call Sound1 Play 的命令區塊

(command block),將其卡入 when Button1 Click do 的事件區塊中,AI2會自動將其嵌入

並發出提示音效,表示已將兩程式區塊合併




「當 Button1 按鈕被按下後,呼叫播放 Sound1 的副程式」。


when Button1 Click do 是一種事件區塊(event handler)


call Sound1 Play 則是一種命令區塊(command block)




11 ) 加入震動

按下貓咪圖案時,不僅會發出一聲喵叫聲,手機還會震動半秒鐘


請在左側 Blocks 選單中點選 Screen1 下的 Sound1,在彈出的拼圖式程式區塊中選取 

call Sound1 Vibrate 的命令區塊(command block),然後將其卡入 when Button1 Click 

do 事件區塊中的 call Sound1 Play 下。



 12) . Build APP   (Provide QR Code for .apk)



13) 利用手機 的APP 程式    MIT AI2 Companion 掃瞄並下載APP程式 測試並執行APP


DHT11 (Node-Red) +PostgreSQL 模擬

 DHT11 (Node-Red) +PostgreSQL 模擬 [{"id":"acddd911a6412f0a","type":"inject","z":"08dc4...