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.

Square wave generation with 18f4520 problem

Status
Not open for further replies.

zero2004

Member level 1
Joined
Dec 12, 2009
Messages
33
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,505
Hi every one

I try to generate a square wave generators of 40 KHz, so i use a 18f4520 PIC and write the following programmes :

int y, yy;
main()
{
do {
portb=0x00;
for(yy=0; yy<20; yy=yy+1)
delay_us(1);
portb = 0xff;
for(yy=0; yy<20; yy=yy+1)
delay_us(1);

}
while(1);
}

the problem is the output frequency is only up to 5 KHz and doesnt increase even i make the for loop condition yy<1

i want to help me

thanks
 

This is a bad way of implementing a wave generator.

Why don't you do like this instead?

nt y, yy;
main()
{
do {
portb=0x00;

delay_us(12.5);

portb = 0xff;

delay_us(12.5);

}
while(1);
}

If delay_us does not support fractional numbers you can do like this:

portb=0x00;

delay_us(12);

portb = 0xff;

delay_us(13);


Both these codes are not the best way to generate a wave. However it seems to me you don't have much experience with PIC's so I will not present you more complicated sollutions.

A good wave generator would use timers and interruptions.

Hope it helped.
 

hi fcfusion

I use this way because my application require to input the frequency by using a keypad, so, when i use it the delay becomes more than 150 us.
 

How much is your clock frequency?

The rest of your code takes time to execute. The way your code works the wave's period will be defined by this:

Period_wave = Period_selected_by_delay_us + Time_to_run_rest_of_code

This means that the time needed to execute your code add some error to signal's frequency. If your code takes too long to execute (because of slow clock or bad coding), the wave's frequency will be completely different of the one you wanted.

May I sugest another solution? PIC18F4520 has a built in PWM generator. It's rellatively easy to use. In this PWM generator you can define the period once and, after that, you no longer have to run any code concerning the wave generation. The circuit's hardware keeps generating the wave with the period you specified. This way you'll get the exact frequency you want. Search the datasheet for "Enhanced Capture/ Compare PWM".
 

frequency in my application must start from 1 Hz, and must be entered from keypad, can PWM do this
 

frequency in my application must start from 1 Hz, and must be entered from keypad, can PWM do this

Yes it can. The PWM circuits works by counting clock pulses. You just have to convert frequency to number of pulses and load it in the appropriate PWM register.
 

    zero2004

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top