McLab12_MOSFET_PWM_3chLED_OS6_tk1 Hardware L432KC or similar with PWM capable outputs. The L432KC pin D13 for onboard LED is not PWM capable! LEDs and xxx Ohm resistor. For example for a 12 V supply, three LEDs in series select a 120 Ohm resistor for 20 mA LEDs. For testing 3 pieces MOSFET IRL540, LEDs, resistors.

Committer:
timo_k2
Date:
Tue Oct 05 12:14:27 2021 +0000
Revision:
1:d3ea51e4c4ff
Parent:
0:d7a17d0b2bd3
Initial commit for the 3 LED circuit.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
timo_k2 0:d7a17d0b2bd3 1 /* mbed Microcontroller Library
timo_k2 0:d7a17d0b2bd3 2 * Copyright (c) 2019 ARM Limited
timo_k2 0:d7a17d0b2bd3 3 * SPDX-License-Identifier: Apache-2.0
timo_k2 0:d7a17d0b2bd3 4 *********************************************
timo_k2 0:d7a17d0b2bd3 5 *
timo_k2 0:d7a17d0b2bd3 6 * PWM example using the PwmOut API
timo_k2 0:d7a17d0b2bd3 7 *
timo_k2 0:d7a17d0b2bd3 8 *********************************************
timo_k2 1:d3ea51e4c4ff 9 *Description: McLab12_MOSFET_PWM_3chLED_OS6_tk1
timo_k2 0:d7a17d0b2bd3 10 * Hardware
timo_k2 0:d7a17d0b2bd3 11 * L432KC or similar with PWM capable outputs.
timo_k2 0:d7a17d0b2bd3 12 * The L432KC pin D13 for onboard LED is not PWM capable!
timo_k2 1:d3ea51e4c4ff 13 * LEDs and xxx Ohm resistor. For example for a 12 V supply,
timo_k2 1:d3ea51e4c4ff 14 * three LEDs in series select a 120 Ohm resistor for 20 mA LEDs.
timo_k2 1:d3ea51e4c4ff 15 * For testing 3 pieces MOSFET IRL540, LEDs, resistors.
timo_k2 0:d7a17d0b2bd3 16 * Connect:
timo_k2 1:d3ea51e4c4ff 17 * L432KC VIN --- 5V from a 5 V regulator on 12 V supply.
timo_k2 1:d3ea51e4c4ff 18 * L432KC D3 -- MOSFET gate
timo_k2 1:d3ea51e4c4ff 19 * L432KC D5 -- MOSFET gate
timo_k2 1:d3ea51e4c4ff 20 * L432KC D6 -- MOSFET gate
timo_k2 1:d3ea51e4c4ff 21 *
timo_k2 1:d3ea51e4c4ff 22 * MOSFET drain -- resistor -- LEDs -- 12 V.
timo_k2 1:d3ea51e4c4ff 23 * MOSFET source -- GND
timo_k2 0:d7a17d0b2bd3 24 * Operating system OS 6.0
timo_k2 1:d3ea51e4c4ff 25 * Timo Karppinen 5.10.2021 Apache-2.0
timo_k2 0:d7a17d0b2bd3 26 */
timo_k2 0:d7a17d0b2bd3 27
timo_k2 0:d7a17d0b2bd3 28 #include "mbed.h"
timo_k2 1:d3ea51e4c4ff 29 #include <cmath>
timo_k2 0:d7a17d0b2bd3 30
timo_k2 1:d3ea51e4c4ff 31 // change every x milliseconds
timo_k2 1:d3ea51e4c4ff 32 #define changing_RATE 10ms
timo_k2 0:d7a17d0b2bd3 33
timo_k2 1:d3ea51e4c4ff 34 // Initialise the digital pins as a pwm output
timo_k2 0:d7a17d0b2bd3 35 // The pin needs to be one of the PWM capable pins !
timo_k2 1:d3ea51e4c4ff 36 PwmOut pwmMOSFET1(D3);
timo_k2 1:d3ea51e4c4ff 37 PwmOut pwmMOSFET2(D5);
timo_k2 1:d3ea51e4c4ff 38 PwmOut pwmMOSFET3(D6);
timo_k2 0:d7a17d0b2bd3 39 // Variables for the pwm
timo_k2 1:d3ea51e4c4ff 40 int intensCount1 = 1;
timo_k2 1:d3ea51e4c4ff 41 int intensCount2 = 1;
timo_k2 1:d3ea51e4c4ff 42 int intensCount3 = 1;
timo_k2 1:d3ea51e4c4ff 43 float angle1 = 0.0;
timo_k2 1:d3ea51e4c4ff 44 float angle2 = 0.0;
timo_k2 1:d3ea51e4c4ff 45 float angle3 = 0.0;
timo_k2 1:d3ea51e4c4ff 46 float intens1 = 1.0; // from 0.0 to 1.0
timo_k2 1:d3ea51e4c4ff 47 float intens2 = 1.0;
timo_k2 1:d3ea51e4c4ff 48 float intens3 = 1.0;
timo_k2 0:d7a17d0b2bd3 49 float usT = 1000; // f = 1000 Hz T = 1 ms = 1000 us
timo_k2 0:d7a17d0b2bd3 50 // f = 10000 Hz T = 100 us
timo_k2 0:d7a17d0b2bd3 51 int main()
timo_k2 0:d7a17d0b2bd3 52 {
timo_k2 1:d3ea51e4c4ff 53 pwmMOSFET1.period_us(usT);
timo_k2 1:d3ea51e4c4ff 54 pwmMOSFET2.period_us(usT);
timo_k2 1:d3ea51e4c4ff 55 pwmMOSFET3.period_us(usT);
timo_k2 1:d3ea51e4c4ff 56 pwmMOSFET1.write(1.0f);
timo_k2 1:d3ea51e4c4ff 57 pwmMOSFET2.write(1.0f);
timo_k2 1:d3ea51e4c4ff 58 pwmMOSFET3.write(1.0f);
timo_k2 1:d3ea51e4c4ff 59
timo_k2 0:d7a17d0b2bd3 60 while (true) {
timo_k2 1:d3ea51e4c4ff 61
timo_k2 1:d3ea51e4c4ff 62 angle1 = angle1 + 0.1;
timo_k2 1:d3ea51e4c4ff 63 intens1 = 0.5 + 0.5 *sin(angle1);
timo_k2 1:d3ea51e4c4ff 64
timo_k2 1:d3ea51e4c4ff 65 angle2 = angle2 + 0.08;
timo_k2 1:d3ea51e4c4ff 66 intens2 = 0.6 + 0.4 *sin(angle2);
timo_k2 0:d7a17d0b2bd3 67
timo_k2 1:d3ea51e4c4ff 68 angle3 = angle3 + 0.02;
timo_k2 1:d3ea51e4c4ff 69 intens3 = 0.7 + 0.3 *sin(angle3);
timo_k2 1:d3ea51e4c4ff 70
timo_k2 1:d3ea51e4c4ff 71 if(angle1 > 628.31852){
timo_k2 1:d3ea51e4c4ff 72 angle1 = 0.0;
timo_k2 1:d3ea51e4c4ff 73 }
timo_k2 1:d3ea51e4c4ff 74 if(angle2 > 628.31852){
timo_k2 1:d3ea51e4c4ff 75 angle2 = 0.0;
timo_k2 0:d7a17d0b2bd3 76 }
timo_k2 1:d3ea51e4c4ff 77 if(angle3 > 628.31852){
timo_k2 1:d3ea51e4c4ff 78 angle3 = 0.0;
timo_k2 1:d3ea51e4c4ff 79 }
timo_k2 1:d3ea51e4c4ff 80
timo_k2 0:d7a17d0b2bd3 81
timo_k2 1:d3ea51e4c4ff 82 pwmMOSFET1.write(intens1);
timo_k2 1:d3ea51e4c4ff 83 pwmMOSFET2.write(intens2);
timo_k2 1:d3ea51e4c4ff 84 pwmMOSFET3.write(intens3);
timo_k2 1:d3ea51e4c4ff 85
timo_k2 1:d3ea51e4c4ff 86 ThisThread::sleep_for(changing_RATE);
timo_k2 0:d7a17d0b2bd3 87 }
timo_k2 0:d7a17d0b2bd3 88 }