My code doesn't work!

Status
Not open for further replies.

sam.mosfet

Newbie level 6
Joined
Dec 25, 2014
Messages
12
Helped
1
Reputation
2
Reaction score
1
Trophy points
3
Visit site
Activity points
79
I am testing a function that is suppose to blink an LED at a certain input frequency. In order to test the function alone I had to define a value for the frequency. I am suding PIC16f17889 I tried to explain each line as much as I could using the following comments. Please advise.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <htc.h>
#include <pic.h>
#include <pic16f1788.h>
#include <xc.h>
// Config word
#define frequ  100000
#define _XTAL_FREQ   20000000




void main()
{
// output
    TRISB5 = 0;
    // configure CCP3
    CCP3CONbits.CCP3M = 0x0C;   // set it to PWM mode; and active high

    // configure Timer 2
    PR2 = ((_XTAL_FREQ)/(4*16*frequ))-1;  // Timer 2 max value is (20MHz / 4*16*f)-1 = max duty, fosc=20MHz, prescaler of 16
    CCPR3 = 0.5*(PR2+1);        // CCP3 compare value is 50% duty cycle
    T2CONbits.T2OUTPS = 0x02;   // Timer 2 prescaler 16
    T2CONbits.TMR2ON = 1;       // Timer 2 on

}
 

Minimum PWM frequency with 20 MHz crystal is 1.2 kHz, so you'll hardly see the LED blinking.
 

Well the LED is not blinking at all and I tried to vary the frequency from very small to almost equal the crystal but nothing happens. The LED is off or it is blinking but I can't see it. any suggestions will be appreciated.
 

Run a timer interrupt, e.g. Timer2 with pre- and postscaler and toggle the LED in interrupt. Or divide a fast timer interrupt down by a counter to generate arbitrary blink patterns.
 

Got it to work for some reason it is not my code but the port I was trying to use does't output PWM I guess even it it says so in the datasheet!!!. So changed ports and now everything is working. But thanks for your help, and for mentioning the config fuses I totally forgot about that. The only problem I still have is I can barely see the difference when changing the frequency. Any suggestions to the following code?

Code:
#include <xc.h>
// Config word
#define frequ  10000
#define _XTAL_FREQ   2000000

// CONFIG1
#pragma config FOSC = INTOSC    // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)
#pragma config WDTE = ON        // Watchdog Timer Enable (WDT enabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable (PWRT disabled)
#pragma config MCLRE = ON       // MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
#pragma config CP = OFF         // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config CPD = OFF        // Data Memory Code Protection (Data memory code protection is disabled)
#pragma config BOREN = ON       // Brown-out Reset Enable (Brown-out Reset enabled)
#pragma config CLKOUTEN = OFF   // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
#pragma config IESO = ON        // Internal/External Switchover (Internal/External Switchover mode is enabled)
#pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)

// CONFIG2
#pragma config WRT = OFF        // Flash Memory Self-Write Protection (Write protection off)
#pragma config VCAPEN = OFF     // Voltage Regulator Capacitor Enable bit (Vcap functionality is disabled on RA6.)
#pragma config PLLEN = ON       // PLL Enable (4x PLL enabled)
#pragma config STVREN = ON      // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
#pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config LPBOR = OFF      // Low Power Brown-Out Reset Enable Bit (Low power brown-out is disabled)
#pragma config LVP = ON         // Low-Voltage Programming Enable (Low-voltage programming enabled)





void main()

{
 
// output
    TRISC1 = 0;
    // configure CCP3
    CCP2CONbits.CCP2M = 0x0C;   // set it to PWM mode; and active high

    // configure Timer 2
    PR2 = ((_XTAL_FREQ)/(4*16*frequ))-1;  // Timer 2 max value is (20MHz / 4*16*f)-1 = max duty, fosc=20MHz, prescaler of 16
    CCPR2 = 0.8*(PR2+1); // CCP3 compare value is 50% duty cycle
    //CCPR2=295;
    //PR2=310;
    T2CONbits.T2OUTPS = 0x02;   // Timer 2 prescaler 16
    T2CONbits.TMR2ON = 1;       // Timer 2 on
    
    

}
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…