MCU infrared remote control + stepper motor + 1602 LCD display C language source program

Publisher:Aq123456258Latest update time:2012-12-05 Source: 51heiKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

This is the original C language program of the stepper motor with 1602 display speed and direction controlled by infrared remote control. It has been debugged successfully! Some modules were made when doing other experiments before, and now they are directly called, including 1602 LCD display technology, TC9012 infrared decoding, and then the control program of the four-phase stepper motor. I combined these things. If you are interested in this thing or want to do graduation project, you can take a look. If you don’t understand, please go to the microcontroller forum http://www.51hei.com/bbs/ for open discussion. I will give you a detailed answer.

#include

#include
#include
#define uchar unsigned char
#define uint unsigned int
static unsigned int count; //Count
unsigned int hour,minute,second,count;

sbit RS = P2^3;//Pin4
sbit RW = P2^2; //Pin5
sbit E = P2^4;//Pin6

#define Data P0 //Data port
static int step_index; //Step index number, value is 0-7
bit k=0; //Infrared decoding judgment flag, 0 is a valid signal, 1 is invalid
uchar n=0; //Used to control external interrupt
static bit turn; //Stepper motor rotation direction
static bit stop_flag; //Stepper motor stop flag
static int speedlevel; //Stepper motor speed parameter, the larger the value, the slower the speed, the minimum value is 1, the fastest speed
static int spcount; //Stepper motor speed parameter count
void delay(unsigned int endcount); //Delay function, the delay is endcount*0.5 milliseconds
void gorun(); //Stepper motor control step function
uchar data date[4]; //The date array is used to store the original code, inverse code, original code and inverse code of the address
char data Test1[]=" ";
#define IR_RE P3_2
void DelayUs(unsigned char us)//delay us
{
unsigned char uscnt;
uscnt=us>>1;/* Crystal frequency in 12MHz*/
while(--uscnt);
}
/*****************************************************/
void DelayMs(unsigned char ms)//delay Ms
{
while(--ms)
{
DelayUs(250);
DelayUs(250);
DelayUs(250);
DelayUs(250);
}
}
void WriteCommand(unsigned char c)
{
DelayMs(5);//short delay before operation
E=0;
RS=0;
RW=0;
_nop_();
E=1;
Data=c;
E=0;
}
/******************************************************/
void WriteData(unsigned char c)
{
DelayMs(5); //short delay before operation
E=0;
RS=1;
RW=0;
_nop_();
E=1;
Data=c;
E=0;
RS=0;
}
/***********http://www.51hei.comMCU original works******************/
void ShowChar(unsigned char pos,unsigned char c)
{
unsigned char p;
if (pos>=0x10)
p=pos+0xb0; //If it is the second line, the high 4 bits of the command code are 0xc
else
p=pos+0x80; //If it is the second line, the high 4 bits of the command code are 0x8
WriteCommand (p);//write command
WriteData (c); //write data
}
/************************************************************/
void ShowString (unsigned char line,char *ptr)
{
unsigned char l,i;
l=line<<4;
for (i=0;i<16;i++)
ShowChar (l++,*(ptr+i));//Loop display 16 characters
}
/***********************************************************/
void InitLcd()
{
DelayMs(15);
WriteCommand(0x38); //display mode
WriteCommand(0x38); //display mode
WriteCommand(0x38); //display mode
WriteCommand(0x06); //Display cursor position
WriteCommand(0x0c); //Display on and cursor settings
WriteCommand(0x01); //Display clear screen

}

/*--------------------------Delay 1ms subroutine-----------------------*/
delay1000()
{
uint i,j;
for(i=0;i<1;i++)
for(j=0;j<124;j++)
;
}

/*---------------------------Delay 882us subroutine-----------------------*/

delay882()
{
uint i,j;
for(i=0;i<1;i++)
for(j=0;j<109;j++)
;
}

/*--------------------------Delay 2400ms subroutine-----------------------*/

delay2400()
{
uint i,j;
for(i=0;i<3;i++)
for(j=0;j<99;j++)
;
}
void IR_decode()
{
uchar i,j;
while(IR_RE==0);
delay2400();
if(IR_RE==1) //After delaying 2.4ms, if it is high level, it is the new code
{
delay2400(); //Delay 4.8ms to avoid the high level of 4.5msfor
(i=0;i<4;i++)
{
for(j=0;j<8;j++)
{
while(IR_RE==0); //Wait for the first high level of the address code to
arrivedelay882(); //Delay 882ms to judge the pin level at this time
///CY=IR_RE;
if(IR_RE==0)
{
date[i]>>=1;
date[i]=date[i]&0x7f;
}
else if(IR_RE==1)
{
delay1000();
date[i]>>=1;
date[i]=date[i]|0x80;
}
} //1-bit data reception completed
} //32-bit binary code reception completed

}
if (date[2]==0x16)
TR0 = 0;
else if(date[2]==0x14)
{TR0 = 1;
}
else if(date[2]==0x10)
{if (speedlevel>1)
speedlevel--;
else
speedlevel=1;
}
else if(date[2]==0x1A)
{if(speedlevel<5)
speedlevel++;
else
speedlevel=5;
}
else if(date[2]==0x11)
turn=~turn;

[page]

}
void int0() interrupt 0 using 1
{
uint i;
for(i=0;i<4;i++)
{
delay1000();
if(IR_RE==1){k=~k;} //The boot code is 4.5ms at the beginning. If a high level appears within 4ms, the decoding program will be exited
.}

if(k==0)
{
EX0=0; //Disable interrupt when valid signal is detected to prevent interference
IR_decode(); //If a valid signal is received, call the decoding program
//Decoding is successful, call the display program to display the key value
}
EX0=1; //Enable external interrupt to allow new remote control buttons
}
void main(void)
{
InitLcd();//
DelayMs(15);

sprintf(Test1,"STEP MOTOR SPEED");//the first line
ShowString(0,Test1);
ShowChar(0x19,\'r\');
ShowChar(0x1a,\'p\');
ShowChar(0x1b,\ 'm\');
count = 0;
step_index = 0;
spcount = 0;
stop_flag = 0;

P1_0 = 0;
P1_1 = 0;
P1_2 = 0;
P1_3 = 0;
SP=0x60;
EA = 1; //Enable CPU interrupt
TMOD = 0x11; //Set timer 0 and 1 to 16-bit mode 1
ET0 = 1; //Enable timer 0 interrupt
EX0=1;
TH0 = 0xFE;
TL0 = 0x0C; //Set to interrupt every 0.5ms
TR0 = 1; //Start counting
turn=0;

speedlevel=1;

while(1)
{
if(turn==0)
ShowChar(0x14,\'+\');
else
ShowChar(0x14,\'-\');
if(TR0 ==0)
{
ShowChar(0x15 ,\'0\');
ShowChar(0x16,\'0\');
ShowChar(0x17,\'0\');
ShowChar(0x18,\'0\');
}
else
{
if(speedlevel==1 )
{
ShowChar(0x15,\'3\');
ShowChar(0x16,\'0\');
ShowChar(0x17,\'0\');
ShowChar(0x18,\'0\');
}
else if( speedlevel==2)
{
ShowChar(0x15,\'1\');
ShowChar(0x16,\'5\');
ShowChar(0x17,\'0\');
ShowChar(0x18,\'0\');
}
else if(speedlevel==3)
{
ShowChar(0x15,\'1\');
ShowChar(0x16,\'0\');
ShowChar(0x17,\'0 \');
ShowChar(0x18,\'0\');
}
else if(speedlevel==4)
{
ShowChar(0x15,\'0\');
ShowChar(0x16,\'7\');
ShowChar(0x17 ,\'5\');
ShowChar(0x18,\'0\');
}
else if(speedlevel==5)
{
ShowChar(0x15,\'0\');
ShowChar(0x16,\'6\') ;
ShowChar(0x17,\'0\');
ShowChar(0x18,\'0\');
}
} }
}
//
Timer 0 interrupt processing
void timeint(void) interrupt 1 using 0
{
TH0=0xFE;
TL0=0x0C; //When set, interrupt once every 0.5ms

count++;

spcount--;
if(spcount<=0)
{
spcount = speedlevel;
gorun();
}

}

void delay(unsigned int endcount)
{
count=0;
do{}while(count }

void gorun()
{
if (stop_flag==1)
{
P1_0 = 0;
P1_1 = 0;
P1_2 = 0;
P1_3 = 0;
return;
}

switch(step_index)
{
case 0: //0
P1_0 = 1;
P1_1
= 0; P1_2
= 0; P1_3 = 0;
break;
case 1: //0 , 1
P1_0 = 1;
P1_1 = 1;
P1_2 = 0;
P1_3 = 0;
break;
case 2: //1
P1_0 = 0;
P1_1
= 1; P1_2
= 0; P1_3 = 0;
break;
case 3: //1, 2
P1_0 = 0;
P1_1 = 1;
P1_2 = 1;
P1_3 = 0;
break;
case 4: //2
P1_0 = 0;
P1_1 = 0
; P1_2
= 1; P1_3 = 0;
break;
case 5: //2, 3 P1_0
= 0;
P1_1 = 0;
P1_2 = 1;
P1_3 = 1;
break;
case 6: //3
P1_0 = 0;
P1_1
= 0; P1_2 = 0; P1_3 =
1;
break;
case 7: //3
, 0 P1_0 = 1;
P1_1 = 0;
P1_2 = 0;
P1_3 = 1;
}

if (turn==0)
{
step_index++;
if (step_index>7)
step_index=0;
}
else
{
step_index--;
if (step_index<0)
step_index=7;
}

}

Keywords:MCU Reference address:MCU infrared remote control + stepper motor + 1602 LCD display C language source program

Previous article:Microcontroller drives buzzer C51 program
Next article:51 single chip microcomputer drives 16×16LED dot matrix to display animated Chinese characters assembly program

Recommended ReadingLatest update time:2024-11-16 16:45

Program for single-chip music and colored lights written with C51
#include #define TLEDS 20 // Duration of each pattern state, in units of 50mS #define NUM_OF_LED_S 4 // Number of LED patterns in each mode typedef unsigned char uchar; typedef unsigned int uint; data uchar led_array_num; //LED pattern status number in current modedata uchar music_array_num; //note numberdata
[Microcontroller]
Freescale MCU DZ60---TPM1 output comparison
#include hidef.h /* for EnableInterrupts macro */ #include "derivative.h" /* include peripheral declarations */   // Output comparison test  //When the value in the timer counter register (TPMxCNTH:TPMxCNTL) matches the channel register (TPMxCnVH:TPMxCnVH), an interrupt flag is generated //And will force the selected
[Microcontroller]
About the operation of internal Flash of Msp430 Flash microcontroller
1 Introduction to the internal Flash memory of Msp430Flash microcontroller The Flash memory of MSP430 is a memory that can be addressed and programmed by bit, byte, or word. The module is controlled by an integrated controller to control the programming and erasing operations. The controller includes three registers,
[Microcontroller]
Design of a logic non-circulating reversible speed regulation system based on 51 single chip microcomputer
1 Introduction In production, many machines require both forward and reverse rotation, and often require rapid starting and braking, that is, a reversible speed regulation system is needed. The reversible speed regulation system implemented by analog circuits has complex circuits, difficult adjustment, low relia
[Microcontroller]
Design of a logic non-circulating reversible speed regulation system based on 51 single chip microcomputer
MCU memory and registers
The memory of the microcontroller is divided into two types: program memory ROM and data memory RAM. These two types of memory are strictly distinguished in use and cannot be mixed. The program memory stores program instructions, constants, tables, etc.; while the data memory stores buffer data. The memory stru
[Microcontroller]
Introduction to MCU Serial Communication
Communication can be divided into parallel communication and serial communication according to the basic type. In parallel communication, each bit of data is transmitted at the same time, which can realize byte-based communication, but the communication lines occupy more resources and are costly. For example, P0 = 0xF
[Microcontroller]
Introduction to MCU Serial Communication
Serial data exchange between PIC microcontroller and PC
1. Introduction The PIC series of microcontrollers from Microchip of the United States is a new type of CMOS process microcontroller. Many of them have internal asynchronous communication modules, such as the PIC16F877 series chips. Although the microcontroller can be equivalent to a CPU in use, in many
[Microcontroller]
Serial data exchange between PIC microcontroller and PC
Analysis of the working principle of PIC microcontroller 4×4 column keyboard
In this article, the editor will introduce the working principle of the 4×4 column keyboard of the PIC microcontroller to help everyone enhance their understanding of the PIC microcontroller keyboard system and facilitate later programming. (1) Principle of single chip computer system keyboard The connection method
[Microcontroller]
Analysis of the working principle of PIC microcontroller 4×4 column keyboard
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号