Generating PWM on PIC12F1571

Thread Starter

Khisraw

Joined Nov 14, 2020
56
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();

}}
 

Papabravo

Joined Feb 24, 2006
21,159
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.
 

Thread Starter

Khisraw

Joined Nov 14, 2020
56
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
21,159
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?
 

Papabravo

Joined Feb 24, 2006
21,159
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.
 
Top