Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
CT16B1_PWM.cpp
00001 /** 00002 * @file CT16B1_PWM.cpp 00003 * @brief Small driver to user CT16B1 for PWM 00004 * 00005 * @author Jeroen Lodder 00006 * @date Oktober 2013 00007 * 00008 * @note This only uses MAT 1 00009 * @{ 00010 */ 00011 #include "mbed.h" 00012 #include "CT16B1_PWM.h" 00013 00014 /* NOTICE: THIS MODULE ONLY WORKS FOR MAT 1 */ 00015 00016 /* Static makes them private to this module */ 00017 volatile static uint8_t stage = 0; /**< @brief Stage identifier for mat2/3 swap */ 00018 volatile static uint16_t period = 0; /**< @brief PWM Period register */ 00019 volatile static uint16_t mat[4]; /**< @brief PWM Mat output registers */ 00020 volatile static uint16_t default_period_us; /**< @brief Given period in us */ 00021 volatile static uint16_t defaultstate; /**< @brief Default PWM state on init */ 00022 volatile static uint16_t prescaler; /**< @brief Given prescaler */ 00023 00024 /** 00025 * @brief Initializes PWM 00026 * 00027 * @param[in] period_us Period in us, when prescaler is 48 00028 * @param[in] defaultstate State after initializing 00029 * @param[in] prescaler Divider from AHBCLK 00030 * @note Prescaler 48 gives 1 us timer ticks 00031 * @note Run Start() to start pwm 00032 */ 00033 void CT16B1_initpwm(uint16_t period_arg, uint16_t defaultstate, uint16_t prescaler_arg){ 00034 // Calculte period 00035 period = period_arg; 00036 prescaler = prescaler_arg; 00037 00038 // Store latest setting 00039 mat[0] = defaultstate; 00040 mat[1] = defaultstate; 00041 mat[2] = defaultstate; 00042 mat[3] = defaultstate; 00043 00044 // Enable timer clock, and GPIO 00045 LPC_SYSCON->SYSAHBCLKCTRL |= (1<<6)|(1<<8); 00046 00047 // Config timer 00048 LPC_CT16B1->IR = 0x5F; // Clear int 00049 LPC_CT16B1->TCR = 2; // Reset 00050 LPC_CT16B1->PR = prescaler; 00051 LPC_CT16B1->CCR = 0; // No capture 00052 LPC_CT16B1->EMR = 0; // Not required, refer to PWMC 00053 LPC_CT16B1->CTCR = 0; // No special counters mode 00054 00055 // Config mat 1 00056 LPC_CT16B1->PWMC = (0<<0)|(1<<1)|(0<<2)|(0<<3); // MAT1 only 00057 LPC_CT16B1->MCR = (1<<10); // Reset on MR3 00058 LPC_CT16B1->MR3 = period_arg; 00059 LPC_CT16B1->MR1 = defaultstate; 00060 // Config met 1 IO 00061 LPC_IOCON->PIO0_22 = 0x2 | (0x1<<3) ; // GPIO 00062 // Take timer out of reset 00063 LPC_CT16B1->TCR = 0; 00064 } 00065 00066 /** 00067 * @brief Re-Initializes PWM 00068 */ 00069 void CT16B1_reinitpwm(void){ 00070 CT16B1_initpwm(default_period_us, defaultstate, prescaler ); 00071 } 00072 00073 /** 00074 * @brief Start PWM 00075 */ 00076 void CT16B1_start(void){ 00077 LPC_CT16B1->TCR = 1; // Enable 00078 } 00079 00080 /** 00081 * @brief Stop PWM 00082 * @param[in] state PWM output state when pwm disabled 00083 */ 00084 void CT16B1_deinit(uint8_t state){ 00085 LPC_CT16B1->TCR = 2; // Disable and reset counter 00086 // Set all to GPIO 00087 LPC_IOCON->PIO0_22 = 0x0 | (0x1<<3) ; // GPIO 00088 LPC_GPIO->DIR[0] |= (1<<22); 00089 LPC_GPIO->CLR[0] |= (1<<22); 00090 } 00091 00092 /** 00093 * @brief Reload all match compare registers 00094 */ 00095 void CT16B1_reload_mat(void){ 00096 LPC_CT16B1->MR1 = mat[1]; 00097 LPC_CT16B1->MR3 = period; 00098 } 00099 00100 /** 00101 * @brief Set channel PWM 00102 */ 00103 void CT16B1_set(uint8_t matnr, uint16_t value){ 00104 mat[matnr] = value; 00105 CT16B1_reload_mat(); 00106 } 00107 00108 /** 00109 * @brief Wait for timer to reach 0 00110 */ 00111 void CT16B1_wait_refresh(void){ 00112 if( (LPC_CT16B1->TCR & 1) ) 00113 while(LPC_CT16B1->TC != 0); 00114 } 00115 00116 /** 00117 * @brief Return period 00118 */ 00119 uint16_t CT16B1_get_period(void){ 00120 return period; 00121 } 00122 00123 /** 00124 * @brief Return 1 if timer is started 00125 */ 00126 uint32_t CT16B1_isStarted(void){ 00127 return LPC_CT16B1->TCR & 1; 00128 } 00129 /** 00130 *@} 00131 */ 00132
Generated on Mon Jul 18 2022 01:28:39 by
1.7.2