/*---------------------------------------------------------------------------------------------------------*/
/* */
/* Copyright(c) 2015 Nuvoton Technology Corp. All rights reserved. */
/* */
/*---------------------------------------------------------------------------------------------------------*/
//***********************************************************************************************************
// Nuvoton Technology Corp.
// E-mail: MicroC-8bit@nuvoton.com
//***********************************************************************************************************
// Application: PWM Function
// Set PWM output channel, PWM frequency and PWM duty before PWM running
// PWM will be braked to a stop when P0.2 is low level
//
// Output : PWM channel 0 => P0.1 (default)
// PWM channel 1 => P1.6
// PWM channel 2 => P1.7
// PWM channel 3 => P0.0
//***********************************************************************************************************
//========================================= How to set PWM register =========================================
//
// 1.set load = 1; PWMRUN=1; ==> PWMCON0=0xC0;
//
// 2.Frequency == Fpwm/(PWMP+1)
//
// 3.Duty = PWMn/PWMP+1
//
//===========================================================================================================
//------------------------- <<< Use Configuration Wizard in Context Menu >>> --------------------------------
// <o0.0..9> PWM Period<0-1023>
// <o1.0..9> PWM0(P01) Duty<0-1023>
// <o2.0..9> PWM1(P16) Duty<0-1023>
// <o3.0..9> PWM2(P17) Duty<0-1023>
// <o4.0..9> PWM3(P00) Duty<0-1023>
// <o5.6> UART pin Select
// <0=> Select P1.0, P1.1 as UART pin(default)
// <1=> Select P2.6, P2.7 as UART pin(28 pin only)
//-------------------------------- <<< end of configuration section >>> -------------------------------------
#define PWMP 0x3FF
#define PWM0_Duty 0x200
#define PWM1_Duty 0x100
#define PWM2_Duty 0x080
#define PWM3_Duty 0x040
#define Uart_Port_Sel 0x00
typedef enum
{
E_CHANNEL0,
E_CHANNEL1,
E_CHANNEL2,
E_CHANNEL3,
} E_PWMCNL_SEL;
#include <stdio.h>
#include "N79E715.h"
#include "Typedef.h"
#include "Define.h"
#include "Common.h"
#include "Version.h"
#include <intrins.h>
UINT16 PWM_shadow;
//-----------------------------------------------------------------------------------------------------------
void Init_PWM(void)
{
AUXR1 |= Uart_Port_Sel; // Select P10/P11 as UART pin(default)
InitialUART0_Timer1(9600); // 9600 Baud Rate [url=home.php?mod=space&uid=72445]@[/url] 11.0592MHz
printf ("\n*===================================================================");
printf ("\n* Name: N79E715 Series PWM Sample Code.");
printf ("\n*===================================================================");
printf ("\nPWM Demo Start.\n");
PWM_shadow = PWMP;
PWMPH = HIBYTE(PWM_shadow); // PWM Period
PWMPL = LOBYTE(PWM_shadow);
PWMCON0 |= 0xD0; // Enable PWM and select auto reload mode
}
//------------------------------------------------------------------------------
void PWM_Pin_Recover_to_High(void)
{
PWMCON0 = 0x00;
PWMCON1 = 0x00;
PWMCON2 = 0x00;
PWMPH = 0x00;
PWMPL = 0x01;
PWM0H = 0x00;
PWM0L = 0x00;
PWM1H = 0x00;
PWM1L = 0x00;
PWM2H = 0x00;
PWM2L = 0x00;
PWM3H = 0x00;
PWM3L = 0x00;
PWMCON0 |= 0xD0;// PWM always non-active(high).
_nop_ (); // NOP
PWMCON0 &= 0x0F;// PWM Stop
PWMPL = 0x00; // Recover to reset value
}
//------------------------------------------------------------------------------
void PWM_Channel(E_PWMCNL_SEL Channel)
{
switch(Channel)
{
case E_CHANNEL0:
PWM_shadow=PWM0_Duty;
PWM0H = HIBYTE(PWM_shadow);// PWM0 Duty (P0.1)
PWM0L = LOBYTE(PWM_shadow);
break;
case E_CHANNEL1:
PWM_shadow=PWM1_Duty;
PWM1H = HIBYTE(PWM_shadow);// PWM1 Duty (P1.6)
PWM1L = LOBYTE(PWM_shadow);
break;
case E_CHANNEL2:
PWM_shadow=PWM2_Duty;
PWM2H = HIBYTE(PWM_shadow);// PWM2 Duty (P1.7)
PWM2L = LOBYTE(PWM_shadow);
break;
case E_CHANNEL3:
PWM_shadow=PWM3_Duty;
PWM3H = HIBYTE(PWM_shadow);// PWM3 Duty (P0.0)
PWM3L = LOBYTE(PWM_shadow);
break;
}
}
//------------------------------------------------------------------------------
void Enabled_Brake_Function(void)
{
PWMCON1 = 0x30; // PWM is running. PWM will be stopped when P0.2 is low level.
}
//-----------------------------------------------------------------------------------------------------------
void main(void)
{
PWM_Channel(E_CHANNEL0); // Select PWM channel.
PWM_Channel(E_CHANNEL1); // Select PWM channel.
PWM_Channel(E_CHANNEL2); // Select PWM channel.
PWM_Channel(E_CHANNEL3); // Select PWM channel.
Init_PWM(); // Enable PWM function and set PWM period.
Enabled_Brake_Function(); // Enable Brake function.
while(1);
}
//-----------------------------------------------------------------------------------------------------------
|