วันพฤหัสบดีที่ 27 กุมภาพันธ์ พ.ศ. 2563



Home work

งานที่ 11


16F877


      จากรูปการเชื่อมต่อวงจรด้านบนนี้ เป็นการเชื่อมต่อ Microcontroller PIC เบอร์ 16F877 ซึ่งเป็นไมโครคอนโทรลเลอร์ขนาด 40 ขา โดยต่อออกพอร์ต B ตั้งแต่ RB0-RB7 ผ่านไอซีบัฟเฟอร์เบอร์ 74LS245 เพื่อขยายกระแสไปขับโหลดแสดงผล LED ทั้ง 8 หลอด โดยต่อสัญญาณไฟ 5V เข้าที่ขา 11 และกราวน์เข้าที่ขา 12 ของไมโครคอนโทรลเลอร์

เขียนโค๊ตใช้งานจริง                  
     #include <16F877.h>
     #fuses    HS, NOPUT, NOWDT, NOPROTECH
     #use      delay (Clock = 4000000) 
     void main()
     {
          while(1)
          {
              output_high(pin_B0);
              delay_ms(500);
              output_low(pin_B0);
              delay_ms(500);
           }   
      }

อธิบายโปรแกรม                        
บรรทัดที่ 1 เป็นการเรียกไฟล์ 16F877.h มาร่วมในการคอมไพล์ ทำให้การเขียนโปรแกรม
         สามารถใช้รีจิสเตอร์ของ PIC ได้
บรรทัดที่ 2 การกำหนดคุณสมบัติของอุปกรณ์
HS      หมายถึง  โหมดสัญญาณนาฬิกา แบบ High Speed Crystal/Resonator
                คือทำงานที่ความถี่ 4 - 20 MHz
NOPUT   หมายถึง  ไม่ใช้งานวงจรเพาเวอร์อัพไทเมอร์
NOWDT   หมายถึง  ไม่ใช้งานวงจรวอตช์ดอกไทเมอร์
NOPROTECH  หมายถึง  ไม่ป้องกันการกอปปี้โปรแกรม
บรรทัดที่ 3 การเรียกฟังก์ชัน delay และกำหนดค่าสัญญาณนาฬิกาให้มีค่าความถี่เท่ากับ 4 MHz
บรรทัดที่ 5 การวนรอบส่งข้อมูลออกพอร์ต B0 และ while(1) หมายถึงวนรอบตลอดการ
บรรทัดที่ 6 ส่งข้อมูล หรือ ลอจิก "1" ออกที่พอร์ต B0
บรรทัดที่ 7 หน่วงเวลา 500ms หรือ 0.5 วินาที
บรรทัดที่ 8 ส่งข้อมูล หรือลอจิก "0" ออกที่พอร์ต B0
บรรทัดที่ 9 หน่วงเวลา 500ms หรือ 0.5 วินาที

Home work

งานที่ 10

       ไมโครคอนโทรลเลอร์ เป็นอุปกรณ์ชิปไอซีพิเศษชนิดหนึ่ง ที่เราสามารถเขียนโปรแกรม
เพื่อควบคุมการทำงานตามที่ต้องการได้

ส่วนประกอบ

-หน่วยประมวลผล
-หน่วยความจำชั่วคราว (RAM)
-หน่วยความจำถาวร (ROM)
-พอร์ตอินพุต,เอาท์พุต


การปรยุกต์ใช้ Microcontroller กับ arduino
    
   IC 74HC595
            ไอซี 74HC595 เป็นไอซีเลื่อนบิต เมื่อมีการป้อนข้อมูลเข้าไปใหม่ บิตจะถูกเลื่อนไปตามข้อมูลที่ป้อน มีขาทั้งหมด 16 ขา เป็นขาเอาต์พุตที่ควบคุมได้ทั้งหมด 8 ขา ตั้งแต่ Q0 Q1 Q2 ... Q7 และมีขาที่ใช้สำหรับการควบคุม อีก 3 ขา

การควบคุมการทำงานไอซี 74HC595

การควบคุมการส่งข้อมูลเพื่อเปลี่ยนแปลง Q0 - Q7 มีขั้นตอนดังนี้
Step 1 เซ็ตให้ขา ST_CP มีสถานะเป็น LOW
Step 2 เซ็ตให้ขา DS เป็นข้อมูลบิตนั้นๆ
Step 3 เซ็ตให้ขา SH_CP มีสถานะเป็น HIGH
Step 4 หน่วงเวลา
Step 5 เซ็ตให้ขา SH_CP มีสถานะเป็น LOW
Step 6 หน่วงเวลา
Step 7 เริ่มกลับไปทำ Step 2 จนครบหมดทุกบิต
Step 8 เซ็ตให้ขา ST_CP มีสถานะเป็น HIGH

การใช้งานจริงกับหลอด LED


         Arduino จะมีฟังก์ชั่น shiftOut() สำเร็จรูปอยู่แล้ว ทำให้สามารถข้ามขั้นตอนตั้งแต่ Step 2 ไปจนถึง Step 7 ได้เลย ทำให้ส่วนโค้ดจริงๆที่ใช้สำหรับส่งข้อมูลไปให้ไอซีมีแค่ส่วนนี้
  
  void DataOut(byte data) {
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, data);
  digitalWrite(latchPin, HIGH);
}
           
         ตัวอย่างโค้ดด้านล่างนี้เป็นโค้ดไฟวิ่งจากขวาไปซ้ายครั้งละดวง โดยใช้ชนิดตัวแปรเป็น byte แล้วเลื่อนบิตไปเรื่อยๆ

int latchPin = 8; // ST_CP
int clockPin = 12; // SH_CP
int dataPin = 11; // DS

byte tmp;

void setup() {
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  digitalWrite(latchPin, HIGH);
}

void loop() {
  if (tmp==0||tmp==0x80) tmp=0x01;
  else tmp=tmp<<1;
  DataOut(tmp);
  delay(500);
}

void DataOut(byte data) {
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, data);
  digitalWrite(latchPin, HIGH);
}


Home work

งานที่ 9

PIC12F1822


คุณสมบัติ

1.ปรับปรุง Core Mid-Range พร้อม 49 Instruction, 16 Stack Levels
2.หน่วยความจำโปรแกรมแฟลชที่มีความสามารถในการอ่าน / เขียนด้วยตนเอง
3.ออสซิลเลเตอร์ภายใน 32MHz
4.โมดูลการตรวจวัด mTouch Capacitive ในตัว
5.โมดูลโมดูเลเตอร์สัญญาณข้อมูล
6.MI2C, SPI, EUSART พร้อมระบบแจ้งเตือนอัตโนมัติ

ตัวอย่าง
แผนผังวงจรตัวอย่างที่ใช้ออสซิลเลเตอร์ภายในของกับไมโครคอนโทรลเลอร์ PIC12F1822


Code

// Interfacing  PIC12F1822 with HC-SR04 ultrasonic sensor CCS PIC C code
// 3-Wire LCD driver must be added
// http://ccspicc.blogspot.com/
// electronnote@gmail.com
// Use at your own risk

//LCD module connections
#define LCD_DATA_PIN PIN_A0
#define LCD_CLOCK_PIN PIN_A1
#define LCD_EN_PIN PIN_A2
//End LCD module connections

#include <12F1822.h>
#fuses NOMCLR INTRC_IO
#use delay(clock = 8000000)
#include <3WireLCD.c>
#use fast_io(A)

