Maker Pro
Maker Pro

Need Help In Program

Hello everyone
I want to write C program to do following task.
first blink LED,
If LED is blinking than do task for motor start, forward , backword , stop
if motor is working , display message '' motor working " on LCD
I have written program individually for each task Now I want to combine all in one program. so I try to make program its incomplete program i need help in main function . I am confused on how to do all in main function
Code:
#include <REG51.h>           // Header file of AT89c51
#define display_port P2      //Data pins connected to port 2 on microcontroller
#define LED          P1      // LED is connected to port P1
#define LED_ON       0xff    // LED ON
#define LED_OFF      0x00    // LED OFF
 
//motor connection
sbit L293D_pin_1 = P3^0;     // pin_1 connected to P3^0
sbit L293D_pin_2 = P3^1;     // pin_2 connected to P3^0
sbit L293D_E     = P3^2;     // pin_E connected to P3^0
 
// LCD pin connection
sbit rs = P2^0;              //RS pin connected to pin 2 of port 3
sbit rw = P2^1;              // RW pin connected to pin 3 of port 3
sbit e =  P2^2;              //E pin connected to pin 4 of port 3
 
 
// delay function for LED
void LED_delay (unsigned int i)
{
    unsigned int j;
    for (i = 0; i < 40000; i++);
    {
  
    }
}
// Delay function for LCD
void LCD_delay(unsigned int time)  // Function for creating delay in milliseconds.
{
    unsigned j,l ;
    for(j=0;j<time;j++)
    for(l=0;l<1275;l++);
}
// Delay function for Motor
void Motor_delay()
{
unsigned int m,n,k;
for(m=0;m<0x20;m++)
for(n=0;n<255;n++)
for(k=0;k<255;k++);
}
 
 
void rotate_f()
{
L293D_pin_1 = 1; //Make positive of motor 1
L293D_pin_2 = 0; //Make negative of motor 0
L293D_E = 1; //Enable L293D
}
 
void rotate_b()
{
L293D_pin_1 = 0; //Make positive of motor 0
L293D_pin_2 = 1; //Make negative of motor 1
L293D_E = 1; //Enable L293D
}
void breaks()
{
L293D_pin_1 = 0; //Make positive of motor 0
L293D_pin_2 = 0; //Make negative of motor 0
L293D_E = 0; //Disable L293D
}
 
void lcd_cmd(unsigned char command)  //Function to send command instruction to LCD
{
    display_port = command;
    rs= 0;
    rw=0;
    e=1;
    msdelay(1);
    e=0;
}
 
void lcd_data(unsigned char disp_data)  //Function to send display data to LCD
{
    display_port = disp_data;
    rs= 1;
    rw=0;
    e=1;
    msdelay(1);
    e=0;
}
 
void lcd_init()    //Function to prepare the LCD  and get it ready
{
    lcd_cmd(0x38);  // for using  2 lines and 5X7 matrix of LCD
    msdelay(10);
    lcd_cmd(0x0c);  // turn display ON, cursor blinking
    msdelay(10);
    lcd_cmd(0x01);  //clear screen
    msdelay(10);
    lcd_cmd(0x81);  // bring cursor to position 1 of line 1
    msdelay(10);
}
void main()
{
  {
        LED = LED_ON;
        LED_delay(500);
        LED = LED_OFF;
        LED_delay(5000);
        LED = LED_OFF;
 
rotate_f(); //Run forward
motor_delay(); //Some delay
breaks(); //Stop
motor_delay(); //Some delay
rotate_b(); //Run Backwards
motor_delay(); //Some delay
breaks(); //Stop
motor_delay(); //Some delay
 
 
    unsigned char a[15]="motor working";    //string of 14 characters with a null terminator.
    int p=0;
    lcd_init();
    while(a[p] != '\0') // searching the null terminator in the sentence
    {
        lcd_data(a[p]);
        p++;
        LCD_delay(50);
    } 
}
 
}
 

Attachments

  • multifunction.jpg
    multifunction.jpg
    95 KB · Views: 59
First thing you must do, loose the delay calls! You are thinking single task, that's why you are failing to get it done.

Use this code below to do a specific task every xxx milliseconds. Set the variable "interval" to say 100 and this code will execute this task every 100mSec. When NOT executing this task, execution proceeds with the next set of instructions. Use several of these (with different variables of course) in your main loop, one for each task, and then you'll be multi tasking at various intervals.

currentMillis=millis();
if ((unsigned long)(currentMillis - previousMillis) >= interval)
{
previousMillis = previousMillis + interval;
//Do this task on a fixed interval
}
 
Top