AVR单片机频率计设计 频率输入源STM32口
完整的程序源码下载:http://www.51hei.com/f/avrplj.rar
//---------------------AVR代码-----------------------
//芯片用ATMEG16
#include
#define uchar unsigned char
#define uint unsigned int
__flash uchar seg[17]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07, //数码管码表
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x80};
__flash uchar tab[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; //数码管位选
uint i; //定时器1秒到时变量
uint tt; //计数频率
uchar start=1; //定时器启动变量
void port_init()
{
PORTA=0xff;
DDRA=0xff;
PORTB=0xff;
DDRB=0xff;
PORTC=0xff;
DDRC=0xff;
PORTD=0xff;
DDRD=0xff;
}
void delay(uint k)
{
while(k--);
}
void time0_init() //定时器0初始化
{
TCCR0=0x00; //关闭定时器
TIMSK|=0x01; //定时器0溢出中断
TCNT0=0x83; //定时1ms
SREG=0x80; //关闭总中断
}
#pragma vector = 0x24 //定时器0中断
__interrupt void tim0_ovr()
{
i++;
TCNT0=0x83;
if(i==1000) //1秒时间到
{
i=0;
TCCR1B=0x00; //停止计数
TCCR0=0x00; //停止定时
tt=TCNT1;//频率值给变量
TCNT1H=0x00;
TCNT1L=0x00; //计数清零
TCNT0=0x83; //定时器初值
start=1; //开启定时器启动变量
}
}
void time1_init()
{
TCCR1A=0x00;
TCCR1B=0x00; //停止计数
TCNT1H=0x00;
TCNT1L=0x00;
}
void show(uint dat) //频率值显示
{
PORTA=seg[dat/1000];
PORTC=tab[3];
delay(100);
PORTA=seg[dat%1000/100];
PORTC=tab[2];
delay(100);
PORTA=seg[dat%100/10];
PORTC=tab[1];
delay(100);
PORTA=seg[dat%10];
PORTC=tab[0];
delay(100);
}
void main()
{
port_init();
time0_init();
time1_init();
while(1)
{
if(start==1)
{
TCCR0=0x03; //定时器开启
TCCR1B=0x06; //计数器开启
start=0; //保证定时1秒是否开启变量
}
show(tt);
}
}
//-------------------STM32代码-------------------------
#include
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
int main()
{
Stm32_Clock_Init(9);
delay_init(72);
led_init();
while(1)
{
LED=1;
delay_ms(2);
LED=0;
delay_ms(2);
}
}
//--------------------------led.c--------------
#include
#include "led.h"
void led_init(void)
{
RCC->APB2ENR|=1<4; //使能PC时钟
GPIOC->CRH&=0xffffff0f;
GPIOC->CRH|=0x00000030;
GPIOC->ODR|=1<9;
}
//----------------------led.h-------------------------
#ifndef led_h
#define led_h
#include "sys.h"
#define LED PCout(9)
void led_init(void);
#endif
AVR单片机频率计STM3 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)