Microcontroller 1602 Infrared Display Experiment Program

Experiment Name: 1602 Display Infrared Value Experiment

IO Used: Motor on P1 port, keyboard uses P3.0, P3.1, P3.2, P3.3

Main Program

#include

#include “lcd.h”

sbit IRIN = P3^2;

unsigned char code CDIS1[13] = {” Red Control “};

unsigned char code CDIS2[13] = {” IR-CODE:–H “};

unsigned char IrValue[6];

unsigned char Time;

void IrInit();

void DelayMs(unsigned int);

void main()

{

unsigned char i;

IrInit();

LcdInit();

LcdWriteCom(0x80);

for(i = 0; i

{

LcdWriteData(CDIS1);

}

LcdWriteCom(0x80 + 0x40);

for(i = 0; i

{

LcdWriteData(CDIS2);

}

while(1)

{

IrValue[4] = IrValue[2] >> 4; // High byte

IrValue[5] = IrValue[2] & 0x0f; // Low byte

if(IrValue[4] > 9)

{

LcdWriteCom(0xc0 + 0x09); // Set display position

LcdWriteData(0x37 + IrValue[4]); // Convert value to ASCII code for display

}

else

{

LcdWriteCom(0xc0 + 0x09);

LcdWriteData(IrValue[4] + 0x30); // Convert value to ASCII code for display

}

if(IrValue[5] > 9)

{

LcdWriteCom(0xc0 + 0x0a);

LcdWriteData(IrValue[5] + 0x37); // Convert value to ASCII code for display

}

else

{

LcdWriteCom(0xc0 + 0x0a);

LcdWriteData(IrValue[5] + 0x30); // Convert value to ASCII code for display

}

}

}

void DelayMs(unsigned int x) // 0.14ms error 0us

{

unsigned char i;

while(x–)

{

for (i = 0; i

{}

}

}

void IrInit()

{

IT0 = 1; // Trigger on falling edge

EX0 = 1; // Enable interrupt 0

EA = 1; // Enable global interrupt

IRIN = 1; // Initialize port

}

void ReadIr() interrupt 0

{

unsigned char j, k;

unsigned int err;

Time = 0;

DelayMs(70);

if(IRIN == 0) // Confirm if the correct signal is received

{

err = 1000; // 1000 * 10us = 10ms, exceeding indicates an incorrect signal

/* Loop while both conditions are true; if one is false, exit to avoid program errors */

while((IRIN == 0) && (err > 0)) // Wait for the previous 9ms low level to pass

{

DelayMs(1);

err–;

}

if(IRIN == 1) // If the correct 9ms low level is detected

{

err = 500;

while((IRIN == 1) && (err > 0)) // Wait for the 4.5ms start high level to pass

{

DelayMs(1);

err–;

}

for(k = 0; k

{

for(j = 0; j

{

err = 60;

while((IRIN == 0) && (err > 0)) // Wait for the 560us low level to pass before the signal

//while (!IRIN)

{

DelayMs(1);

err–;

}

err = 500;

while((IRIN == 1) && (err > 0)) // Calculate the duration of the high level.

{

DelayMs(1); // 0.14ms

Time++;

err–;

if(Time > 30)

{

EX0 = 1;

return;

}

}

IrValue[k] >>= 1; // k indicates the group of data

if(Time >= 8) // If the high level appears for more than 565us, it is 1

{

IrValue[k] |= 0x80;

}

Time = 0; // Reset time after use

}

}

if(IrValue[2] != ~IrValue[3])

{

return;

}

}

}

#include “lcd.h”

Description: This function provides a delay for the microcontroller with a 12MHz crystal oscillator at a 12 frequency division.

#include “lcd.h”

void Lcd1602_Delay1ms(uint c) // Error 0us

{

uchar a, b;

for (; c > 0; c–)

{

for (b = 199; b > 0; b–)

{

for(a = 1; a > 0; a–);

}

}

}

#ifndef LCD1602_4PINS // When this LCD1602_4PINS is not defined

void LcdWriteCom(uchar com) // Write command

{

LCD1602_E = 0; // Enable

LCD1602_RS = 0; // Select command to send

LCD1602_RW = 0; // Select write

LCD1602_DATAPINS = com; // Put command

Lcd1602_Delay1ms(1); // Wait for data stability

LCD1602_E = 1; // Write timing

Lcd1602_Delay1ms(5); // Hold time

LCD1602_E = 0;

}

#else

void LcdWriteCom(uchar com) // Write command

{

LCD1602_E = 0; // Enable clear

LCD1602_RS = 0; // Select write command

LCD1602_RW = 0; // Select write

LCD1602_DATAPINS = com; // Since the 4-bit connection is connected to the high four bits of P0, there is no need to change for sending high four bits

Lcd1602_Delay1ms(1);

LCD1602_E = 1; // Write timing

Lcd1602_Delay1ms(5);

LCD1602_E = 0;

//Lcd1602_Delay1ms(1);

LCD1602_DATAPINS = com

Lcd1602_Delay1ms(1);

LCD1602_E = 1; // Write timing

Lcd1602_Delay1ms(5);

LCD1602_E = 0;

}

#endif

#ifndef LCD1602_4PINS

void LcdWriteData(uchar dat) // Write data

{

LCD1602_E = 0; // Enable clear

LCD1602_RS = 1; // Select input data

LCD1602_RW = 0; // Select write

LCD1602_DATAPINS = dat; // Write data

Lcd1602_Delay1ms(1);

LCD1602_E = 1; // Write timing

Lcd1602_Delay1ms(5); // Hold time

LCD1602_E = 0;

}

#else

void LcdWriteData(uchar dat) // Write data

{

LCD1602_E = 0; // Enable clear

LCD1602_RS = 1; // Select write data

LCD1602_RW = 0; // Select write

LCD1602_DATAPINS = dat; // Since the 4-bit connection is connected to the high four bits of P0, there is no need to change for sending high four bits

Lcd1602_Delay1ms(1);

LCD1602_E = 1; // Write timing

Lcd1602_Delay1ms(5);

LCD1602_E = 0;

LCD1602_DATAPINS = dat

Lcd1602_Delay1ms(1);

LCD1602_E = 1; // Write timing

Lcd1602_Delay1ms(5);

LCD1602_E = 0;

}

#endif

#ifndef LCD1602_4PINS

void LcdInit() // LCD initialization subroutine

{

LcdWriteCom(0x38); // Turn on display

LcdWriteCom(0x0c); // Turn on display without cursor

LcdWriteCom(0x06); // Increment pointer by 1

LcdWriteCom(0x01); // Clear screen

LcdWriteCom(0x80); // Set data pointer start

}

#else

void LcdInit() // LCD initialization subroutine

{

LcdWriteCom(0x32); // Convert 8-bit bus to 4-bit bus

LcdWriteCom(0x28); // Initialization under 4-bit line

LcdWriteCom(0x0c); // Turn on display without cursor

LcdWriteCom(0x06); // Increment pointer by 1

LcdWriteCom(0x01); // Clear screen

LcdWriteCom(0x80); // Set data pointer start

}

#endif

Microcontroller 1602 Infrared Display Experiment Program

Microcontroller 1602 Infrared Display Experiment Program

To facilitate better learning, Changxue Electronics Network has specially added a public account for microcontrollers and EDA, pushing relevant knowledge daily, hoping to assist your studies!

Microcontroller 1602 Infrared Display Experiment ProgramMicrocontroller 1602 Infrared Display Experiment Program

Leave a Comment