#include "intrins.h"
#include "STC8H.h"
//高級(jí) PWM 定時(shí)器 PWM1P/PWM1N,PWM2P/PWM2N,PWM3P/PWM3N,PWM4P/PWM4N 每個(gè)通道都可獨(dú)立實(shí)現(xiàn) PWM 輸出,或者兩兩互補(bǔ)對(duì)稱(chēng)輸出。演示使用 PWM1P,PWM1N 產(chǎn)生互補(bǔ)的 SPWM。
//主時(shí)鐘選擇 33MHZ,PWM 時(shí)鐘選擇 1T,PWM 周期 3500,死區(qū) 99個(gè)時(shí)鐘(3us),正弦波表用 200 點(diǎn),輸出正弦波頻率 = 33000000 / 3500 / 2 47.1HZ
#define MAIN_Fosc 33000000L //定義主時(shí)鐘 #include "reg51.h"
typedef unsigned char u8;
typedef unsigned int u16;
typedef unsigned long u32;
/*sfr TH2 = 0xD6;
sfr TL2 = 0xD7;
sfr IE2 = 0xAF;
sfr INT_CLKO = 0x8F;
sfr AUXR = 0x8E;
sfr P_SW1 = 0xA2;
sfr P_SW2 = 0xBA;
sfr P4 = 0xC0;
sfr P5 = 0xC8;
sfr P6 = 0xE8;
sfr P7 = 0xF8;
sfr P1M1 = 0x91;
sfr P1M0 = 0x92;
sfr P0M1 = 0x93;
sfr P0M0 = 0x94;
sfr P2M1 = 0x95;
sfr P2M0 = 0x96;
sfr P3M1 = 0xB1;
sfr P3M0 = 0xB2;
sfr P4M1 = 0xB3;
sfr P4M0 = 0xB4;
sfr P5M1 = 0xC9;
sfr P5M0 = 0xCA;
sfr P6M1 = 0xCB;
sfr P6M0 = 0xCC;
sfr P7M1 = 0xE1;
sfr P7M0 = 0xE2; */
/****************************** 用戶定義宏 ***********************************/
/*
#define PWMA_ENO (*(unsigned char volatile xdata *) 0xFEB1)
#define PWMA_PS (*(unsigned char volatile xdata *) 0xFEB2)
#define PWMB_ENO (*(unsigned char volatile xdata *) 0xFEB5)
#define PWMB_PS (*(unsigned char volatile xdata *) 0xFEB6)
#define PWMA_CR1 (*(unsigned char volatile xdata *) 0xFEC0)
#define PWMA_CR2 (*(unsigned char volatile xdata *) 0xFEC1)
#define PWMA_SMCR (*(unsigned char volatile xdata *) 0xFEC2)
#define PWMA_ETR (*(unsigned char volatile xdata *) 0xFEC3)
#define PWMA_IER (*(unsigned char volatile xdata *) 0xFEC4)
#define PWMA_SR1 (*(unsigned char volatile xdata *) 0xFEC5)
#define PWMA_SR2 (*(unsigned char volatile xdata *) 0xFEC6)
#define PWMA_EGR (*(unsigned char volatile xdata *) 0xFEC7)
#define PWMA_CCMR1 (*(unsigned char volatile xdata *) 0xFEC8)
#define PWMA_CCMR2 (*(unsigned char volatile xdata *) 0xFEC9)
#define PWMA_CCMR3 (*(unsigned char volatile xdata *) 0xFECA)
#define PWMA_CCMR4 (*(unsigned char volatile xdata *) 0xFECB)
#define PWMA_CCER1 (*(unsigned char volatile xdata *) 0xFECC)
#define PWMA_CCER2 (*(unsigned char volatile xdata *) 0xFECD)
#define PWMA_CNTRH (*(unsigned char volatile xdata *) 0xFECE)
#define PWMA_CNTRL (*(unsigned char volatile xdata *) 0xFECF)
#define PWMA_PSCRH (*(unsigned char volatile xdata *) 0xFED0)
#define PWMA_PSCRL (*(unsigned char volatile xdata *) 0xFED1)
#define PWMA_ARRH (*(unsigned char volatile xdata *) 0xFED2)
#define PWMA_ARRL (*(unsigned char volatile xdata *) 0xFED3)
#define PWMA_RCR (*(unsigned char volatile xdata *) 0xFED4)
#define PWMA_CCR1H (*(unsigned char volatile xdata *) 0xFED5)
#define PWMA_CCR1L (*(unsigned char volatile xdata *) 0xFED6)
#define PWMA_CCR2H (*(unsigned char volatile xdata *) 0xFED7)
#define PWMA_CCR2L (*(unsigned char volatile xdata *) 0xFED8)
#define PWMA_CCR3H (*(unsigned char volatile xdata *) 0xFED9)
#define PWMA_CCR3L (*(unsigned char volatile xdata *) 0xFEDA)
#define PWMA_CCR4H (*(unsigned char volatile xdata *) 0xFEDB)
#define PWMA_CCR4L (*(unsigned char volatile xdata *) 0xFEDC)
#define PWMA_BKR (*(unsigned char volatile xdata *) 0xFEDD)
#define PWMA_DTR (*(unsigned char volatile xdata *) 0xFEDE)
#define PWMA_OISR (*(unsigned char volatile xdata *) 0xFEDF)
*/
/*****************************************************************************/
#define PWMA_1 0x00 //P:P1.0 N:P1.1
#define PWMA_2 0x01 //P:P2.0 N:P2.1
#define PWMA_3 0x02 //P:P6.0 N:P6.1
#define PWMB_1 0x00 //P:P1.2/P5.4 N:P1.3
#define PWMB_2 0x04 //P:P2.2 N:P2.3
#define PWMB_3 0x08 //P:P6.2 N:P6.3
#define PWM3_1 0x00 //P:P1.4 N:P1.5
#define PWM3_2 0x10 //P:P2.4 N:P2.5
#define PWM3_3 0x20 //P:P6.4 N:P6.5
#define PWM4_1 0x00 //P:P1.6 N:P1.7
#define PWM4_2 0x40 //P:P2.6 N:P2.7
#define PWM4_3 0x80 //P:P6.6 N:P6.7
#define PWM4_4 0xC0 //P:P3.4 N:P3.3
#define ENO1P 0x01
#define ENO1N 0x02
#define ENO2P 0x04
#define ENO2N 0x08
#define ENO3P 0x10
#define ENO3N 0x20
#define ENO4P 0x40
#define ENO4N 0x80
/************* 本地變量聲明 **************/
unsigned int code T_SinTable[]=
{
1780,1834,1887,1941,1995,2049,2102,2155,2207,2259, //200點(diǎn)
2311,2362,2413,2463,2512,2560,2608,2655,2701,2746,
2790,2834,2876,2917,2957,2996,3033,3070,3105,3139,
3171,3202,3232,3260,3287,3312,3336,3358,3379,3398,
3415,3431,3445,3458,3469,3478,3486,3492,3496,3499,
3500,3499,3496,3492,3486,3478,3469,3458,3445,3431,
3415,3398,3379,3358,3336,3312,3287,3260,3232,3202,
3171,3139,3105,3070,3033,2996,2957,2917,2876,2834,
2790,2746,2701,2655,2608,2560,2512,2463,2413,2362,
2311,2259,2207,2155,2102,2049,1995,1941,1887,1834,
1780,1725,1672,1618,1564,1510,1457,1404,1352,1300,
1248,1197,1146,1096,1047,999,951,904,858,813,
769,725,683,642,602,563,526,489,454,420,
388,357,327,299,272,247,223,201,180,161,
144,128,114,101,90,81,73,67,63,60,
60,60,63,67,73,81,90,101,114,128,
144,161,180,201,223,247,272,299,327,357,
388,420,454,489,526,563,602,642,683,725,
769,813,858,904,951,999,1047,1096,1146,1197,
1248,1300,1352,1404,1457,1510,1564,1618,1672,1725
};
unsigned int code T_SinTable9[]=
{ 1780,1798,1817,1836,1855,1873,1892,1910,1929,1947,
1965,1983,2000,2018,2035,2052,2069,2085,2101,2117,
2132,2147,2162,2176,2190,2204,2217,2230,2242,2254,
2265,2276,2286,2296,2305,2314,2322,2330,2337,2344,
2350,2356,2361,2365,2369,2372,2375,2377,2378,2379,
2380,2379,2378,2377,2375,2372,2369,2365,2361,2356,
2350,2344,2337,2330,2322,2314,2305,2296,2286,2276,
2265,2254,2242,2230,2217,2204,2190,2176,2162,2147,
2132,2117,2101,2085,2069,2052,2035,2018,2000,1983,
1965,1947,1929,1910,1892,1873,1855,1836,1817,1798,
1780,1761,1742,1723,1704,1686,1667,1649,1630,1612,
1594,1576,1559,1541,1524,1507,1490,1474,1458,1442,
1427,1412,1397,1383,1369,1355,1342,1329,1317,1305,
1294,1283,1273,1263,1254,1245,1237,1229,1222,1215,
1209,1203,1198,1194,1190,1187,1184,1182,1181,1180,
1180,1180,1181,1182,1184,1187,1190,1194,1198,1203,
1209,1215,1222,1229,1237,1245,1254,1263,1273,1283,
1294,1305,1317,1329,1342,1355,1369,1383,1397,1412,
1427,1442,1458,1474,1490,1507,1524,1541,1559,1576,
1594,1612,1630,1649,1667,1686,1704,1723,1742,1761
} ;
unsigned int code T_SinTable8[]=
{
1780,1806,1832,1859,1885,1911,1937,1963,1988,2014,
2039,2064,2089,2113,2137,2161,2184,2207,2230,2252,
2273,2294,2315,2335,2355,2373,2392,2410,2427,2443,
2459,2474,2489,2503,2516,2528,2540,2550,2561,2570,
2578,2586,2593,2599,2605,2609,2613,2616,2618,2619,
2620,2619,2618,2616,2613,2609,2605,2599,2593,2586,
2578,2570,2561,2550,2540,2528,2516,2503,2489,2474,
2459,2443,2427,2410,2392,2373,2355,2335,2315,2294,
2273,2252,2230,2207,2184,2161,2137,2113,2089,2064,
2039,2014,1988,1963,1937,1911,1885,1859,1832,1806,
1780,1753,1727,1700,1674,1648,1622,1596,1571,1545,
1520,1495,1470,1446,1422,1398,1375,1352,1329,1307,
1286,1265,1244,1224,1204,1186,1167,1149,1132,1116,
1100,1085,1070,1056,1043,1031,1019,1009,998,989,
981,973,966,960,954,950,946,943,941,940,
940,940,941,943,946,950,954,960,966,973,
981,989,998,1009,1019,1031,1043,1056,1070,1085,
1100,1116,1132,1149,1167,1186,1204,1224,1244,1265,
1286,1307,1329,1352,1375,1398,1422,1446,1470,1495,
1520,1545,1571,1596,1622,1648,1674,1700,1727,1753
};
unsigned int code T_SinTable7[]=
{
1780,1802,1825,1847,1870,1892,1914,1937,1959,1980,
2002,2023,2045,2065,2086,2106,2126,2146,2165,2184,
2203,2221,2238,2256,2272,2289,2304,2320,2334,2348,
2362,2375,2387,2399,2410,2421,2431,2440,2449,2457,
2464,2471,2477,2482,2487,2491,2494,2496,2498,2499,
2500,2499,2498,2496,2494,2491,2487,2482,2477,2471,
2464,2457,2449,2440,2431,2421,2410,2399,2387,2375,
2362,2348,2334,2320,2304,2289,2272,2256,2238,2221,
2203,2184,2165,2146,2126,2106,2086,2065,2045,2023,
2002,1980,1959,1937,1914,1892,1870,1847,1825,1802,
1780,1757,1734,1712,1689,1667,1645,1622,1600,1579,
1557,1536,1514,1494,1473,1453,1433,1413,1394,1375,
1356,1338,1321,1303,1287,1270,1255,1239,1225,1211,
1197,1184,1172,1160,1149,1138,1128,1119,1110,1102,
1095,1088,1082,1077,1072,1068,1065,1063,1061,1060,
1060,1060,1061,1063,1065,1068,1072,1077,1082,1088,
1095,1102,1110,1119,1128,1138,1149,1160,1172,1184,
1197,1211,1225,1239,1255,1270,1287,1303,1321,1338,
1356,1375,1394,1413,1433,1453,1473,1494,1514,1536,
1557,1579,1600,1622,1645,1667,1689,1712,1734,1757
}; unsigned int code T_SinTable6[]=
{
1780,1810,1840,1870,1900,1930,1959,1989,2018,2047,
2076,2105,2133,2161,2188,2215,2242,2268,2294,2319,
2344,2368,2391,2414,2437,2458,2479,2500,2519,2538,
2556,2573,2590,2606,2621,2635,2648,2661,2672,2683,
2693,2701,2709,2716,2722,2728,2732,2735,2738,2739,
2740,2739,2738,2735,2732,2728,2722,2716,2709,2701,
2693,2683,2672,2661,2648,2635,2621,2606,2590,2573,
2556,2538,2519,2500,2479,2458,2437,2414,2391,2368,
2344,2319,2294,2268,2242,2215,2188,2161,2133,2105,
2076,2047,2018,1989,1959,1930,1900,1870,1840,1810,
1780,1749,1719,1689,1659,1629,1600,1570,1541,1512,
1483,1454,1426,1398,1371,1344,1317,1291,1265,1240,
1215,1191,1168,1145,1122,1101,1080,1059,1040,1021,
1003,986,969,953,938,924,911,898,887,876,
866,858,850,843,837,831,827,824,821,820,
820,820,821,824,827,831,837,843,850,858,
866,876,887,898,911,924,938,953,969,986,
1003,1021,1040,1059,1080,1101,1122,1145,1168,1191,
1215,1240,1265,1291,1317,1344,1371,1398,1426,1454,
1483,1512,1541,1570,1600,1629,1659,1689,1719,1749
};
unsigned int code T_SinTable5[]=
{ 1780,1806,1832,1859,1885,1911,1937,1963,1988,2014,
2039,2064,2089,2113,2137,2161,2184,2207,2230,2252,
2273,2294,2315,2335,2355,2373,2392,2410,2427,2443,
2459,2474,2489,2503,2516,2528,2540,2550,2561,2570,
2578,2586,2593,2599,2605,2609,2613,2616,2618,2619,
2620,2619,2618,2616,2613,2609,2605,2599,2593,2586,
2578,2570,2561,2550,2540,2528,2516,2503,2489,2474,
2459,2443,2427,2410,2392,2373,2355,2335,2315,2294,
2273,2252,2230,2207,2184,2161,2137,2113,2089,2064,
2039,2014,1988,1963,1937,1911,1885,1859,1832,1806,
1780,1753,1727,1700,1674,1648,1622,1596,1571,1545,
1520,1495,1470,1446,1422,1398,1375,1352,1329,1307,
1286,1265,1244,1224,1204,1186,1167,1149,1132,1116,
1100,1085,1070,1056,1043,1031,1019,1009,998,989,
981,973,966,960,954,950,946,943,941,940,
940,940,941,943,946,950,954,960,966,973,
981,989,998,1009,1019,1031,1043,1056,1070,1085,
1100,1116,1132,1149,1167,1186,1204,1224,1244,1265,
1286,1307,1329,1352,1375,1398,1422,1446,1470,1495,
1520,1545,1571,1596,1622,1648,1674,1700,1727,1753
};
unsigned int code T_SinTable4[]=
{ 1780,1813,1847,1881,1915,1948,1982,2015,2048,2081,
2113,2145,2177,2208,2239,2270,2300,2329,2358,2387,
2414,2441,2468,2494,2519,2543,2567,2590,2612,2633,
2653,2673,2691,2709,2726,2742,2757,2771,2784,2796,
2807,2817,2826,2833,2840,2846,2851,2855,2857,2859,
2860,2859,2857,2855,2851,2846,2840,2833,2826,2817,
2807,2796,2784,2771,2757,2742,2726,2709,2691,2673,
2653,2633,2612,2590,2567,2543,2519,2494,2468,2441,
2414,2387,2358,2329,2300,2270,2239,2208,2177,2145,
2113,2081,2048,2015,1982,1948,1915,1881,1847,1813,
1780,1746,1712,1678,1644,1611,1577,1544,1511,1478,
1446,1414,1382,1351,1320,1289,1259,1230,1201,1172,
1145,1118,1091,1065,1040,1016,992,969,947,926,
906,886,868,850,833,817,802,788,775,763,
752,742,733,726,719,713,708,704,702,700,
700,700,702,704,708,713,719,726,733,742,
752,763,775,788,802,817,833,850,868,886,
906,926,947,969,992,1016,1040,1065,1091,1118,
1145,1172,1201,1230,1259,1289,1320,1351,1382,1414,
1446,1478,1511,1544,1577,1611,1644,1678,1712,1746
};
unsigned int code T_SinTable3[]=
{ 1780,1817,1855,1892,1930,1967,2004,2041,2078,2114,
2150,2186,2221,2256,2290,2324,2358,2390,2422,2454,
2485,2515,2544,2573,2601,2628,2654,2680,2704,2728,
2750,2772,2793,2812,2831,2849,2865,2881,2895,2909,
2921,2932,2942,2951,2958,2965,2970,2974,2977,2979,
2980,2979,2977,2974,2970,2965,2958,2951,2942,2932,
2921,2909,2895,2881,2865,2849,2831,2812,2793,2772,
2750,2728,2704,2680,2654,2628,2601,2573,2544,2515,
2485,2454,2422,2390,2358,2324,2290,2256,2221,2186,
2150,2114,2078,2041,2004,1967,1930,1892,1855,1817,
1780,1742,1704,1667,1629,1592,1555,1518,1481,1445,
1409,1373,1338,1303,1269,1235,1201,1169,1137,1105,
1074,1044,1015,986,958,931,905,879,855,831,
809,787,766,747,728,710,694,678,664,650,
638,627,617,608,601,594,589,585,582,580,
580,580,582,585,589,594,601,608,617,627,
638,650,664,678,694,710,728,747,766,787,
809,831,855,879,905,931,958,986,1015,1044,
1074,1105,1137,1169,1201,1235,1269,1303,1338,1373,
1409,1445,1481,1518,1555,1592,1629,1667,1704,1742
};
unsigned int code T_SinTable2[]=
{ 1780,1812,1844,1875,1907,1939,1971,2002,2033,2064,
2095,2125,2155,2185,2214,2243,2271,2299,2326,2353,
2379,2405,2430,2454,2478,2501,2523,2545,2565,2585,
2605,2623,2641,2657,2673,2688,2702,2716,2728,2739,
2750,2759,2767,2775,2781,2787,2791,2795,2797,2799,
2800,2799,2797,2795,2791,2787,2781,2775,2767,2759,
2750,2739,2728,2716,2702,2688,2673,2657,2641,2623,
2605,2585,2565,2545,2523,2501,2478,2454,2430,2405,
2379,2353,2326,2299,2271,2243,2214,2185,2155,2125,
2095,2064,2033,2002,1971,1939,1907,1875,1844,1812,
1780,1747,1715,1684,1652,1620,1588,1557,1526,1495,
1464,1434,1404,1374,1345,1316,1288,1260,1233,1206,
1180,1154,1129,1105,1081,1058,1036,1014,994,974,
954,936,918,902,886,871,857,843,831,820,
809,800,792,784,778,772,768,764,762,760,
760,760,762,764,768,772,778,784,792,800,
809,820,831,843,857,871,886,902,918,936,
954,974,994,1014,1036,1058,1081,1105,1129,1154,
1180,1206,1233,1260,1288,1316,1345,1374,1404,1434,
1464,1495,1526,1557,1588,1620,1652,1684,1715,1747
};
unsigned int code T_SinTable1[]=
{ 1780,1821,1862,1904,1945,1986,2027,2067,2108,2148,
2187,2227,2265,2304,2342,2379,2415,2451,2487,2521,
2555,2589,2621,2652,2683,2713,2742,2770,2797,2823,
2847,2871,2894,2916,2936,2956,2974,2991,3007,3021,
3035,3047,3058,3068,3076,3083,3089,3094,3097,3099,
3100,3099,3097,3094,3089,3083,3076,3068,3058,3047,
3035,3021,3007,2991,2974,2956,2936,2916,2894,2871,
2847,2823,2797,2770,2742,2713,2683,2652,2621,2589,
2555,2521,2487,2451,2415,2379,2342,2304,2265,2227,
2187,2148,2108,2067,2027,1986,1945,1904,1862,1821,
1780,1738,1697,1655,1614,1573,1532,1492,1451,1411,
1372,1332,1294,1255,1217,1180,1144,1108,1072,1038,
1004,970,938,907,876,846,817,789,762,736,
712,688,665,643,623,603,585,568,552,538,
524,512,501,491,483,476,470,465,462,460,
460,460,462,465,470,476,483,491,501,512,
524,538,552,568,585,603,623,643,665,688,
712,736,762,789,817,846,876,907,938,970,
1004,1038,1072,1108,1144,1180,1217,1255,1294,1332,
1372,1411,1451,1492,1532,1573,1614,1655,1697,1738
};
unsigned int code T_SinTable0[]=
{ 1780,1827,1875,1923,1970,2017,2064,2111,2158,2204,
2249,2294,2339,2383,2427,2470,2512,2553,2594,2634,
2673,2711,2748,2785,2820,2854,2888,2920,2951,2981,
3009,3037,3063,3088,3111,3134,3155,3174,3193,3210,
3225,3239,3252,3263,3273,3281,3288,3293,3297,3299,
3300,3299,3297,3293,3288,3281,3273,3263,3252,3239,
3225,3210,3193,3174,3155,3134,3111,3088,3063,3037,
3009,2981,2951,2920,2888,2854,2820,2785,2748,2711,
2673,2634,2594,2553,2512,2470,2427,2383,2339,2294,
2249,2204,2158,2111,2064,2017,1970,1923,1875,1827,
1780,1732,1684,1636,1589,1542,1495,1448,1401,1355,
1310,1265,1220,1176,1132,1089,1047,1006,965,925,
886,848,811,774,739,705,671,639,608,578,
550,522,496,471,448,425,404,385,366,349,
334,320,307,296,286,278,271,266,262,260,
260,260,262,266,271,278,286,296,307,320,
334,349,366,385,404,425,448,471,496,522,
550,578,608,639,671,705,739,774,811,848,
886,925,965,1006,1047,1089,1132,1176,1220,1265,
1310,1355,1401,1448,1495,1542,1589,1636,1684,1732
};
u16 PWMA_Duty;
u8 PWM_Index; //SPWM查表索引
u16 first=0; // 首次啟動(dòng)
/******************** 主函數(shù) **************************/
void Delay1500ms(void) //@33.000MHz
{
unsigned char data i, j, k;
i = 252;
j = 30;
k = 12;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void main(void)
{
P0M1 = 0; P0M0 = 0; //設(shè)置為準(zhǔn)雙向口
P1M1 = 0; P1M0 = 0; //設(shè)置為準(zhǔn)雙向口
P2M1 = 0; P2M0 = 0; //設(shè)置為準(zhǔn)雙向口
P3M1 = 0; P3M0 = 0; //設(shè)置為準(zhǔn)雙向口
P4M1 = 0; P4M0 = 0; //設(shè)置為準(zhǔn)雙向口
P5M1 = 0; P5M0 = 0; //設(shè)置為準(zhǔn)雙向口
P6M1 = 0; P6M0 = 0; //設(shè)置為準(zhǔn)雙向口
P7M1 = 0; P7M0 = 0; //設(shè)置為準(zhǔn)雙向口
PWMA_Duty = 1780;
Delay1500ms(); //延時(shí)1.5S 開(kāi)試單片機(jī)
P_SW2 |= 0x80; //使能訪問(wèn)XFR
PWMA_CCER1 = 0x00; //寫(xiě) CCMRx 前必須先清零 CCxE 關(guān)閉通道
PWMA_CCER2 = 0x00;
PWMA_CCMR1 = 0x60; //通道模式配置 為比較輸出模式pwm1 模式。 0110 0000
PWMA_CCMR2 = 0x60; //通道模式配置 為比較輸出模式pwm1 模式。 0110 0000
PWMA_CCMR3 = 0x60;
PWMA_CCMR4 = 0x60;
// PWMA_CCER1 = 0x05; //配置1通道輸出使能和極性 。
PWMA_CCER1 = 0x55; //配置1和2通道輸出使能和極性 。
PWMA_CCER2 = 0x55; // 1176頁(yè)文檔,//配置3,4通道輸出使能和極性
// PWMA_CCER2 = 0x05; //配置3通道 輸出使能和極性 3通道輸出正玄波,
PWMA_ARRH = 0x0C; //設(shè)置周期時(shí)間3300==0x0CE4 2400 == 0x0960
PWMA_ARRL = 0xE4;
PWMA_CCR4H = (u8)(PWMA_Duty >> 8); //設(shè)置占空比時(shí)間 PWMA_CCR4H 為4通道的捕獲/比較寄存器 輸出正玄波
PWMA_CCR4L = (u8)(PWMA_Duty);
PWMA_CCR1H = (u8)(3460>> 8); //設(shè)置占空比時(shí)間,PWMA_CCR1H 為1通道的捕獲/比較寄存器 連續(xù)輸出方波波。
PWMA_CCR1L = (u8)(3460);
PWMA_CCR2H = (u8)(0); //設(shè)置占空比時(shí)間,PWMA_CCR1H 為4通道的捕獲/比較寄存器 連續(xù)輸出方波波。
PWMA_CCR2L = (u8)(0);
// PWMA_DTR = 0x0C; //設(shè)置死區(qū)時(shí)間, n=0~127: DTR= n T =13*1/24=0.5us T=0.5/13=0.041
PWMA_DTR = 0x63; //0x63=99 99/33=3us
PWMA_ENO = 0x00;
PWMA_ENO |= ENO1P; //使能輸出
PWMA_ENO |= ENO1N; //使能輸出
PWMA_ENO |= ENO2P; //使能輸出
PWMA_ENO |= ENO2N; //使能輸出
PWMA_ENO |= ENO3P; //使能輸出 3通道單獨(dú)
PWMA_ENO |= ENO3N; //使能輸出
PWMA_ENO |= ENO4P; //使能輸出 4通道單獨(dú)
PWMA_ENO |= ENO4N; //使能輸出
first=10;
PWMA_PS = 0x00; //高級(jí) PWM 通道輸出腳選擇位
PWMA_PS |= PWMA_1; //選擇 PWMA_1 通道
// PWMA_PS |= PWMB_3; //選擇 PWMB_3 通道
// PWMA_PS |= PWM3_1; //選擇 PWM3_3 通道
// PWMA_PS |= PWM4_1; //選擇 PWM4_1 通道
PWMA_BKR = 0x80; //使能主輸出
PWMA_IER = 0x01; //使能中斷
PWMA_CR1 |= 0x01; //開(kāi)始計(jì)時(shí)
P_SW2 &= 0x7f;
EA = 1; //打開(kāi)總中斷
P32=1;
P37=1;
//P36==1過(guò)壓保護(hù),關(guān)閉。
while (1)
{ if(P37==0) //過(guò)流
{ P32=0; //SD# 關(guān)閉。
first=10; //過(guò)流標(biāo)記,也是電源開(kāi)機(jī)標(biāo)記
}
if(P36==1) //過(guò)壓 比如大于交流246
{first=1;} //減低幾v--10v.
}
}
/******************** 中斷函數(shù) **************************/
void PWMA_ISR() interrupt 26
{
P_SW2 |= 0x80;
if (PWMA_SR1 & 0x01) //狀態(tài)寄存器, 硬件產(chǎn)生中斷置1 , 需軟件清零。
{
PWMA_SR1 &=~0x01; //軟件清零 中斷。
if(first>0) //大于0,為低壓?jiǎn)?dòng)標(biāo)志
{if(first==10)
PWMA_Duty= T_SinTable9[PWM_Index];
if(first==9)
PWMA_Duty= T_SinTable8[PWM_Index];
if(first==8)
PWMA_Duty= T_SinTable7[PWM_Index];
if(first==7)
PWMA_Duty= T_SinTable6[PWM_Index];
if(first==6)
PWMA_Duty= T_SinTable5[PWM_Index];
if(first==5)
PWMA_Duty= T_SinTable4[PWM_Index];
if(first==4)
PWMA_Duty= T_SinTable3[PWM_Index];
if(first==3)
PWMA_Duty= T_SinTable2[PWM_Index];
if(first==2)
PWMA_Duty= T_SinTable1[PWM_Index];
if(first==1)
PWMA_Duty= T_SinTable0[PWM_Index];
if(PWM_Index==199)
first--;
}
else
PWMA_Duty = T_SinTable[PWM_Index];
if (++PWM_Index >= 200) // 之前是>=200 點(diǎn)
{ PWM_Index = 0;
}
if( PWM_Index <= 99)
{ PWMA_CCR1H = (u8)(3460>> 8); //設(shè)置占空比時(shí)間,PWMA_CCR1H 為1通道的捕獲/比較寄存器 連續(xù)輸出方波波。
PWMA_CCR1L = (u8)(3460);
PWMA_CCR2H = (u8)(0); //設(shè)置占空比時(shí)間,PWMA_CCR2H 為2通道的捕獲/比較寄存器 連續(xù)輸出方波波。
PWMA_CCR2L = (u8)(0);
//////////////
PWMA_CCR4H = (u8)(PWMA_Duty >> 8); //設(shè)置占空比時(shí)間,PWMA_CCR4H 為4通道的捕獲/比較寄存器 連續(xù)輸出正旋波。
PWMA_CCR4L = (u8)(PWMA_Duty); //輸出正的上半波
PWMA_CCR3H = (u8)(0); //設(shè)置占空比時(shí)間,PWMA_CCR3H 為3通道的捕獲/比較寄存器 連續(xù)輸出正旋波。
PWMA_CCR3L = (u8)(0);
}
if ( PWM_Index >= 100)
{ PWMA_CCR1H = (u8)(0); //設(shè)置占空比時(shí)間,PWMA_CCR1H 為1通道的捕獲/比較寄存器 連續(xù)輸出方波波。
PWMA_CCR1L = (u8)(0);
PWMA_CCR2H = (u8)(3460>> 8); //設(shè)置占空比時(shí)間,PWMA_CCR2H 為2通道的捕獲/比較寄存器 連續(xù)輸出方波波。
PWMA_CCR2L = (u8)(3460);
////////////////
PWMA_CCR4H = (u8)(0); //設(shè)置占空比時(shí)間,PWMA_CCR4H 為4通道的捕獲/比較寄存器 。
PWMA_CCR4L = (u8)(0);
PWMA_CCR3H = (u8)(PWMA_Duty >> 8); //設(shè)置占空比時(shí)間,PWMA_CCR3H 為3通道的捕獲/比較寄存器 連續(xù)輸出正旋波。
PWMA_CCR3L = (u8)(PWMA_Duty); //輸出負(fù)的正玄波
}
}
PWMA_SR1 = 0;
P_SW2 &= 0x7f;
}
|