Author Topic: dsPIC33FJ128GP804 PWM  (Read 9438 times)

0 Members and 1 Guest are viewing this topic.

Offline richcj10Topic starter

  • Supporter
  • ****
  • Posts: 201
  • Country: us
dsPIC33FJ128GP804 PWM
« on: August 15, 2012, 05:23:47 pm »
I am using a PIC for the first time. I would like to use C but a client wants it to be in Basic. So, here is what i have:


Code: [Select]

program Pwm_Demo
dim current_duty, old_duty, current_duty1, old_duty1 as word
    pwm_period1, pwm_period2, i, hold as word

sub procedure InitMain()
  ADPCFG = 0xFFFF                            ' initialize AN pins as digital
  PORTB = 0                                  ' set PORTD to 0
  TRISB = 0                                  ' designate PORTD pins as output
 
  PPS_Mapping(11, _OUTPUT, _OC1)    ' RPx pin mapping OC1 to pin 8
  PPS_Mapping(10, _OUTPUT, _OC2)    ' RPx pin mapping OC2 to pin 9
end sub

main:
  InitMain()
  current_duty  = 16                         ' initial value for current_duty
  current_duty1 = 16                         ' initial value for current_duty1
  SetBit(PORTB, 8)
  Delay_ms(100)
  ClearBit(PortB, 8)
  Delay_ms(100)
  SetBit(PORTB, 8)
  Delay_ms(100)
  ClearBit(PortB, 8)
  Delay_ms(100)
  SetBit(PORTB, 8)
  Delay_ms(100)
  ClearBit(PortB, 8)
  Delay_ms(100)

  pwm_period1 = PWM_Init(5000 , 1, 1, 2)
  pwm_period2 = PWM_Init(5000,  2, 1, 3)

  PWM_Start(1)
  PWM_Start(2)

  PWM_Set_Duty(current_duty,  1)             ' Set current duty for PWM1
  PWM_Set_Duty(current_duty1, 2)             ' Set current duty for PWM2
  if  hold = 0xFFFF then
    While TRUE
      SetBit(PORTB, 8)
      Delay_ms(100)
      ClearBit(PortB, 8)
      Delay_ms(100)
    wend
  end if
  i = 1
  while (TRUE)                               ' endless loop
     for current_duty = 0 to 300
      PWM_Set_Duty(current_duty,  1)         ' set newly acquired duty ratio
      PWM_Set_Duty(current_duty,  2)
      Delay_ms(5)
      i = Not i
     next current_duty
    current_duty = 0
  wend
end.



PWM output on OC1 works but not on OC2... any ideas?

Thanks,

rick
 

Offline PeterG

  • Frequent Contributor
  • **
  • Posts: 835
  • Country: au
Re: dsPIC33FJ128GP804 PWM
« Reply #1 on: August 16, 2012, 12:06:20 am »
What dialect of Basic are you using?

Regards
Testing one two three...
 

Offline richcj10Topic starter

  • Supporter
  • ****
  • Posts: 201
  • Country: us
Re: dsPIC33FJ128GP804 PWM
« Reply #2 on: August 16, 2012, 12:10:38 am »
Well, I use the
mikro Basic IDE
 

Offline richcj10Topic starter

  • Supporter
  • ****
  • Posts: 201
  • Country: us
Re: dsPIC33FJ128GP804 PWM
« Reply #3 on: August 16, 2012, 03:35:44 am »
I found out the issue, "a real trap for young players..." as Dave would say it
It is in the Config bits...  Peripheral Pin select config

If you leave it in the default state, You can only do this once,
Since, I needed to do this 4 times, it only changed one pin and ignored the rest.

lol

Rick
 

Offline poorchava

  • Super Contributor
  • ***
  • Posts: 1673
  • Country: pl
  • Troll Cave Electronics!
Re: dsPIC33FJ128GP804 PWM
« Reply #4 on: August 16, 2012, 09:33:10 am »
I must say that these microcontrollers (together with pic24h) are my favourite in terms of mid-range. I might have some reccomendations in for you, if you're just beggining with them:

