Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Generate 38kHZ PWM signal?

Status
Not open for further replies.

winson

New Member
Hi everybody,

Anybody can point me to a right direction with some sample code regarding how to generate a PWM signal? Actually i was trying to generate a 38kHz PWM signal as the carrier for my UART IR transmission, then when i need to transmit data i will make the 38KHz PWM signal ON/OFF to drive an infrared LED and communicate with the IR receiver.

I was using CCS C compiler(just start learn up assembly) and PIC16F877A, and i haved read through the PWM section of the data sheet but just not so understand about how to set the register.

Anybody can help?

Thanks.
 
Hi everybody,

Anybody can point me to a right direction with some sample code regarding how to generate a PWM signal? Actually i was trying to generate a 38kHz PWM signal as the carrier for my UART IR transmission, then when i need to transmit data i will make the 38KHz PWM signal ON/OFF to drive an infrared LED and communicate with the IR receiver.

I was using CCS C compiler(just start learn up assembly) and PIC16F877A, and i haved read through the PWM section of the data sheet but just not so understand about how to set the register.

Anybody can help?

Thanks.

hi,
The link shows an example in assembler, if you can read assembler code you should be able to convert to 'C'.
 
check this out for 16F628A..........in C

void SetPWM(void)
{
set_tris_B(0x01);
//App_init();
setup_ccp1(CCP_PWM); // Configure CCP1 as a PWM
setup_timer_2(T2_DIV_BY_1,25,1);
//Generating 38khz PWM with 50% duty cycle
set_pwm1_duty(13);
}
 
Last edited:
Hi,

This is my code in C18 that sends a IR command using the Sony format. It's not very well documented and I can't even remember why I wrote it but hopefully it will be of some use.
Code:
#include <p18f1320.h> 
#include "delays.h"

#pragma config WDT = OFF, LVP = OFF, OSC = INTIO2

void sendbyte(char Num,char bits){
char i;
    for(i=0;i<bits;i++){
    	CCPR1L = 0x1A;                  // 0
        CCP1CON = 0x1C;                 // 50%
        if(Num&1==1)
            Delay100TCYx(24);           //1.2mS
        else
            Delay100TCYx(12);           //0.6mS
        Num=Num>>1;
        CCPR1L = 0x00;                  //Space
        CCP1CON = 0x0C ;                // 0%
        Delay100TCYx(12);               //0.6mS
    }
}

void main(void) {    
    OSCCON=0x72;                        // speed up the clock to 8MHz
    ADCON1 = 0; 
    TRISBbits.TRISB3 = 0;
    TRISBbits.TRISB6=1;
    PR2 = 0b00110100 ;
    T2CON = 0b00000100 ;
    CCPR1L = 0b00011010 ;
    CCP1CON = 0b00011100 ;

    while(1) {
        CCPR1L = 0x1A;                  // 0
        CCP1CON = 0x1C ;                // 50%
        Delay100TCYx(48);               //2.4mS pulse
        CCPR1L = 0x00;                  //Space
        CCP1CON = 0x0C ;                // 0%
        Delay100TCYx(12);               //0.6mS
        sendbyte(18,7);                 //device 18
        sendbyte(1,5);                  //command 1
        Delay1KTCYx(100);
     }    
}

Mike.
 
Thanks for providing all the useful information!:)

Now spending time to understand all the thing, later if i haved any progress will post a follow up.
 
Hi everybody,

I already able to generate the PWM signal and the infrared communication by this PWM signal also working as expected. Thanks
 
Hello, I also want to try to make a control with IR. I have this code for the receiver, but it does not receive the data I send. And also the data transmitter, but this only turned on and off the output at 27uS (13us high, 13 low), if they could help and thank you.

#include <18f252.h>
#delay (clock = 4000000)

int A [3];

void main
{

while (! input (PIN_A0))
{
// waiting input
}
delay_us (420);

if (! input (PIN_A0))
{
A [2] = 1;
}
else
A [2] = 0;
delay_us (1680);

if (! input (PIN_A0))
{
A [1] = 1;
}
else
A [1] = 0;
delay_us (1680);

if (! input (PIN_A0))
{
A [0] = 1;
}
else
A [0] = 0;
delay_us (1680);

if (A [2] == 1)
{
if (A [1] == 0)
{
if (A [0] == 1)
{
output_high (PIN_B0);
}
}
}

if (A [2] == 0)
{
if (A [1] == 1)
{
if (A [0] == 0)
{
output_high (PIN_B1);
}
}
}

}
 
Status
Not open for further replies.

Latest threads

Back
Top