dsPIC33FJ128GP804 PWM - Page 1
Forum section: electronics / general

author: richcj10
author_rank: Unknown
timestamp: August 15, 2012, 05:23:47 pm
content: 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:program Pwm_Demodim current_duty, old_duty, current_duty1, old_duty1 as wordpwm_period1, pwm_period2, i, hold as wordsub procedure InitMain()ADPCFG = 0xFFFF ' initialize AN pins as digitalPORTB = 0 ' set PORTD to 0TRISB = 0 ' designate PORTD pins as outputPPS_Mapping(11, _OUTPUT, _OC1) ' RPx pin mapping OC1 to pin 8PPS_Mapping(10, _OUTPUT, _OC2) ' RPx pin mapping OC2 to pin 9end submain:InitMain()current_duty = 16 ' initial value for current_dutycurrent_duty1 = 16 ' initial value for current_duty1SetBit(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 PWM1PWM_Set_Duty(current_duty1, 2) ' Set current duty for PWM2if hold = 0xFFFF thenWhile TRUESetBit(PORTB, 8)Delay_ms(100)ClearBit(PortB, 8)Delay_ms(100)wendend ifi = 1while (TRUE) ' endless loopfor current_duty = 0 to 300PWM_Set_Duty(current_duty, 1) ' set newly acquired duty ratioPWM_Set_Duty(current_duty, 2)Delay_ms(5)i = Not inext current_dutycurrent_duty = 0wendend.PWM output on OC1 works but not on OC2... any ideas?Thanks,rick

author: PeterG
author_rank: Frequent Contributor
timestamp: August 16, 2012, 12:06:20 am
content: What dialect of Basic are you using?Regards

author: richcj10
author_rank: Unknown
timestamp: August 16, 2012, 12:10:38 am
content: Well, I use themikro Basic IDE

author: richcj10
author_rank: Unknown
timestamp: August 16, 2012, 03:35:44 am
content: I found out the issue, "a real trap for young players..." as Dave would say itIt is in the Config bits... Peripheral Pin select configIf 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.lolRick

author: poorchava
author_rank: Super Contributor
timestamp: August 16, 2012, 09:33:10 am
content: 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!

author: richcj10
author_rank: Unknown
timestamp: August 16, 2012, 12:49:19 pm
content: 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 40MhzBTW, 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:program Stuff'****************************************************'* Remember to edit Peripheral Pin select config *'****************************************************' Declarations sectiondim i as worddim count as worddim prevcount as worddim prevcount2 as worddim prevcount3 as worddim L as bitdim Value as integerdim Value2 as integersub procedure Initsubsys()TRISB.8 = 0Unlock_IOLOCK()PPS_Mapping(22, _OUTPUT, _U1TX) ' mapping RPx as uart1 txPPS_Mapping(23, _INPUT, _U1RX) ' mapping RPx as uart1 rxPPS_Mapping(11, _OUTPUT, _OC2) ' Sets RPx pin 10 to be PWMPPS_Mapping(10, _OUTPUT, _OC1) ' Sets RPx pin 11 to be PWMLock_IOLOCK()UART1_Init(19200) ' Initialize UART module at 19200 bpsDelay_ms(100) ' Stabilize UARTUART1_Write_Text("OK")PWM_Init(5000 , 1, 8, 2) ' Setup PWMPWM_Init(5000 , 2, 8, 3)PWM_Start(1) ' Start PWMPWM_Start(2)PWM_Set_Duty(0, 1) ' Set current duty for PWM1PWM_Set_Duty(0, 2)UART1_Write_Text("Setup Done") ' Debugend subsub procedure InitSensors() ' Read SensorsPortB.8 = NOT PortB.8Delay_ms(100)PortB.8 = NOT PortB.8Delay_ms(100)end submain:Initsubsys() ' setup pinsSetBit(PORTB, 8) ' Alive LED ONInitSensors() ' Setup SensorsUART1_Write(0x0D) ' menu system (RS232 ONLY)UART1_Write_Text("Send char for Menu")UART1_Write(0x0D)Delay_ms(1000)if ( UART1_Data_Ready() >= 1) thenUART1_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 ifwhile (TRUE) ' endless loopcount = count + 1Delay_ms(1)if (count >= prevcount + 250) then ' Rtn 1 - LED blinkprevcount = countPortB.8 = NOT PortB.8end ifif (count >= prevcount2 + 30) then ' Rtn 2 - PID loop 1prevcount2 = countValue = Value + 5if Value <= 300 thenPWM_Set_Duty(Value, 1)elseValue = 0end ifend ifif (count >= prevcount3 + 40) then ' Rtn 3 - PID loop 2prevcount3 = countValue2 = Value2 + 5if Value2 <= 300 thenPWM_Set_Duty(Value2, 2)elseValue2 = 0end ifend ifwendend.Any help would be great!Thanks...Rick

author: richcj10
author_rank: Unknown
timestamp: August 17, 2012, 01:59:09 am
content: 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

author: poorchava
author_rank: Super Contributor
timestamp: August 17, 2012, 02:59:18 pm
content: 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?

author: richcj10
author_rank: Unknown
timestamp: August 17, 2012, 03:01:42 pm
content: I am a newb to PIC's, would you have any examples?Thanks,Rick

author: plex
author_rank: Newbie
timestamp: August 19, 2012, 02:46:54 pm
content: 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!

author: richcj10
author_rank: Unknown
timestamp: August 19, 2012, 03:13:35 pm
content: 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...

author: poorchava
author_rank: Super Contributor
timestamp: August 19, 2012, 06:52:05 pm
content: 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).

author: richcj10
author_rank: Unknown
timestamp: August 19, 2012, 07:46:19 pm
content: I don't know.... research is in order.... lol