generating square wave using pic18f4520

Thread Starter

محمد نور

Joined Dec 17, 2016
45
Hi
I'm trying to program PIC to generate two different square waves according to a switch state.
The first one is 1KHz with 0.5 duty cycle and the second is 5KHz with 0.2 duty cycle.
The first one is working but this is what I get for the second state.
upload_2017-2-11_16-9-17.png
And this is the code
C:
(
#include <p18F4520.h>
#include <xc.h>
#pragma config OSC=INTIO67,WDT=OFF,MCLRE=OFF
#include<Delays.h>
void main (void)
{
    OSCCON=0x70;
    ADCON1=0xF;
    TRISA0=1;
    TRISC0=0;
    while(1)
    {
        while(RA0==1)
        {
            RC0=1;
            Delay100TCYx(10);
            RC0=(~RC0);
            Delay100TCYx(10);
        }
        while(RA0==0)
        {
            RC0=1;
            Delay100TCYx(0.8);
            RC0=(~RC0);
            Delay100TCYx(3.2);
        }
    }
}
Note:TCY is machine cycle and equals to 0.5 micro Second.
Mod edit: code tags
 
Last edited by a moderator:

spinnaker

Joined Oct 29, 2009
7,837
And much easier to use __delay_ms and __delay_us though you do have to remember to define _XTAL_FREQ

example: #define _XTAL_FREQ 16000000

I haven't checked but Delay100TCYx and similar functions are likely to have been depreciated. They are legacy functions from the hi-tech c days.
 
Top