For Bertl2014 myslef PWM

Dependencies:   mbed

Committer:
hemmer_matthias
Date:
Tue Dec 29 11:19:55 2015 +0000
Revision:
0:f100451653d6
PWM fpr LED's

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hemmer_matthias 0:f100451653d6 1 //********************************
hemmer_matthias 0:f100451653d6 2 // Matthias Hemmer, 14.12.2015
hemmer_matthias 0:f100451653d6 3 // Creator: Dominik Sukic 30.11.2015
hemmer_matthias 0:f100451653d6 4 // Desing; EB 14.12.2015
hemmer_matthias 0:f100451653d6 5 //
hemmer_matthias 0:f100451653d6 6 // Create a PWM with delay
hemmer_matthias 0:f100451653d6 7 // For LEDs 1 to 4 BERTL14
hemmer_matthias 0:f100451653d6 8 //
hemmer_matthias 0:f100451653d6 9 //**********************************
hemmer_matthias 0:f100451653d6 10
hemmer_matthias 0:f100451653d6 11
hemmer_matthias 0:f100451653d6 12 #include "mbed.h" // Include thbe "mbed.h" libary
hemmer_matthias 0:f100451653d6 13
hemmer_matthias 0:f100451653d6 14 #define PWM_Periode 125 // Defination of the periode
hemmer_matthias 0:f100451653d6 15
hemmer_matthias 0:f100451653d6 16 // Defination of LEDs
hemmer_matthias 0:f100451653d6 17 DigitalOut Led1(P1_8); // Name(Port)
hemmer_matthias 0:f100451653d6 18 DigitalOut Led2(P1_9); // Name(Port)
hemmer_matthias 0:f100451653d6 19 DigitalOut Led3(P1_10); // Name(Port)
hemmer_matthias 0:f100451653d6 20 DigitalOut Led4(P1_11); // Name(Port)
hemmer_matthias 0:f100451653d6 21
hemmer_matthias 0:f100451653d6 22 // BEGIN pwm_delay
hemmer_matthias 0:f100451653d6 23 // dutycycle: 0 - MAX; the brightness of the LEDs
hemmer_matthias 0:f100451653d6 24 // on: 1 - 4; to turn each LED on
hemmer_matthias 0:f100451653d6 25 void pwm_delay(unsigned int dutycycle, int LED_index) //PWM underprogramm
hemmer_matthias 0:f100451653d6 26 {
hemmer_matthias 0:f100451653d6 27 int PWM_time_index = 0;
hemmer_matthias 0:f100451653d6 28
hemmer_matthias 0:f100451653d6 29 // Loop to increase PWM-Time to 1 ms (PWM_time_index x 250µs)
hemmer_matthias 0:f100451653d6 30 for (PWM_time_index = 0; PWM_time_index < 4; PWM_time_index++)
hemmer_matthias 0:f100451653d6 31 {
hemmer_matthias 0:f100451653d6 32 switch(LED_index)
hemmer_matthias 0:f100451653d6 33 {
hemmer_matthias 0:f100451653d6 34 case 1: // pwm for LED1
hemmer_matthias 0:f100451653d6 35 Led1 = 1;
hemmer_matthias 0:f100451653d6 36 wait_us(dutycycle); //Time which the LED is powered
hemmer_matthias 0:f100451653d6 37 Led1 = 0;
hemmer_matthias 0:f100451653d6 38 wait_us(250-dutycycle); //Time which the LED is unpowered
hemmer_matthias 0:f100451653d6 39 break;
hemmer_matthias 0:f100451653d6 40
hemmer_matthias 0:f100451653d6 41 case 2: // pwm for LED2
hemmer_matthias 0:f100451653d6 42 Led2 = 1;
hemmer_matthias 0:f100451653d6 43 wait_us(dutycycle); //Time which the LED is powered
hemmer_matthias 0:f100451653d6 44 Led2 = 0;
hemmer_matthias 0:f100451653d6 45 wait_us(250-dutycycle); //Time which the LED is unpowered
hemmer_matthias 0:f100451653d6 46 break;
hemmer_matthias 0:f100451653d6 47
hemmer_matthias 0:f100451653d6 48 case 3: // pwm for LED3
hemmer_matthias 0:f100451653d6 49 Led3 = 1;
hemmer_matthias 0:f100451653d6 50 wait_us(dutycycle); //Time which the LED is powered
hemmer_matthias 0:f100451653d6 51 Led3 = 0;
hemmer_matthias 0:f100451653d6 52 wait_us(250-dutycycle); //Time which the LED is unpowered
hemmer_matthias 0:f100451653d6 53 break;
hemmer_matthias 0:f100451653d6 54
hemmer_matthias 0:f100451653d6 55 case 4: // pwm for LED4
hemmer_matthias 0:f100451653d6 56 Led4 = 1;
hemmer_matthias 0:f100451653d6 57 wait_us(dutycycle); //Time which the LED is powered
hemmer_matthias 0:f100451653d6 58 Led4 = 0;
hemmer_matthias 0:f100451653d6 59 wait_us(250-dutycycle); //Time which the LED is unpowered
hemmer_matthias 0:f100451653d6 60 break;
hemmer_matthias 0:f100451653d6 61
hemmer_matthias 0:f100451653d6 62 default: // if the nummber isn't listed, it woudn't crash
hemmer_matthias 0:f100451653d6 63 ;
hemmer_matthias 0:f100451653d6 64 break;
hemmer_matthias 0:f100451653d6 65 }
hemmer_matthias 0:f100451653d6 66 // End switch
hemmer_matthias 0:f100451653d6 67 }
hemmer_matthias 0:f100451653d6 68 // END for
hemmer_matthias 0:f100451653d6 69 }
hemmer_matthias 0:f100451653d6 70 // END pwm_delay
hemmer_matthias 0:f100451653d6 71
hemmer_matthias 0:f100451653d6 72
hemmer_matthias 0:f100451653d6 73 // BEGIN main
hemmer_matthias 0:f100451653d6 74 // Call pwm_delay for testing
hemmer_matthias 0:f100451653d6 75 // all 4 Leds pwm-dimmed, phase-shifted
hemmer_matthias 0:f100451653d6 76 int main()
hemmer_matthias 0:f100451653d6 77 {
hemmer_matthias 0:f100451653d6 78 // all LEDs off
hemmer_matthias 0:f100451653d6 79 Led1 = 0;
hemmer_matthias 0:f100451653d6 80 Led2 = 0;
hemmer_matthias 0:f100451653d6 81 Led3 = 0;
hemmer_matthias 0:f100451653d6 82 Led4 = 0;
hemmer_matthias 0:f100451653d6 83
hemmer_matthias 0:f100451653d6 84 // Initial Brightness for each LED
hemmer_matthias 0:f100451653d6 85 int dutycycle1 = 0; // Brightness in %
hemmer_matthias 0:f100451653d6 86 int dutycycle2 = 25;
hemmer_matthias 0:f100451653d6 87 int dutycycle3 = 50;
hemmer_matthias 0:f100451653d6 88 int dutycycle4 = 75;
hemmer_matthias 0:f100451653d6 89
hemmer_matthias 0:f100451653d6 90 // Define direction of dimming
hemmer_matthias 0:f100451653d6 91
hemmer_matthias 0:f100451653d6 92 //Bools for brighter or darker
hemmer_matthias 0:f100451653d6 93 bool led1_darker = false; //If false => brighter
hemmer_matthias 0:f100451653d6 94 bool led2_darker = false; //Wenn true => darker
hemmer_matthias 0:f100451653d6 95 bool led3_darker = false;
hemmer_matthias 0:f100451653d6 96 bool led4_darker = false;
hemmer_matthias 0:f100451653d6 97
hemmer_matthias 0:f100451653d6 98 // central routine for triangle-dimming
hemmer_matthias 0:f100451653d6 99 while(1)
hemmer_matthias 0:f100451653d6 100 {
hemmer_matthias 0:f100451653d6 101 //Led1
hemmer_matthias 0:f100451653d6 102 if(led1_darker == false)
hemmer_matthias 0:f100451653d6 103 { //False: Led is getting brighter
hemmer_matthias 0:f100451653d6 104
hemmer_matthias 0:f100451653d6 105 if (dutycycle1 != PWM_Periode)
hemmer_matthias 0:f100451653d6 106 {
hemmer_matthias 0:f100451653d6 107 pwm_delay(dutycycle1,1); //Call the underprogramm PWM
hemmer_matthias 0:f100451653d6 108 dutycycle1++; //Brightness rice up
hemmer_matthias 0:f100451653d6 109 }
hemmer_matthias 0:f100451653d6 110
hemmer_matthias 0:f100451653d6 111 else
hemmer_matthias 0:f100451653d6 112 led1_darker = true; //Led 1 = true: it will be darker
hemmer_matthias 0:f100451653d6 113 }
hemmer_matthias 0:f100451653d6 114 if(led1_darker == true)
hemmer_matthias 0:f100451653d6 115 {
hemmer_matthias 0:f100451653d6 116 //True: Led is getting darker
hemmer_matthias 0:f100451653d6 117 if (dutycycle1 != 0)
hemmer_matthias 0:f100451653d6 118 {
hemmer_matthias 0:f100451653d6 119 pwm_delay(dutycycle1,1);
hemmer_matthias 0:f100451653d6 120 dutycycle1--; //brightness rice down
hemmer_matthias 0:f100451653d6 121 }
hemmer_matthias 0:f100451653d6 122
hemmer_matthias 0:f100451653d6 123 else
hemmer_matthias 0:f100451653d6 124 led1_darker = false; //To make it brighter
hemmer_matthias 0:f100451653d6 125 }
hemmer_matthias 0:f100451653d6 126
hemmer_matthias 0:f100451653d6 127 //Repead it for each LED
hemmer_matthias 0:f100451653d6 128
hemmer_matthias 0:f100451653d6 129 //Led2 brighter
hemmer_matthias 0:f100451653d6 130 if(led2_darker == false)
hemmer_matthias 0:f100451653d6 131 {
hemmer_matthias 0:f100451653d6 132 if (dutycycle2 != PWM_Periode)
hemmer_matthias 0:f100451653d6 133 {
hemmer_matthias 0:f100451653d6 134 pwm_delay(dutycycle2,2);
hemmer_matthias 0:f100451653d6 135 dutycycle2 ++;
hemmer_matthias 0:f100451653d6 136 }
hemmer_matthias 0:f100451653d6 137 else
hemmer_matthias 0:f100451653d6 138 led2_darker = true;
hemmer_matthias 0:f100451653d6 139 }
hemmer_matthias 0:f100451653d6 140
hemmer_matthias 0:f100451653d6 141 //Led2 darker
hemmer_matthias 0:f100451653d6 142 if(led2_darker == true)
hemmer_matthias 0:f100451653d6 143 {
hemmer_matthias 0:f100451653d6 144 if (dutycycle2 != 0)
hemmer_matthias 0:f100451653d6 145 {
hemmer_matthias 0:f100451653d6 146 pwm_delay(dutycycle2,2);
hemmer_matthias 0:f100451653d6 147 dutycycle2 --;
hemmer_matthias 0:f100451653d6 148 }
hemmer_matthias 0:f100451653d6 149 else
hemmer_matthias 0:f100451653d6 150 led2_darker = false;
hemmer_matthias 0:f100451653d6 151 }
hemmer_matthias 0:f100451653d6 152
hemmer_matthias 0:f100451653d6 153 //Led3 brighter
hemmer_matthias 0:f100451653d6 154 if(led3_darker == false)
hemmer_matthias 0:f100451653d6 155 {
hemmer_matthias 0:f100451653d6 156 if (dutycycle3 != PWM_Periode)
hemmer_matthias 0:f100451653d6 157 {
hemmer_matthias 0:f100451653d6 158 pwm_delay(dutycycle3,3);
hemmer_matthias 0:f100451653d6 159 dutycycle3 ++;
hemmer_matthias 0:f100451653d6 160 }
hemmer_matthias 0:f100451653d6 161 else
hemmer_matthias 0:f100451653d6 162 led3_darker = true;
hemmer_matthias 0:f100451653d6 163 }
hemmer_matthias 0:f100451653d6 164
hemmer_matthias 0:f100451653d6 165 //Led3 darker
hemmer_matthias 0:f100451653d6 166 if(led3_darker == true)
hemmer_matthias 0:f100451653d6 167 {
hemmer_matthias 0:f100451653d6 168 if (dutycycle3 != 0)
hemmer_matthias 0:f100451653d6 169 {
hemmer_matthias 0:f100451653d6 170 pwm_delay(dutycycle3,3);
hemmer_matthias 0:f100451653d6 171 dutycycle3 --;
hemmer_matthias 0:f100451653d6 172 }
hemmer_matthias 0:f100451653d6 173 else
hemmer_matthias 0:f100451653d6 174 led3_darker = false;
hemmer_matthias 0:f100451653d6 175 }
hemmer_matthias 0:f100451653d6 176
hemmer_matthias 0:f100451653d6 177 //Led4 brighter
hemmer_matthias 0:f100451653d6 178 if(led4_darker == false)
hemmer_matthias 0:f100451653d6 179 {
hemmer_matthias 0:f100451653d6 180 if (dutycycle4 != PWM_Periode)
hemmer_matthias 0:f100451653d6 181 {
hemmer_matthias 0:f100451653d6 182 pwm_delay(dutycycle4,4);
hemmer_matthias 0:f100451653d6 183 dutycycle4 ++;
hemmer_matthias 0:f100451653d6 184 }
hemmer_matthias 0:f100451653d6 185 else
hemmer_matthias 0:f100451653d6 186 led4_darker = true;
hemmer_matthias 0:f100451653d6 187 }
hemmer_matthias 0:f100451653d6 188
hemmer_matthias 0:f100451653d6 189 //Led4 darker
hemmer_matthias 0:f100451653d6 190 if(led4_darker == true)
hemmer_matthias 0:f100451653d6 191 {
hemmer_matthias 0:f100451653d6 192 if (dutycycle4 != 0)
hemmer_matthias 0:f100451653d6 193 {
hemmer_matthias 0:f100451653d6 194 pwm_delay(dutycycle4,4);
hemmer_matthias 0:f100451653d6 195 dutycycle4 --;
hemmer_matthias 0:f100451653d6 196 }
hemmer_matthias 0:f100451653d6 197 else
hemmer_matthias 0:f100451653d6 198 led4_darker = false;
hemmer_matthias 0:f100451653d6 199 }
hemmer_matthias 0:f100451653d6 200
hemmer_matthias 0:f100451653d6 201 } // End of While
hemmer_matthias 0:f100451653d6 202 } // En dof Main