Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

pwm to generate user defined pulses using pic18f4550

Status
Not open for further replies.

vterminater

Junior Member level 1
Joined
Jul 16, 2010
Messages
19
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,413
hey i have to generate 3 pulses...on,off,on using pic18f4550,will be using 20Mhz crystal...and the time period of each is specified by the user..any ideas on how to do it..:any program?
 

Since in PWM the on time & the Off time are dependent upon the duty cycle...So every time u perform PWM u load only one value(x) it may be between 0-FF.ie ur duty cycle is adjusted by this value. Thus the On time would be x and offtime would be (255-x) maintaining the total time period to be constant..Now this was for one cycle of PWM. As u want 1 1/2 cycle of PWM, u can count the number of times On/Off cycle is performed & exit when count is equal to defined number...Regards
 
oki..lemme take an example if i want a pulse of 20ms then delay of 10ms and then another pulse of 30ms then???what is the easiest way to do this??
 

Is this a once of or does it repeat ? A PWM is a bit of
overkill if its just the 3 pulses you say in your orgingal post. For that I'd just use timer0 and perhaps portb.1 and just switch it on and off.

Read the data sheet about the prescalers
too. (Or the free Mikroelectronica tutorial from their book - that will probably tell you all you need to know in the easiest way.)

If you must use the PWM module and you use MikroC you can just tell it the frequency
and duty cycle.

The compiler is free for programs up to 2K and generates assembler and hex

jack
 
have to generate this only once...can i get a sample program fo this??
 

Why dont u just use a delay sequence....fe.g

delay_ms(20)
delay_ms(10)
delay_ms(30)

In between these delays..u can place code for setting the port pin to be high or low...

Regards..
 
let's say that you have a pwm period of pwm_period (in ms), and you want to have a pulse width of pwm_width (in ms).

all you need is

Code:
turn on the pwm pin;
delay_ms(pwm_width);
turn off the pwm pin;
delay_ms(pwm_period-pwm_width);
 
oh cool...actually a full code would be better...am pretty new to programming....anyone?

thanks...:D
 

@millwood
No dude...What i meant to say was that i would create a simple delay in mS...It wont do PWM..e.g

delay_ms(20) //function delay provides delay of 1 ms...the passed valued specifies how many times repeat the delay ie total delay = 20x1mS=20mS

Although its possible what u are saying...& the code below demonstrates it.

@vterminater
Why do u need to do PWM if a simple delay sequence can do this...e.g

Code:
Pin on                  
delay_ms(20);        //Pin on for a delay of 20mS
Pin off
delay_ms(10);       //Pin off for a delay of 10mS
Pin on
delay_ms(30);       //Pin on for a delay of 30mS
Pin off

I think this will provide the Pulse sequence u need...

Here is the program which demonstrates similar(not exact) functionalityas per millwood...But this is for 8051

Code:
#include<stdio.h>
#include<AT89X51.h>
//fclk=(focs/12)=(12M/12)=1MHz=1uS //Maximum Delay(Timer0 Mode 2) = 255 x 1uS = 255uS
void delay(unsigned char);

void  main(){
unsigned char i;
		TMOD=0X02;
		i=0xFF;
	while(1){
		P2_7=0;
		delay(i);
		P2_7=1;
		delay(255-i);
	}
}


void delay(unsigned char value){	 
unsigned char x;
		for(x=0;x<value;x++){
			TH0=0xE4;		 
			TR0=1;
			while(TF0!=1);
			TF0=0;
			TR0=0;	
		}
}


Regards.
Sidy.
 
@millwood

Yeah man...Lol its actually the samething in the code...
 
it is not terribly efficient and it is hard to do multi-channel independent pwm with this approach.

But it can be made to work.
 
Yeah man...

But before this code...i tried to do PWM using interruput function...

Could you just tell me...whenever an interrupt function occurs, the contents in the PSW may change while executing the interrupt function...

But when the control returns to the main function...
the changes in the PSW are not maintained...I mean they are different from those during execution of interrupt function...Is it happens to be this way or im wrong?
 
I don't use 8051 so I cannot be specific on the hardware.

with an interrupt, the approach is to use a timer to trigger the interrupt.

let's say that you have a count-up timer that trips at 0xff (8-bit timer). you have a pwm period of pwm_period (<0xff) and pwm width of pwm_width.

it would be something like this for your interrupt service routine:

Code:
pwm_isr() {
  static pwm_status=0;  //0=pwm off and 1=pwm on

  if (timer overflow) {  // in case you have other interrupt source
     reset timer flag;
     if (pwm_status) {  // pwm is on
       load timer to256-(pwm_period-pwm_width);  // count pwm_period-pwm_width cycle 
       turn off pwm output;
       pwm_status=0;
    }
    else {
       load timer to 256-pwm_width; //count pwm_width ticks;
       turn on pwm_output;
       pwm_status=1;
    }
}

you can get away with having pwm_status if you can directly read pwm_output's output status - most likely you can.

alternatively, you can keep pwm_status in the isr and use that as a flag to change the pwm output pin in the main loop.
 
not used to the c dode...asm code please..:|

Added after 35 minutes:

since the user has to provide the 3 time periods of the delays, i have a few hurdles
1)The formula for timer0 to calculate count is 'Td=instruction cycle clock*prescalar*count'...now to calculate count i have to fix prescalar so since my delay time is goin to be small and i am using external oscillator of 20MHz i fix prescalar to 1 or 2..this would limit my Td to in the range 0.2us to 13ms..!!!!(did this by calculating Tdmax=4*Tosc*1*65536 and tdmin=4*Tosc*1*1.."hope atleast this is right")
2)now since my t0con is related to count i am unsure of how i will assign it everytime for my delays!!!
anyone with an asm code or an explanation for the above??! :| :!:
 

some sort of solution for the hurdles atleast??
 

oki so at last i ahve tried something

Code:
#include<p18f4550.inc>
CONFIG WDT= OFF; disable watchdog timer
CONFIG MCLRE = ON; MCLEAR Pin on
CONFIG DEBUG = ON; Enable Debug Mode
CONFIG LVP = OFF; Low-Voltage programming disabled (necessary for debugging)
CONFIG FOSC = INTOSC_EC
;for external clocK
Td1 equ 0x00
Td2 equ 0x01
Td3 equ 0x02
Td equ 0x03
org 0
goto start
org 0x20
start:clrf TRISD
movlw 0x32
movwf Td1
movlw 0x64
movwf Td2
movlw 0xc8
movwf Td3
movlw 0xff
movwf PORTD
movlw b'10111000'
movwf T0CON
movff Td1,Td
call delay
movlw 0x00
movwf PORTD
movff Td2,Td
call delay
movlw 0xff
movwf PORTD
movff Td3,Td
movlw 0x00
movwf PORTD

org 0x60
delay:movlw 0x05
mullw Td
movff PRODL,TMR0L
movff PRODH,TMR0H
bcf INTCON,TMR0IF
here:goto here
return
end



not getting the output properly for this!!!why?????????help:cry:
 

not getting the output properly for this!!!why?????????help:cry:

so what output did you get? and why do you think it is NOT the proper output?
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top