-did your client give any particular reason why he wants it to be written in basic? It's worse than C in probably every possible way.
-generally there are some things to remember when using dspic33's/pic24h's: set correct settings (#pragma config and so on), setup the PLL is you're using it (you probably are because otherwise you wouldn't need that powerful MCU), wait until it locks, then make sure that analog/multiplexed pins are set properly (usually the are analog by default which often causes people to go crazy not being able to figure out what's going on), make sure that peripheral pin select is setup correctly.

When this is done you are good to go :). btw, you can reconfigure pps at runtime, which means that you can (for example) connect multiple devices to your mcu using UART with totally different pairs of pins (eg. for the sake of easier pcb layout) and then service them with single uart hardware module just by switching the PPS. Thing to notice: PPS covers only standard digital pins and interfaces. Analog stuff and comm interfaces using OC outputs (like I2C) are not covered!
I love the smell of FR4 in the morning!
 

Offline richcj10Topic starter

  • Supporter
  • ****
  • Posts: 201
  • Country: us
Re: dsPIC33FJ128GP804 PWM
« Reply #5 on: August 16, 2012, 12:49:19 pm »
I agree that C is sooo much better. My client wanted me to write in a language that he knew....
I have the PLL set up.. Im clocking the chip at 40Mhz

BTW, I made little programs to test different systems on the PIC. I have PWM, Serial, and Analog are done and are working. I need to work on CAN next.
I combined PWM and Serial and now the serial data seems corrupted... All I get is gibberish coming from the device...

Code:

Code: [Select]

program Stuff

'****************************************************
'*  Remember to edit  Peripheral Pin select config  *
'****************************************************

' Declarations section
dim i as word
dim count as word
dim prevcount as word
dim prevcount2 as word
dim prevcount3 as word
dim L as bit
dim Value as integer
dim Value2 as integer

sub procedure Initsubsys()
  TRISB.8 = 0
  Unlock_IOLOCK()
    PPS_Mapping(22, _OUTPUT, _U1TX)          ' mapping RPx as uart1 tx
    PPS_Mapping(23, _INPUT, _U1RX)           ' mapping RPx as uart1 rx
    PPS_Mapping(11, _OUTPUT, _OC2)           ' Sets RPx pin 10 to be PWM
    PPS_Mapping(10, _OUTPUT, _OC1)           ' Sets RPx pin 11 to be PWM
  Lock_IOLOCK()

  UART1_Init(19200)                        ' Initialize UART module at 19200 bps
  Delay_ms(100)                            ' Stabilize UART
  UART1_Write_Text("OK")
 
  PWM_Init(5000 , 1, 8, 2)                 ' Setup PWM
  PWM_Init(5000 , 2, 8, 3)

  PWM_Start(1)                             ' Start PWM
  PWM_Start(2)

  PWM_Set_Duty(0, 1)                       ' Set current duty for PWM1
  PWM_Set_Duty(0, 2)
 
  UART1_Write_Text("Setup Done")           ' Debug
end sub

sub procedure InitSensors()                ' Read Sensors

  PortB.8 = NOT PortB.8
  Delay_ms(100)
  PortB.8 = NOT PortB.8
  Delay_ms(100)

end sub

main:
  Initsubsys()                             ' setup pins
  SetBit(PORTB, 8)                         ' Alive LED ON
  InitSensors()                            ' Setup Sensors
  UART1_Write(0x0D)                        ' menu system (RS232 ONLY)
  UART1_Write_Text("Send char for Menu")
  UART1_Write(0x0D)
  Delay_ms(1000)
  if ( UART1_Data_Ready() >= 1) then
    UART1_Write_Text("1) T/C ")
    UART1_Write(0x0D)
    UART1_Write_Text("2) stuff ")
    UART1_Write(0x0D)
    UART1_Write_Text("3) Current ")
    UART1_Write(0x0D)
    UART1_Write_Text("3) Reset ")
    UART1_Write(0x0D)
    UART1_Write_Text("5) About")
    UART1_Write(0x0D)
  end if
  while (TRUE)                             ' endless loop
  count = count + 1
  Delay_ms(1)
  if (count >= prevcount + 250) then       ' Rtn 1 - LED blink
      prevcount = count
      PortB.8 = NOT PortB.8
     end if
  if (count >= prevcount2 + 30) then       ' Rtn 2 - PID loop 1
      prevcount2 = count
      Value = Value + 5
      if Value <= 300 then
        PWM_Set_Duty(Value, 1)
      else
        Value = 0
      end if
    end if
  if (count >= prevcount3 + 40) then       ' Rtn 3 - PID loop 2
    prevcount3 = count
    Value2 = Value2 + 5
    if Value2 <= 300 then
      PWM_Set_Duty(Value2, 2)
    else
      Value2 = 0
    end if
   end if
  wend
end.


Any help would be great!

Thanks...

Rick
« Last Edit: August 16, 2012, 04:42:36 pm by richcj10 »
 

Offline richcj10Topic starter

  • Supporter
  • ****
  • Posts: 201
  • Country: us
Re: dsPIC33FJ128GP804 PWM
« Reply #6 on: August 17, 2012, 01:59:09 am »
I adjusted the system back to 25Mhz and the serial fixed it's self. Any way to speed  up the UART? I would like to clock the pic at 40 Mhz.

Thanks,

Rick
 

Offline poorchava

  • Super Contributor
  • ***
  • Posts: 1673
  • Country: pl
  • Troll Cave Electronics!
Re: dsPIC33FJ128GP804 PWM
« Reply #7 on: August 17, 2012, 02:59:18 pm »
did you try to manually configure the registers for uart? I mean at 40Mhz and 19.2k baud the error is something like 0.0015. That shouldn't mess up your comm at that speed.

Maybe built in basic libraries are crewd up?
I love the smell of FR4 in the morning!
 

Offline richcj10Topic starter

  • Supporter
  • ****
  • Posts: 201
  • Country: us
Re: dsPIC33FJ128GP804 PWM
« Reply #8 on: August 17, 2012, 03:01:42 pm »
I am a newb to PIC's, would you have any examples?

Thanks,

Rick
 

Offline plex

  • Newbie
  • Posts: 8
  • Country: gr
Re: dsPIC33FJ128GP804 PWM
« Reply #9 on: August 19, 2012, 02:46:54 pm »
I use the mikroE compilers also. Sometimes things dont work correctly with the built in peripheral setup functions. You could try setting up the uart using the configuration registers. This will give you a better understanding of its function and limitations and you might also find the problem. For example you cannot set any baud rate with any oscillator setting. At some baud rates the timing error might be too large for reliable communication.
It is also essential to check the ERRATA sheet from microchip to find out if there any special problems with the chip hardware revision you are using.
Hope this helps!
 

Offline richcj10Topic starter

  • Supporter
  • ****
  • Posts: 201
  • Country: us
Re: dsPIC33FJ128GP804 PWM
« Reply #10 on: August 19, 2012, 03:13:35 pm »
I checked the ERRATA before I ever made the PCB. (This lesson was provided by Dave in one of his videos)
I can use the UART at 25 Mhz. So, I know it isn't the PCB. I use a crystal with correct trimming caps for clock.
I will look at the registers...


 

Offline poorchava

  • Super Contributor
  • ***
  • Posts: 1673
  • Country: pl
  • Troll Cave Electronics!
Re: dsPIC33FJ128GP804 PWM
« Reply #11 on: August 19, 2012, 06:52:05 pm »
I don't have any examples at hand (no access to main PC :/).

Main idea is that you read the datasheet/refence sheet/application notes/etc and then set bits in registers as per these documents. They contain all the informations like in register ADCON0 bit0 does this or that when set to '1' or '0'. This way you can be sure how the code works.

I had very unpleasant experiences with ready-to-use libraries (although official Microchip C30m not mikroE). I was in a rush and used their UART library. For some reason i was getting total gibberish out in terminal. after like 2 days of hair pulling i have examined registers with debugger and it turned out, that the PIC i was using had an option to reverse the polarity of UART lines (i guess for driving lines with schmitt inverters or some open collector/open drain drivers). It so happend that library didn't support this feature and moreover: it was setting the reverse polarity bit on occasion when setting adjecent bit (i guess in some other pic this bit was unused). Generally when i have set up all the registers one by one thing miraculously started to work.

Since you use basic, you probably cannot do disassembly debugging (or can you? - i'm not familiar with mikroE).
I love the smell of FR4 in the morning!
 

Offline richcj10Topic starter

  • Supporter
  • ****
  • Posts: 201
  • Country: us
Re: dsPIC33FJ128GP804 PWM
« Reply #12 on: August 19, 2012, 07:46:19 pm »
I don't know.... research is in order.... lol
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf