-
Notifications
You must be signed in to change notification settings - Fork 0
/
DCcontrol_switch.c
133 lines (108 loc) · 3.96 KB
/
DCcontrol_switch.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "PLL.h"
#include "tm4c123gh6pm.h"
// Constant declarations to access port registers using
// symbolic names instead of addresses
unsigned long ADC0_InSeq3(void);
void ADC0_InitSWTriggerSeq3_Ch1(void);
void SysTick_Wait10ms(unsigned long delay);
void DisableInterrupts(void); // Disable interrupts
void EnableInterrupts(void); // Enable interrupts
void WaitForInterrupt(void); // low power mode
void Port_Init(void); // start sound output
void SysFun(void);
void SysLoad(unsigned long period);
unsigned int SW;
unsigned int ADCvalue;
unsigned long T=1600000 ;
unsigned long TON=800000;
// 80 MHz
unsigned long T1;
// 3. Subroutines Section
// MAIN: Mandatory for a C Program to be executable
int main(void){
PLL_Init();
Port_Init();
ADC0_InitSWTriggerSeq3_Ch1();
SysFun();
EnableInterrupts();
/*Initialize ports , ADC and timers*/
while(1){
SW = GPIO_PORTA_DATA_R&0x04; // read PA2 into SW1
if(SW){
GPIO_PORTA_DATA_R |=0x01; //PA0 HIGH
GPIO_PORTA_DATA_R&=~0x02; //PA1 LOW
} else
{
GPIO_PORTA_DATA_R |=0x02; //PA1 HIGH
GPIO_PORTA_DATA_R&=~0x01; //PA0 LOW
}
ADCvalue=ADC0_InSeq3();
T1=TON+(ADCvalue *175);
if (ADCvalue==0){
GPIO_PORTA_DATA_R |=0x20;
SysLoad(TON);
GPIO_PORTA_DATA_R =~0x20;
SysLoad(TON);
}
else{
GPIO_PORTA_DATA_R |=0x20;
SysLoad(T1);
GPIO_PORTA_DATA_R =~0x20;
SysLoad(T-T1);
}
}
}
// **************Port_Init*********************
// Initialize SysTick periodic interrupts
// Input: none
// Output: none
void Port_Init(void){ unsigned long volatile delay;
SYSCTL_RCGC2_R |= 0x00000001; // activate port A
delay = SYSCTL_RCGC2_R;
GPIO_PORTA_AMSEL_R &= ~0x20; // no analog
GPIO_PORTA_PCTL_R &= ~0x00F00FFF; // regular function
GPIO_PORTA_DIR_R |= 0x23; // make PA5 out
GPIO_PORTA_DR8R_R |= 0x20; // can drive up to 8mA out
GPIO_PORTA_AFSEL_R &= ~0x27; // disable alt funct on PA5
GPIO_PORTA_DEN_R |= 0x27; // enable digital I/O on PA5
}
void SysLoad(unsigned long period){
NVIC_ST_RELOAD_R = period-1; // number of counts to wait
NVIC_ST_CURRENT_R = 0; // any value written to CURRENT clears
while((NVIC_ST_CTRL_R&0x00010000)==0){ // wait for count flag
}
}
void SysFun(void){
NVIC_ST_CTRL_R = 0; // disable SysTick during setup
NVIC_ST_CTRL_R = 0x00000005;
}
//ADC initialisation on PE2
void ADC0_InitSWTriggerSeq3_Ch1(void){ volatile unsigned long delay;
SYSCTL_RCGC2_R |= 0x00000010; // 1) activate clock for Port E
delay = SYSCTL_RCGC2_R; // allow time for clock to stabilize
GPIO_PORTE_DIR_R &= ~0x04; // 2) make PE2 input
GPIO_PORTE_AFSEL_R |= 0x04; // 3) enable alternate function on PE2
GPIO_PORTE_DEN_R &= ~0x04; // 4) disable digital I/O on PE2
GPIO_PORTE_AMSEL_R |= 0x04; // 5) enable analog function on PE2
SYSCTL_RCGC0_R |= 0x00010000; // 6) activate ADC0
delay = SYSCTL_RCGC2_R;
SYSCTL_RCGC0_R &= ~0x00000300; // 7) configure for 125K
ADC0_SSPRI_R = 0x0123; // 8) Sequencer 3 is highest priority
ADC0_ACTSS_R &= ~0x0008; // 9) disable sample sequencer 3
ADC0_EMUX_R &= ~0xF000; // 10) seq3 is software trigger (continuously ON)
ADC0_SSMUX3_R = (ADC0_SSMUX3_R&0xFFFFFFF0)+1; // 11) channel Ain1 (PE2)
ADC0_SSCTL3_R = 0x0006; // 12) no TS0 D0, yes IE0 END0 (FLAG AFT COMPLETION)
ADC0_ACTSS_R |= 0x0008; // 13) enable sample sequencer 3
}
//------------ADC0_InSeq3------------
// Busy-wait Analog to digital conversion
// Input: none
// Output: 12-bit result of ADC conversion
unsigned long ADC0_InSeq3(void){
unsigned long result;
ADC0_PSSI_R = 0x0008; // 1) initiate SS3
while((ADC0_RIS_R&0x08)==0){}; // 2) wait for conversion done
result = ADC0_SSFIFO3_R&0xFFF; // 3) read result
ADC0_ISC_R = 0x0008; // 4) acknowledge completion
return result;
}