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.

need code which generate ir 1hz and 5 hz pwm using pic 18f4520

Status
Not open for further replies.

HARIS RASHID

Newbie level 4
Newbie level 4
Joined
Jul 19, 2011
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,334
i want complete code which generate 1 and 5 hz pwm from pic 18f4520 or any other pic using micro c.kindly help me urgently.
 

I have not tested the code below, but hopefully it will give you an idea of how this might be done.

Code:
// 1Hz PWM ( and 5Hz PWM with the changes indicated in comments )

void main() {
char duty;
int mils;
int maxmils;
int n;

    LATC = 0;
    TRISC = 0;

    duty=50;                    // enter percent duty cycle here

    mils=duty*10;               // comment out this line for 5Hz PWM
    maxmils=(100-duty)*10;      // comment out this line for 5Hz PWM
//    mils=duty*2;              // comment in this line for 5Hz PWM
//    maxmils=(100-duty)*2      // comment in this line for 5Hz PWM

    while(1){
        LATC=1;
        for(n=0;n<mils;n++){
            Delay_ms(1);
        }
        LATC=0;
        for(n=mils;n<maxmils;n++){
            Delay_ms(1);
        }
    }
}
 
Last edited:

i try this code in pic 18f4620 ...........but it is not working .........can u tell me........why this code is not working mean it does not generate 1 or 5 hz pwm.
which pin no. is used for generating this pwm.....
thanks for paying attention........

// 1Hz PWM ( and 5Hz PWM with the changes indicated in comments )

void main() {
char duty;
int mils;
int maxmils;
int n;

LATC = 0;
TRISC = 0;

duty=50; // enter percent duty cycle here

mils=duty*10; // comment out this line for 5Hz PWM
maxmils=(100-duty)*10; // comment out this line for 5Hz PWM
// mils=duty*2; // comment in this line for 5Hz PWM
// maxmils=(100-duty)*2 // comment in this line for 5Hz PWM

while(1){
LATC=1;
for(n=0;n<mils;n++){
Delay_ms(1);
}
LATC=0;
for(n=mils;n<maxmils;n++){
Delay_ms(1);
}
}
}
 

Yes, you are right, the code does not work, though I cannot work out why.

I need to go back to school.
 

whats wrong with this code ..can any one help...



#include <p18cxxx.h>
#include <delays.h>

#pragma config OSC = HS /* Sets the oscillator mode to HS */
#pragma config WDT = OFF /* Turns the watchdog timer off */
#pragma config LVP = OFF /* Turns low voltage programming off */
#pragma config DEBUG = OFF /* Compiles without extra debug code */
//LCD Control pins




const unsigned char sinetable[10] = {0,77,147,202,237,250,237,202,147,77};
int i=0;




void main(void)
{
TRISC=0xFB;
LATC=0;

CCP1CON=0x0C;
PR2=234;
T2CON=0x00;

while(1)
{
i=0;


for(i=0; i < 10; i++)
{
CCPR1L= sinetable;
TMR2=0x0;
PIR1bits.TMR2IF=0;
T2CONbits.TMR2ON=1;
while(PIR1bits.TMR2IF ==0);
}
}
}
 

can you modify and correct it i am using 12Mhz crystal
 

you will require a number of NOPs to create a delay

in 8051 assembly may be code for delay may be like that

start:
setb p1.7
acall onehzdelay
clr p1.7
acall onehzdelay
jmp start

onehzdelay:
mov r7,#100d
d2:
mov r6,#100d
d1:
mov r5,#100d
djnz r5,$
djnz r6,d1
djnz r1,d2
ret
 

look at this code this may help...



