USART+PWM Problem


Closed Thread
Results 1 to 40 of 44

Hybrid View

  1. #1
    Join Date
    May 2007
    Posts
    16

    Default USART+PWM Problem

    Dear Friends im new in this forum , im a DJ and i interested with pic programming ,
    İ want to make my own device for light intensty (PWM) controls at home but i have some problem on my programs i use pbp 2,47 and mcs2300 and PIC16F877

    .Before i writte my problem i have looked this forum but couldt find it …

    İ want to make a device to make control lights by the dmx512 signal , i found a code in this forum

    But my device must have 4 chanel and 4 out ? i want to contlrol 4 lights intensty between
    0-255 brightness…
    and when it is 4 output it means that i cant use HPWM it must be normaly pwm between 0-255 .
    Can you help me or if you have any example code like this to get dmx512 signals

    This Code i have found in this forum…



    DEFINE OSC 20
    DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
    DEFINE HSER_TXSTA 24h ' Enable transmit, BRGH = 1
    DEFINE HSER_SPBRG 4 ' 25000 Baud @ 20MHz, 0,0%
    DEFINE HSER_BAUD 250000
    DEFINE HSER_CLROERR 1 ' Clear overflow automatically



    counter VAR word ' Variable de travail WORD
    idleflag VAR WORD
    dummy VAR WORD
    RCIF VAR byte
    startcode VAR WORD
    aminus VAR WORD
    adresse_grada VAR WORD
    x VAR WORD
    newlevel1 VAR WORD
    ADCON0=0
    ADCON1=7
    adresse_grada = 2


    checkdmx:

    counter = 1
    pulsin portc.7,0,counter 'the break signal

    if counter = 0 then
    idleflag = 1 'either no dmx, or break was too long to count
    'return
    endif


    if counter < 40 then checkdmx 'watching for 'break
    'if you get here, an active low pulse was detected, but it was too short.

    'probably just part of the datastream. So go back and look again.

    'otherwise, a valid break was found and it's time to read the start code

    dummy = RCREG 'clear out any garbage which may be in the USART
    dummy = RCREG
    SPBRG = 0
    TXSTA.2 = 0 'brgh = 0
    TXSTA.4 = 0
    RCSTA.7 = 1
    RCSTA.6 = 0 'setting 8 bit receive mode, no parity, etc
    RCSTA.4 = 0 'check the datasheet to see what all these bits do
    RCSTA.4 = 1 'now, the USART is on and ready to receive

    while RCIF = 0:wend 'hover here after break, before start code

    startcode = RCREG 'This is the first byte received after the break

    if startcode <> 0 then checkdmx 'do your own stuff here on a non-zero code

    aminus = adresse_grada - 1 'address1 is my target address

    for x = 1 to aminus 'set up a loop to count almost to the address
    while RCIF = 0:WEND 'sit here until a byte is received
    dummy = RCREG 'stash the byte somewhere unimportant
    next x

    newlevel1 = RCREG 'This is your target channel data
    pwm portc.2, newlevel1 ,100 'i want dimming a LED on PORTC.5
    RCSTA.7 = 0 'turn off the USART

    goto checkdmx
    return

    ================================================== =====
    ============== AND OTHER CODE FOR INTERUPT I THİNK==========
    ================================================== =====

    DimmerCount VAR WORD ; Location in DMX string
    DMXStartAddr VAR WORD ; DMX start address
    DMXStartCode VAR BYTE ; DMX Start code (Dimmer=0)
    Dummy VAR BYTE
    DMX_State VAR BYTE ; Stage of DMX reception

    Valid_Break CON 1
    Valid_SC CON 2
    Valid_Data CON 3

    Disable

    INTERRUPT_SUB:
    IF OERR Then
    CREN = 0
    CREN = 1 ; Clear Overrun Errors
    High Err_LED ; Notify user of overrun error
    DMX_State=0
    EndIF

    IF FERR Then
    FERR = 0
    DMX_State=Valid_Break
    Else
    GoSub RX_DMX_Data
    EndIF

    Dummy=RCREG ; Empty UART of junk data
    Resume

    RX_DMX_Data:
    IF DMX_State=Valid_Break Then
    HSerin 20,NoData,[DMXStartCode] ; Start code of 0 = dimmer data

    IF DMXStartCode=0 Then
    DMX_State=Valid_SC
    Else
    High Err_LED ; Notify user of Error in Start Code
    DMX_State=0
    EndIF
    EndIF

    IF DMX_State=Valid_SC Then
    For DimmerCount=0 TO DMXStartAddr; Count incoming data bytes (frames)
    Dummy=RCREG ; Read unwanted values
    Next DimmerCount
    DMX_State=Valid_Data
    EndIF

    IF DMX_State=Valid_Data Then
    HSerin 20,NoData,[STR DataBuffer\Number_Of_Channels]
    DMX_State=0
    EndIF

    IF DMX_State=0 Then
    Resume
    EndIF

    GoTo RX_DMX_Data

    NoData:
    DMX_State=0
    Resume

    Enable
    End

  2. #2
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by alex-x View Post
    Dear Friends im new in this forum , im a DJ and i interested with pic programming ,İ want to make my own device for light intensty (PWM) controls at home but i have some problem on my programs i use pbp 2,47 and mcs2300 and PIC16F877
    Sound familiar to anyone else? Same spelling mistakes...same basic format...different PIC...

  3. #3
    Join Date
    May 2007
    Posts
    16


    Did you find this post helpful? Yes | No

    Default Sİgnal

    Yes ,
    same problem with that friends but i use pic 16f877 and i see that they also still wait for help. but i think it is very difficult to make this device... cause i see many peoples here they look for this problem
    it is impossible to learn everything by ourself if some friends will help us we can also learn how to make it.

    Best Regards..
    Last edited by alex-x; - 11th May 2007 at 21:04. Reason: mistake

  4. #4
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by alex-x View Post
    Yes ,
    same problem with that friends but i use pic 16f877 and i see that they also still wait for help. but i think it is very difficult to make this device... cause i see many peoples here they look for this problem
    it is impossible to learn everything by ourself if some friends will help us we can also learn how to make it.

    Best Regards..
    Be that as it may...
    The first program has the ability (barely) to read one channel, if it weren't for a few fairly well hidden bugs.
    The second chunk of code really isn't a program at all, it's just a chunk of code to receive the data (again, barely). It doesn't do anything with it after that. In car terms...what you've got is an engine...no body, no wheels.

    How much PIC programming have you done in the past? Working with DMX is most definetely not a project for the beginner, not saying it can't be done, but you'll be shooting yourself in the foot in a few days.

  5. #5
    Join Date
    May 2007
    Posts
    16


    Did you find this post helpful? Yes | No

    Default usart

    Thanks for your helping and gave me nice ADVICE :-))))
    i dont think that it is big project for beginner and im not beginner too
    i made many programs since years but not on DMX or some serial community .
    i need help for programing like other friends . i look on internet and search it in all site but nobody knows how to make it i dont think that it is so big problem i need only how to decode more chanles there is some sample codes here but they are not working if somebody can help us to make this project we are so happy

  6. #6
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by alex-x View Post
    i need help for programing like other friends . i look on internet and search it in all site but nobody knows how to make it i dont think that it is so big problem i need only how to decode more chanles there is some sample codes here but they are not working if somebody can help us to make this project we are so happy
    Well, if nobody knew how to make it, then I guess DMX wouldn't be as big a standard and be as widely used as it is now would it? There are examples on this forum that seem to be working just fine, I say 'seem to be' as I have not tried them for myself, but the code looks good.

    As I said before, in that program you posted above, there's a couple of fairly well hidden bugs in it that'll prevent it from doing anything...find those and you'll most likely have a working example to build from.

  7. #7
    Join Date
    May 2007
    Posts
    16


    Did you find this post helpful? Yes | No

    Default dmx

    This is the DMX recive code that does work.

    It reads the first 8 byte of a DMX stream and sends the values to a RS232 output.

    DEVICE = 16F648A
    CONFIG HS_OSC, WDT_OFF, PWRTE_OFF, BODEN_OFF, LVP_OFF, CP_OFF
    XTAL = 20
    ALL_DIGITAL = TRUE

    SYMBOL DmxIn = PORTB.1
    SYMBOL RS232out = PORTB.4

    DIM BreakDet AS WORD
    DIM Ch AS WORD
    DIM DMXdata[9] AS BYTE '8 DMX channel readout array
    DIM Value AS BYTE

    HSERIAL_CLEAR = ON

    TXSTA.2 = 1 'bit BRGH; set USART in high speed mode
    SPBRG = 4 'byte SPBRG; 250Kbaud@20MHz gives SPBRG=4
    TXSTA.4 = 0 'bit SYNC; set asynchronous mode (=0) or synchronous mode (=1)
    RCSTA.6 = 0 'bit RX9; setting 8bit(=0) or 9bit(=1) asynchronous receive mode

    '--init--------------------------

    Value = 0
    Ch = 0
    BreakDet = 0

    DMXdata[1] = 0
    DMXdata[2] = 0
    DMXdata[3] = 0
    DMXdata[4] = 0
    DMXdata[5] = 0
    DMXdata[6] = 0
    DMXdata[7] = 0
    DMXdata[8] = 0

    '---main-------------------------

    Main:
    RCSTA.4=0 'bit CREN; disable serial reception
    RCSTA.7=0 'bit SPEN; disable serial port input
    CLEAR RCREG 'clear USART recieve register
    '--------------------
    REPEAT 'BREAK detection (88us)
    BreakDet=PULSIN DmxIn,0
    UNTIL BreakDet>=44
    '--------------------
    RCSTA.7=1 'bit SPEN; enable serial port input
    RCSTA.4=1 'bit CREN; enable serial reception
    CLEAR RCREG 'clear USART recieve register
    '--------------------
    REPEAT 'Skip 1st start byte
    UNTIL PIR1.5=1 'bit RCIF; if reception is complete, RCIF=1
    CLEAR RCREG 'clear USART recieve register
    '--------------------
    FOR Ch=1 TO 8 'readout first 8 channels
    REPEAT
    UNTIL PIR1.5=1 'bit RCIF; if reception is complete, RCIF=1
    Value=RCREG 'read DMX data from USART recieve register
    DMXdata[Ch]=Value
    CLEAR RCREG 'clear USART recieve register
    NEXT
    GOSUB SendCommand 'send RS232 commands
    GOTO Main
    '----------------------------------
    SendCommand:
    SEROUT RS232out,84,[DEC DMXdata[1],"-",DEC DMXdata[2],"-",DEC DMXdata[3],"-",DEC DMXdata[4],"-",DEC DMXdata[5],"-",DEC DMXdata[6],"-",DEC DMXdata[7],"-",DEC DMXdata[8],13]
    RETURN
    '----------------------------------

  8. #8
    Join Date
    May 2007
    Posts
    16


    Did you find this post helpful? Yes | No

    Default dmx

    Device 16f877
    xtal 20

    HSERIAL_BAUD = 250000
    HSERIAL_RCSTA = %10010000
    HSERIAL_TXSTA = %00100000
    HSERIAL_CLEAR = On

    'CONFIG HS_OSC, WDT_OFF, PWRTE_OFF, BODEN_OFF, LVP_OFF, CP_OFF

    ALL_DIGITAL = TRUE

    Dim Sb As Byte
    Dim Channel As Byte
    Dim Var1[42] as byte' for (1-42) channels

    delayms 100
    RCSTA.7 = 1
    RCSTA.6 = 0 'setting 8 bit receive mode, no parity, etc
    RCSTA.4 = 0

    Start:
    RCSTA.4 = 0 'dis-enable serial

    DMX_Break:

    Sb=PulsIn portc.7,0 'measure a pulse

    If Sb < = 44 Then DMX_Break '( test the pulse is greater than 88 us if not valid so return to DMX_Break and test again)

    Clear RCREG 'clears rcreg twice as it can hold 2 bytes

    RCSTA.4 = 1 'enable the serial to read value

    For Channel = 1 To 42
    Var1[Channel] = HRSin 'read value of dmx
    Next Channel

    goto start

  9. #9
    Join Date
    May 2007
    Posts
    16


    Did you find this post helpful? Yes | No

    Default it is ok

    it is ok i solved my problem and made 4 chanel dmx512 decoder i have found some example code for proton and basic too you can see them

  10. #10
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by alex-x View Post
    it is ok i solved my problem and made 4 chanel dmx512 decoder i have found some example code for proton and basic too you can see them
    Proton Basic will never compile with PBP...
    And it may work, but do you understand how it works so you can customize it to your needs...

  11. #11
    Join Date
    May 2007
    Posts
    16


    Did you find this post helpful? Yes | No

    Default dmx

    @skimask you are right this are in proton but can translate to pbp

    Yes i understand how does it work , i know dmx since long time but not in pbp i made my project before in asm and it was easy to find example asm code to make dmx decoder and transmitter in microchip site also they give it in asm examples,

    but when i want to make it in PBP i had some problem on interupts and to receive more chanel but now i understand how does it workin PBP too

  12. #12
    Join Date
    May 2007
    Posts
    16


    Did you find this post helpful? Yes | No

    Default usart

    Yes we canmake it so as a 1 chanel and workşng no problem but if i want to use it 4 or 8 chanel then i will need a pwm and pic has max 3 pwm no more how can we use and make other chanels pwm if i want to use 5 chanel dmx data for led inensty?Then i will need more pwm and i have to make it with code i think*

Similar Threads

  1. Variable PWM PIC18F2431
    By trr1985 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 12th June 2009, 06:03
  2. byte Tx w/ USART 16f876a problem
    By GargamelDigi in forum mel PIC BASIC
    Replies: 2
    Last Post: - 15th July 2008, 05:50
  3. USART Problem , but don't know where, in pc? or in PIC?
    By precision in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 15th July 2007, 08:12
  4. problem with USART
    By leemin in forum Serial
    Replies: 4
    Last Post: - 11th December 2006, 17:56
  5. PWM problem CCP2 port
    By NL2TTL in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 19th September 2006, 23:34

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts