View previous topic :: View next topic |
Author |
Message |
Printpix52
Joined: 18 Jun 2014 Posts: 282 Location: D.F.
|
|
Back to top |
|
|
six1
Joined: 27 Feb 2009 Posts: 553
|
Posted: Sat Apr 29, 2017 8:21 am Post subject: |
|
|
try it this way...
Code: |
$regfile = "m16adef.dat"
$crystal = 4000000
$hwstack = 40
$swstack = 16
$framesize = 32
$baud = 19200
Config Portb = Output : Lightlevel Alias Portb : Portb = &B11111111
Config Pina.0 = Input
Config Adc = Single , Prescaler = Auto
Start Adc
Dim Lightlevel As Word
Do
Lightlevel = Getadc(0)
Select Case Lightlevel
Case 102 To 199 : Portb = &B01111111
Case 200 To 399 : Portb = &B10111111
Case 400 To 499 : Portb = &B11011111
Case 500 To 599 : Portb = &B11101111
Case 600 To 699 : Portb = &B11110111
Case 700 To 799 : Portb = &B11111011
Case 800 To 899 : Portb = &B11111101
Case 900 To 1023 : Portb = &B11111110
End Select
Loop
End
|
_________________ For technical reasons, the signature is on the back of this message. |
|
Back to top |
|
|
Printpix52
Joined: 18 Jun 2014 Posts: 282 Location: D.F.
|
Posted: Sat Apr 29, 2017 3:13 pm Post subject: |
|
|
Thanks friend.. Error!!!
[/img] |
|
Back to top |
|
|
Printpix52
Joined: 18 Jun 2014 Posts: 282 Location: D.F.
|
|
Back to top |
|
|
enniom
Joined: 20 Oct 2009 Posts: 537
|
Posted: Sun Apr 30, 2017 8:26 pm Post subject: |
|
|
Either you, your code or both are confused.
Code: | Lightlevel Alias Portb
Dim Lightlevel As Word |
|
|
Back to top |
|
|
Printpix52
Joined: 18 Jun 2014 Posts: 282 Location: D.F.
|
|
Back to top |
|
|
Printpix52
Joined: 18 Jun 2014 Posts: 282 Location: D.F.
|
Posted: Sun Apr 30, 2017 9:34 pm Post subject: Re: select-case-end select |
|
|
Printpix52 wrote: | Hi All!!
ADC converts the input voltage level to a number in the range of 0 to 1023 of the 8 values to turn on the corresponding LED 900, 800, 700, 600, 500, 400, 200, 102 .
Why do you mark me error ?
Code: | $regfile = "m16adef.dat"
$crystal = 4000000
$hwstack = 40
$swstack = 16
$framesize = 32
$baud = 19200
Config Portb = Output : Lightlevel Alias Portb : Portb = &B11111111
Config Pina.0 = Input
Config Adc = Single , Prescaler = Auto
Start Adc
Dim Lightlevel As Word
Do
Lightlevel = Getadc(0)
Select Case Lightlevel
Case Is > 102 : Portb = &B01111111
Case Is > 200 : Portb = &B10111111
Case Is > 400 : Portb = &B11011111
Case Is > 500 : Portb = &B11101111
Case Is > 600 : Portb = &B11110111
Case Is > 700 : Portb = &B11111011
Case Is > 800 : Portb = &B11111101
Case Is < 900 : Portb = &B11111110
End Select
Loop
End |
|
corrected....
Code: | $regfile = "m16adef.dat"
$crystal = 4000000
$hwstack = 40
$swstack = 16
$framesize = 32
$baud = 19200
Ddrb = &B11111111 : Portb = &B11111111 'portb ouput , pullup high
Config Adc = Single , Prescaler = Auto , Reference = Avcc ' 5v
'Cap 100N On Aref To Gnd
'avcc to +5v
Start Adc
Dim Lightlevel As Word
Do
Lightlevel = Getadc(0)
Select Case Lightlevel
Case Is > 950 : Portb = &B01111111
Case Is > 900 : Portb = &B10111111
Case Is > 800 : Portb = &B11011111
Case Is > 700 : Portb = &B11101111
Case Is > 600 : Portb = &B11110111
Case Is > 500 : Portb = &B11111011
Case Is > 300 : Portb = &B11111101
Case Is < 150 : Portb = &B11111110
End Select
Loop
End
|
|
|
Back to top |
|
|
bzijlstra
Joined: 30 Dec 2004 Posts: 1179 Location: Tilburg - Netherlands
|
Posted: Sun Apr 30, 2017 11:57 pm Post subject: Alias and variabele... |
|
|
Before you had lightlevel as alias for Portb (8 bit) but also as variabele dimmentioned as Word (16 bit). Think there was your problem. Correct me if I am wrong.
Have fun
Ben Zijlstra |
|
Back to top |
|
|
Printpix52
Joined: 18 Jun 2014 Posts: 282 Location: D.F.
|
Posted: Mon May 01, 2017 4:18 am Post subject: |
|
|
Hello bzijlstra!!!
Very good question.
The variable used for this program is word size, ie 2 bytes (16 bits) because the values of the analog to digital converter is 0-1023.
|
|
Back to top |
|
|
six1
Joined: 27 Feb 2009 Posts: 553
|
Posted: Mon May 01, 2017 1:02 pm Post subject: |
|
|
another way is to shift right by 2 lightlevel... without selct case... _________________ For technical reasons, the signature is on the back of this message. |
|
Back to top |
|
|
Printpix52
Joined: 18 Jun 2014 Posts: 282 Location: D.F.
|
Posted: Mon May 01, 2017 4:13 pm Post subject: |
|
|
Hello Six!!
See how it works out? |
|
Back to top |
|
|
six1
Joined: 27 Feb 2009 Posts: 553
|
Posted: Tue May 02, 2017 6:02 am Post subject: |
|
|
if you divide the ADC Value by 4, you will get a byte value... (1024 / 4 = 256)
Dividing by 4 is the same than shifting right by 2
Code: |
Lightlevel = Getadc(0)
Shift Lightlevel , Right , 2
Portb = Lightlevel
|
so your whole code looks like this:
Code: |
$regfile = "m16adef.dat"
$crystal = 4000000
$hwstack = 40
$swstack = 16
$framesize = 32
$baud = 19200
Config Portb = Output
Config Adc = Single , Prescaler = Auto
Start Adc
Dim Lightlevel As Word
Do
Lightlevel = Getadc(0)
Shift Lightlevel , Right , 2
Portb = Lightlevel
Loop
End
|
_________________ For technical reasons, the signature is on the back of this message. |
|
Back to top |
|
|
Printpix52
Joined: 18 Jun 2014 Posts: 282 Location: D.F.
|
Posted: Tue May 02, 2017 1:07 pm Post subject: |
|
|
Hi six1!!
It works but it's not the same friend !! |
|
Back to top |
|
|
|