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

งานที่10 Home Automation using Bluetooth and Mobile App



อุปกรณ์

  • PIC 16F876A Microcontroller
  • HC-06 Bluetooth Module
  • ULN2803 Driver IC
  • 8x 12V Sugar Cube Relays
  • Voltage Regulator IC 7812
  • Voltage Regulator IC 7805
  • 4x 1N4007 Diode
  • 8MHz Crystal
  • Red LED
  • Orange LED
  • 8x Green LED
  • 1000uF Capacitor
  • 3x 100uF Capacitor
  • 10uF Capacitor
  • 1uF Capacitor
  • 0.1uF Capacitor
  • 2x 22pF Capacitor
  • 10KΩ Resistor
  • 1KΩ Resistor
  • 10x 680Ω Resistor
code

int cnt,t,st;
void interrupt()  
{
  if(UART1_Data_Ready())      // If data received
  {
    RCIE_bit = 0;
    if(cnt == 0)              //If it is the first character
    {
      if(UART1_Read() == 'A') // Check whether it is the valid character
      {
        cnt = 1;
        TMR1L = 0;
        TMR1H = 0;
        TMR1ON_bit = 1;       // Start the timer for timing out
      }
    }
    else if(cnt == 1)         // If second character
    {
      t = UART1_Read() - 48;
      cnt++;
    }
    else if(cnt == 2)         // If third character
    {
      st = UART1_Read() - 48;
      cnt++;
    }
    else if(cnt >= 3)         // Clear if count is greater than 2
    {
      cnt = 0;
      TMR1ON_bit = 0;
      TMR1L = 0;
      TMR1H = 0;
    }
    RCIE_bit = 1;
  }
  else if(TMR1IF_bit == 1)   // UART Timeout
  {
    TMR1IE_bit = 0;
    TMR1IF_bit = 0;
    TMR1H = 0;
    TMR1L = 0;
    cnt = 0;
    TMR1IE_bit = 1;
  }
}

void main()
{
  TRISB = 0x00;   // Make all PORTB pins output
  PORTB = 0x00;   // Turn off All relays

  GIE_bit = 1;    // Global Interrupt Enable
  PEIE_bit = 1;   // Peripheral Interrupt Enable
  RCIE_bit = 1;   // UART Interrupt Enable
  T1CON = 0x30;   // Configure Timer 1
  TMR1IE_bit = 1; // Enable Timer1 Interrupt

  cnt = 0;
  UART1_Init(9600);

  while(1)  
  {
    if(st == 1)    // Relay Status Flag is 1
    {
      if(t != 0)   // t = 0, to control all realys, master switch
        PORTB = PORTB | (0xFF & (1<<(t-1))); // Turn ON individual relay
      else
        PORTB = 0xFF; // Turn ON All
    }
    else           // Relay status flag is 0
    {
      if(t != 0)   // t = 0, to control all realys, master switch
        PORTB = PORTB & (0xFF & ~(1<<(t-1))); // Turn OFF individual relay
      else
        PORTB = 0; // Turn OFF all
    }
  }
}
อ้างอิง

ไม่มีความคิดเห็น:

แสดงความคิดเห็น