looking help on 8051 timer interrupt and switch input

JohnInTX

Joined Jun 26, 2012
4,787
You don't have to be sorry. But something is not connecting here. Is it the language differences?
It is late. I'll do some thinking on this.

Meanwhile, study this from another thread until it makes complete sense from start to finish.
It is a good example of a complete solution to a problem.
https://forum.allaboutcircuits.com/attachments/parthflowled-pdf.131934/

You should also go back to those older threads and read the parts on how to state a problem and construct a complete step-by-step process to solve it:

Decide what you want to do.
Express the problem in plain language.
Reduce the plain language to a step-by-step process that a computer can do.
Draw a flow chart that shows that step-by-step process.
Validate the process by tracing the flow for all conditions
When it works on paper, code it.

That is what has to happen.
 
Last edited:

Thread Starter

Parth786

Joined Jun 19, 2017
642
You don't have to be sorry. But something is not connecting here. Is it the language differences?
It is late. I'll do some thinking on this.

Meanwhile, study this from another thread until it makes complete sense from start to finish.
It is a good example of a complete solution to a problem.
https://forum.allaboutcircuits.com/attachments/parthflowled-pdf.131934/

You should also go back to those older threads and read the parts on how to state a problem and construct a complete step-by-step process to solve it:

Decide what you want to do.
Express the problem in plain language.
Reduce the plain language to a step-by-step process that a computer can do.
Draw a flow chart that shows that step-by-step process.
Validate the process by tracing the flow for all conditions
When it works on paper, code it.

That is what has to happen.
Thanks a lot for helping me. I think its language problem. I was looking a old threads and I found this one https://forum.allaboutcircuits.com/...-and-loop-statements-in-c-programming.139161/

In this thread Mr. Wbhan gave me assignment to write program for following condition
If the switch is closed and the LED is off, turn it on.
Otherwise, don't change it. So if it was already OFF, it should stay OFF
C:
#include<REGX51.h>
#define LED_ON  (1)
#define LED_OFF  (0)
#define Switch_Closed  (1)

/*set bit P1^7 to LED*/
sbit LED = P1^7;
/*set bit P2^0 to Switch*/
sbit Switch = P2^0;
void main ()
{
  
         Switch = Switch_Closed;

While (1)
{
           LED = LED_OFF;
  
         if (Switch == Switch_Closed)
         {
              LED = LED_ON;
         }
        else
        {
          LED = LED_OFF;
        }
     } 
}
Is this code do, what he want to do?
 
Last edited:

cmartinez

Joined Jan 17, 2007
8,220
Parth... you're not yet done with the flow chart that we got started with. I strongly suggest you finish it before jumping into writing code.

We know that you have some basic skills regarding code. But it's important that you first learn how to express logic procedures in plain, clear language. That's what the flow charts are for.

I'm under the impression that you're being impatient and want to make things work in the shortest time, so you're skipping steps that you either find boring or annoying.

Trust me when I tell you that that is a mistake, and it's not the best way to proceed.

This is a learning site, and we do our best to assist people through the process of understanding circuit design and mcu programming, among other things. We teach, but we DO NOT directly solve other people's problems because our aim is to help them grow in knowledge and skills.

You're almost there with the flowchart. Be patient and finish it, and then I'll be more than happy to help you with the code, step by step.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
Parth... you're not yet done with the flow chart that we got started with. I strongly suggest you finish it before jumping into writing code.
But it's important that you first learn how to express logic procedures in plain, clear language. That's what the flow charts are for.
Trust me when I tell you that that is a mistake, and it's not the best way to proceed.

You're almost there with the flowchart. Be patient and finish it, and then I'll be more than happy to help you with the code, step by step.
Feeling very sad for taking so much time to understand things. I have made new flow chart. there is little change in this flow charts. I have explain something, that I want to do with program. I am sure it will definitely work. so Please look at flow chart. does it match with Explanations ?
 

Attachments

JohnInTX

Joined Jun 26, 2012
4,787
Don't feel sad. I am sorry for getting frustrated as well. Let's continue.

The flow charts are great! Each has a plain language statement of what the program is to do and a step by step description of how to do it. In each, you can trace the entire program flow and see that it does what it is supposed to do with no missing pieces. None of them EXIT.

In fact, they are so good that I can see a problem in the your logic in 'for loop' program. The flow chart accurately implements that logic as it should, and you have indicated that the 'for' loop is used for a delay. But what happens immediately after LED OFF? Remember that the computer is much faster than your eye. Do you think you will actually be able to see the LED off? Assuming that you want to see the LED flash on and off, what would you add to your logic?
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
But what happens immediately after LED OFF? Remember that the computer is much faster than your eye. Do you think you will actually be able to see the LED off? Assuming that you want to see the LED flash on and off, what would you add to your logic?
I will add another delay. you asked for logic so I am just explaining logic only. also I can draw flow chart
like this
do forever
LED ON
wait for some time
LED OFF
Wait for some time
 

JohnInTX

Joined Jun 26, 2012
4,787
I will add another delay. you asked for logic so I am just explaining logic only. also I can draw flow chart
like this
do forever
LED ON
wait for some time
LED OFF
Wait for some time
Yes. I think that will fix it. Let's concentrate on this program for awhile. We'll do the others later.

Revise the flow chart to reflect your new logic fix.
Do you see anything that can be done with a sub-routine (also called a 'function' in C)?
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
Yes. I think that will fix it. Let's concentrate on this program for awhile. We'll do the others later.

Do you see anything that can be done with a sub-routine (also called a 'function' in C)?
Yes I know the program. we will make delay function and than we will call that function from main function.I don't have idea how to draw flow chart when we are calling to other function from main function. I can make flow chart for delay routine and main routine separately but how how to connect them when we are calling
 

JohnInTX

Joined Jun 26, 2012
4,787
Yes I know the program. we will make delay function and than we will call that function from main function.I don't have idea how to draw flow chart when we are calling to other function from main function. I can make flow chart for delay routine and main routine separately but how how to connect them when we are calling
In the main flow, just make a box that says Delay xxx mSec or Call Delay xxx mSec if you prefer. In this one, that box will appear twice yes?

Next, make a separate flow chart for the Delay Subroutine - no arrows to or from main - it stands alone. If it is a fixed delay, name it Delay_100mS or whatever the delay is. If you want a variable delay, name it Delay_mSec (for example). The routine will take a passed-by-value parameter (the number of milliseconds to delay), delay that long then EXIT. Note that in a subroutine you CAN use EXIT in your flow chart since you are returning to main. When coding is C, try to have only one EXIT point, at the bottom of the flow chart.

So, two things to do:
Modify the main flow chart to call a delay subroutine
Write the delay subroutine using the same methods used to write main - describe it in plain language, flow chart it, run the flow chart mentally to detect any errors.

Keep at it, you are making progress.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
In the main flow, just make a box that says Delay xxx mSec or Call Delay xxx mSec if you prefer. In this one, that box will appear twice yes?

Next, make a separate flow chart for the Delay Subroutine - no arrows to or from main - it stands alone. If it is a fixed delay, name it Delay_100mS or whatever the delay is. If you want a variable delay, name it Delay_mSec (for example). The routine will take a passed-by-value parameter (the number of milliseconds to delay), delay that long then EXIT. Note that in a subroutine you CAN use EXIT in your flow chart since you are returning to main. When coding is C, try to have only one EXIT point, at the bottom of the flow chart.

So, two things to do:
Modify the main flow chart to call a delay subroutine
Write the delay subroutine using the same methods used to write main - describe it in plain language, flow chart it, run the flow chart mentally to detect any errors.

Keep at it, you are making progress.
I made like this
upload_2017-9-11_22-14-11.png
 

JohnInTX

Joined Jun 26, 2012
4,787
Looks pretty good! One thing you left out is the initialization step in main before the loop. All programs have to run some sort of init routine to set up the IO etc. before entering the main loop. What does this program need to init?
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
Looks pretty good! One thing you left out is the initialization step in main before the loop. All programs have to run some sort of init routine to set up the IO etc. before entering the main loop. What does this program need to init?
ohh I forgot initialization step. First LED will set as High (LED ON). it will work like following program
C:
void delay (unsigned int j)
{
  unsigned int j;
  for (j = 0; j < 40000; j++);
  {

  }
}

void main()
{
  LED = LED_ON
  while (1)
  {
  LED = LED_ON;
  delay(500);
  LED = LED_OFF;
  delay(1000);
  }
}
this was the sample program to just understanding how does main function call to another function
In your example there is only one subroutine. delay routine .but how do we make flow chart for complicated task like display data on LCD screen. do we use same process like draw main routine than draw flow chart for Data , command ,delay, and message routine. I am asking this because I have already done with program. Is it too fast to jump on LCD or still should have focus on LED ?
 
Last edited:

JohnInTX

Joined Jun 26, 2012
4,787
You basically have it but there are a few bugs:

  • You haven't defined 'LED' or LED_ON. The compiler needs to know those things.
  • In delay, you pass in the parameter and call it 'j'. But then you declare another int and also call it 'j'. I expect what you are trying to do is just use the passed parameter to determine what the delay is. In that case, you don't need (or want) the second declaration. The compiler will make a copy of 'j' (passed by value), count using that copy then discard it when it returns from the function.
Don't do a big rewrite. We have agreed that the logic does what we want it to do. Now it is time to make the compiler happy by fixing up declarations, syntax etc.

Which compiler are you using?
Are you going to simulate the code, run it on actual hardware or are you just getting the feel of it for now?

Let's stick with this one until we get something that is fully specified AND will compile with no errors. Only then will it be ready to try running/simulating and debuging. When this is complete, we will proceed to finish the other two in post #45. If those go smoothly, we might take a look at the LCD or visit some of the remaining threads left unfinished. The lessons learned here will help greatly in the more complex LCD code.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
You basically have it but there are a few bugs:

  • You haven't defined 'LED' or LED_ON. The compiler needs to know those things.
  • In delay, you pass in the parameter and call it 'j'. But then you declare another int and also call it 'j'. I expect what you are trying to do is just use the passed parameter to determine what the delay is. In that case, you don't need (or want) the second declaration. The compiler will make a copy of 'j' (passed by value), count using that copy then discard it when it returns from the function.
I didn't write complete code. I just wanted to explain how delay routine work in program
I have keil compiler. I have written code with no errors and I use simulator. I have tested code. its working fine with design. I have attached hardware design
C:
#include <REG51.h>
#define LED_ON  (1)  /* LED ON */
#define LED_OFF  (0)  /* LED OFF */
sbit LED = P1^7;  /* LED connected to port P1 of pin 7 */

/* this is delay function */
void Delay (unsigned int Loop)
{
  for (Loop = 0; Loop < 40000; Loop++);
  {

  }
}

void main()
{
  LED = LED_ON;  /* at start LED will should ON */

  while (1)
  {
  LED = LED_ON;  /* Turn ON LED */
  Delay(500);  /* Wait for some time */
  LED = LED_OFF;  /* turn off LED */
  Delay(1000);  /* Wait for some time */

  }
}
I have already done work with LCD look at this thread https://forum.allaboutcircuits.com/threads/program-to-display-message-on-lcd.137686/
I have tested this code. code is working. also I have tested code with multiple messages.
I have done almost with switch,LED and LCD and I wanted to work with Keypad. but I was struggling with keypad interfacing. I have revisit many time on my threads I tried to understand the flow chart that you posted in thread but I didn't understand. still I am struggling with keypad interfacing
 

Attachments

Last edited:

JohnInTX

Joined Jun 26, 2012
4,787
I have already done work with LCD look at this thread https://forum.allaboutcircuits.com/threads/program-to-display-message-on-lcd.137686/
I have tested this code. code is working. also I have tested code with multiple messages.
I have done almost with switch,LED and LCD and I wanted to work with Keypad. but I was struggling with keypad interfacing. I have revisit many time on my threads I tried to understand the flow chart that you posted in thread but I didn't understand. still I am struggling with keypad interfacing
You now have a good idea of how to get from idea to plain language statement of a problem, flow charting the solution, checking the flow chart and finally coding and testing. Let's be sure we go back and get the switch input stuff and any other left-overs working. Tag me in the thread that has the stuff we need to work on. When you have done a few more simple things, we can move on to LCD, keypads etc.

Am I correct that you eventually want to combine all of this stuff into one big project? If that is so, you should start working on the problem statement for that. Note that in big projects with lots of modules (LCD, keypad, switch inputs, LED indicators), it is helpful to use a BLOCK DIAGRAM as part of your problem statement that shows the individual modules and how they relate to each other.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
You now have a good idea of how to get from idea to plain language statement of a problem, flow charting the solution, checking the flow chart and finally coding and testing. Let's be sure we go back and get the switch input stuff and any other left-overs working. Tag me in the thread that has the stuff we need to work on. When you have done a few more simple things, we can move on to LCD, keypads etc.
I think I have all done with switch and LED. And also I have tested code with LCD. I am facing problem only with keypad interfacing.
https://forum.allaboutcircuits.com/threads/keypad-interfacing-using-8051.137828/
https://forum.allaboutcircuits.com/threads/how-to-display-number-on-lcd-through-user-input.138089/
https://forum.allaboutcircuits.com/threads/reading-data-from-device-in-programming.138302/

I started with matrix keypad but I couldn't complete it. than I started with switches but again failed.

