|
下面这段程序的功能是每组数据从左向右滚动进入显示屏,保持刷新一段时间后显示下一组。我用了两个定时器,T0用作滚动进入计时,T2用作刷新计时。
可是实际T2的刷新计时比设定的长好多,不知道哪里出问题了,还有每组最后一位滚出显示屏会出现乱码的现象,这是为什么呢?
我也希望大家能不吝赐教,写出更好的代码实现该功能。
附件有源程序和仿真图。
#define F_CPU 4000000UL
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
typedef unsigned char uint8;
typedef unsigned int uint16;
const PROGMEM uint8 SEG_CODE[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92,
0x82, 0xf8, 0x80, 0x90, 0xbf, 0xff};
const PROGMEM uint8 SCAN_BITs[] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};
const PROGMEM uint8 Table_OF_Digits[][16] = {
{11, 11, 11, 11, 11, 11, 11, 11, 5, 2, 10, 2, 1, 10, 9, 0},
{11, 11, 11, 11, 11, 11, 11, 11, 9, 3, 10, 7, 5, 10, 1, 2},
{11, 11, 11, 11, 11, 11, 11, 11, 7, 7 ,10, 7, 7, 10, 7, 7}
};
uint8 row, col;
int main(void)
{
// uint8 i, j;
DDRC = 0xff; PORTC = 0x00;
DDRD = 0xff; PORTD = 0x00;
TCCR0 = 0x03; //预设分频:64
TCCR2 = 0x00; //预设分频:64
TCNT0 = 256 - F_CPU / 64.0 * 0.004;
TCNT2 = 256 - F_CPU / 64.0 * 0.004;
TIMSK = _BV(TOIE2) | _BV(TOIE0);
sei();
while(1);
}
ISR(TIMER0_OVF_vect)
{
static uint8 j = 0;
static uint8 Keep_T1 = 0;
TCNT0 = 256 - F_CPU / 64.0 * 0.004;
// PORTD = 0x00;
PORTC = 0xff;
PORTC = pgm_read_byte(&SEG_CODE[pgm_read_byte(&Table_OF_Digits[row][(col + j) % 16])]);
PORTD = pgm_read_byte(&SCAN_BITs[j]);
j = (j + 1) % 8;
if (col == 8)
{
TCCR0 = 0x00;
TCCR2 = 0x04;
}
if (col == 15)
row = (row + 1) % 3;
if (++Keep_T1 == 15)
col = (col + 1) % 16;
}
ISR(TIMER2_OVF_vect)
{
static uint8 n = 0;
static uint16 Keep_T2 = 0;
TCNT2 = 256 - F_CPU / 64.0 * 0.004;
// PORTD = 0x00;
PORTC = 0xff;
PORTC = pgm_read_byte(&SEG_CODE[pgm_read_byte(&Table_OF_Digits[row][col + n])]);
PORTD = pgm_read_byte(&SCAN_BITs[n]);
n = (n + 1) % 8;
if (++Keep_T2 != 10)
return;
Keep_T2 = 0;
TCCR0 = 0x03;
TCCR2 = 0x00;
} |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
阿莫论坛20周年了!感谢大家的支持与爱护!!
知道什么是神吗?其实神本来也是人,只不过神做了人做不到的事情 所以才成了神。 (头文字D, 杜汶泽)
|