beautypg.com

Texas Instruments MSC1210 User Manual

Page 136

background image

PWM Generator

11-12

// PWM

#include

#define OneUsConst

(2−1)

#define CLEAR 0

#define SET

1

sbit p33=P3^3;

sbit p14=P1^4;

unsigned char p,d;

void pwm_isr( void) interrupt 2 //External Interrupt 1

{

p14=!p14;

// debug

PWMCON &= 0xef;

// select PWMPeriod

PWM=p;

// Set PWMPeriod

PWMCON |=0x10;

// select PWMDuty

PWM=d;

IE1=CLEAR;

// Clear pending interrupt

EX1=CLEAR;

}

void setpwm(period, duty)

{

p14=!p14;

// debug

p=period; d=duty;

IE1=CLEAR;

// Clear any pending interrupt

EX1=SET;

// Enable *INT1 pin interrupt

}

void main(void)

{

char i;

// Setup External INT1

IT1=SET;

// Config *INT1 pin for falling edge trigger

EA=SET;

// Global Int Enable

PDCON &= 0x0ed;

//turn on tone gen & sys timer

USEC = OneUsConst;

p33=1;

// turn on P3.3

PWMCON=0;

// select PWMPeriod

PWM=500;

// Set PWMPeriod

PWMCON=0x10; // select PWMDuty

PWM=200;

PWMCON=0x19; // Enable PWM

for (i=0;i<5;i++) {;}

setpwm(200,100); // set period/duty after current PWM cycle

while(1) {}

}