📄 main._c
字号:
/*---------------------------------------------------------------
工程名称:1602液晶演示频率计
编译器类型及版本:ICCAVR 6.9
设计者:余威明
设计日期:2007.08.09
芯片类型:ATmega16L
时钟频率:4M外部时钟
硬件接口说明:MCU ~ LCD
PORTD ~ dataport对应1602的d0~d7
PA1 ~ RS
PA2 ~ RW
PA3 ~ E
---------------------------------------------------------------*/
#include <iom16v.h>
#include <macros.h>
#include "lcd1602.h"
void hextoasc(void); //十六进制转ASCII码
void timer1_init(void); //定时器1初始化
void port_init(void); //端口初始化
void init_devices(void);
//显示的两行字符
unsigned char str00[]=" author:ancient"; //液晶屏初始显示内容
unsigned char str01[]=" time:2008/08/09";
unsigned char str1[]=" frequency meter";
unsigned char str2[]=" F:000HZ "; //频率显示形式
unsigned int count; //外部脉冲计数值
unsigned char tim0_ovf_count; //t0溢出次数
//在LCD上显示两行字符
void main(void)
{
init_devices(); //初始化
display_a_string(0,str00); //开机界面
delay_nms(500);
display_a_string(1,str01);
delay_nms(1000);
lcd_write_command(0x01,1);//显示清屏
delay_nms(200);
display_a_string(0,str1); //显示“frequncy meter”
delay_nms(200);
while(1)
{
hextoasc(); //十六进制转ASCII码
display_a_string(1,str2);
delay_nms(200);
}
}
//十六进制转ASCII码,为LCD显示作准备
void hextoasc(void)
{
if(count>=1000) //检测数值大于1000HZ,溢出
{
str2[6] ='O'; //显示"OVF"
str2[7] ='V';
str2[8] ='F';
PORTC|=(1<<PC0);//PC0置1,发光管亮
}
else
{
str2[6]=count/100+0x30;
str2[7]=count%100/10+0x30;
str2[8]=count%10+0x30;
PORTC&=~(1<<PC0); //PC0清0,发光管灭
}
}
void port_init(void)
{
PORTA = 0x00;
DDRA = 0xFF;
PORTB = 0x01;
DDRB = 0xFE;
PORTC = 0x00; //m103 output only
DDRC = 0x01;
PORTD = 0xff;
DDRD = 0x00;
lcd_data_port=0xff;
lcd_data_ddr=0xff;
lcd_control_port=0x07;
lcd_control_ddr=0x07;
}
//TIMER1 initialize - prescale:64
// WGM: 0) Normal, TOP=0xFFFF
// desired value: 1Hz
// actual value: 1.000Hz (0.0%)
void timer1_init(void)
{
TCCR1B = 0x00; //stop
TCNT1H = 0x0B; //setup
TCNT1L = 0xDC;
TCCR1A = 0x00;
TCCR1B = 0x03; //start Timer
}
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
timer1_init();
TCCR0=0x06;
TCNT0=0x00;
count=0x00;
tim0_ovf_count=0x00;
timer1_init();
lcd_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x05; //timer interrupt sources
SEI(); //re-enable interrupts
}
#pragma interrupt_handler timer0_ovf_isr:iv_TIM0_OVF
void timer0_ovf_isr(void)
{
TCNT0 = 0x00 /*INVALID SETTING*/; //reload counter value
tim0_ovf_count++;
}
#pragma interrupt_handler timer1_ovf_isr:iv_TIM1_OVF
void timer1_ovf_isr(void)
{
//TIMER1 has overflowed
TCNT1H = 0x0B; //reload counter high value
TCNT1L = 0xDC; //reload counter low value
count=tim0_ovf_count*256+TCNT0;
tim0_ovf_count=0x00;
TCNT0=0x00;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -