本帖最后由 wazhzlp 于 2012-12-24 10:17 编辑
硬件配置。//------------------------------------
----------------
// C main line
//----------------------------------------------------------------------------
#include <m8c.h> // part specific constants and macros
#include "PSoCAPI.h" // PSoC API definitions for all User Modules
#pragma interrupt_handler timer;
#define PWM1L 0X04 //P1.2
#define PWM1H 0X08 //P1.3
#define PWM2L 0X20 //P1.5
#define PWM2H 0X10 //P1.4
#define PWM3L 0X40 //P1.6
#define PWM3H 0X80 //P1.7
#define led 0x02 //P0.1
char i,n,m,port1_value;
void timer()
{
Timer8_1_Stop();
// LED_1_Invert();
i++;
if(i==100){i=0;}
if(i==99)
{
m=m+10;
if(m==180){m=20;}
n++;
if(n==7){n=1;}
PRT0DR=PRT0DR | led;//led on
PWMDB8_1_WritePulseWidth(m);
}
if(i!=99){PRT0DR=PRT0DR &(~led);}//led off
Timer8_1_WritePeriod(200);
Timer8_1_Start();
}
//enable PWM function
void Enable_PWM(char pin_value)
{
PRT1GS = PRT1GS | pin_value;
PRT1DM0 = PRT1DM0 | pin_value;
}
//disable PWM function
void Disable_PWM(char pin_value)
{
pin_value = ~pin_value;
port1_value = port1_value & pin_value;
PRT1DR = port1_value;
PRT1GS = PRT1GS & pin_value;
PRT1DM0 = PRT1DM0 & pin_value;
}
//Turn PWM always on
void PWM_ON(char pin_value)
{
port1_value = port1_value | pin_value;
PRT1DR = port1_value;
PRT1GS = PRT1GS & (~pin_value);
PRT1DM0 = PRT1DM0 | pin_value;
}
//Turn PWM always off
void PWM_OFF(char pin_value)
{
port1_value = port1_value &(~pin_value);
PRT1DR = port1_value;
PRT1GS = PRT1GS &(~pin_value);
PRT1DM0 = PRT1DM0 | pin_value;
}
void Disable_PWMs(char port1)
{
port1 = ~port1;
port1_value = port1_value & port1;
PRT1DR = port1_value;
PRT1GS = PRT1GS & port1;
port1 = ~port1;
PRT1DM0 = PRT1DM0 | port1;
}
void main()
{
// Insert your main routine code here.
i=0;
n=0;
m=100;
// PRT0GS =PRT0GS & (~pwm1) & (~pwm2) & (~led);//p0
//PRT0DM0 = PRT0DM0 | led | pwm1 | pwm2;
//PRT1GS =PRT1GS & (~pwm3) & (~pwm4) & (~pwm5) & (~pwm6);//p1
//PRT1DM0 = PRT1DM0 | pwm3 | pwm4 | pwm5 | pwm6;
M8C_EnableGInt;
Timer8_1_Start();
Timer8_1_EnableInt();
PWMDB8_1_Start();
while(1)
{
// if(n==6){n=0;}
switch(n)
{
case 1: //2
Disable_PWMs(PWM1L|PWM2H|PWM2L|PWM3H);
Enable_PWM(PWM1H);
Enable_PWM(PWM3L);
//sector = 0;
break;
case 6: //a
Disable_PWMs(PWM1H|PWM1L|PWM2L|PWM3H);
Enable_PWM(PWM2H);
Enable_PWM(PWM3L);
//sector = 1;
break;
case 5: //8
Disable_PWMs(PWM1H|PWM2L|PWM3H|PWM3L);
Enable_PWM(PWM2H);
Enable_PWM(PWM1L);
//sector = 2;
break;
case 4: //0x28
Disable_PWMs(PWM1H|PWM2H|PWM2L|PWM3L);
Enable_PWM(PWM3H);
Enable_PWM(PWM1L);
//sector = 3;
break;
case 3: //0x20
Disable_PWMs(PWM1H|PWM1L|PWM2H|PWM3L);
Enable_PWM(PWM3H);
Enable_PWM(PWM2L);
//sector = 4;
break;
case 2: //0x22
Disable_PWMs(PWM1L|PWM2H|PWM3H|PWM3L);
Enable_PWM(PWM1H);
Enable_PWM(PWM2L);
//sector = 5;
break;
default:
Disable_PWMs(PWM1H|PWM1L|PWM2H|PWM2L|PWM3H|PWM3L);
break;
}
}
}
这个程序是网上下载的动态分配pwm的2路到6个IO口。但是我始终没有跳出来。不知道为什么。请大家解答。
|