sam.mosfet
Newbie level 6
- Joined
- Dec 25, 2014
- Messages
- 12
- Helped
- 1
- Reputation
- 2
- Reaction score
- 1
- Trophy points
- 3
- 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
}