Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

select-case-end select

 
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
Printpix52

Bascom Member



Joined: 18 Jun 2014
Posts: 218
Location: D.F.

mexico.gif
PostPosted: Sat Apr 29, 2017 2:20 am    Post subject: select-case-end select Reply with quote

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 ? Rolling Eyes

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
Back to top
View user's profile
six1

Bascom Expert



Joined: 27 Feb 2009
Posts: 550

germany.gif
PostPosted: Sat Apr 29, 2017 8:21 am    Post subject: Reply with quote

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

Bascom Member



Joined: 18 Jun 2014
Posts: 218
Location: D.F.

mexico.gif
PostPosted: Sat Apr 29, 2017 3:13 pm    Post subject: Reply with quote

Thanks friend.. Error!!! Confused Confused
[/img]
Back to top
View user's profile
Printpix52

Bascom Member



Joined: 18 Jun 2014
Posts: 218
Location: D.F.

mexico.gif
PostPosted: Sun Apr 30, 2017 3:42 am    Post subject: Reply with quote

Hello all!!

Before:



Code:
Config Portb = Output : Lightlevel Alias Portb : Portb = &B11111111
Config Pina.0 = Input



After:


Code:
Ddrb = 255                                                  'Salida
Portb = 255                                                 'Led apagado (STK500)


Wink Wink Wink
Back to top
View user's profile
enniom

Bascom Member



Joined: 20 Oct 2009
Posts: 472

PostPosted: Sun Apr 30, 2017 8:26 pm    Post subject: Reply with quote

Either you, your code or both are confused.

Code:
Lightlevel Alias Portb

Dim Lightlevel As Word
Back to top
View user's profile
Printpix52

Bascom Member



Joined: 18 Jun 2014
Posts: 218
Location: D.F.

mexico.gif
PostPosted: Sun Apr 30, 2017 9:30 pm    Post subject: Reply with quote

Helo!!ENNIOM...

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 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

 
Very Happy Very Happy
Back to top
View user's profile
Printpix52

Bascom Member



Joined: 18 Jun 2014
Posts: 218
Location: D.F.

mexico.gif
PostPosted: Sun Apr 30, 2017 9:34 pm    Post subject: Re: select-case-end select Reply with quote

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 ? Rolling Eyes

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
 


Wink Wink Wink Wink
Back to top
View user's profile
bzijlstra

Bascom Ambassador



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

netherlands.gif
PostPosted: Sun Apr 30, 2017 11:57 pm    Post subject: Alias and variabele... Reply with quote

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

Bascom Member



Joined: 18 Jun 2014
Posts: 218
Location: D.F.

mexico.gif
PostPosted: Mon May 01, 2017 4:18 am    Post subject: Reply with quote

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.

Very Happy Very Happy
Back to top
View user's profile
six1

Bascom Expert



Joined: 27 Feb 2009
Posts: 550

germany.gif
PostPosted: Mon May 01, 2017 1:02 pm    Post subject: Reply with quote

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

Bascom Member



Joined: 18 Jun 2014
Posts: 218
Location: D.F.

mexico.gif
PostPosted: Mon May 01, 2017 4:13 pm    Post subject: Reply with quote

Hello Six!!

See how it works out? Confused
Back to top
View user's profile
six1

Bascom Expert



Joined: 27 Feb 2009
Posts: 550

germany.gif
PostPosted: Tue May 02, 2017 6:02 am    Post subject: Reply with quote

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

Bascom Member



Joined: 18 Jun 2014
Posts: 218
Location: D.F.

mexico.gif
PostPosted: Tue May 02, 2017 1:07 pm    Post subject: Reply with quote

Hi six1!!
It works but it's not the same friend !! Very Happy Very Happy
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