单片机交流
直播中

王伟

7年用户 1493经验值
私信 关注
[问答]

如何对一种基于ATMEG8AVR单片机的频率计进行protues仿真?

如何对一种基于ATMEG8AVR单片机的频率计进行protues仿真
基于ATMEG8AVR单片机的频率计的protues仿真附源码是什么?

回帖(1)

刘婷

2021-7-7 11:37:09
  #include
  #include
  #define SYSCLK 16000000L
  #pragma interrupt_handler timer1_ovf_isr:9
  #pragma interrupt_handler timer0_ovf_isr:10
  unsigned char const table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e}; //共阳极数码管编码
  unsigned long int count=0;
  unsigned long int temp=0;
  void delay(unsigned char ms)
  {
  unsigned char i,j,k;
  for(i=160;i》0;i--)
  for(j=20;j》0;j--)
  for(k=ms;k》0;k--);
  }
  //关闭看门狗
  void Init_WatchDog(void)
  {
  WDR();
  WDTCR |= (1《《WDCE) | (1《《WDE);
  WDTCR = 0x00;
  }
  void timer1_init(void) //定时器1来提供1s的定时
  {
  TCCR1B=0x00; //停止定时器1
  TCNT1H=0xC2;
  TCNT1L=0XF7;
  TCCR1B=0x05; //来自预分频器的1024 外部时钟为16M
  }
  void timer0_init(void)
  {
  TCCR0=0X00; //停止计数器工作
  TCNT0=0X00; //设置初值为0
  TCCR0=0X07; //时钟由外部时钟输入,上升沿触发
  }
  void port_init(void)
  {
  PORTB=0XFF;
  DDRB=0XFF;
  PORTC = 0x7F; //输出状态,初始后为高电平
  DDRC = 0x7F;
  PORTD |= 0xEF;
  DDRD = 0xEF;
  }
  void timer1_ovf_isr(void)
  {
  CLI(); //关闭所有中断
  TCCR0=0X00; //停止计数器0工作
  TCCR1B=0x00; //停止定时器1
  TCNT1H=0xC2;
  TCNT1L=0xF7;
  count=(temp《《8)+TCNT0;
  temp=0;
  TCNT0=0X00;
  TCCR1B=0x05;//定时器1的时钟为1024分频。
  TCCR0=0X07; //时钟由外部时钟输入,上升沿触发
  SEI(); //开中断
  }
  void timer0_ovf_isr(void)
  {
  unsigned char flag=0;
  CLI(); //关闭所有中断
  TCCR0=0X00; //停止计数器0工作
  temp++;
  TCNT0=0X00;
  TCCR0=0X07; //时钟由外部时钟输入,上升沿触发
  SEI(); //开中断
  }
  void init_devices(void)
  {
  CLI(); //关闭所有中断
  Init_WatchDog();
  port_init();//IO口初始化
  timer0_init();
  timer1_init(); //定时器初始化
  TIMSK = 0x05; //使能定时器0和定时器1
  SEI(); //开中断
  }
  void Scan_DPY(void)
  {
  PORTC = 0x20;
  PORTD = (table[count/100000]&0x01)|(table[count/100000]&0x02)|(table[count/100000]&0x04)|(table[count/100000]&0x08)|((table[count/100000]&0x10)《《1)|((table[count/100000]&0x20)《《1)|((table[count/100000]&0x40)《《1);
  delay(1);
  PORTC = 0x10;
  PORTD = (table[(count0000)/10000]&0x01)|(table[(count0000)/10000]&0x02)|(table[(count% 100000)/10000]&0x04)|(table[(count0000)/10000]&0x08)|((table[(count0000)/10000]&0x10)《《1)|((table[(count0000)/10000]&0x20)《《1)|((table[(count0000)/10000]&0x40)《《1);
  delay(1);
  PORTC = 0x08;
  PORTD = (table[(count000)/1000]&0x01)|(table[(count000)/1000]&0x02)|(table[(count000)/1000]&0x04)|(table[(count000)/1000]&0x08)|((table[(count000)/1000]&0x10)《《1)|((table[(count000)/1000]&0x20)《《1)|((table[(count000)/1000]&0x40)《《1);
  delay(1);
  PORTC = 0x04;
  PORTD = (table[(count00)/100]&0x01)|(table[(count00)/100]&0x02)|(table[(count00)/100]&0x04)|(table[(count00)/100]&0x08)|((table[(count00)/100]&0x10)《《1)|((table[(count00)/100]&0x20)《《1)|((table[(count00)/100]&0x40)《《1);
  delay(1);
  PORTC = 0x02;
  PORTD = (table[(count0)/10]&0x01)|(table[(count0)/10]&0x02)|(table[(count0)/10]&0x04)|(table[(count0)/10]&0x08)|((table[(count0)/10]&0x10)《《1)|((table[(count0)/10]&0x20)《《1)|((table[(count0)/10]&0x40)《《1);
  delay(1);
  PORTC = 0x01;
  PORTD = (table[count]&0x01)|(table[count]&0x02)|(table[count]&0x04)|(table[count]&0x08)|((table[count]&0x10)《《1)|((table[count]&0x20)《《1)|((table[count]&0x40)《《1);
  delay(1);
  }
  void main(void)
  {
  init_devices();
  while(1)
  {
  Scan_DPY();
  }
  }
  
举报

更多回帖

发帖
×
20
完善资料,
赚取积分