-
Notifications
You must be signed in to change notification settings - Fork 0
/
PWM.c
66 lines (55 loc) · 2.07 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
62
63
64
65
66
/*
* PWM.c
*
* Created on: Jan 28, 2020
* Author: Ahmed Magdy
*/
#include"util.h"
#include"stdtypes.h"
#include "GPIO.h"
#include"PWM.h"
void PWM_Init()
{
CLR_BIT(RCC,20); //use the same system clock 16 MHZ without any divison.
SET_BIT(RCGC0,20); // Enable Clock of PWM0
SET_BIT(RCGC2,1); // Enable clock to GPIOB because we use PORTB ---> PB6
unsigned long delay0=RCGC2; // Delay to make sure that clock is stable
SET_BIT(RCGCGPIO,1); // enable clock to portB
unsigned long delay1_B=RCGCGPIO; // Delay to make sure that clock is stable
GPIOLOCK_B=0x4C4F434B; //UNLOCK PORTB
SET_BIT(GPIOCR_B,6); //Enable GPIODEN & GPIOAFSEL Registers for PB6
SET_BIT(GPIODEN_B,6); //enable PB6
SET_BIT(GPIOAFSEL_B,6); // PB6 to alternative function
GPIOPCTL_B&=~(0x0F000000);
GPIOPCTL_B|=(0x04000000); //pins is working in PWM mode by puting value (4) in PMC6
CLR_BIT(PWM0CTL,0); // Disable PWM0
//SET_BIT(PWM0CTL,1); //The counter counts up from 0 to the load value, back down to 0, and then repeats (Count-Up/Down mode).
PWM0GENA|=0x8C;
PWM0LOAD=16000;
PWM0CMPA=15999;
// PWM0CMPA=x;
PWM0CTL|=0x01; //Enable PWM0
PWMENABLE=0x00000001; //Enable PWM output
}
unsigned long Number_OF_Ticks(unsigned long Frequency)
{
unsigned long Period=1/Frequency;
unsigned long Ticks=(Period*(16000000))-1;
return Ticks;
}
void change_duty(f32 Duty_cycle)
{
u32 x;
x=(1-Duty_cycle)*16000;
PWM0CMPA=x;
}
unsigned long Duty_FUNCTION(unsigned long DutyCycle,unsigned long Frequency)
{
unsigned long compA=(((1/Frequency)*16000000)*(DutyCycle))-1;
return compA;
/* unsigned long Period=1/Frequency;
unsigned long Ticks=Period*16000000;
unsigned long comp=DutyCycle*Ticks;
unsigned long compA=comp-1;
return compA;*/
}