#include <STC8.h> #include <intrins.h> #define FOSC 24000000UL #define BRT (65536 - FOSC / 115200 / 4) #define PWM_T 0x1000 // PWM计数器,设置PWM周期为0x1000,即4096 #define PWMnT1 0x0000 //PWM的0通道T1计数值为0 #define PWMnT2 0x0028 //PWM的0通道T2计数值为0x28 #define PWMnCR 0x85 bit busy,p=0; char wptr; char rptr; char buffer[4]; void UartSend(char dat) { while (busy); busy = 1; SBUF = dat; } void UartIsr() interrupt 4 using 1 { if (TI) { TI = 0; busy = 0; } if (RI) { RI = 0; buffer[wptr++] = SBUF; wptr &= 0x03; while(!RI); RI = 0; buffer[wptr++] = SBUF; wptr &= 0x03; while(!RI); RI = 0; buffer[wptr++] = SBUF; wptr &= 0x03; wptr = 0x00; p=1; } } void UartInit() { P_SW1 &= ~0xc0; SCON = 0x50; T2L = BRT; T2H = BRT >> 8; AUXR = 0x15; wptr = 0x00; rptr = 0x00; TI = 0; } void UartSendStr(char *p) { while (*p) { UartSend(*p++); } } void main() { unsigned int auto_adjust[8]; unsigned char adjust_flag=0x00; UartInit(); ES = 1; EA = 1; /************************************以下是PWM的初始化********************************/ P_SW2 |= 0x80; PWMCKS = 0x01;// PWM时钟选择,系统时钟2分频:12MHz,但实际测得的输出是 约2.93kHz PWMC = PWM_T; // PWM计数器,设置PWM周期为0x1000,即4096 PWM0T1 = PWMnT1; PWM0T2 = PWMnT2; PWM0CR = PWMnCR; PWM1T1 = PWMnT1; PWM1T2 = PWMnT2; PWM1CR = PWMnCR; PWM2T1 = PWMnT1; PWM2T2 = PWMnT2; PWM2CR = PWMnCR; PWM3T1 = PWMnT1; PWM3T2 = PWMnT2; PWM3CR = PWMnCR; PWM4T1 = PWMnT1; PWM4T2 = PWMnT2; PWM4CR = PWMnCR; PWM5T1 = PWMnT1; PWM5T2 = PWMnT2; PWM5CR = PWMnCR; PWM6T1 = PWMnT1; PWM6T2 = PWMnT2; PWM6CR = PWMnCR; PWM7T1 = PWMnT1; PWM7T2 = PWMnT2; PWM7CR = PWMnCR; PWMIF = 0x00; PWMCFG &= ~0xc0; P_SW2 &= ~0x80; IP2H &= ~0x08; IP2 |= 0x08; PWMCR = 0x80; /*PWM0的控制寄存器,PWM输出使能,初始电平=0, P2脚作为PWM功能脚,使能中断,使能T1翻转点中断*/ /************************************以上是PWM的初始化********************************/ while (1) { if (p) { p=0; auto_adjust[buffer[0]] = buffer[2]; auto_adjust[buffer[0]] = (auto_adjust[buffer[0]]<<8) + buffer[1]; adjust_flag |= 1<<buffer[0]; UartSend(buffer[0]); UartSend((auto_adjust[buffer[0]]>>8)); UartSend(((auto_adjust[buffer[0]]<<8)>>8)); } _nop_(); _nop_(); _nop_(); UartSend(p); _nop_(); _nop_(); _nop_(); } }
您需要 登录 才可以下载或查看,没有账号?注册
举报
libre01 发表于 2017-5-6 19:16 通信不成功是收不到数据还是数据传输错误
libre01 发表于 2017-5-7 09:29 你看下头文件中P_SW2定义的地址正确不?用的串口一的话,你把P_SW1&=~0xc0去掉,再运行下 ...
coody 发表于 2017-5-8 14:04 主程序: P_SW2 |= 0x80; //访问XSFR XSFR访问代码
#include <STC8.h> #include <intrins.h> #define FOSC 24000000UL #define BRT (65536 - FOSC / 115200 / 4) #define PWM_T 0x1000 // PWM计数器,设置PWM周期为0x1000,即4096 #define PWMnT1 0x0000 //PWM的0通道T1计数值为0 #define PWMnT2 0x0028 //PWM的0通道T2计数值为0x28 #define PWMnCR 0x85 bit busy,p=0; char wptr,adjust_flag; char rptr; char buffer[4]; void UartIsr() interrupt 4 using 1 { if (TI) { TI = 0; busy = 0; } if (RI) { RI = 0; buffer[wptr++] = SBUF; wptr &= 0x03; while(!RI); RI = 0; buffer[wptr++] = SBUF; wptr &= 0x03; while(!RI); RI = 0; buffer[wptr++] = SBUF; wptr &= 0x03; wptr = 0x00; p=1; } } void PWM_Isr() interrupt 22 using 3 { unsigned char i; i = P_SW2; P_SW2 |= 0x80; if (PWMCFG & 0x80) PWMCFG &= ~0x80; if (PWMIF & 0x80) { PWMIF &= ~0x80; if(adjust_flag & 0x80) { adjust_flag &= ~0x80; } } if (PWMIF & 0x40) { PWMIF &= ~0x40; if(adjust_flag & 0x40) { adjust_flag &= ~0x40; } } if (PWMIF & 0x20) { PWMIF &= ~0x20; if(adjust_flag & 0x20) { adjust_flag &= ~0x20; } } if (PWMIF & 0x10) { PWMIF &= ~0x10; if(adjust_flag & 0x10) { adjust_flag &= ~0x10; } } if (PWMIF & 0x08) { PWMIF &= ~0x08; if(adjust_flag & 0x08) { adjust_flag &= ~0x08; } } if (PWMIF & 0x04) { PWMIF &= ~0x04; if(adjust_flag & 0x04) { adjust_flag &= ~0x04; } } if (PWMIF & 0x02) { PWMIF &= ~0x02; if(adjust_flag & 0x02) { adjust_flag &= ~0x02; } } if (PWMIF & 0x01) { PWMIF &= ~0x01; if(adjust_flag & 0x01) { adjust_flag &= ~0x01; } } P_SW2 = i; } void UartInit() { P_SW1 &= ~0xc0; SCON = 0x50; T2L = BRT; T2H = BRT >> 8; AUXR = 0x15; wptr = 0x00; rptr = 0x00; TI = 0; } void UartSend(char dat) { while (busy); busy = 1; SBUF = dat; } void UartSendStr(char *p) { while (*p) { UartSend(*p++); } } void main() { unsigned int auto_adjust[8]; unsigned char i,adjust_flag=0x00; UartInit(); ES = 1; EA = 1; /************************************以下是PWM的初始化********************************/ i = P_SW2; P_SW2 |= 0x80; PWMCKS = 0x01;// PWM时钟选择,系统时钟2分频:12MHz,但实际测得的输出是 约2.93kHz PWMC = PWM_T; // PWM计数器,设置PWM周期为0x1000,即4096 PWM0T1 = PWMnT1; PWM0T2 = PWMnT2; PWM0CR = PWMnCR; PWM1T1 = PWMnT1; PWM1T2 = PWMnT2; PWM1CR = PWMnCR; PWM2T1 = PWMnT1; PWM2T2 = PWMnT2; PWM2CR = PWMnCR; PWM3T1 = PWMnT1; PWM3T2 = PWMnT2; PWM3CR = PWMnCR; PWM4T1 = PWMnT1; PWM4T2 = PWMnT2; PWM4CR = PWMnCR; PWM5T1 = PWMnT1; PWM5T2 = PWMnT2; PWM5CR = PWMnCR; PWM6T1 = PWMnT1; PWM6T2 = PWMnT2; PWM6CR = PWMnCR; PWM7T1 = PWMnT1; PWM7T2 = PWMnT2; PWM7CR = PWMnCR; PWMIF = 0x00; PWMCFG &= ~0xc0; P_SW2 = i; PWMCR = 0x80; /*PWM0的控制寄存器,PWM输出使能,初始电平=0, P2脚作为PWM功能脚,使能中断,使能T1翻转点中断*/ /************************************以上是PWM的初始化********************************/ while (1) { if (p) { p=0; auto_adjust[buffer[0]] = buffer[2]; auto_adjust[buffer[0]] = (auto_adjust[buffer[0]]<<8) + buffer[1]; adjust_flag |= 1<<buffer[0]; UartSend(buffer[0]); UartSend((auto_adjust[buffer[0]]>>8)); UartSend(((auto_adjust[buffer[0]]<<8)>>8)); } _nop_(); _nop_(); _nop_(); UartSend(p); _nop_(); _nop_(); _nop_(); } }
coody 发表于 2017-5-12 23:29 LZ还没搞好? 我用STC8做的多轴飞控,用了PWM,还用了2个串口,串口1用来调试或数传,串口2用来接收GPS模块 ...
清风819 发表于 2017-5-13 21:26 请问你用的哪个型号?频率用到多高?使用串口使用内部RC时钟源吗?
coody 发表于 2017-5-15 00:03 我用的是STC8A8K64S4A12 LQFP44,跑24MHZ,使用内部时钟。姿态解算是125HZ,即8ms解算一次,浮点数处理, ...
dcxq13 发表于 2017-5-23 08:30 楼主你现在调试的如何了?!是PWM与串口都不工作还是只正常一个?!
#include <STC8.h> #include <intrins.h> #define FOSC 24000000UL #define BRT (65536 - FOSC / 115200 / 4) #define PWM_T 0x1000 // PWM计数器,设置PWM周期为0x1000,即4096 #define PWMnT1 0x0000 //PWM的0通道T1计数值为0 #define PWMnT2 0x0028 //PWM的0通道T2计数值为0x28 #define PWMnCR 0x85 bit busy,p=0; char wptr,adjust_flag; char rptr; char buffer[4]; void UartIsr() interrupt 4 using 1 { if (TI) { TI = 0; busy = 0; } if (RI) { RI = 0; buffer[wptr++] = SBUF; wptr &= 0x03; while(!RI); RI = 0; buffer[wptr++] = SBUF; wptr &= 0x03; while(!RI); RI = 0; buffer[wptr++] = SBUF; wptr &= 0x03; wptr = 0x00; p=1; } } void PWM_Isr() interrupt 22 using 3 { unsigned char i; i = P_SW2; P_SW2 |= 0x80; if (PWMCFG & 0x80) PWMCFG &= ~0x80; if (PWMIF & 0x80) { PWMIF &= ~0x80; if(adjust_flag & 0x80) { adjust_flag &= ~0x80; } } if (PWMIF & 0x40) { PWMIF &= ~0x40; if(adjust_flag & 0x40) { adjust_flag &= ~0x40; } } if (PWMIF & 0x20) { PWMIF &= ~0x20; if(adjust_flag & 0x20) { adjust_flag &= ~0x20; } } if (PWMIF & 0x10) { PWMIF &= ~0x10; if(adjust_flag & 0x10) { adjust_flag &= ~0x10; } } if (PWMIF & 0x08) { PWMIF &= ~0x08; if(adjust_flag & 0x08) { adjust_flag &= ~0x08; } } if (PWMIF & 0x04) { PWMIF &= ~0x04; if(adjust_flag & 0x04) { adjust_flag &= ~0x04; } } if (PWMIF & 0x02) { PWMIF &= ~0x02; if(adjust_flag & 0x02) { adjust_flag &= ~0x02; } } if (PWMIF & 0x01) { PWMIF &= ~0x01; if(adjust_flag & 0x01) { adjust_flag &= ~0x01; } } P_SW2 = i; } void UartInit() { P_SW1 &= ~0xc0; /*P3.0是RXD,receive,接收,属于输入模式,所以不能设为强推挽输出*/ P3M0 |= 0x02; P3M0 &= ~0x01; P3M1 &= ~0x03;//强推挽输出 SCON = 0x50; T2L = BRT; T2H = BRT >> 8; AUXR = 0x15; wptr = 0x00; rptr = 0x00; TI = 0; } void UartSend(char dat) { while (busy); busy = 1; SBUF = dat; } //void UartSendStr(char *p) //{ // while (*p) // { // UartSend(*p++); // } //} void main() { unsigned int auto_adjust[8]; unsigned char i,adjust_flag=0x00; UartInit(); ES = 1; EA = 1; /************************************以下是PWM的初始化********************************/ i = P_SW2; P_SW2 |= 0x80; PWMCKS = 0x01;// PWM时钟选择,系统时钟2分频:12MHz,但实际测得的输出是 约2.93kHz PWMC = PWM_T; // PWM计数器,设置PWM周期为0x1000,即4096 PWM0T1 = PWMnT1; PWM0T2 = PWMnT2+1; PWM0CR = PWMnCR; PWM1T1 = PWMnT1+2; PWM1T2 = PWMnT2+3; PWM1CR = PWMnCR; PWM2T1 = PWMnT1+4; PWM2T2 = PWMnT2+5; PWM2CR = PWMnCR; PWM3T1 = PWMnT1+6; PWM3T2 = PWMnT2+7; PWM3CR = PWMnCR; PWM4T1 = PWMnT1+8; PWM4T2 = PWMnT2+9; PWM4CR = PWMnCR; PWM5T1 = PWMnT1+10; PWM5T2 = PWMnT2+11; PWM5CR = PWMnCR; PWM6T1 = PWMnT1+12; PWM6T2 = PWMnT2+13; PWM6CR = PWMnCR; PWM7T1 = PWMnT1+14; PWM7T2 = PWMnT2+15; PWM7CR = PWMnCR; PWMIF = 0x00; PWMCFG &= ~0xc0; P_SW2 = i; PWMCR = 0x80; /*PWM0的控制寄存器,PWM输出使能,初始电平=0, P2脚作为PWM功能脚,使能中断,使能T1翻转点中断*/ /************************************以上是PWM的初始化********************************/ while (1) { if (p) { p=0; auto_adjust[buffer[0]] = buffer[1]; auto_adjust[buffer[0]] = (auto_adjust[buffer[0]]<<8) + buffer[2]; adjust_flag |= 1<<buffer[0]; UartSend(buffer[0]); UartSend((auto_adjust[buffer[0]]>>8)); UartSend(((auto_adjust[buffer[0]]<<8)>>8)); } } }
wfxsgsg 发表于 2017-6-1 11:01 【结贴啦!】【问题已解决】真正的问题原因,芯片不稳定,8路PWM同时翻转导致单片机崩溃,因此串 ...
datouyuan 发表于 2017-6-1 11:57 你靠近mcu有没有滤波电容?会不会是由于这原因?
wfxsgsg 发表于 2017-6-2 14:41 真正的原因是这款stc单片机承受不了8路同频同相的PWM运转,导致单片机崩溃(进而包括串口在内的等功能都 ...
datouyuan 发表于 2017-6-2 14:51 这原因站不住脚。
本版积分规则 发表回复 回帖并转播 回帖后跳转到最后一页
等级类勋章
发帖类勋章
人才类勋章
时间类勋章
4
29
0
扫码关注 21ic 官方微信
扫码关注嵌入式微处理器
扫码关注电源系统设计
扫码关注21ic项目外包
扫码浏览21ic手机版
本站介绍 | 申请友情链接 | 欢迎投稿 | 隐私声明 | 广告业务 | 网站地图 | 联系我们 | 诚聘英才
京公网安备 11010802024343号