- //==================================================================================
- #include <pic.h>
- #include "TM16xx.h"
- volatile INT8U FreqCountL = 0; //频率计数值
- volatile INT8U FreqCountH = 0; //频率计数值
- volatile INT8U FreqUpdata = 0; //频率更新
- const INT8U CodeTable[] = {0xee,0x28,0xb6,0xba,0x78,0xda,0xde,0xa8,0xfe,0xfa};
- //----------------------------------------------------------------------------------
- // 系统初始化
- //
- //描述:上电初始化系统
- //参数: 无
- //返回:无
- //----------------------------------------------------------------------------------
- void Init(void)
- {
- GIE = 0; //禁止中断
- //初始化时钟 //内部4M
- IRCF0 = 0;
- IRCF1 = 1;
- IRCF2 = 1;
- //配置端口
- CMCON0 = 0x07; //关闭比较器
- ANSEL = 0x00; //指定数字IO
- //配置端口
- TRISA = 0xF8; // 1:输入 0: 输出
- PORTA = 0x00; // 设置IO状态
- TRISC = 0xFF; // 1:输入 0: 输出
- PORTC = 0x00; // 设置IO状态
- //初始化AD
- ANS7 = 1; //设IO为模拟输入
- ADCS0 = 1; //指定专用时钟
- ADCS1 = 1;
- ADCS2 = 1;
- VCFG = 0; //参考电压为VCC
- CHS0 = 1; //选择通道7
- CHS1 = 1;
- CHS2 = 1;
- ADFM = 0; //结果格式右对
- ADIF = 0; //清标记位
- ADON = 1; //启动AD
- //初始化计数器1
- TMR1CS = 1; //外部时钟计数
- TMR1ON = 0; //停止计数器
- //初始化定时器0
- T0CS = 0; //时钟源主F/4
- PSA = 0; //分频器分配定时器
- PS0 = 1; //分频256
- PS1 = 1;
- PS2 = 1;
- TMR0 = 61; //初值
- T0IE = 1; //使能定时器中断
- GIE = 1; //使能中断
- //初始化看门狗
- WDTPS0 = 1;
- WDTPS1 = 1;
- WDTPS2 = 1;
- WDTPS3 = 0;
- }
- //----------------------------------------------------------------------------------
- // 主程序和函数入口
- //
- //描述:主程序和函数入口
- //参数: 无
- //返回:无
- //----------------------------------------------------------------------------------
- void main(void)
- {
- INT16U Freq;
- INT8U Precent;
- INT16U FreqTemp[3];
- INT8U PrecentTemp[3];
- INT8U Index = 0;
- INT8U CtrlCount = 3;
- Init();
- while(1)
- {
- //采样
- if(FreqUpdata)
- {
- //外控开关
- if((RA4==1)) {if(CtrlCount<5) CtrlCount++;}
- else CtrlCount=0;
- //外部调功
- GODONE = 1;
- while(ADIF == 0);ADIF = 0;
- PrecentTemp[Index] = ((INT16U)ADRESH)*4/10;
- //频率
- FreqTemp[Index] = (((INT16U)FreqCountH)<<8) + FreqCountL;
- FreqUpdata = 0;
- Index++;
- }
- //显示刷新
- if(Index >= 3)
- {
- Freq = (FreqTemp[0]+FreqTemp[1]+FreqTemp[2])/3;
- Precent = (PrecentTemp[0]+PrecentTemp[1]+PrecentTemp[2])/3;
- if(CtrlCount>=2)
- {
- TM16xx_DisSeg(0, 0x10);
- TM16xx_DisSeg(2, 0x10);
- TM16xx_DisSeg(4, 0x10);
- TM16xx_DisSeg(6, 0x10);
- TM16xx_DisSeg(8, 0x10);
- TM16xx_DisSeg(10, 0x10);
- TM16xx_DisSeg(12, 0x10);
- TM16xx_DisSeg(14, 0x10);
- }
- else
- {
- TM16xx_DisSeg(14,CodeTable[0]);
- TM16xx_DisSeg(10,CodeTable[Freq/1000]);
- TM16xx_DisSeg(8, CodeTable[Freq%1000/100]|0x01);
- TM16xx_DisSeg(12,CodeTable[Freq%1000%100/10]);
- if(Precent > 100) Precent = 100;
- Precent = 100 - Precent;
- TM16xx_DisSeg(4, 0x00);
- TM16xx_DisSeg(2, 0x00);
- TM16xx_DisSeg(0, CodeTable[Precent%100/10]);
- TM16xx_DisSeg(6, CodeTable[Precent%100%10]);
- }
- Index = 0;
- CLRWDT();
- }
- }
- }
- //----------------------------------------------------------------------------------
- // 中断服务程序
- //
- //描述:中断服务程序
- //参数: 无
- //返回:无
- //----------------------------------------------------------------------------------
- static void interrupt ISR(void)
- {
- static INT8U status = 0;
- if(T0IF)
- {
- TMR0 = 61;
- TMR1ON = 0; //停止计数器
- if(status ==0)
- {
- status = 1;
- TMR1L = 0;
- TMR1H = 0;
- }
- else if (status ==1)
- {
- status = 2;
- }
- else
- {
- status = 0;
- FreqCountH = TMR1H;
- FreqCountL = TMR1L;
- FreqUpdata = 1;
- }
- TMR1ON = 1; //启动计数器
- T0IF = 0; //清除标记
- }
- }
- //==================================================================================
复制代码
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>