⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pro.c

📁 数字频率计数字频率计数字频率计数字频率计
💻 C
字号:
//包含所需头文件
#include <ioM16v.h>
#include <macros.h>
#include "12864.h"
#include "delay.h"
/*------宏定义------*/
#define uchar	unsigned char
#define uint	unsigned int

uchar Second=0,Minute=0,Hour=0;
uchar Second_H,Second_L,Minute_H,Minute_L,Hour_H,Hour_L;
uchar time_buffer[8]={0,0,0,0,0,0,0,0};
unsigned char Timer1_Counter_H=0;
unsigned char Timer1_Counter_L=0;
unsigned long value=0;
long Freq=0,Freq_H16=0,Freq_L8=0,counter=0;
uchar T1_OV_Times=0; //T1溢出次数
uchar temp[8];

void time(void)
{
 Second++;
 if(Second >= 60)
 {
   Second=0;
   Minute++;
   if(Minute >=60)
   {
	  Hour++;
	  if(Hour >=24)
	   Hour=0;
	}
 }
}

void display_time(void)
{
  time_buffer[0]=Hour/10+0x30;
  time_buffer[1]=Hour%10+0x30;
  time_buffer[2]=':';
  time_buffer[3]=Minute/10+0x30;
  time_buffer[4]=Minute%10+0x30;
  time_buffer[5]=':';
  time_buffer[6]=Second/10+0x30;
  time_buffer[7]=Second%10+0X30;
 
  LCD12864_gotoXY(2,2);
  LCD12864_sendstr(time_buffer);
  /*LCD12864_sendbyte(1,Hour_H);
  LCD12864_sendbyte(1,Hour_L);
  LCD12864_sendbyte(1,':');
  
  LCD12864_sendbyte(1,Minute_H);
  LCD12864_sendbyte(1,Minute_L);
  LCD12864_sendbyte(1,':');

  LCD12864_sendbyte(1,Second_H);
  LCD12864_sendbyte(1,Second_L);*/
}
//端口初始化
void port_init(void)
{
 PORTA = 0xFF;
 DDRA  = 0xFF;
 PORTB = 0xf3;
 DDRB  = 0xf1;
 PORTC = 0x00;
 DDRC  = 0x00;
 PORTD = 0x00;
 DDRD  = 0x00;
}

//定时器T0初始化
void timer0_init(void)
{
 TCCR0  = 0x00;//停止定时器
 TCNT0  = 0x83;//初始值
 OCR0   = 0x82;//匹配值
 TIMSK |= 0x02;//中断允许
 TCCR0  = 0x04;//启动定时器
}

uchar display_is_ok=0;
uchar count=0;
//T0比较中断服务程序
#pragma interrupt_handler timer0_comp_isr:20
void timer0_comp_isr(void)
{
 //中断发生时刻在TCNT0=OCR0]
 if(++count >=5)
  {
   display_is_ok=1;
   count=0;
   }
}

//定时T1初始化
void timer1_init(void)
{
 TCCR1B = 0x00;//停止定时器
 TIMSK |= 0x04;//中断允许
 TCNT1H = 0x00;
 TCNT1L = 0x00;//初始值
 OCR1AH = 0xFF;
 OCR1AL = 0xFF;//匹配A值
 OCR1BH = 0xFF;
 OCR1BL = 0xFF;//匹配B值
 ICR1H  = 0xFF;
 ICR1L  = 0xFF;//输入捕捉匹配值
 TCCR1A = 0x0C;
 TCCR1B = 0x06;//启动定时器
}


//定时器T1溢出中断服务程序
#pragma interrupt_handler timer1_ovf_isr:9
void timer1_ovf_isr(void)
{
// TCNT1H = 0x00; //重装值高位
// TCNT1L = 0x00; //重装值低位
 T1_OV_Times=1;
}

//定时器T2初始化
void timer2_init(void)
{
 TCCR2  = 0x00;//停止定时器
 ASSR   = 0x08;//异步时钟模式
 TCNT2  = 0x80;//初始值
 OCR2   = 0x7F;//匹配值
 TIMSK |= 0x40;//中断允许
 TCCR2  = 0x06;//启动定时器
}

