Content originally posted in LPCWare by ctchen on Mon Aug 24 18:38:12 MST 2015
Despite of your kine, I'm so sorry for late reply because of my other work.
My simplified source code is bellow
void GpioInit(void)
{
// Set to inputs
GPIO0DIR = \
GPIO1DIR = \
GPIO2DIR = \
GPIO3DIR = 0;
// Reset all GPIO pins to default primary function
IOCON_PIO2_6 = \
IOCON_PIO2_0 = \
IOCON_RESET_PIO0_0 = \
//IOCON_PIO0_1 = 0x12 ;// Enable CT32B0_MAT2, Pull-Up
IOCON_PIO1_8 = \
IOCON_PIO0_2 = 0x10 ; // Low Side Enable for Sol-A
IOCON_PIO2_7 = \
IOCON_PIO2_8 = \
IOCON_PIO2_1 = \
IOCON_PIO0_3 = 0x10 ; // Low Side Enable for Sol-B
IOCON_PIO1_9 = \
IOCON_PIO2_4 = \
IOCON_PIO2_5 = \
IOCON_PIO0_6 = \
IOCON_PIO0_7 = \
IOCON_PIO2_9 = \
IOCON_PIO2_10 = \
IOCON_PIO2_2 = \
IOCON_PIO0_8 = \
IOCON_SWCLK_PIO0_10 = \
IOCON_PIO2_11 = \
IOCON_PIO3_0 = \
IOCON_PIO3_1 = \
IOCON_PIO2_3 = \
IOCON_PIO3_2 = \
IOCON_PIO1_5 = \
IOCON_PIO3_3 = 0x50 ;
IOCON_R_PIO1_0 = \
IOCON_PIO0_4 = \
IOCON_PIO0_5 = 0x00;
}
void InitClock(Int32U clock, Int32U ahbdiv)
{
SYSOSCCLTRL = (MAIN_OSC_FREQ>(20MHZ))?(0x2):(0x0); // Sys Ocilator Enable, 12MHz
PDRUNCFG_bit.SYSOSC_PD = 0; // Power Up SYS Oscilator
PDRUNCFG_bit.IRC_PD = 0; // Enable Internal RC oscilator
/*Select internal RC oscilator for
Sys clock source*/
MAINCLKUEN = 0;
MAINCLKSEL = 0;
MAINCLKUEN = 1;
/*Configure SYS PLL*/
/*Power Down SYS PLL*/
PDRUNCFG_bit.SYSPLL_PD = 1;
/*Select Sys Oscilator for
SYS PLL source*/
SYSPLLCLKUEN = 0;
SYSPLLCLKSEL = 1;
SYSPLLCLKUEN = 1;
/*Calc M*/
Int32U m = clock/MAIN_OSC_FREQ - 1 ;
assert(m<32);
/*Configure PLL frequency*/
SYSPLLCTRL = (m) /*MSEL*/
| (1<<5) /*PSEL = 1*/ // original value 0<<5
| (0<<7) /*DIRECT = 0*/
| (0<<8); /*BYPASS=0*/
/*Power Up PLL*/
PDRUNCFG_bit.SYSPLL_PD = 0;
/*Set Sys AHB Clock devider*/
SYSAHBCLKDIV_bit.DIV = ahbdiv;
/*Wain until PLL locks*/
while(!(SYSPLLSTAT_bit.LOCK));
/*Select Sys PLL Output for
Sys clock source*/
MAINCLKUEN = 0;
MAINCLKSEL = 3;
MAINCLKUEN = 1;
}
/* PWM Generation using TMR32B0 & TMR32B1 */
void _PWM32BIT_INIT()
{
IOCON_PIO0_1 = 0x0 ;
IOCON_PIO0_1 = 0x12 ;// Enable CT32B0_MAT2, Pull-Up
IOCON_PIO0_2 = 0x10 ; // Low Side Enable for Sol-A (Pull-Up)
IOCON_R_PIO1_1 = 0x0 ;
IOCON_R_PIO1_1 = 0x13 ; // Enable CT32B0_MAT3, Pull-up
IOCON_PIO0_3 = 0x10 ; // Low Side Enable for Sol-B (Pull-Up)
GPIO0DIR = 1<<2 | 1<<3 ; // Define as output
GPIO0DATA = 1<<2 | 1<<3 ; // Enable Output PIO0_2 & PIO0_3
SYSAHBCLKCTRL_bit.CT32B0 = 1 ; // Enable TIMER 0 clocks
SYSAHBCLKCTRL_bit.CT32B1 = 1 ; // Enable TIMER 1 clocks
TMR32B0MR3 = Max_Duty ; //period, 9600 : 5kHz, 4800000 : 10Hz
TMR32B1MR3 = Max_Duty ; //period, 9600 : 5kHz, 4800000 : 10Hz
// Match Control Register for MR2 & MR3
TMR32B0MCR_bit.MR3I = 0; // Enable Interrupt on MR3
TMR32B0MCR_bit.MR3R = 1; // Enable reset on MR3
TMR32B0MCR_bit.MR3S = 0; // Disable stop on MR3
TMR32B1MCR_bit.MR3I = 0; // Enable Interrupt on MR2
TMR32B1MCR_bit.MR3R = 1; // Enable reset on MR2
TMR32B1MCR_bit.MR3S = 0; // Disable stop on MR2
TMR32B0TC = 0 ;
TMR32B1TC = 0 ;
// PWM Control Register
TMR32B0PWMC_bit.PWM2ENA = 1 ; // Enable PWM ch2
TMR32B1PWMC_bit.PWM0ENA = 1 ; // Enable PWM ch0
// Timer Control Register
TMR32B0TCR_bit.CE = 1 ;
TMR32B1TCR_bit.CE = 1 ;
}
inline void _PWM_SIG_INITIALIZATION(Int32U duty_A, Int32U duty_B)
{
unsigned int dumy_cnt = 0 ;
for(dumy_cnt = 0; dumy_cnt < 5000; dumy_cnt++)
{
TMR32B0MR2 = Max_Duty - duty_A ;
TMR32B1MR0 = Max_Duty - duty_B ;
}
}
int main(void)
{
SYSAHBCLKCTRL_bit.GPIO = 1 ; // Enable GPIO Clock
SYSAHBCLKCTRL_bit.IOCON = 1 ; // Enable IOCON Clock
InitClock(48MHZ, 1) ; // Init clock, 132MHz / 3 = 48MHz
GpioInit(); // Set all pins as input ports except some part
_PWM32BIT_INIT() ; // Meaning of function input, Enable Timer. Ticks 10 times per second
while(1)
{
_PWM_SIG_INITIALIZATION(5000, 5000) ;
}
}