unsigned int16 i, distance;
int1 wait_sensor(){
  i = 0;
  set_timer1(0);                                      // Reset Timer1
  while(!input(PIN_A5) && (i < 1000))
    i = get_timer1();                                 // Read Timer1 and store its value in i
  if(i > 990)
    return 0;
  else
    return 1;
}
unsigned int16 get_distance(){
  i = 0;
  set_timer1(0);
  while(input(PIN_A5) && (i < 25000))
    i = get_timer1();
  return i;
}
void main() {
  setup_oscillator(OSC_8MHZ);                         // Set internal oscillator to 8MHz
  setup_adc_ports(NO_ANALOGS);                        // Configure all pins as digital
  set_tris_a(0x20);                                   // Configure RA5 pin as input
  lcd_initialize();                                   // Initialize LCD module
  lcd_cmd(LCD_CLEAR);                                 // LCD Clear
  lcd_goto(4, 1);                                     // Go to column 4 row 1
  lcd_out("Distance:");
  SETUP_TIMER_1(T1_INTERNAL | T1_DIV_BY_2);           // Configure Timer 1 to increment by 1 every 1 us
  while(TRUE){
    // Send 10us pulse to HC-SR04 Trigger pin
    output_high(PIN_A4);
    delay_us(10);
    output_low(PIN_A4);
    // Read pulse comes from HC-SR04 Echo pin
    if(wait_sensor()) {
      distance = get_distance();
      if(distance > 24990) {
        lcd_goto(3, 2);                               // Go to column 3 row 2
        lcd_out("Out Of Range");
      }
      else {
        distance = i/58;                              // Calculate the distance
        lcd_goto(3, 2);                               // Go to column 3 row 2
        lcd_out("       cm   ");
        lcd_goto(6, 2);                               // Go to column 6 row 2
        printf(lcd_out,"%3Lu",distance);
      }
    }
    else {
      lcd_goto(3, 2);                                 // Go to column 3 row 2
      lcd_out("  Time Out  ");
    }
  delay_ms(100);
  }
}


Home work

งานที่ 8

PIC18F4550


ตัวอย่างการใช้งาน

เมาส์ USB ควบคุมจากระยะไกลโดยใช้วงจร PIC18F4550

           
           ตัวรับสัญญาณ IR มี 3 พิน: GND, VCC และ OUT PIN OUT เชื่อมต่อกับ RB0 (INT0 pin)
วงจรที่แสดงด้านบนใช้พลังงานจากแหล่งจ่ายไฟ 5V ภายนอกซึ่งหมายความว่าไม่จำเป็นต้องเชื่อมต่อขา VCC ของตัวเชื่อมต่อ USB แต่สามารถใช้พินนี้เพื่อจัดหาวงจรและดังนั้นจึงต้องถอดแหล่งจ่ายไฟ 5V ภายนอก ในโครงการนี้ไมโครคอนโทรลเลอร์ใช้กับคริสตัลออสซิลเลเตอร์ 8 MHz และฟังก์ชั่นพิน MCLR ถูกปิดใช้

code
// IR Remote controlled USB mouse using PIC18F4550 microcontroller CCS C code.
// Car MP3 IR remote control is used (NEC protocol)
// http://ccspicc.blogspot.com/
// electronnote@gmail.com

#include <18F4550.h>
#fuses  HS, CPUDIV1, PLL2, USBDIV, VREGEN, NOMCLR, NOWDT, NOLVP
#use delay(clock = 8MHz)
#include <usb_desc_mouse.h>
#include<pic18_usb.h>
#include<usb.c>

signed int8 out_data[4] = {0, 0, 0, 0};
short nec_ok = 0, repeated = 0;
int8 i, nec_state = 0;
int32 nec_code;
#INT_EXT                                         // External interrupt
void ext_isr(void){
  int16 time;
  if(nec_state != 0){
    time = get_timer1();                         // Store Timer1 value
    set_timer1(0);                               // Reset Timer1
  }
  switch(nec_state){
    case 0 :                                     // Start receiving IR data (we're at the beginning of 9ms pulse)
      setup_timer_1( T1_INTERNAL | T1_DIV_BY_2 );   // Enable Timer1 module with internal clock source and prescaler = 2
      set_timer1(0);                             // Reset Timer1 value
      nec_state = 1;                             // Next state: end of 9ms pulse (start of 4.5ms space)
      i = 0;
      ext_int_edge( L_TO_H );                    // Toggle external interrupt edge
      return;
    case 1 :                                     // End of 9ms pulse
      if((time > 9500) || (time < 8500)){        // Invalid interval ==> stop decoding and reset
        nec_state = 0;                           // Reset decoding process
        setup_timer_1(T1_DISABLED);              // Stop Timer1 module
      }
      else
        nec_state = 2;                           // Next state: end of 4.5ms space (start of 562µs pulse)
      ext_int_edge( H_TO_L );                    // Toggle external interrupt edge
      return;
    case 2 :                                     // End of 4.5ms space
      if((time > 5000) || (time < 1500)){        // Invalid interval ==> stop decoding and reset
        nec_state = 0;                           // Reset decoding process
        setup_timer_1(T1_DISABLED);              // Stop Timer1 module
        return;
      }
      nec_state = 3;                             // Next state: end of 562µs pulse (start of 562µs or 1687µs space)
      if(time < 3000)                            // Check if previous code is repeated
        repeated = 1;
      ext_int_edge( L_TO_H );                    // Toggle external interrupt edge
      return;
    case 3 :                                     // End of 562µs pulse
      if((time > 700) || (time < 400)){          // Invalid interval ==> stop decoding and reset
        nec_state = 0;                           // Reset decoding process
        setup_timer_1(T1_DISABLED);              // Disable Timer1 module
      }
      else{
        // Check if the code is repeated
        if(repeated){
          set_timer3(25000);
          repeated = 0;
          nec_ok = 1;                            // Decoding process is finished with success
          return;
        }
        nec_state = 4;                           // Next state: end of 562µs or 1687µs space
      }
      ext_int_edge( H_TO_L );                    // Toggle external interrupt edge
      return;
    case 4 :                                     // End of 562µs or 1687µs space
      if((time > 1800) || (time < 400)){         // Invalid interval ==> stop decoding and reset
        nec_state = 0;                           // Reset decoding process
        setup_timer_1(T1_DISABLED);              // Disable Timer1 module
        return;
      }
      if( time > 1000)                           // If space width > 1ms (short space)
        bit_set(nec_code, (31 - i));             // Write 1 to bit (31 - i)
      else                                       // If space width < 1ms (long space)
        bit_clear(nec_code, (31 - i));           // Write 0 to bit (31 - i)
      i++;
      if(i > 31){                                // If all bits are received
        nec_ok = 1;                              // Decoding process OK
      }
      nec_state = 3;                             // Next state: end of 562µs pulse (start of 562µs or 1687µs space)
      ext_int_edge( L_TO_H );                    // Toggle external interrupt edge
  }
}
#INT_TIMER1                                      // Timer1 interrupt (used for time out)
void timer1_isr(void){
  nec_ok = 0;
  nec_state = 0;                                 // Reset decoding process
  ext_int_edge( H_TO_L );                        // External interrupt edge from high to low
  setup_timer_1(T1_DISABLED);                    // Disable Timer1 module
  clear_interrupt(INT_TIMER1);                   // Clear Timer1 interrupt flag bit
}
#INT_TIMER3                                      // Timer1 interrupt (used for time out)
void timer3_isr(void){
  nec_code = 0;
  setup_timer_3(T3_DISABLED);                    // Disable Timer3 module
  clear_interrupt(INT_TIMER3);                   // Clear Timer3 interrupt flag bit
}
void main(){
  delay_ms(500);
  usb_init();                                    // Initialize USB hardware
  enable_interrupts(GLOBAL);                     // Enable global interrupts
  enable_interrupts(PERIPH);
  enable_interrupts(INT_EXT_H2L);                // Enable external interrupt
  clear_interrupt(INT_TIMER1);                   // Clear Timer1 interrupt flag bit
  enable_interrupts(INT_TIMER1);                 // Enable Timer1 interrupt
  clear_interrupt(INT_TIMER3);                   // Clear Timer3 interrupt flag bit
  enable_interrupts(INT_TIMER3);                 // Enable Timer3 interrupt
  while(TRUE){
    if(nec_ok){                                  // If the mcu successfully receives NEC protocol message
      nec_ok = 0;                                // Reset decoding process
      nec_state = 0;
      setup_timer_1(T1_DISABLED);                // Disable Timer1 module
      set_timer3(25000);
      setup_timer_3( T3_INTERNAL | T1_DIV_BY_8 );    // Enable Timer3 module with internal clock source and prescaler = 8
      if(nec_code == 0x40BF30CF)                 // Button 1 code (mouse left button)
        out_data[0] = 1;
      if(nec_code == 0x40BFB04F)                 // Button 2 code (move cursor up)
        out_data[2] = -1;
      if(nec_code == 0x40BF708F)                 // Button 3 code (mouse right button)
        out_data[0] = 3;
      if(nec_code == 0x40BF08F7)                 // Button 4 code (move cursor left)
        out_data[1] = -1;
      if(nec_code == 0x40BF8877)                 // Button 5 code (move cursor down)
        out_data[2] = 1;
      if(nec_code == 0x40BF48B7)                 // Button 6 code (move cursor right)
        out_data[1] = 1;
      while(nec_code != 0)
        usb_put_packet(1, out_data, 4, USB_DTS_TOGGLE);    // Send USB packet
      delay_ms(10);
      out_data[0] = 0;
      out_data[1] = 0;
      out_data[2] = 0;
      usb_put_packet(1, out_data, 4, USB_DTS_TOGGLE);      // Send USB packet
    }
  }
}

