Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Led fading with PWM

 
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 Archive
View previous topic :: View next topic  
Author Message
ApastalasM

Bascom Member



Joined: 12 Feb 2007
Posts: 23

lithuania.gif
PostPosted: Thu Jan 31, 2008 9:33 pm    Post subject: Led fading with PWM Reply with quote

Hi,

I have tried some examples which i found in the search, but I still can't understand how to fade a led Sad I used a short code which just blinked the led

Code:
'$regfile = "m8def.dat"
'$crystal = 4000000

Dim A As Word
Dim B As Word
Config Timer1 = Pwm , Pwm = 8 , Compare A Pwm = Clear Up , Compare B Pwm = Clear Down , Prescale = 8
Config Pinb.1 = Output

Do

For A = 255 To 0 Step -1
Pwm1a = A
Waitms 100
Next


For A = 0 To 255
Pwm1a = A
'Waitms 100
Next

Loop
End


 


I tried to change all the values but couldn't get the result I want

I want to fade the led in and out

Can someone post an example code which will fade the led in and out slowly?

I would be very grateful.
Back to top
View user's profile
ApastalasM

Bascom Member



Joined: 12 Feb 2007
Posts: 23

lithuania.gif
PostPosted: Sat Feb 02, 2008 6:59 pm    Post subject: Reply with quote

Someone? Rolling Eyes
Back to top
View user's profile
Visovian

Bascom Member



Joined: 31 Oct 2007
Posts: 584
Location: Czech

czechrepublic.gif
PostPosted: Sat Feb 02, 2008 9:39 pm    Post subject: Reply with quote

This code worked with attiny2313.
Code:

'''''''''''''''''''''''''''''''''''''
'PWM.bas
'
'8-bit PWM
'Voltage on pin OC1A changes slowly from 0 to 5V and back.

'''''''''''''''''''''''''''''''''''''

'$sim
$regfile = "Attiny2313.DAT"
$crystal = 10000000
Config Timer1 = Pwm , Pwm = 8 , Prescale = 1 , Compare A Pwm = Clear Up

Config Portb.3 = Output   'OC1A pin  (Portb.1 for Atmega8)

'''''''''''''''''''''''''''' MAIN ''''''''''''''''''''''''''''''''''''
Defbyte B

 Do
   For B = 1 To 255
   Pwm1a = B
   Waitms 20
   Next

   For B = 255 To 1 Step -1
   Pwm1a = B
   Waitms 20
   Next
 Loop

'''''''''''''''''''''''''''' ENDMAIN ''''''''''''''''''''''''''
 End

 
Back to top
View user's profile
mattcro

Bascom Member



Joined: 03 Oct 2007
Posts: 324
Location: Scotland

uk.gif
PostPosted: Sat Feb 02, 2008 9:44 pm    Post subject: Reply with quote

You can try removing the "Compare B Pwm = Clear Down" part of the timer config, and only set up Compare A. The Compare B might be confusing things. Also check that you are using the right oscillator fuses and that $crystal is set correctly (you appear to have it commented out).

Here's my code (a bit more complicated that your example!) for a simple R-G-B colour cycler on a tiny45:
Code:
' BASCOM version of Elektor TinyRGB LED flasher
' Using 8MHz int RC osc

' RED = OC0A = PORTB.0 = PIN5
' GRN = OC0B = PORTB.1 = PIN6
' BLU = OC1B = PORTB.4 = PIN3

$regfile = "ATtiny45.DAT"
$crystal = 8000000

$hwstack = 16
$swstack = 16
$framesize = 24

'speed is number of ms to wait between pwm updates
const speed = 40

dim i as byte
dim table(100) as byte

red_led alias PORTB.0
green_led alias PORTB.1
blue_led alias PORTB.4

red_pwm alias pwm0a
green_pwm alias pwm0b
blue_pwm alias ocr1b

reset red_led
reset green_led
reset blue_led

Config Pinb.0 = Output
Config Pinb.1 = Output
Config Pinb.4 = Output

'set up PWM on OC0A, OC0B, OC1B at approx 250Hz
Config Timer0 = Pwm , Prescale = 64 , Compare A Pwm = Clear up , Compare B Pwm = Clear up
set wgm01                                                   'change mode to fast pwm

'config Timer1 (can't use "config timer1"), PWM, prescale=64
tccr1 = &B0000_0111                                         'disable OCR1A, set prescale=64
gtccr = &B0110_0000                                         'PWM mode for OCR1B, clear OC1B on match
'ocr1c = 255                                                 'full count range

'load pwm lookup table
restore pwm_table
for i = 1 to 99
   read table(i)
next i

'set pwm's to initial state
red_pwm = table(1)
green_pwm = table(1)
blue_pwm = table(1)


'ramp up red
for i = 1 to 99
   red_pwm = table(i)
   waitms speed
next i

'plain cycle R -> R+G -> G -> G+B -> B -> B+R -> repeat
do
   'ramp up green
   for i = 1 to 99
      green_pwm = table(i)
      waitms speed
   next i

   'ramp down red
   for i = 99 to 1 step -1
      red_pwm = table(i)
      waitms speed
   next i

   'ramp up blue
   for i = 1 to 99
      blue_pwm = table(i)
      waitms speed
   next i

   'ramp down green
   for i = 99 to 1 step -1
      green_pwm = table(i)
      waitms speed
   next i

   'ramp up red
   for i = 1 to 99
      red_pwm = table(i)
      waitms speed
   next i

   'ramp down blue
   for i = 99 to 1 step -1
      blue_pwm = table(i)
      waitms speed
   next i
loop

'look-up table to provide semi-log output from linear ramp (gives better apparent brightness than linear duty cycle ramp)
'99 entries in lookup table
pwm_table:
'idx 01   02   03   04   05   06   07   08   09   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56    57    58    59    60    61    62    63    64    65    66    67    68    69    70    71    72    73    74    75    76    77    78    79    80    81    82    83    84    85    86    87    88    89    90    91    92    93    94    95    96    97    98    99
data 10 , 11 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 , 25 , 26 , 27 , 28 , 30 , 31 , 33 , 34 , 35 , 37 , 39 , 40 , 42 , 43 , 45 , 47 , 49 , 50 , 52 , 54 , 56 , 58 , 60 , 62 , 64 , 66 , 68 , 70 , 73 , 75 , 77 , 79 , 82 , 84 , 87 , 89 , 91 , 94 , 97 , 99 , 102 , 105 , 107 , 110 , 113 , 116 , 119 , 121 , 124 , 127 , 130 , 133 , 137 , 140 , 143 , 146 , 149 , 153 , 156 , 159 , 163 , 166 , 170 , 173 , 177 , 181 , 184 , 188 , 192 , 196 , 199 , 203 , 207 , 211 , 215 , 219 , 223 , 227 , 231 , 236 , 240 , 244 , 248 , 250
END


PS well, well - how's that? Two examples within a few minutes of each other!

_________________
If all else fails, read the manual. Even better: read the manual before something fails. If you can't find it in the manual, search the forum.
BascomAVR 2.0.8.2
Back to top
View user's profile
ApastalasM

Bascom Member



Joined: 12 Feb 2007
Posts: 23

lithuania.gif
PostPosted: Thu Feb 07, 2008 3:06 pm    Post subject: Reply with quote

Hi,

Thanks for your answers. I have examined Visovian code, and got everything. mattacro I have looked at your code, but I don't have bigger chip than tiny2313 at the moment. I can't understand how to use more pwm channels?

Code:
'$sim
$regfile = "Attiny2313.DAT"
$crystal = 4000000
$baud = 9600
Config Timer1 = Pwm , Pwm = 8 , Prescale = 64 , Compare A Pwm = Clear Up

Config Portb.3 = Output                                     'OC1A pin  (Portb.1 for Atmega8)
Config Portb.0 = Output                                     'OC0A pin


I have tryed like this, but the led don't want to work. What I need to add to use other pwm outputs?

Thanks.
Back to top
View user's profile
mattcro

Bascom Member



Joined: 03 Oct 2007
Posts: 324
Location: Scotland

uk.gif
PostPosted: Thu Feb 07, 2008 9:21 pm    Post subject: Reply with quote

Did you get Visovian's code working ok?

The tiny45 is actually a smaller chip (8 pins) but it has 3 PWM channels, using timer0 and timer1 (both 8bit).

The tiny2313 has 4 PWM channels - 2 on timer0 (8bit) and 2 on timer1 (16bit). You must configure both timers for PWM mode and all the PWM channels as outputs. Then you can write the duty cycle values to OCR0A, OCR0B, Pwm1a and Pwm1b.

The timers can be configured something like this, I think:
Code:
Config Timer0 = Pwm , Prescale = 8 , Compare A Pwm = Clear up , Compare B Pwm = Clear up
Config Timer1 = Pwm , Pwm = 8 , Prescale = 8 , Compare A Pwm = Clear up , Compare B Pwm = Clear up

_________________
If all else fails, read the manual. Even better: read the manual before something fails. If you can't find it in the manual, search the forum.
BascomAVR 2.0.8.2
Back to top
View user's profile
Visovian

Bascom Member



Joined: 31 Oct 2007
Posts: 584
Location: Czech

czechrepublic.gif
PostPosted: Sat Feb 09, 2008 10:50 pm    Post subject: Reply with quote

Maybe a complete code would help?
Code:

'PWM.bas
'
'8-bit PWM (2-channel)
'Voltage on pins OC1A and OC1B changes slowly from 0 to 5V and back.

'''''''''''''''''''''''''''''''''''''

'$sim
$regfile = "Attiny2313.DAT"
$crystal = 10000000
Config Timer1 = Pwm , Pwm = 8 , Prescale = 1 , Compare A Pwm = Clear Up , Compare B Pwm = Clear Up

Config Portb.3 = Output                 'OC1A pin  (Portb.1 for Atmega8)
Config Portb.4 = Output                 'OC1B pin


'''''''''''''''''''''''''''' MAIN ''''''''''''''''''''''''''''''''''''
Defbyte B

 Do
   For B = 1 To 255
   Pwm1a = B                            'set duty cycle OC1A pin
   Pwm1b = 256 - B                      'set duty cycle OC1B pin
   Waitms 20
   Next

   For B = 255 To 1 Step -1
   Pwm1a = B
   Pwm1b = 256 - B
   Waitms 20
   Next
 Loop

'''''''''''''''''''''''''''' ENDMAIN ''''''''''''''''''''''''''
 End
Back to top
View user's profile
ApastalasM

Bascom Member



Joined: 12 Feb 2007
Posts: 23

lithuania.gif
PostPosted: Sun Feb 10, 2008 5:46 pm    Post subject: Reply with quote

Thanks a lot, works fine. 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 Archive 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