各位大佬:
本人在用AI8H4K64TL单片机在做捕获方波周期时,发现数码管显示会突跳,频率越低越厉害
请有时间的大佬给予些指点。
系统时钟20MHZ;
谢谢!
- //包含头文件
- #include "all.h"
-
- ulong cap;
- ulong cap_new;
- ulong cap_old;
- unsigned long num = 0;
- //数码管显示函数
- void Display()
- {
- SMG_Write_Buffer[0] = SEG_DISPLAY[num/10000];//赋值给每一位寄存器;
- SMG_Write_Buffer[1] = SEG_DISPLAY[num%10000/1000];
- SMG_Write_Buffer[2] = SEG_DISPLAY[num%1000/100];
- SMG_Write_Buffer[3] = SEG_DISPLAY[num%100/10];
- SMG_Write_Buffer[4] = SEG_DISPLAY[num%10];
- DISPLAY_SER();
- }
- //PWMA初始化程序
- void PWMA_INIT()
- {
- PWMA_PS = 0x00;
- PWMA_PSCR = 0x13; //设置预分频器的值为50
- PWMA_CCER1 = 0x00; //先清零后写入
- PWMA_CCMR1 = 0x01; //(CC1连续捕获TI1上升沿)
- PWMA_CCER1 = 0x01; //CC1为输入模式,且映射到TI1FP1上
- PWMA_IER = 0x02; //使能PWMA中断
- PWMA_CR1 |= 0x01; //使能PWMA捕获
-
- }
- // 定义数组存储数据
- #define accumulative_frequency 20
- unsigned int calculate_average() {
- unsigned long sum = 0;
- unsigned int i;
-
- // 累加数组中的所有数据
- for (i = 0; i < accumulative_frequency; i++) {
- sum += cap;
- }
- num = (unsigned int)(sum / accumulative_frequency);
- return num;
- }
- //主程序
- void main()
- {
- P_SW2 |= 0x80; //允许访问特殊功能寄存器XFR;
- P0M0 |= 0x0f;
- P0M1 &= ~0x0f;
- P1M0 &= ~0x3b;
- P1M1 &= ~0x3b;
- P2M0 = 0x1f;
- P2M1 = 0x1f;
- P3M0 = 0xf0;
- P3M1 = 0x00;
- P3DR &= ~0xf0;
- P0DR &= ~0x0f;
- P2DR &= ~0x1f;
- XOSCCR = 0xc0; //启动外部晶振
- while (!(XOSCCR & 1)); //等待时钟稳定
- CLKDIV = 0x00; //时钟不分频
- CLKSEL = 0x01; //选择外部晶振
- PWMA_INIT();
- EA = 1; //开总中断
- while(1)
- {
- calculate_average();
- Display();
- }
- }
- void PWMA_ISR() interrupt 26 //PWMA中断服务程序
- {
- if(PWMA_SR1 & 0x02)
- {
- cap_old = cap_new;
- cap_new = PWMA_CCR1;
- cap = cap_new - cap_old;
- PWMA_SR1 &= ~0x02; //清除中断标志位
- }
- }
-
-
-
复制代码
- #include "stc8h.h" //包含stc8h.h文件
- #include "intrins.h" //包含intrins.h文件
-
- typedef unsigned int uint; //定义uint类型
- typedef unsigned char uchar; //定义uchar类型
- typedef unsigned long ulong; //定义ulong类型
-
- #include "Dis_Play.h" //包含Dis_Play.h文件
复制代码
- #include "all.h"
-
- uchar data SMG_Write_Buffer[5];//用于给数码管各个位赋值之用
- //0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E F, -, 空, ℃, ℉
- uchar code SEG_DISPLAY[20] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0xB9,0x71,0x40,0x00,0x79,0xF1};
-
- // 主函数
- void DISPLAY_SER() //显示服务函数;
- {
- //P_SW2 |= 0x80; //允许访问特殊功能寄存器XFR;
- COMEN = 0x1f; //打开低位的5个COM口;
- SEGENL = 0x00; //不使能低8位SEG口;
- SEGENH = 0xff; //使能高8位SEG口;
- LEDCTRL = 0x80; //设置打开LED驱动,使用共阴模式,亮度100%;
- LEDCKS = 0x2a; //调整刷新频率;
- COM0_DC_H = SMG_Write_Buffer[0];//赋值给每一位寄存器;
- COM1_DC_H = SMG_Write_Buffer[1];
- COM2_DC_H = SMG_Write_Buffer[2];
- COM3_DC_H = SMG_Write_Buffer[3];
- COM4_DC_H = SMG_Write_Buffer[4];
- LEDCTRL |= 0x80; //启动LED驱动;
- }
复制代码
|