View previous topic :: View next topic |
Author |
Message |
drejansen
Joined: 10 Jun 2006 Posts: 26 Location: Hoeksche Waard
|
Posted: Sat Aug 18, 2012 9:02 am Post subject: use several ports and/or PWM's in a loop |
|
|
Dear forum,
The problem:
I want to rename ports in a for-next loop, but it does not work.
How can I do it?
Not working code:
Code: |
Lamp1 alias portb.1
Lamp2 alias portb.2
Etc.
For n=1 to 10
If situation = true then Lamp(n) = on
Next n
|
Or else name direct the port like: portb.(n)
Or use the look-up method:
Code: |
For n = 0 to 10
Lamp = lookupstr(n, lampen)
Lamp = on
Next n
End
Lampen:
Data "portb.0" , "Portb.1" , "etc"
|
I also tried to use the alias in the for-next loop, this also does not work.
Please give me the solution.
Thanks in advance
Greetings, Dré _________________ Pluk het moment!
een hele dag lukt toch niet.
Groeten, Dré Jansen
|
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Sat Aug 18, 2012 9:54 am Post subject: |
|
|
dim n as byte
for n=0 to 7
set portb.N ' notice the dot and how you specify the bit index after it
waitms 1000
next _________________ Mark |
|
Back to top |
|
|
drejansen
Joined: 10 Jun 2006 Posts: 26 Location: Hoeksche Waard
|
Posted: Sat Aug 18, 2012 12:25 pm Post subject: use several ports and/or PWM's in a loop |
|
|
hoi Albert,
hartelijk dank, het werk!
nu de volgende stap
This code comes from the Arduino examples: Code: | Mega analogWrite() test
This sketch fades LEDs up and down one at a time on digital pins 2 through 13.
This sketch was written for the Arduino Mega, and will not work on previous boards.
The circuit: 12 LEDs attached from pins 2 through 13 to ground.
// These constants won't change. They're used to give names
// to the pins used:
const int lowestPin = 2;
const int highestPin = 13;
void setup() {
// set pins 2 through 13 as outputs:
for (int thisPin =lowestPin; thisPin <= highestPin; thisPin++) {
pinMode(thisPin, OUTPUT);
}
}
void loop() {
// iterate over the pins:
for (int thisPin =lowestPin; thisPin <= highestPin; thisPin++) {
// fade the LED on thisPin from off to brightest:
for (int brightness = 0; brightness < 255; brightness++) {
analogWrite(thisPin, brightness);
delay(2);
}
// fade the LED on thisPin from brithstest to off:
for (int brightness = 255; brightness >= 0; brightness--) {
analogWrite(thisPin, brightness);
delay(2);
}
// pause between LEDs:
delay(100);
}
} | This code works, but I do not understand it, I do understand the idea.
Below is what I made from it in Bascom:
I want to use the same kind of routine, the name of the port and a number that changes.
So I called the ports: "lamp1”; "lamp2”; "lamp3"; etc.
PORTs are set to output and PWMs can fade. I set the port -1- and set PWM -255-
Later I will fade them; first I want to solve the assignment problem.
The sequence is PORTD.2 - PWM2B - PORTD.4 - PWM0B - PWM0A - PORTD.7 - PORTB.0 - PWM1A - PWM1B - PWM2A - PORTB.4 - PORTB.5
When I use “PORT.n” then it works, but not in the Arduino way.
I guess it must be possible, because Arduino can do it, then Bascom must also can do it.
I am looking step by step for the final solution, so the fading itself comes later. (in a subroutine)
Code: | $regfile = "m328pdef.dat" ' Atmega-328p
$crystal = 16000000
$baud = 9600
$hwstack = 32
$swstack = 10
$framesize = 40
Config Pind.2 = Output : Config Pind.4 = Output : Config Pind.7 = Output
Config Pinb.0 = Output : Config Pinb.4 = Output : Config Pinb.5 = Output
Config Timer0 = Pwm , Prescale = 8 , Compare A Pwm = Clear Up , Compare B Pwm = Clear Up
Config Timer1 = Pwm , Prescale = 8 , Pwm = 8 , Compare A Pwm = Clear Up , Compare B Pwm = Clear Up
Config Timer2 = Pwm , Prescale = 8 , Compare A Pwm = Clear Up , Compare B Pwm = Clear Up
Lamp1 Alias Portd.2 : Lamp2 Alias Pwm2b : Lamp3 Alias Portd.4 : Lamp4 Alias Pwm0b
Lamp5 Alias Pwm0a : Lamp6 Alias Portd.7 : Lamp7 Alias Portb.0 : Lamp8 Alias Pwm1a
Lamp9 Alias Pwm1b : Lamp10 Alias Pwm2a : Lamp11 Alias Portb.4 : Lamp12 Alias Portb.5
Dim Pwm As Byte : Dim Hulpvar As Byte : Dim N As Byte : N = 1
Do
lamp1 =1
waitms 500
lamp1 = 0
waitms 500
lamp2 = 255
waitms 500
lamp2 = 0
waitms 500
Lamp3 = 1
waitms 500
lamp3 = 0
waitms 500
Lamp4 = 255
waitms 500
lamp4 = 0
waitms 500
Lamp5 = 255
waitms 500
lamp5 = 0
waitms 500
Lamp6 = 1
waitms 500
lamp6 = 0
waitms 500
Lamp7 = 1
waitms 500
lamp7 = 0
waitms 500
Lamp8 = 255
waitms 500
lamp8 = 0
waitms 500
Lamp9 = 255
waitms 500
lamp9 = 0
waitms 500
Lamp10 = 255
waitms 500
Lamp10 = 0
waitms 500
Lamp11 = 1
waitms 500
lamp11 = 0
waitms 500
Lamp12 = 1
waitms 500
lamp12 = 0
waitms 500
Loop | I want to make something like: Code: | dim n as byte
For n = 1 to 12
Lamp.n = 1 ‘(or 255)
Waitms 500
Lamp.n = 0
Waitms 500
Next | Is there a solution for this? _________________ Pluk het moment!
een hele dag lukt toch niet.
Groeten, Dré Jansen
|
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Sat Aug 18, 2012 4:19 pm Post subject: |
|
|
Arduino does addressing of two ports as one, Bascom treats every port individual, so one has to do the split oneself. The Arduino AnalogWrite() is the equivalent to a soft-pwm on the selected port pins. No function exist within Bascom for soft-pwm, so one has to write own code for this purpose. |
|
Back to top |
|
|
drejansen
Joined: 10 Jun 2006 Posts: 26 Location: Hoeksche Waard
|
Posted: Sat Aug 18, 2012 9:49 pm Post subject: use several ports and/or PWM's in a loop |
|
|
Hi MWS,
Thanks for your response, but the question is: HOW?
Can you give me an example for pronouncing the ports and pwm’s
Later I will work on the fading itself. _________________ Pluk het moment!
een hele dag lukt toch niet.
Groeten, Dré Jansen
|
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Sat Aug 18, 2012 10:48 pm Post subject: |
|
|
Each portpin is addressable from 0..7, if the absolute led-address is below 8, you use portx.lednr, if it's equal or above 8, you first subtract 8 from lednr and then use porty.lednr. |
|
Back to top |
|
|
drejansen
Joined: 10 Jun 2006 Posts: 26 Location: Hoeksche Waard
|
Posted: Sun Aug 19, 2012 10:45 am Post subject: use several ports and/or PWM's in a loop |
|
|
Hi MWS,
This is like the answer Albert gave me, that works perfect.
How do I the same with the PWM channels? The number in the keyword is IN the string (PWM2B).
I tried it with the look-up method but how can I change the string: Pwm0b in to the keyword: Pwm0b?
For instance the (not working) code below: Code: | For N = 0 to 11
For X = 0 To 255 Step 5
Lookupstr(N , Lampen) = X
Waitms 10
Next X
For X = 255 To 0 Step -5
Lookupstr(N , Lampen) = X
Waitms 10
Next X
Next N
End
Lampen:
Data "PORTD.2" , "PWM2B" , "PORTD.4" , "PWM0B" , "PWM0A" , "PORTD.7" , "Etc." |
This code does NOT work, because the data retrieved by the lookup is not the same as the words when you put them directly in the code. _________________ Pluk het moment!
een hele dag lukt toch niet.
Groeten, Dré Jansen
|
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Sun Aug 19, 2012 11:35 am Post subject: |
|
|
Hello drejansen,
I would suggest you read a bit more about hardware, i.e. the controller you use, as well as about proper Bascom syntax.
This is for sure not meant as insult, but your code shows that you have understood little.
For example you can't assign anything to LookupStr(), you can't directly convert a string into a Bascom keyword, you can't use hardware PWM on any other portpin than the dedicated one, which is described in the datasheet, a.s.o.
As already said, the Arduino environment sets up a timer to create an 8bit soft-pwm for the AnalogWrite() command, which is usable on any of the port-pins.
Based on your current level of knowledge, a soft-pwm that works in the background over the range of two ports seems to me a guarantee for failure.
My advice is: start with the basics and learn about hard- and software, do the simple stuff, try out the samples included within Bascom and work your way up.
Like you do it now, it leads to nothing.
As well you'll find a bunch of people here helping you out in case you run into a problem, I doubt that anyone wants to write your code or play the tutor for you, and that's me neither. |
|
Back to top |
|
|
drejansen
Joined: 10 Jun 2006 Posts: 26 Location: Hoeksche Waard
|
Posted: Sun Aug 19, 2012 12:45 pm Post subject: |
|
|
Hi MWS,
You are right, I am just a beginner, and have a lot to learn.
I will do as you suggest 1st read about the hardware and 2nd looking for a teacher. _________________ Pluk het moment!
een hele dag lukt toch niet.
Groeten, Dré Jansen
|
|
Back to top |
|
|
drejansen
Joined: 10 Jun 2006 Posts: 26 Location: Hoeksche Waard
|
Posted: Sat Aug 25, 2012 11:01 am Post subject: use several ports and/or PWM's in a loop |
|
|
Hi Members,
Finally I solved the problem, or must I say challenge.
For who wants to know, here is the Code: |
' AnalogWriteMega
$regfile = "m328pdef.dat"
$crystal = 16000000
$baud = 9600
$hwstack = 32
$swstack = 10
$framesize = 40
' timers voor PWM dimmers
Config Timer0 = Pwm , Prescale = 8 , Compare A Pwm = Clear Up , Compare B Pwm = Clear Up
Config Timer1 = Pwm , Prescale = 8 , Pwm = 8 , Compare A Pwm = Clear Up , Compare B Pwm = Clear Up
Config Timer2 = Pwm , Prescale = 8 , Compare A Pwm = Clear Up , Compare B Pwm = Clear Up
' geschakelde poorten als uitgang configureren
Config Pind.2 = Output : Config Pind.4 = Output : Config Pind.7 = Output
Config Pinb.0 = Output : Config Pinb.4 = Output : Config Pinb.5 = Output
' teller = betreffende poort, licht = intensiteit of aan/uit , N = hulpvariabele voor lampvolgorde
Dim Teller As Byte , Licht As Byte , N As Byte
Declare Sub Dimmer(byval Teller As Byte , Byval Licht As Byte) ' dimmer instelling
Declare Sub Lampen(byval Teller As Byte , Licht As Byte) ' lampen aan en uit schakelen
Do
For N = 1 To 12 ' lampen teller 1...12
Select Case N
Case 1
Teller = 2 ' nummer van de gekozen poort
Licht = 1 ' inschakelen
Call Lampen(teller , Licht) ' schakel routine
Licht = 0 ' uitschakelen
Call Lampen(teller , Licht) ' schakel routine
Case 2
For Licht = 0 To 255 Step 5 ' dimmer instelling
Call Dimmer(teller , Licht) ' dimmer routine op regelen
Next
For Licht = 255 To 0 Step -5 ' dimmer routine
Call Dimmer(teller , Licht) ' dimmer routine terug regelen
Next
Case 3 ' geschakelde lamp
Teller = 4
Licht = 1
Call Lampen(teller , Licht)
Licht = 0
Call Lampen(teller , Licht)
Case 4 ' gedimde lamp
For Licht = 0 To 255 Step 5
Call Dimmer(teller , Licht)
Next
For Licht = 255 To 0 Step -5
Call Dimmer(teller , Licht)
Next
Case 5 ' gedimde lamp
For Licht = 0 To 255 Step 5
Call Dimmer(teller , Licht)
Next
For Licht = 255 To 0 Step -5
Call Dimmer(teller , Licht)
Next
Case 6 ' geschakelde lamp
Teller = 7
Licht = 1
Call Lampen(teller , Licht)
Licht = 0
Call Lampen(teller , Licht)
Case 7 ' geschakelde lamp
Teller = 0
Licht = 1
Call Lampen(teller , Licht)
Licht = 0
Call Lampen(teller , Licht)
Case 8 ' gedimde lamp
For Licht = 0 To 255 Step 5
Call Dimmer(teller , Licht)
Next
For Licht = 255 To 0 Step -5
Call Dimmer(teller , Licht)
Next
Case 9 ' gedimde lamp
For Licht = 0 To 255 Step 5
Call Dimmer(teller , Licht)
Next
For Licht = 255 To 0 Step -5
Call Dimmer(teller , Licht)
Next
Case 10 ' gedimde lamp
For Licht = 0 To 255 Step 5
Call Dimmer(teller , Licht)
Next
For Licht = 255 To 0 Step -5
Call Dimmer(teller , Licht)
Next
Case 11 ' geschakelde lamp
Teller = 4
Licht = 1
Call Lampen(teller , Licht)
Licht = 0
Call Lampen(teller , Licht)
Case 12 ' geschakelde lamp
Teller = 5
Licht = 1
Call Lampen(teller , Licht)
Licht = 0
Call Lampen(teller , Licht)
End Select
Next N
Loop
Sub Lampen(byval Teller As Byte , Byval Licht As Byte) ' schakel routine
If N < 7 Then ' selecteer poort D of B
Portd.teller = Licht
Else
Portb.teller = Licht
End If
Waitms 600 ' pauze, brandtijd, uittijd
End Sub
Sub Dimmer(byval Teller As Byte , Byval Licht As Byte) ' dimmer routine
If N = 2 Then Pwm2b = Licht ' pwm generator wordt geselecteerd
If N = 4 Then Pwm0b = Licht ' ik zie geen mogelijkheid om dit anders te doen
If N = 5 Then Pwm0a = Licht ' ik ben niet tevreden over deze manier
If N = 8 Then Pwm1a = Licht ' graag een betere methode, ik wacht op uw oplossing
If N = 9 Then Pwm1b = Licht ' op voorhand bedankt
If N = 10 Then Pwm2a = Licht
Waitms 10 ' pauze tussen elke dimmerstand
End Sub |
The commends are in Dutch, but for who knows it is not necessary
Have fun, and may the force be with you.
Case closed _________________ Pluk het moment!
een hele dag lukt toch niet.
Groeten, Dré Jansen
|
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Sat Aug 25, 2012 11:55 am Post subject: Re: use several ports and/or PWM's in a loop |
|
|
Maybe you want to check this:
drejansen wrote: |
Code: | ' ...
Teller = 5
Licht = 1
Call Lampen(teller , Licht)
' ...
Sub Lampen(byval Teller As Byte , Byval Licht As Byte)
' ...
Portd.teller = Licht |
|
As you see, you address PORTD.5 to switch this pin on(off).
Pin D5 however is also OC0B on the ATMega328, which you have already in use for one of your hardware-pwm's.As long the hardware-pwm is in control of the pin, you can't change its state yourself.
To make this possible, you have to do a re-Config TIMERx and use then the parameter DISCONNECT. |
|
Back to top |
|
|
|
|
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
|
|