LCD1602定时计数测试
单片机源程序如下:
- #include<iom16v.h>
- #include <macros.h>
- #include "LCD1602.h"
- #define uchar unsigned char
- #define uint unsigned int
- uchar num;
- // IO端口定义
- void IO_init()
- {
- DDRA = 0XFF;
- DDRB = 0XFF;
- DDRC = 0XFF;
- DDRD = 0XFF;
-
- PORTA = 0X00;
- PORTB = 0X00;
- PORTC = 0X00;
- PORTD = 0XFF;
- }
- //延时函数
- void delayms(uint z) //8M晶振下,延时1ms
- {
- uint x,y;
- for(x=z;x>0;x--)
- for(y=1333;y>0;y--);
- }
- void timer1_init()//定时器1初始化
- {
- TCCR1B = 0x04; //T/C1控制寄存器 设置256分频
- TCNT1H = 0x85; //计数寄存器 设置初值 H (65536-(8M/256)=34286=85EE)
- TCNT1L = 0xEE; //计数寄存器 设置初值 L (34286:十进制 85EE:十六进制)
- TIMSK |=BIT(2);//T/C1中断屏蔽寄存器 设置定时器1中断
- SREG|=BIT(7); //开启总中断
- }
- void display()
- {
- LCD_write_char(0,1,0x30+num/10);
- LCD_write_char(1,1,0x30+num%10);
- }
- void main()
- {
- IO_init();
- timer1_init();//定时器1初始化
- LCD_init(); //LCD初始化
- LCD_clear();//LCD清屏
- while(1)
- {
- display();
- }
- }
- #pragma interrupt_handler timer1_isr:9
- void timer1_isr()
- {
- num++;
- if(num==60)
- {
- num=0;
- }
- TCNT1H = 0x85;
- TCNT1L = 0xEE;
- }
复制代码
所有资料51hei提供下载:
LCD1602定时计数测试.zip
(26.93 KB, 下载次数: 23)
|