Sensor and LCD do not work. Only the light is controlled through 'App Inventor'.(done)

1 post / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The compiler and the debugger are fine.

 

PIR sensor does not work

The LCD does not indicate the status (ON / OFF) of the light bulb (PB4)

 

Why is it only light control?(using 'App Inventor')

 

Show lcd now

"Hello induction"

"State : Plz key" // "Plz key" is not displayed as "On" or "OFF".

 

In addition, the infrared light works but now it does not work.

 

<Code vision>

#include <io.h>
#include <delay.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <mega128.h>

#define LCD_WDATA PORTA  

#define LCD_WINST PORTA  

#define LCD_CTRL PORTG 

#define LCD_EN 0       

#define LCD_RW 1

#define LCD_RS 2

#define Byte unsigned char

#define PIR_sensor PIND4 // PIR Sensor PD4

#define sw PINB // led PORTB

 
void tx_data(unsigned char data)

{

  while(!(UCSR1A & 0x20));

  UDR1 = data;
 }

unsigned char rx_data(void)

{

  while(!(UCSR1A & 0x80));

  return UDR1;
 
 }
 
 
void LCD_Data(Byte ch)

{

    LCD_CTRL |=  (1 << LCD_RS); 

    LCD_CTRL &= ~(1 << LCD_RW);

    LCD_CTRL |=  (1 << LCD_EN);  

    delay_us(50);

    LCD_WDATA = ch;              

    delay_us(50);

    LCD_CTRL &= ~(1 << LCD_EN);  

}

                               

 

void LCD_Comm(Byte ch)              

{                                  

    LCD_CTRL &= ~(1 << LCD_RS);  

    LCD_CTRL &= ~(1 << LCD_RW);   

    LCD_CTRL |=  (1 << LCD_EN);

    delay_us(50);

    LCD_WINST = ch;            

    delay_us(50);

    LCD_CTRL &= ~(1 << LCD_EN);  

}

 

void LCD_CHAR(Byte c)

{

    LCD_Data(c);    

    delay_ms(1);

}

 

void LCD_STR(Byte *str)  

{    while(*str !=0)

    {

        LCD_CHAR(*str);

        str++;

    }

}

 

void LCD_pos(unsigned char col, unsigned char row)

{

    LCD_Comm(0x80 | (row+col*0x40)); 

}

 

void LCD_Clear(void)

{

    LCD_Comm(0x01);

    delay_ms(2); 

}

 

 

void LCD_Init(void)  

{

    LCD_Comm(0x38);  

    delay_ms(2);

    LCD_Comm(0x38); 

    delay_ms(2);

    LCD_Comm(0x38); 

    delay_ms(2);

    LCD_Comm(0x0e);

    delay_ms(2);

    LCD_Comm(0x06);

    delay_ms(2);

    LCD_Clear();

 

}

void Switch_Verify(void)
{
    Byte ON[] = "ON";
    Byte OFF[] = "OFF";

   
    switch(sw) // PIR Sensor output -> led PB2, PB3(01,10)
    {
        case 0xeb : LCD_STR(ON); break; // PB4 led on  11101011     
        case 0xed : LCD_STR(ON); break;  // 11101101
        case 0xfb : LCD_STR(OFF); break; // PB4 led off 11111011
        case 0xfd : LCD_STR(OFF); break; // 11111101
    }
}

 void main(void)

 {
    unsigned char getdata=0; // getdata reset
   
    Byte str[] = "Hello induction";

    Byte str1[] = "State : Plz key";

    DDRA = 0xff;

    DDRG = 0x0f;
    DDRB = 0xff;
    DDRE = 0xFE;
    PORTB = 0xFF;
   
    UCSR1A=0; UCSR1B=0x18; UCSR1C=6; UBRR1H=0; UBRR1L=103;
    //9600, no parity, 8data a bit, 1stop bit, I use a 16Mhz clock.
    

   delay_ms(15);

    LCD_Init();

    LCD_pos(0,0);

    LCD_STR(str);

    LCD_pos(1,0);

    LCD_STR(str1);
       
   while(1)
    {
       LCD_pos(1,8);
       Switch_Verify();
       // LCD display according to LED output value
      
       getdata = rx_data();  // Enter 'App Inventor' value
       if(getdata == '1')
       {
           PORTB.4 = 0; // PB4 led ON

      }
       else if(getdata == '2')
       {
           PORTB.4 = 1; // led OFF

      }

    }
  
   DDRD = (0 << PIR_sensor);  

   while(1)
   {  
       while(PIND & (1<<PIR_sensor) == 0) 
       {  
            PORTB.1=0; // PB1 led ON
            PORTB.2=1; // PB2 led OFF

            PORTB.3=1; // PB3 led OFF NOT USE
       }  
    
    
    
    
    
       while(PIND & (1<<PIR_sensor))
       {
           PORTB.1=1;  // PB1 led OFF
           PORTB.2=0;  // PB2 led ON

           PORTB.3=1; // PB3 led OFF NOT USE
       }
         
                 
   } 
   
 }

 

<Schematic>

 

It is not easy to check whether there is an operation.

So I decided to check it with a light.

 

Please give me a hint. *_*

Last Edited: Tue. May 29, 2018 - 04:37 PM