program Eccp_sine_pwm_403
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' sinusoidal PWM can be found in many power conversion application
'such as UPS or HV AC motor controler.
'This is very simple development example wich can be executed on Easy Pic3
'whit out any external components.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Hardware: EP3 , PIC 18f458 , 4 MHZ crystal
' Configuration: clock 16MHZ
' HSPLL_OCS
' PWRT_ON
' WDT_OFF
' LVP_OFF
' (Can be 16MHz crystal and HS_osc)
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Used peripherie: Enhanced CCP1 in full-bridge mode
'
' FORWARD (Positiv half-wave) REVERSE (Negativ half-wave)
' ___________________________________________________________
' RD4 P1A Activ RD4 P1A Inactiv
' RD5 P1B Inactiv RD5 P1B Modulated
' RD6 P1C Inactiv RD6 P1C Activ
' RD7 P1D Modulated RD7 P1D Inactiv
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' PWM Frequncy 16KHz ( Period 62,5uS )
'
' sinusoidal frequency from 0,125Hz to 100HZ step 0,125 HZ
' Initial Frequency 1HZ
'
' RB0 Button -> Frequncy Up
' RB1 Button -> Frequency Down
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' 32 value of the half sine table. Used for up-date duty cycle.
' Second or negativ half-wave using same table, only bridge direction is changed.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

const sin_table as byte[32]=(0,25,49,73,96,118,137,
159,177,193,208,220,231,239,245,249,250,249,245,
239,231,220,208,193,177,159,137,118,96,73,49,25)


DIM TBL_POINTER_NEW AS WORD
DIM TBL_POINTER_OLD AS WORD
DIM TBL_POINTER_SHIFT AS WORD
DIM SET_FREQ as word
DIM DUTY_CYCLE AS BYTE



sub procedure interrupt

IF PIR1.TMR2IF=1 ''''''''''''''''''''''''''
THEN TBL_POINTER_NEW=TBL_POINTER_OLD+SET_FREQ 'IF OVERFLOW, THEN CHANGE
IF TBL_POINTER_NEW < TBL_POINTER_OLD 'DIRECTION OF THE BRIDGE
THEN ''''''''''''''''''''''''''
ASM
BTG ECCP1CON,EPWM1M1
END ASM
END IF '''''''''''''''''''''''''''
TBL_POINTER_SHIFT =TBL_POINTER_NEW >> 11 ' GET 6 BIT WIDE INDEX
DUTY_CYCLE=LO(TBL_POINTER_SHIFT) ' FOR ONE OF 32 VALUE FROM
ECCPR1L=SIN_TABLE[DUTY_CYCLE] ' SINE_TABLE AND UP-DATE
TBL_POINTER_OLD=TBL_POINTER_NEW ' DUTY CYCLE
PIR1.TMR2IF=0 '''''''''''''''''''''''''''
END IF

END SUB



main: '''''''''''''''''''''''''''''''''''''''''''''
cmcon=7 '
SET_FREQ=8 ' initial frequency 1Hz
TBL_POINTER_SHIFT=0 '
TBL_POINTER_NEW=0 '
TBL_POINTER_OLD=0 '
DUTY_CYCLE=0 '
TRISD=255 '
PR2=249 ' 16KHz
ECCPR1L=0 '
ECCP1CON=%01001100 'FULL-BRIDGE, FORWARD
PIR1.TMR2IF=0 '
T2CON.T2CKPS0=0 '
T2CON.T2CKPS1=0 ' PRESCALER TMR2=1
T2CON.TMR2ON=1 ' START TMR2
''''''''''''''''''''''''''''''''''''''''''''''''
WHILE PIR1.TMR2IF=0 ''''''''''''''''''''''''''''''''''''''''''''''''
WEND ' Starting-up ECCP
PIR1.TMR2IF=0 ' Recommended by Data-Sheet
TRISD=0 '
PIE1.TMR2IE=1 '
INTCON.PEIE=1 '
INTCON.GIE=1 ''''''''''''''''''''''''''''''''''''''''''''''''

WHILE 1=1 '''''''''''''''''''''''''''''''''''''''''''''''
if button(portb,0,10,1) ' Frequency UP/Down
then '
if SET_FREQ<819 ' 100HZ
then '
SET_FREQ=SET_FREQ+1 '
end if '
end if '
if button(portb,1,10,1) '
then '
if SET_FREQ>1 '0.125HZ
then '
SET_FREQ=SET_FREQ-1 '
end if '
end if '''''''''''''''''''''''''''''''''''''''''''''''

WEND

END.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top