-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhotoresistor Code
96 lines (77 loc) · 2.94 KB
/
Photoresistor Code
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
#include "msp.h"
#include "math.h"
#define DELAY 1200000
#define ADC P5
#define PHOTO_ADC BIT5
#define PHOTO P2
#define PHOTO_LED BIT7
void delaysetup (void);
void delayms (uint16_t delay);
void ADC14_init (void);
void TA_init (void);
void level_assign(void);
float ref_voltage = 3.5;
float NADC, brightness;
float max = 2.5, min = 1.5; //max and min voltages from ambient conditions
float pwm_LED = 0;
void main(void){
WDT_A->CTL = WDT_A_CTL_PW | WDT_A_CTL_HOLD; // stop watchdog timer
ADC14_init(); //for A0
TA_init();
while(1){
level_assign();
ADC14 -> CTL0 |= ADC14_CTL0_SC; //starts conversion
NADC = ADC14->MEM[0];
brightness = ref_voltage * (NADC / pow(2, 14)); //reference pin voltage 3.3V mult by ADC resolution (adc/2^14)
if(brightness > max) //max brightness error check
max = brightness;
if(brightness < min) //min brightness error check
min = brightness;
TIMER_A0->CCR[4] = (pwm_LED); //period mult by LED brightness val for brightness
}
}
void ADC14_init (void) {
ADC->SEL0 |= PHOTO_ADC; // configure pin 5.5 for A0 input
ADC->SEL1 |= PHOTO_ADC;
ADC14->CTL0 &=~ 0x00000002; // disable ADC14ENC during configuration
ADC14->CTL0 |= 0x44200210; // S/H pulse mode, SMCLK, 16 sample clocks
ADC14->CTL1 = 0x00000030; // 14 bit resolution
ADC14->CTL1 |= 0x00000000; // Selecting ADC14CSTARTADDx mem0 REGISTER
ADC14->MCTL[0] = 0; // ADC14INCHx = 0 for mem[0]
ADC14->CTL0 |= 0x00000002; // enable ADC14ENC, starts the ADC after configuration
}
void TA_init (void) {
//TA0.4 connected to led on pin PHOTO.7
PHOTO->SEL0 |= PHOTO_LED;
PHOTO->SEL1 &= ~PHOTO_LED;
PHOTO->DIR |= PHOTO_LED;
TIMER_A0->CCR[0]= 12000-1; // CCR0 PWM period for LED
TIMER_A0->CCTL[4] = TIMER_A_CCTLN_OUTMOD_7; // CCTL reset/set
TIMER_A0->CCR[4] = pwm_LED; // CCR4 PWM duty cycle
TIMER_A0->CTL = 0x0290; //SMCLK, /4, up mode, no interrupts
}
void level_assign(){ //check brightness and assign to a level to print
//math: 14460 min to 14670 max:
if(NADC < 14560)
pwm_LED = 0;
else if ((14560 <= NADC) && (NADC <= 14584))
pwm_LED = 1450;
else if ((14585 <= NADC) && (NADC <= 14608))
pwm_LED = 2900;
else if ((14609 <= NADC) && (NADC <= 14632))
pwm_LED = 4340;
else if ((14633 <= NADC) && (NADC <= 14656))
pwm_LED = 5800;
else if ((14657 <= NADC) && (NADC <= 14680))
pwm_LED = 7250;
else if ((14681 <= NADC) && (NADC <= 14704))
pwm_LED = 8700;
else if ((14705 <= NADC) && (NADC <= 14728))
pwm_LED = 10150;
else if ((14729 <= NADC) && (NADC <= 14752))
pwm_LED = 11600;
else if ((14753 <= NADC) && (NADC <= 14776))
pwm_LED = 13050;
else if (14777 < NADC)
pwm_LED = 14500;
}