QuakeGod
2024-04-12 5dd1b7fdcc52a43867f924e8d220de8467af372a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include "STC15F2K60S2.H"
 
void CCP_PWMInit(void)    //³õʼ»¯PCA£¨Programmable Counter Array£©Ä£¿é×÷ΪPWMÊä³ö
{
    CCON = 0;           //PCA¿ØÖƼĴæÆ÷³õʼ»¯(xx-- -xxx£¬0000 0000£º¶¼ÊÇÇåÁã¾ÍÍêÊÂÁË)
    CMOD = 0x02;          //PCA¹¤×÷ģʽ¼Ä´æÆ÷(x--- xxxx£¬0000 0010£ºÏµÍ³Ê±ÖÓ£¬SYSclk/2)
    CL = 0;                //PCA¼ÆÊýÆ÷µÍ8λÇåÁã
    CH = 0;                   //PCA¼ÆÊýÆ÷¸ß8λÇåÁã
                        
    AUXR1 = 0x00;            //¸¨Öú¼Ä´æÆ÷1ÇåÁ㣬³õʼ»¯
    PCA_PWM0 = 0x00;    //PCAµÄPWM¼Ä´æÆ÷(xx-- --xx£¬0000 0000£º¹¤×÷ÓÚ8 bit PWM)
    CCAP0H = 0x80;        //±È½Ï/²¶»ñ¼Ä´æÆ÷0¸ß8λÉèÖÃΪ 0x80£¬±íʾPWM1µÄÖØ×°ÔØÖµµÄ¸ß8λ
    CCAP0L = 0x80;        //±È½Ï/²¶»ñ¼Ä´æÆ÷0µÍ8λÉèÖÃΪ 0x80£¬±íʾPWM1µÄÖØ×°ÔØÖµµÄµÍ8λ
    CCAPM0 = 0x42;       //±È½Ï/²¶»ñ¹¤×÷ģʽ¼Ä´æÆ÷0(-xxx xxxx£¬0100 0010£º±È½ÏÆ÷´ò¿ª£¬CCP0½ÅÓÃ×÷PWMÊä³ö)
    
    AUXR1 = 0x00;
    PCA_PWM1 = 0x00;    
    CCAP1H = 0x80;      //256-x/256 = pulse width
    CCAP1L = 0x80;       
  CCAPM1 = 0x42;      //±È½ÏÆ÷´ò¿ª£¬CCP1½ÅÓÃ×÷PWMÊä³ö
    CR = 1;                 //Æô¶¯PCA¼ÆÊýÆ÷
}
 
void PWMInit(void)
{
                        //¸ÃоƬÔöÇ¿ÐÍ PWM Êä³ö¶Ë¿Ú£º1.6 1.7 2.1 2.2 2.3 3.7
    P1M0 &= ~0xc0;        //½« 0011 1111 Óë P1M0 ½øÐÐλÓëÔËË㣬ÇåÁã P1M0 µÄ¸ß2룬½áºÏP1M1£¬P1.6 P1.7Òý½ÅÅäÖøü¸ÄΪ׼˫Ïò¿Ú£¬ÆäËû½Å¿ªÂ©
  //P1M0 &=  0x3f  
    P1M1 &= ~0xc0;        //P1M1_P1M0״̬(ͬһλÖÃÁ½¸ö¼Ä´æÆ÷µÄÊýÖµ×éºÏ)£º00£º×¼Ë«Ïò£»01£ºÍÆÍ죻10£º¸ß×裻11£º¿ªÂ©
    P1   &= ~0xc0;      //resetP1.6/.P1.7
    P2M0 &= ~0x0e;        //1111 0001
    P2M1 &= ~0x0e;                     
    P2   &= ~0x0e;                   
    P3M0 &= ~0x80;        //0111 1111
    P3M1 &= ~0x80;
    P3   &= ~0x80;                   
    
    P_SW2 |= 0x80;      //ʹÄÜ·ÃÎÊXSFR
    PWMCFG = 0x3f;      //ÅäÖÃPWMµÄ³õʼµçƽΪ high
    PWMCKS = 0x00;      //Ñ¡ÔñPWMµÄʱÖÓFosc/(0+1) 22118400/1024=21.6k    
     //PWMCKS = 0x07;   //Ñ¡ÔñPWMµÄʱÖÓFosc/(8)   22118400/1000/8=2.76k
    PWMC = CYCLE;       //ÉèÖÃPWMµÄÖÜÆÚ
    
    //-----PWM2--------------------------------
    PWM2T1 = 0x0001;                //ÉèÖÃPWMµÚ1´Î·­×ªµÄPWM¼ÆÊý
    PWM2T2 = CYCLE * 2L / 100;      //ÉèÖÃPWMµÚ2´Î·­×ªµÄPWM¼ÆÊý
                                    //Õ¼¿Õ±È(PWM2T2-PWM2T1)/PWMC
    PWM2CR = 0x00;                  //Ñ¡ÔñPWM2Êä³öµ½P3.7,²»Ê¹ÄÜPWM2ÖжÏ
    //-------PWM3-----------------------------
    PWM3T1 = 0x0001;                //ÉèÖÃPWMµÚ1´Î·­×ªµÄPWM¼ÆÊý
    PWM3T2 = CYCLE * 2L / 100;      //ÉèÖÃPWMµÚ2´Î·­×ªµÄPWM¼ÆÊý
                                    //Õ¼¿Õ±È(PWM2T2-PWM2T1)/PWMC
    PWM3CR = 0x00;                  //Ñ¡ÔñPWM2Êä³öµ½P2.1,²»Ê¹ÄÜPWM2ÖжÏ
    //--------pwm4----------------------------------
    PWM4T1 = 0x0001;                //ÉèÖÃPWMµÚ1´Î·­×ªµÄPWM¼ÆÊý
    PWM4T2 = CYCLE * 2L / 100;      //ÉèÖÃPWMµÚ2´Î·­×ªµÄPWM¼ÆÊý
                                    //Õ¼¿Õ±È(PWM2T2-PWM2T1)/PWMC
    PWM4CR = 0x00;                  //Ñ¡ÔñPWM2Êä³öµ½P2.2,²»Ê¹ÄÜPWM2ÖжÏ
    //--------pwm5----------------------------------
    PWM5T1 = 0x0001;                //ÉèÖÃPWMµÚ1´Î·­×ªµÄPWM¼ÆÊý
    PWM5T2 = CYCLE * 2L / 100;      //ÉèÖÃPWMµÚ2´Î·­×ªµÄPWM¼ÆÊý
                                    //Õ¼¿Õ±È(PWM2T2-PWM2T1)/PWMC
    PWM5CR = 0x00;                  //Ñ¡ÔñPWM2Êä³öµ½P2.3,²»Ê¹ÄÜPWM2ÖжÏ
        //--------pwm6----------------------------------
    PWM6T1 = 0x0001;                //ÉèÖÃPWMµÚ1´Î·­×ªµÄPWM¼ÆÊý
    PWM6T2 = CYCLE * 2L / 100;      //ÉèÖÃPWMµÚ2´Î·­×ªµÄPWM¼ÆÊý
                                    //Õ¼¿Õ±È(PWM2T2-PWM2T1)/PWMC
    PWM6CR = 0x00;                  //Ñ¡ÔñPWM2Êä³öµ½P1.6,²»Ê¹ÄÜPWM2ÖжÏ
        
        //--------pwm7----------------------------------
    PWM7T1 = 0x0001;                //ÉèÖÃPWMµÚ1´Î·­×ªµÄPWM¼ÆÊý
    PWM7T2 = CYCLE * 2L / 100;      //ÉèÖÃPWMµÚ2´Î·­×ªµÄPWM¼ÆÊý
                                    //Õ¼¿Õ±È(PWM2T2-PWM2T1)/PWMC
    PWM7CR = 0x00;                  //Ñ¡ÔñPWM2Êä³öµ½P1.7,²»Ê¹ÄÜPWM2ÖжÏ
        
    PWMCR = 0x3f;                   //ʹÄÜPWM2.3.4.5.6.7,6·ÐźÅÊä³ö
    PWMCR |= 0x80;                  //ʹÄÜPWMÄ£¿é
        //PWMCR |= 0x40;            //ʹÄÜPWM¹éÁãÖжÏ
    P_SW2 &= ~0x80;
}
//----------pwm2/3/4/5-------------------------------------------------------------------
void pwm2( unsigned short Wide)           //PWM2
{   
        if((Wide==0)||(Wide==1))
        {
        P_SW2 |= 0x80;              //ÉèÖø¨Öú¼Ä´æÆ÷2µÄλ7Ϊ1£¬½«PWM2Òý½ÅÇл»µ½P1.3
        PWMCR &=~0x01;              //PWM¿ØÖƼĴæÆ÷µÄλ0ÇåÁ㣬¼´½ûÓÃPWM2
        PWM2=1;                   //PWM¿ØÖƼĴæÆ÷µÄλ0ÇåÁ㣬¼´½ûÓÃPWM2
        P_SW2 &=~0x80;               //Çå³ý¸¨Öú¼Ä´æÆ÷2µÄλ7£¬½«PWM2Òý½ÅÇл»»ØÔ­À´µÄλÖÃ
        }
        else if (Wide==CYCLE)
        {
        PWMCR &=~0x01;              //PWM¿ØÖƼĴæÆ÷µÄλ0ÇåÁ㣬¼´½ûÓÃPWM2
        PWM2=0;                   //PWM2Òý½ÅÖÃΪµÍµçƽ
        }
        else
        {
       P_SW2 |= 0x80;             //ÉèÖø¨Öú¼Ä´æÆ÷2µÄλ7Ϊ1£¬½«PWM2Òý½ÅÇл»µ½P1.3     
       PWM2T1 = 0x01;             //PWM2¶¨Ê±Æ÷1¼Ä´æÆ÷µÄÖµÉèÖÃΪ0x01   
       PWM2T2 = Wide;             //PWM2¶¨Ê±Æ÷2¼Ä´æÆ÷µÄÖµÉèÖÃΪ Wide         
       PWMCR |= 0x01;               //PWM¿ØÖƼĴæÆ÷µÄλ0ÖÃΪ1£¬¼´ÆôÓÃPWM2
       P_SW2 &=~0x80;              //Çå³ý¸¨Öú¼Ä´æÆ÷2µÄλ7£¬½«PWM2Òý½ÅÇл»»ØÔ­À´µÄλÖà   
 
        }
 
}
void pwm3(unsigned short Wide)                  //PWM3
{         
        if((Wide==0)||(Wide==1))
        {
        P_SW2 |= 0x80; 
        PWMCR &=~0x02;
        PWM3=1;                     
        P_SW2 &=~0x80;                 
                    
        }
        else if(Wide==CYCLE)
        {
        PWMCR &=~0x02;
        PWM3=0; 
        }        
        else
        { 
        P_SW2 |= 0x80; 
       PWMCFG = 0x3f;                  //ÅäÖÃPWMµÄ³õʼµçƽΪ high                    
       PWM3T1 =0x01 ;                
       PWM3T2 =Wide;                                     
       P_SW2 &=~0x80;                 
       PWMCR |= 0x02;                   
 
        }        
}
void pwm4(unsigned short  Wide)                  //PWM4
{         
         if((Wide==0)||(Wide==1))
        {
                P_SW2 |= 0x80;
        PWMCR &=~0x04;
        PWM4=1; 
                P_SW2 &=~0x80;    
        }
        else if (Wide==CYCLE)
        {
        PWMCR &=~0x04;
        PWM4=0; 
        }
        else
        {         
       P_SW2 |= 0x80;                 
       PWM4T1 = 0x01;               
       PWM4T2 = Wide;                                
       P_SW2 &=~0x80;                  
       PWMCR |= 0x04;                   
 
        }
}
void pwm5(unsigned short  Wide)                  //PWM5
{         
         if((Wide==0)||(Wide==1))
        {
                P_SW2 |= 0x80;     
        PWMCR &=~0x08;
                PWM5=1; 
                P_SW2 &=~0x80;    
        
        }
        else if (Wide==CYCLE)
        {
        PWMCR &=~0x08;
        PWM5=0; 
        }
        else
        {         
       P_SW2 |= 0x80;                 
       PWM5T1 = 0x01;               
       PWM5T2 = Wide;                                
       P_SW2 &=~0x80;                  
       PWMCR |= 0x08;  
                    
        }
}