-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpwm.c
61 lines (50 loc) · 1.1 KB
/
pwm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
* File: pwm.c
* Author: Raphael
*
* Created on 24 de Fevereiro de 2016, 16:33
*/
#include <xc.h>
#include "pwm.h"
#include "config.h"
void setup_ccp1()
{
CCP1CON = 0b00001100;
}
void Config_pwm1()
{
// 10kHz
//PR2 = 199;
//T2CON = 0b00000100;
//CCPR1L = 0b11001000;
//CCP1CON = 0b00001100;
// 10kHz
//PR2 = 199;
//T2CON = 0b00000100;
//CCPR2L = 0b11001000;
//CCP2CON = 0b00001100;
// 14kHz
//PR2 = 142;
//T2CON = 0b00000100;
//CCPR2L = 0b01000010;
//CCP2CON = 0b00001100;
//30kHz
PR2 = 66;
T2CON = 0b00000100;
CCPR1L = 0b00100001;
CCP1CON = 0b00101100;
//100kHz
//PR2 = 19;
//T2CON = 0b00000100;
//CCPR2L = 0b00001010;
//CCP2CON = 0b00001100;
}
void set_pwm1_duty(unsigned int duty_ratio)
{
union PWMDC DCycle;
DCycle.lpwm = duty_ratio << 6;
// Write the high byte into CCPR1L
CCPR1L = DCycle.bpwm[1];
// Write the low byte into CCP1CON5:4
CCP1CON = (CCP1CON & 0xCF) | ((DCycle.bpwm[0] >> 2) & 0x30);
}