uchar second_is_ok=0;
//T2溢出中断服务程序

#pragma interrupt_handler timer2_ovf_isr:5
void timer2_ovf_isr(void)
{
 
 TCCR1B = 0x00;//停止定时器
 PORTB &=~(1<<PB0);  //关闭闸门
 second_is_ok=1;
 value=TCNT1L;
 value|=(TCNT1H << 8); //然后读高位
  //读取计数值
Freq_H16=value;
counter=Freq_H16;
Freq_H16 =Freq_H16*256;

Freq_L8 =(PIND & 0b11111111);    //读取计数值的低8位
Freq=Freq_H16+Freq_L8;
 if(T1_OV_Times !=0)
 {
   Freq+=(unsigned long)0xffff*0xff*T1_OV_Times;
   T1_OV_Times=0;
 }
 PORTB |=(1<<PB7);                 //74LS393清零
 TCNT1H=0;
 TCNT1L=0;                        //清空T1计数器
 PORTB &=~(1<<PB7);               //使能计数功能
 PORTB |=(1<<PB0);                //打开闸门
 TCNT2 = 0x80;                    //定时时间1秒
 TCCR1B = 0x06;
}

void PutChar()
{
    unsigned long freq=0;
	freq=Freq;
    temp[0]= freq /10000000+0x30;
    freq = freq %10000000;
    temp[1]=freq  /1000000+0x30;
    freq  =freq  %1000000;
    temp[2]=freq  /100000+0x30;
    freq =freq  %100000;
    temp[3]=freq  /10000+0x30;
    freq=freq  %10000;
    temp[4]=freq  /1000+0x30;
    freq=freq %1000;
    temp[5]=freq  /100+0x30;
    freq=freq %100;
    temp[6]=freq  /10 + 0x30;
    temp[7]=freq  %10 + 0x30;
  
  LCD12864_gotoXY(3,2);
  LCD12864_sendstr(temp);
  LCD12864_sendstr("Hz");
 /* LCD12864_sendbyte(1, temp[0]);
  LCD12864_sendbyte(1, temp[1]);
  LCD12864_sendbyte(1, temp[2]);
  LCD12864_sendbyte(1, temp[3]);
  LCD12864_sendbyte(1, temp[4]);
  LCD12864_sendbyte(1, temp[5]);
  LCD12864_sendbyte(1, temp[6]);
  LCD12864_sendbyte(1, temp[7]);*/
  
}

void init_devices(void)
{
 CLI(); //禁止所有中断
 MCUCR  = 0x00;
 GICR   = 0x00;
 port_init();
 LCD12864_initial(); 
 timer0_init();
 timer1_init();
 timer2_init();
 SEI();//开全局中断
}
//主函数
void main(void)
{
 init_devices();
 //在这继续添加你的代码
   LCD12864_gotoXY(1,0);
  LCD12864_sendstr("作者: 张秋伟");
  LCD12864_gotoXY(2,0);
  LCD12864_sendstr("班级: 06电气");
   LCD12864_gotoXY(3,0);
  LCD12864_sendstr("学号: 06");
  delay_nms(5000);
  LCD12864_clear();						//清屏
  LCD12864_gotoXY(1,2);
  LCD12864_sendstr("数字频率计");
   LCD12864_gotoXY(2,0);
  LCD12864_sendstr("时间");
  LCD12864_gotoXY(3,0);
  LCD12864_sendstr("频率");

  DDRB |=(1<<PB7)|(1<<PB0);//设置74ls393的清零端,闸门控制端为输出
  PORTB|= (1<<PB7); //74LS393清零
  delay_nus(5);
  PORTB &=~(1<<PB7); //74LS393开始计数
  PORTB|=(1<<PB0);  //打开闸门
  while(1)
  {
  if(second_is_ok == 1)
   {
    second_is_ok =0;
	time();
   }
   if(display_is_ok)
   {
	display_is_ok=0;
	PutChar();
 	display_time();
	}
  }
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -