multiple pwm using TMR0

General discussion on mikroBasic.
Post Reply
Author
Message
georgefry
Posts: 12
Joined: 30 Jul 2005 18:26
Location: Scotland

multiple pwm using TMR0

#1 Post by georgefry » 04 Nov 2005 12:08

Can anyone help me?

I am trying to use timer0 interupt to create 4 PWM outputs.

I do not seem to be able to get a counter to increment in the ISR.

Code: Select all

program Usart_lib

DIM COUNT as byte
DIM PWMCOUNT as byte

sub Procedure interrupt
    COUNT = COUNT +1
       if COUNT = 6
        THEN PWMCOUNT = PWMCOUNT +1
           COUNT= 0
          ' if PWMCOUNT >= 254
           '   then PWMCOUNT = 0
            '  else
             ' end if

        else
       end if

    TMR0 = 10
    INTCON = $20
 end sub
 
Sub Procedure PWM_out(dim byref pwmcount,red,green,blue,white as byte)
       if red <= pwmcount
          then PortB.4 = 0
          else
          portB.4 =1
          end if
       if green <= pwmcount
          then PortB.5 = 0
          else
          portB.5 = 1
          end if
       if blue >= pwmcount
          then Portb.6 = 0
          else
          portb.6 =1
          end if
       if white >= pwmcount
          then Portb.7 = 0
          else
          portb.7 =1
          end if
 end sub
 

dim i as byte
dim RXCOUNT  as word
dim DMXaddress as byte
dim DMXaddress1 as byte
dim DMXaddress2 as byte
dim DMXaddress3 as byte
DIM ChanelR as Byte
DIM ChanelG as Byte
DIM ChanelB as Byte
DIM ChanelW as Byte
'DIM DimrVal as byte[20]

  DMXaddress = 5
  DMXaddress1 =6
  DMXaddress2 =7
  DMXaddress3 = 8
  trisa=0
  trisb=0
  trisc=$80
  trisd=0
  PWMCOUNT = 0
  COUNT = 0
  OPTION_REG = $87

  TMR0 = 10
  INTCON = $20
  PORTC = 0
main:

   Usart_init(250000)                ' Initialize USART module (8 bit, 250000 baud rate, no parity bit...

        rcsta.7  = 1                 ' use 9th data bit

   while true
     if Usart_Data_Ready = 1 then ' If data is received:
       'i= i+1
       i = Usart_Read              ' Read the received data
       IF RCSTA.2=1 then
          RXCOUNT = 0
          'i=1
          ELSE
          INC (RXCOUNT)
          end if
        If RXCOUNT = DMXaddress+1 then
           ChanelR = i
           else
              If RXCOUNT = DMXaddress1+1 then
           ChanelG = i
           else
           end if
               if RXCOUNT = DMXaddress2+1 then
           ChanelB = i
           else
           end if
           if RXCOUNT = DMXaddress3+1 then
           ChanelW = i
           else
              end if
           end if
     end if
     PWM_out(PWMCOUNT,ChanelR,ChanelG,ChanelB,ChanelW)
      PORTA = ChanelB
      ';PORTB = ChanelR
      PORTD = ChanelW
    wend
end.
I am using a PIC18F877A running at 20Mhz.

The code riads in data via serial port. This data is in a long string up to 255 bytes I use a counter to pick of the bytes i am intrested in.
This seems to work fine I can display the information I am sending on LEDs.

I cannot seem to get TMR0 to work though. Can anyone see if I am doing anything wrong.

Thanks
George Fry

georgefry
Posts: 12
Joined: 30 Jul 2005 18:26
Location: Scotland

#2 Post by georgefry » 04 Nov 2005 13:18

Oops

Just realised I have not initialised the interrupt For Timer 0

Thanks for all your hlp
George Fry

xor
Posts: 5465
Joined: 18 May 2005 00:59
Location: NYC
Contact:

#3 Post by xor » 04 Nov 2005 13:30

[color=darkred][b]xor[/b][/color]
[url=http://circuit-ed.com]CircuitED -[/url]

georgefry
Posts: 12
Joined: 30 Jul 2005 18:26
Location: Scotland

#4 Post by georgefry » 04 Nov 2005 15:50

Thanks for feedback xor.

Timing is an issue. Now that I got over my finger trouble with the interrupt routine.

I have adjusted PWM for a 50hz period. seems to be working now.
Just concerned now with how stable it wil be.


My next task is to synchronise this with mains frequency 47 to 63hz.

Designed a circuit to give me a pulse with each mains cycle. Will ultimately set frequency to double the mains frequency.
Intending to use RB0/INT to try come up with a way of setting the frequency.
Not sure how yet.

This application is for wide ranging ac input (85 to 264V) 200w isolated 4 channel driver to power banks of Luxeon LED's (350mA to 1A) .
Approx 16 LED's in each bank.

There will be predefined settings, and the option of control using DMX512 protocol.

Cheers
George
George Fry

Post Reply

Return to “mikroBasic General”