Am I correct that you eventually want to combine all of this stuff into one big project? If that is so, you should start working on the problem statement for that. Note that in big projects with lots of modules (LCD, keypad, switch inputs, LED indicators), it is helpful to use a BLOCK DIAGRAM as part of your problem statement that shows the individual modules and how they relate to each other.
I don't have specific project in mind but so I will try to include all in one project
For Example
Features of Project

1. Alarm clock that will show current time and date. and set Alarm for DC motor,
2. When Alarm will happen, buzzer will sound beep..
3. switch is use to Power On system,
4. LED will work as indicator to show the system is ON.
5. LCD to show date and time
6. I will add keypad for specific reason but at this I don't know whats purpose

I think it will cover all that I have learned here. I am just thinking I will be combine all of this stuff into one big project ?

Hardware Part lists
8051 MCU : At89c51
LCD display: 16x2
RTC: DS1307
Motor : DC Motor
Simple Keypad, Buzzer, LED, Switch

but at this time I am focusing on Keypad interfacing and RTC with I2C programming ?
 
Last edited:

cmartinez

Joined Jan 17, 2007
8,220
I think I have all done with switch and LED. And also I have tested code with LCD. I am facing problem only with keypad interfacing.
https://forum.allaboutcircuits.com/threads/keypad-interfacing-using-8051.137828/
https://forum.allaboutcircuits.com/threads/how-to-display-number-on-lcd-through-user-input.138089/
https://forum.allaboutcircuits.com/threads/reading-data-from-device-in-programming.138302/

I started with matrix keypad but I couldn't complete it. than I started with switches but again failed.


I don't have specific project in mind but so I will try to include all in one project
For Example
Features of Project

1. Alarm clock that will show current time and date. and set Alarm for DC motor,
2. When Alarm will happen, buzzer will sound beep..
3. switch is use to Power On system,
4. LED will work as indicator to show the system is ON.
5. LCD to show date and time
6. I will add keypad for specific reason but at this I don't know whats purpose

I think it will cover all that I have learned here. I am just thinking I will be combine all of this stuff into one big project ?

Hardware Part lists
8051 MCU : At89c51
LCD display: 16x2
RTC: DS1307
Motor : DC Motor
Simple Keypad, Buzzer, LED, Switch

but at this time I am focusing on Keypad interfacing and RTC with I2C programming ?
Word of advice, aim for something simple at first, and then build on it after you've completed it. If you choose to do something useful, then so much the better. But make sure you completely finalize each stage before moving on with the next one. That way you'll stay motivated and more consistently grow in abilities and skills.
 

JohnInTX

Joined Jun 26, 2012
4,787
@Parth786
I share @cmartinez 's reservations on this one. Are you sure you have the LCD and reading switch inputs working and completely understood? I'll check those threads when I have the chance but my recollection is that they did not really get finished - at least to the standards that we have agreed on in this thread. If there are any remaining problems at all, those need to be cleaned up before going on to keyboard scanning. For example, we really didn't finish the original topic of this thread i.e. LED flashing under timer interrupt.

Are you pushing this to keep up with a school class and schedule? I am getting a little busy myself.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
@Parth786
I share @cmartinez 's reservations on this one. Are you sure you have the LCD and reading switch inputs working and completely understood? .
No. I have done only with LCD. I wrote program for message display on screen using array and pointer. I wrote program to display multiple message on LCD. I have still problem with reading switch inputs. I want to understand reading switch input than go for keyboard scanning. I was reading old threads and looking flow chart.
I am taking small example for better understanding. For Example password system. I have one LCD and four switch button.
switch button 1 = "1"
switch button 2 = "2"
switch button3 = "3"
switch button 4 ="4"
so this four switch button will work as keypad. user have to press buttons
Suppose password is "4312"

if user enter correct password , LCD display will show message " WELCOME" and If password is wrong. it will show " wrong password "

I come with following conditions for one switch button
reading switch button input
condition 1: check if switch is not pressed
condition 2: check if switch button pressed
condition 2 (a)check if still pressed
condition 2 (b) check if no longer pressed
Note : switch button can be pressed up to 4 times

If there are any remaining problems at all, those need to be cleaned up before going on to keyboard scanning. For example, we really didn't finish the original topic of this thread i.e. LED flashing under timer interrupt.
Are you pushing this to keep up with a school class and schedule? I am getting a little busy myself
we can continue with one example. I want to learn switch reading first than keypad reading and than I want to go for interrupts. I am reading some material and trying to make flow chart first
 
Last edited:
Top