Advertisement
pleasedontcode

"PWM Generation" rev_06

Feb 14th, 2024
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: "PWM Generation"
  13.     - Source Code compiled for: Arduino Nano
  14.     - Source Code created on: 2024-02-15 01:05:34
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Run a half bridge switch mode power supply MOSFETs */
  21.     /* at 125khz with pins 9 and 10 */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24. /****** DEFINITION OF LIBRARIES *****/
  25. #include <TimerOne.h>
  26.  
  27. /****** FUNCTION PROTOTYPES *****/
  28. void setup(void);
  29. void loop(void);
  30. void blinkLED(void);
  31.  
  32. /***** DEFINITION OF PWM OUTPUT PINS *****/
  33. const uint8_t low_PIN_D9 = 9;
  34. const uint8_t high_PIN_D10 = 10;
  35.  
  36. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  37. /***** used to store raw data *****/
  38. uint8_t low_PIN_D9_rawData = 0;
  39. uint8_t high_PIN_D10_rawData = 0;
  40.  
  41. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  42. /***** used to store data after characteristic curve transformation *****/
  43. float low_PIN_D9_phyData = 0.0;
  44. float high_PIN_D10_phyData = 0.0;
  45.  
  46. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  47. TimerOne myTimer;
  48.  
  49. void setup(void)
  50. {
  51.   // put your setup code here, to run once:
  52.   pinMode(low_PIN_D9, OUTPUT);
  53.   pinMode(high_PIN_D10, OUTPUT);
  54.  
  55.   // Initialize myTimer to generate PWM frequency at 125kHz
  56.   myTimer.initialize(8); // 125kHz = 1MHz / (8 * 2)
  57.   myTimer.pwm(low_PIN_D9, low_PIN_D9_rawData);
  58.   myTimer.pwm(high_PIN_D10, high_PIN_D10_rawData);
  59.  
  60.   attachInterrupt(digitalPinToInterrupt(low_PIN_D9), blinkLED, RISING);
  61.   attachInterrupt(digitalPinToInterrupt(high_PIN_D10), blinkLED, FALLING);
  62. }
  63.  
  64. void loop(void)
  65. {
  66.   // put your main code here, to run repeatedly:
  67.   updateOutputs(); // Refresh output data
  68.   delay(100); // Wait for 100 ms
  69. }
  70.  
  71. void updateOutputs()
  72. {
  73.   // Update the PWM output on the pins
  74.   myTimer.setPwmDuty(low_PIN_D9, low_PIN_D9_rawData);
  75.   myTimer.setPwmDuty(high_PIN_D10, high_PIN_D10_rawData);
  76. }
  77.  
  78. /***** INTERRUPT SERVICE ROUTINE *****/
  79. void blinkLED(void)
  80. {
  81.   // Toggle the data on pins 9 and 10
  82.   low_PIN_D9_rawData = !low_PIN_D9_rawData;
  83.   high_PIN_D10_rawData = !high_PIN_D10_rawData;
  84. }
  85.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement