View previous topic :: View next topic |
Author |
Message |
Ikerepc
Joined: 28 Jan 2016 Posts: 5
|
Posted: Thu Feb 18, 2016 8:59 am Post subject: If statement problem with ATmega328P |
|
|
Hi everyone...
I'm still really new to mcu-s...
I worked at 8091 series till now and I have a question...
I made program where admin will pick true answer between 1 and 2 and that will be true answer.
Than led for answer set will light.
Than users will need to pick true or false.
When user pick, diode at his "table" will light.
When both users choosed aswer, software will light up a diode for each of two of them is it right or wrong answer (based at first admins answer.
There are diodes (outputs) for answer set - first, for user choose - 2 diodes for each user, and true / false diodes for each of them...
Problem is that it looks like program just run and don't wait for button to be pressed by returning at subprograms in it...
I think...
I mean, firstly it just lighted that both users choosen right answer, because of "if tocno = Odgov1" so I added "if tocno = Ogov1 and tocno > 0" so it stopped to show anything at all...
Anyone help pls?
Here is my try...
Code: | $regfile = "m328pdef.dat"
$crystal = 8000000
Config Portd = Input
Config Portb = Input
Config portc = output
Dim tocno as byte
Dim odgi1 as byte
Dim odgi2 as byte
Dim odgov1 as byte
Dim odgov2 as byte
Do
Odgi1 = 0
Odgi2 = 0
Odgov1 = 0
Odgov2 = 0
Tocno = 0
Reset Portc
Pj:
If Pind.1 = 1 Then
Tocno = 1
If Pind.4 = 1 Then
Tocno = 2
End If
If Pind.1 = 0 And Pind.4 = 0 Then Goto Pj
End If
Pd:
If Pind.2 = 1 Or Pind.5 = 1 And Odgi1 = 0 Then
Portc.0 = 1
Odgi1 = 1
If Pind.2 = 1 Then
Odgov1 = 1
Elseif Pind.5 = 1 Then
Odgov1 = 2
End If
Elseif Pind.3 = 1 Or Pind.6 = 1 And Odgi2 = 0 Then
Portc.1 = 1
Odgi2 = 1
If Pind.3 = 1 Then
Odgov2 = 1
Elseif Pind.6 = 1 Then
Odgov2 = 2
Endif
If Odgi1 = 0 Or Odgi2 = 0 Then Goto Pd
End If
Pr:
Wait 2
If Tocno = Odgov1 And Tocno > 0 Then
Portc.2 = 1
Elseif Tocno > 0 Then
Portc.3 = 1
End If
If Tocno = Odgov2 And Tocno > 0 Then
Portc.4 = 1
Elseif Tocno > 0 Then
Portc.5 = 1
End If
Wait 2
If Tocno = 0 Or Odgov1 = 0 Or Odgov2 = 0 Then Goto Pr
Loop |
(BASCOM-AVR version : 2.0.7.5 , Latest : 2.0.7.8 )
Last edited by Ikerepc on Thu Feb 18, 2016 1:10 pm; edited 2 times in total |
|
Back to top |
|
|
bzijlstra
Joined: 30 Dec 2004 Posts: 1179 Location: Tilburg - Netherlands
|
Posted: Thu Feb 18, 2016 12:01 pm Post subject: Code |
|
|
Hello
Perhaps you can make your example a bit more readable for us. Put code before and /code after the example and you will see a change of layout. Just hit the edit in the right top corner and add these two lines.
Have fun
Ben Zijlstra |
|
Back to top |
|
|
Ikerepc
Joined: 28 Jan 2016 Posts: 5
|
Posted: Thu Feb 18, 2016 1:09 pm Post subject: |
|
|
Ok, done...
Anyone help pls? |
|
Back to top |
|
|
bzijlstra
Joined: 30 Dec 2004 Posts: 1179 Location: Tilburg - Netherlands
|
Posted: Thu Feb 18, 2016 5:42 pm Post subject: debounce? |
|
|
Are your inputs switches?
Check the debounce command.
Will check the code later on, busy at the moment...
Have fun
Ben Zijlstra |
|
Back to top |
|
|
Ikerepc
Joined: 28 Jan 2016 Posts: 5
|
Posted: Thu Feb 18, 2016 6:16 pm Post subject: Re: debounce? |
|
|
bzijlstra wrote: | Are your inputs switches?
Check the debounce command.
Will check the code later on, busy at the moment...
Have fun
Ben Zijlstra |
All in in logic 0 at start and on click they go to logic 1.
It isn't about debounce as it is always in logical 0 and it just runs out...
I putted debounces at start but removed them :/
So, pls help... |
|
Back to top |
|
|
enniom
Joined: 20 Oct 2009 Posts: 537
|
Posted: Fri Feb 19, 2016 3:41 am Post subject: |
|
|
Sometimes it is easier to separate
IF X And Y Then
From
If X Or Y Then |
|
Back to top |
|
|
bzijlstra
Joined: 30 Dec 2004 Posts: 1179 Location: Tilburg - Netherlands
|
Posted: Fri Feb 19, 2016 11:48 am Post subject: |
|
|
If you take this part:
Code: |
Pj:
If Pind.1 = 1 Then
Tocno = 1
If Pind.4 = 1 Then
Tocno = 2
End If
If Pind.1 = 0 And Pind.4 = 0 Then Goto Pj
End If
|
Tocno will only get 2 when both buttons are pressed at once..
It is a IF inside a IF
Think you should make it
Code: |
Pj:
Do
If Pind.1 = 1 Then
Tocno = 1
Exit do
End if
If pind.4 =1 then
Tocno=2
Exit do
Endif
Loop
|
In this routine you can also put the LED on.
A small schematic would help. What are the admin buttons what are the contestant buttons?
Do you know how the simulator of Bascom works?
You can test this without any hardware.
Ben Zijlstra |
|
Back to top |
|
|
EDC
Joined: 26 Mar 2014 Posts: 971
|
Posted: Fri Feb 19, 2016 5:49 pm Post subject: |
|
|
Quote: | I worked at 8091 series till now and I have a question... |
Do you have pull down resistors in your design? If not then HI state of inputs may be recognized randomly
In AVR "better" way is to recognize LOW state of INPUT PIN after they are set HIGH using internal PULL UP resistors.
So common used configuration is to config port/pin as Input( this will set DDR register), then Set PORT of that pin to 1 (HIGH)
This will enable ~50k resistor to VCC
..and then if switch is not pressed then the input status have Hi state(1)
Design then must short input to ground if pin is pressed
So at the end, commonly used If statement is: "If pin is shorted to ground then.."
"If Pin = 0 then" |
|
Back to top |
|
|
Ikerepc
Joined: 28 Jan 2016 Posts: 5
|
Posted: Sun Feb 21, 2016 2:07 pm Post subject: |
|
|
Thanks you all, all of you helped me with something, little bit of editing and it will work perfectly.
It's working ok now
Thanks a lot guys! |
|
Back to top |
|
|
Ev3658
Joined: 30 Nov 2014 Posts: 32
|
|
Back to top |
|
|
Ev3658
Joined: 30 Nov 2014 Posts: 32
|
Posted: Thu Apr 12, 2018 9:09 am Post subject: |
|
|
Thanks a lot, I figured out PortC's problem with Atmega328p after replacing Atmega8. As it turned out to work PORTC in Atmega328p, you need to supply + 5V. to AVCC. |
|
Back to top |
|
|
|