CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

SPWM generation using PIC18F4431 and Timer0 interrupt

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
XYZ_ABC



Joined: 13 May 2015
Posts: 6

View user's profile Send private message

SPWM generation using PIC18F4431 and Timer0 interrupt
PostPosted: Wed May 13, 2015 6:33 am     Reply with quote

Hello everyone,

I am using the Power Control PWM module (PCPWM) in PIC18F4431 to generate the 6 PWM signals (SPWM) required for my three phase inverter. Since this is a three phase
application, i need three PWM signals 120 degrees phase shifted from one another and the other three PWM signals complement of these. For this i have
created a sine look up table with the duty cycle equivalents and three indices to point to three locations on the sine table to get the 120 degree
equivalent phase shifts in the respective PWM signals. PWM frequency requirement is 20kHz and the sine wave inverter output frequency is 50Hz. The sine table thus has 400 values.

I am using the continuous up/down count mode of PWM and for 20kHz PWM frequency, 20MHz oscillator frequency, PTMR pre-scale value 1:1,
the PTPER value was found to be 0x7D (Calculated using the equation in the data sheet). So the range of values that can be loaded into the duty cycle registers is 0 to 249.

I want the duty cycle registers to be updated from the sine lookup table every 50us (for one PWM period) so that the 400 values are read in one full cycle of the sine wave which is 20ms.

I thought of using the timer0 to generate an interrupt every 50us, and in the interrupt routine i will update the PDC (Duty cycle registers 0, 1 and 2) by reading a value from the sine lookup table.

I am able to watch the registers updated in the MPLAB SIM.. but when the hex file is loaded to PROTEUS circuit with the microcontroller chip , there is no PWM output...

What could be the problem? Kindly guide me with this. Am i right with the concept of timer0 interrupt to access the values from the sine table? anything else i should be checking or initializing for this?

PS: I have initialized the PCPWM module, initialized the timer0, loaded the timer0 with a value to generate interrupt at every 50us in the C program.

Its really very urgent kindly help me out with this. Thank you.

Regards,

XYZ_ABC
Ttelmah



Joined: 11 Mar 2010
Posts: 19432

View user's profile Send private message

PostPosted: Wed May 13, 2015 8:16 am     Reply with quote

First, forget Proteus.

It rarely gets the more complex peripherals on the PIC 'right'. This is why there is a header here on the forum saying 'no Proteus questions'. The odds are if you get it all working perfectly in Proteus, you will then have to redesign it completely to get it working on a real chip.....

Then don't use a separate timer. Use the interrupt from the PWM module. The PWM period updates occur on the 'next' PWM period, it only takes even one cycle of difference between the PWM rate, and your timer rate, to result in missing changes, and glitches. INT_PWMTB. With a 1:1 postscaler.
XYZ_ABC



Joined: 13 May 2015
Posts: 6

View user's profile Send private message

SPWM generation using PIC18F4431 and Timer0 interrupt
PostPosted: Wed May 13, 2015 9:58 am     Reply with quote

Thank you so much for your reply Ttelmah... Ok yes i will not worry about the program not working in proteus from now on...

And i will use the interrupt available in the PWM module like you suggested and get back to you sir.

Thank you
XYZ_ABC



Joined: 13 May 2015
Posts: 6

View user's profile Send private message

SPWM generation using PIC18F4431 and Timer0 interrupt
PostPosted: Wed May 13, 2015 10:04 am     Reply with quote

How do i check the program for its correct working once i have got "Build Successful" in the MPLAB IDE?
XYZ_ABC



Joined: 13 May 2015
Posts: 6

View user's profile Send private message

SPWM generation using PIC18F4431 and Timer0 interrupt
PostPosted: Wed May 13, 2015 10:27 am     Reply with quote

Ttelmah...

Please find the code of initialization for Timer0 and the interrupt routine i wrote to provide interrupts for every 50us, kindly correct if there is a mistake and let me know if anything else is required to get this working.

Could you also kindly explain how the interrupt in the PWM module itself can be used to update the duty cycle register as per the sine table?

I will try then try to rectify the code for both these and use the one which gets working.

Thank you.


Last edited by XYZ_ABC on Mon May 18, 2015 12:58 am; edited 1 time in total
Ttelmah



Joined: 11 Mar 2010
Posts: 19432

View user's profile Send private message

PostPosted: Wed May 13, 2015 1:36 pm     Reply with quote

Seriously, use CCS code.

Basically you almost never ever, need to write directly to any register using CCS. If you want to setup the timer, use the CCS setup_timer function.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed May 13, 2015 1:42 pm     Reply with quote

I think his compiler is C18 or XC8.
Ttelmah



Joined: 11 Mar 2010
Posts: 19432

View user's profile Send private message

PostPosted: Wed May 13, 2015 1:49 pm     Reply with quote

AAARGH!....... Mad

To him then. _This_ is a CCS forum.
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Wed May 13, 2015 3:44 pm     Reply with quote

Quote:
How do i check the program


Answer:
Build the circuit.
Program the pic.
Apply power.
debug the code and hardware together.

The same way the pros do it..........

Very Happy Very Happy Very Happy
XYZ_ABC



Joined: 13 May 2015
Posts: 6

View user's profile Send private message

SPWM generation using PIC18F4431 and Timer0 interrupt
PostPosted: Wed May 13, 2015 7:13 pm     Reply with quote

PCM programmer.. yes you are right... i use HI TECH C18 for compiling and have written the code in MPLAB IDE....

Ttelmah... i am sorry i know it is CCS forum... Had read posts of yours and PCM programmer on other topics... You both are just a library of knowledge... I didn't find any answer to my questions anywhere from other forums... thought the concept would be the same and queried in here...

I am sorry guys... If you can guide me in any way pls do... or if this post has to be removed i will do that too so that its not a trouble for others...

asmboy... Why i meant by asking "How do i check the program is".. wanted to know if there is any way to simulate the code and check the results (in software) than every time programming the PIC and hardwiring.... But yes if there is no other way to check if my code is working as per requirements... i will hardwire the circuit and check....

Thank you so much guys...
temtronic



Joined: 01 Jul 2010
Posts: 9197
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed May 13, 2015 7:50 pm     Reply with quote

In the 20+ years of using PICs, I've never, ever found a 'simulator' that actually simulates a PIC properly. ALL have bugs or errors or do not simulate the real World correctly, NONE work as they should.
That's why it's critical that you use a real PIC, program it and see what happens. You'll soon find that PCB layout is CRITICAL to proper operation, that power supplies MUST be clean and that the datasheet needs to be read and re-read several times (boring BUT necessary) to see why 'things' have to be done this way and not that way.
You should know that there are several ways to generate 3 phase signals and some do not require a fancy internal peripheral. Depending on the design specs the 'simple' techniques' could work fine for you.

Jay
XYZ_ABC



Joined: 13 May 2015
Posts: 6

View user's profile Send private message

SPWM generation using PIC18F4431 and Timer0 interrupt
PostPosted: Wed May 13, 2015 8:05 pm     Reply with quote

temtronic (Jay)... thank you for the kind reply...

This is my first ever program written for a PIC application... I am just well versed with the C language... I have learnt things from scratch to write this code...

I chose to use the internal PCPWM module since it easily gives the 3 PWM outputs and the complementary 3 PWM's by just writing into the duty cycle register, which is the main requirement for generating three phase signals...

and i am now stuck up with this problem... Got to somehow fix it at the earliest....

And ok i will check the program in the hardware circuit itself for its working...

Thank you
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
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


Powered by phpBB © 2001, 2005 phpBB Group