Home work

งานที่ 7

AT89C52

คุณสมบัติ

•เข้ากันได้กับผลิตภัณฑ์ MCS-51 ™
• 8K Bytes ของหน่วยความจำแฟลชในระบบ Reprogrammable
•ความทนทาน: 1,000 รอบการเขียน / ลบ
•การทำงานแบบคงที่: 0 Hz ถึง 24 MHz
•ล็อคหน่วยความจำโปรแกรมสามระดับ
• RAM ภายใน 256 x 8 บิต
• 32 บรรทัด I / O ที่ตั้งโปรแกรมได้
•ตัวจับเวลา / ตัวนับ 16 บิตสามตัว
•แปดแหล่งขัดจังหวะ
•ช่องอนุกรมที่ตั้งโปรแกรมได้
•โหมดเดินเบาและโหมดประหยัดพลังงาน




ภาพวงจร


CODE
#include < reg51.h >
sbit switch1=P1^0;
sbit switch2=P1^1;
sbit switch3=P1^2;
sbit switch4=P1^3;
sbit switch5=P1^4;
sbit switch6=P1^5;
int i,j,l;
int n=7;
void delay(int k) //This function produces a delay in msec.
{
for(i=0;i=0;l–)
{
P2=arr[l];
delay(50);
}
}
if(switch3==0)
{
P2=0x7E;
delay(50);
P2=0xBD;
delay(50);
P2=0xDB;
delay(50);
P2=0xE7;
delay(50);
}
if(switch4==0)
{
P2=0xE7;
delay(50);
P2=0xDB;
delay(50);
P2=0xBD;
delay(50);
P2=0x7E;
delay(50);
}
if(switch5==0)
{
P2=0xAA;
delay(50);
P2=0x55;
delay(50);
}
if(switch6==0)
{
P2=0x00;
delay(50);
P2=0xFF;
delay(50);
}
}


Home work

งานที่ 6


PIC12F683


คุณสมบัติ

1.แม่นยำภายใน Oscillator:

   - โรงงานปรับเทียบเป็น± 1% โดยทั่วไป
   - ซอฟต์แวร์เลือกช่วงความถี่ได้ 8 MHz ถึง 125 kHz
   - ซอฟต์แวร์ปรับได้
   - โหมดเริ่มต้นสองความเร็ว
   - Crystal ล้มเหลวตรวจหาแอปพลิเคชันที่สำคัญ
   - สลับโหมดนาฬิการะหว่างการใช้งาน ประหยัดพลังงาน
2.โหมดประหยัดพลังงาน Sleep
3.ช่วงแรงดันไฟฟ้าที่กว้าง (2.0V-5.5V)

ตัวอย่างการต่อใช้งาน


CODE
/*

  Project: Mini LED Christmas Tree
  Author: Raj Bhatt
  Date: Dec 12, 2013
  Description: PIC12F683 microcontroller drives 20 LEDs through five I/O pins
               using the Charlieplexing technique. The LEDs are arranged in
               a foam board cut in the shape of a Christmas tree.
*/

// Five Charlieplexing signal lines
#define Row_A GP0_bit
#define Row_B GP1_bit
#define Row_C GP2_bit
#define Row_D GP4_bit
#define Row_E GP5_bit

unsigned int i, j, k, loop=120;

// Pin I/O direction and data values for lighting up each LED
const unsigned short TRISIO_Data[]={0x3c, 0x39, 0x2b, 0x0f, 0x0f, 0x2b, 0x39, 0x3c, \
         0x3a, 0x1b, 0x1b, 0x3a, 0x2d, 0x2d, 0x2e, 0x2e, 0x1d, 0x1d, 0x1e, 0x1e};
const unsigned short PORT_Data[] = {0x01, 0x02, 0x04, 0x10, 0x20, 0x10, 0x04, 0x02, \
         0x01, 0x04, 0x20, 0x04, 0x02, 0x10, 0x01, 0x10, 0x02, 0x20, 0x01, 0x20};
         
// Four user defined LED glow patterns
const unsigned short Pattern_Data1[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, \
                                      0, 0, 0, 0, 0, 0};   // Border LEDs ON
const unsigned short Pattern_Data2[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, \
                                      0, 1, 0, 0, 0, 1};   // Light 14, 16, and 20
const unsigned short Pattern_Data3[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, \
                                      0, 1, 1, 0, 1, 1};   // Light 13, 17, and 19
const unsigned short Pattern_Data4[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \
                                      1, 1, 1, 1, 1, 1};   // Light all


void ALL_LEDs_OFF(){
  GPIO = 0x00;
  TRISIO = 0xFF;
}

void LED_ON(unsigned short num){
  TRISIO = TRISIO_Data[num-1];
  GPIO = PORT_Data[num-1];
  Delay_us(500);
}

void Display_Pattern(unsigned short m){
  for(j=1; j<=20; j++){
    switch (m) {
      case 1 : if(Pattern_Data1[j-1] == 1) LED_ON(j);
               break;
      case 2 : if(Pattern_Data2[j-1] == 1) LED_ON(j);
               break;
      case 3 : if(Pattern_Data3[j-1] == 1) LED_ON(j);
               break;
      case 4 : if(Pattern_Data4[j-1] == 1) LED_ON(j);
               break;

    } //case end
  }
}

void main() {
CMCON0 = 7;
ANSEL = 0;
GPIO = 0;
TRISIO = 0xFF;
do {
  for(k=0; k<loop; k++){
    Display_Pattern(1);
  }
  for(k=0; k<loop; k++){
    Display_Pattern(2);
  }
  
  for(k=0; k<loop; k++){
    Display_Pattern(3);
  }
  
  for(k=0; k<loop; k++){
    Display_Pattern(4);
  }

  ALL_LEDs_OFF();
  Delay_ms(200);
  
  for(k=0; k<loop; k+=4){
    Display_Pattern(4);
  }
  
  ALL_LEDs_OFF();
  Delay_ms(200);

  for(k=0; k<loop; k+=4){
    Display_Pattern(4);
  }
  
  ALL_LEDs_OFF();
  Delay_ms(200);
  
  for(k=0; k<loop; k+=4){
    Display_Pattern(4);
  }
  ALL_LEDs_OFF();
  Delay_ms(500);

 } while(1) ;
}

งานที่2

 เกลียว ชนิดและส่วนต่างๆของเกลียว เกลียว ( Thread)  หมายถึงสันหรือร่องที่เกิดขึ้นบนผิวงานวนไปรอบ ๆจะซ้ายหรือขวาก็ไดด้วยระยะทางที่สม่ำเสมอ เ...