Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

If statement problem with ATmega328P

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    www.mcselec.com Forum Index -> BASCOM-AVR Unsupported versions
View previous topic :: View next topic  
Author Message
Ikerepc

Bascom Member



Joined: 28 Jan 2016
Posts: 5

croatia.gif
PostPosted: Thu Feb 18, 2016 8:59 am    Post subject: If statement problem with ATmega328P Reply with quote

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
View user's profile
bzijlstra

Bascom Ambassador



Joined: 30 Dec 2004
Posts: 1179
Location: Tilburg - Netherlands

netherlands.gif
PostPosted: Thu Feb 18, 2016 12:01 pm    Post subject: Code Reply with quote

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.

Code:

Hello World!
 


Have fun
Ben Zijlstra
Back to top
View user's profile Visit poster's website
Ikerepc

Bascom Member



Joined: 28 Jan 2016
Posts: 5

croatia.gif
PostPosted: Thu Feb 18, 2016 1:09 pm    Post subject: Reply with quote

Ok, done...
Anyone help pls?
Back to top
View user's profile
bzijlstra

Bascom Ambassador



Joined: 30 Dec 2004
Posts: 1179
Location: Tilburg - Netherlands

netherlands.gif
PostPosted: Thu Feb 18, 2016 5:42 pm    Post subject: debounce? Reply with quote

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
View user's profile Visit poster's website
Ikerepc

Bascom Member



Joined: 28 Jan 2016
Posts: 5

croatia.gif
PostPosted: Thu Feb 18, 2016 6:16 pm    Post subject: Re: debounce? Reply with quote

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
View user's profile
enniom

Bascom Member



Joined: 20 Oct 2009
Posts: 537

PostPosted: Fri Feb 19, 2016 3:41 am    Post subject: Reply with quote

Sometimes it is easier to separate

IF X And Y Then

From

If X Or Y Then
Back to top
View user's profile
bzijlstra

Bascom Ambassador



Joined: 30 Dec 2004
Posts: 1179
Location: Tilburg - Netherlands

netherlands.gif
PostPosted: Fri Feb 19, 2016 11:48 am    Post subject: Reply with quote

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
View user's profile Visit poster's website
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 971

poland.gif
PostPosted: Fri Feb 19, 2016 5:49 pm    Post subject: Reply with quote

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
View user's profile Visit poster's website
Ikerepc

Bascom Member



Joined: 28 Jan 2016
Posts: 5

croatia.gif
PostPosted: Sun Feb 21, 2016 2:07 pm    Post subject: Reply with quote

Thanks you all, all of you helped me with something, little bit of editing and it will work perfectly.

It's working ok now Very Happy

Thanks a lot guys!
Back to top
View user's profile
Ev3658

Bascom Member



Joined: 30 Nov 2014
Posts: 32

russia.gif
PostPosted: Wed Apr 11, 2018 9:31 pm    Post subject: Reply with quote

I can not determine the problem.
When I use Atmega8 I get everything, but it's worth replacing with Atmega328p, then LCD 5110 stops working.

The only difference is in m8def and m328pdef, using port C.

Code:
$lib"glcd-Nokia_5110_2071.lib"
Config Graphlcd = 128x64sed, Cs1 = Portc.2, A0 = Portc.3, Si = Portc.4, Sclk = Portc.5


The LCD screen is working, here it is on Atmega8A-PU:


I tried different solutions with the LCD 5110 and could not work with Atmega328p through port C.
Back to top
View user's profile
Ev3658

Bascom Member



Joined: 30 Nov 2014
Posts: 32

russia.gif
PostPosted: Thu Apr 12, 2018 9:09 am    Post subject: Reply with quote

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
View user's profile
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    www.mcselec.com Forum Index -> BASCOM-AVR Unsupported versions All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum