# Generating PWM on PIC12F1571

#### Khisraw

Joined Nov 14, 2020
34
Hi,
I am trying to generate PWM with the PIC12F1571. Somehow my code just doesn't work. I know, I am new to C and PIC MCU so I appreciate if someone can help me.

#include <stdio.h>
#include <stdlib.h>
#include "Config.h"
#include <xc.h>
#include <PIC12F1571.h>

void PWM1_Initialize(void)
{
//Turn the PWM on the in RA0
APFCONbits.P1SEL = 0;
//PS Divide_clock_src_by_2; CS FOSC;
PWM1CLKCON = 0x10;

//PWM1DCH 0;
PWM1DCH = 0x00;

//PWM1DCL 3;
PWM1DCL = 0x03;

//PWM1PRH 0;
PWM1PRH = 0x00;

//PWM1PRL 6;
PWM1PRL = 0x06;

//PWM1OFH 1;
PWM1OFH = 0x01;

//PWM1OFL 244;
PWM1OFL = 0xF4;

//PWM1TMRL 0;
PWM1TMRL = 0x00;

//MODE standard_PWM; POL active_hi; OE(Output Enable) enabled; EN(Module Enable bit)enabled;
PWM1CON = 0xC0;

}

void PWM1_Start(void)
{
PWM1CONbits.EN = 1;
}

void PWM1_Stop(void)
{
PWM1CONbits.EN = 0;
}
int main()
{
//PORTAbits.RA1 = 0;
TRISAbits.TRISA1 = 0;
ANSELAbits.ANSA1 = 0;
LATAbits.LATA1 = 0;
PWM1_Initialize();
while(1)
{

PWM1_Start();

}}

#### trebla

Joined Jun 29, 2019
460
There is no need put the PWM1_start() into endless while loop, just put it after initailizing and before while(1)

#### Khisraw

Joined Nov 14, 2020
34
Thanks. Tried but no luck

#### Papabravo

Joined Feb 24, 2006
16,938
At this point it would be helpful to say exactly what you want the PWM to do so we can check that it is correctly initialized. Second you should figure out if the "null" while loop is actually executing or not. The most visible way to do this is to toggle a GPIO pin that is not being used for any other purpose. Third a schematic of your board or prototype setup. You may have a problem unrelated to the PWM peripheral.

#### Khisraw

Joined Nov 14, 2020
34
1. I need to generate 38Khz signal on Pin RA1.
2. The While loop is executing. I use the debuger to see step by step execution of the program.
3. I am programing the chip on Curisity board.

#### Papabravo

Joined Feb 24, 2006
16,938
1. I need to generate 38Khz signal on Pin RA1.
2. The While loop is executing. I use the debuger to see step by step execution of the program.
3. I am programing the chip on Curisity board.
Is anything at all happening on RA1? Is it always High or always low, or somethin else?

#### Khisraw

Joined Nov 14, 2020
34
Is anything at all happening on RA1? Is it always High or always low, or somethin else?
RA1 is low as soon as the program starts running but when I stop it, the pin goes high

#### click_here

Joined Sep 22, 2020
439
This won't fix your problem, but "int main" should be "void main" because you are not returning anything.

#### click_here

Joined Sep 22, 2020
439
Has the LFINTOSC been set up properly?

#### Khisraw

Joined Nov 14, 2020
34
I set the
#define XTAL_FREQ 160000 // Fosc clock
and
PWM1CLKCONbits.CS = 0;
PWM1CLKCONbits.PS = 1;

#### Papabravo

Joined Feb 24, 2006
16,938
Without going into great detail I see two potential problems:
1. It sounds like RA1 is still configured as an output low.
2. Isn't there a bit that says you want an alternate function on pin RA1?
Maybe toggle RA1 in the main loop to confirm that it is not connected to the PWM peripheral. If the PWM peripheral is connected to RA1, this won't hurt anything.

#### click_here

Joined Sep 22, 2020
439
Code:
PWM1CLKCON = 0x10;
Code:
PWM1CLKCON = 0b10;
Which would set the CS to LFINTOSC

#### Papabravo

Joined Feb 24, 2006
16,938
Not to worry. We've all been where you just came from. Welcome to the club.