PIC单片机利用视觉暂留现象显示字符

Persistance of Vision (PoV), 即视觉暂留现象, 人眼在观察景物时,光信号传人大脑神经,需经过一段短暂的时间,光的作用结束后,视觉形象并不立即消失,这种残留的视觉称“后像”,视觉的这一现象则被称为“视觉暂留”。其具体应用是电影的拍摄和放映。原因是由视神经的反应速度造成的.其时值是二十四分之一秒。是动画、电影等视觉媒体形成和传播的根据。

利用PoV,还可以制作电子小玩意,比如利用几个LED,就能显示图案,字符等。

下面是一个利用PoV显示时钟的例子:

 

下面是一段利用PoV显示字符的PIC单片机程序,利用CCS C编译器编译:


主程序文件: pov.c

#include <16F877A.h>
#include "font.h"
#include "font.c"

#fuses HS,NOWDT,NOPROTECT,NOLVP /* DONNOT use WDT */

/* Port D drives the LEDs */
#use fast_io(D)

#use delay(clock = 4000000) /* the crystal frequency is 4.000 MHz*/

void display_string(int8 *str)
{
   int8 font_matrix_buf[FONT_WIDTH];
   int8 i, j;
  
   for(i = 0; str[i] != 0; i++)
   {
      get_font_matrix(str[i], font_matrix_buf);
       for(j = 0; j < FONT_WIDTH; j++)
      {
         output_d(font_matrix_buf[j]); /* output the dots */
         delay_ms(5); /* delay some time */
      }
      output_d(0x00);
      delay_ms(5);
   }
}

/**********************************************************
Description:
   The entry point of the whole program.
Parameters:
   None.
Return Value:
   None.
Note:
   N/A.
Date:
   2006-04-02
**********************************************************/
void main( void)
{
   int8 string[]= {'C', 'h', 'i', 'n', 'a', 0x00};

   set_tris_d(0x00);   /* set portD to output mode */

   while(1)
   {
      display_string( string);
      delay_ms(200);
   }
}



字模头文件: font.h

#ifndef FONT_H
#define FONT_H

#define FONT_WIDTH 5

/*
CCS C doesn't support double-byte array subscript. so we must put the font table
in different arrays.
*/
#define FONT_BLANK_TABLE_SIZE (1 * FONT_WIDTH)
#define FONT_NUMBER_TABLE_SIZE (10 * FONT_WIDTH)
#define FONT_UPPER_CASE_TABLE_SIZE (26 * FONT_WIDTH)
#define FONT_LOWER_CASE_TABLE_SIZE (26 * FONT_WIDTH)

void get_font_matrix(int8 ascii_code, int8 *buf);


#endif



字模c文件: font.c

/********************************************************************
*8*8ASC码字模头文件
*右转90度ASC字模
*Modify:2005-3-16 14:31
********************************************************************/

const int8 font_blank_table[FONT_BLANK_TABLE_SIZE] = {
0x00,0x00,0x00,0x00,0x00 //    blank
};

const int8 font_number_table[FONT_NUMBER_TABLE_SIZE] = {
0x7C,0xA2,0x92,0x8A,0x7C, //    0
0x00,0x84,0xFE,0x80,0x00, //    1
0xC4,0xA2,0x92,0x8A,0x84, //    2
0x42,0x82,0x8A,0x96,0x62, //    3
0x30,0x28,0x24,0xFE,0x20, //    4
0x4E,0x8A,0x8A,0x8A,0x72, //    5
0x78,0x94,0x92,0x92,0x60, //    6
0x02,0xE2,0x12,0x0A,0x06, //    7
0x6C,0x92,0x92,0x92,0x6C, //    8
0x0C,0x92,0x92,0x52,0x3C //    9
};

const int8 font_uletter_table[FONT_UPPER_CASE_TABLE_SIZE] = {
0xFC,0x22,0x22,0x22,0xFC, //    A
0xFE,0x92,0x92,0x92,0x6C, //    B
0x7C,0x82,0x82,0x82,0x44, //    C
0xFE,0x82,0x82,0x44,0x38, //    D
0xFE,0x92,0x92,0x92,0x82, //    E
0xFE,0x12,0x12,0x12,0x02, //    F
0x7C,0x82,0x92,0x92,0x74, //    G
0xFE,0x10,0x10,0x10,0xFE, //    H
0x00,0x82,0xFE,0x82,0x00, //    I
0x40,0x80,0x82,0x7E,0x02, //    J
0xFE,0x10,0x28,0x44,0x82, //    K
0xFE,0x80,0x80,0x80,0x80, //    L
0xFE,0x04,0x18,0x04,0xFE, //    M
0xFE,0x08,0x10,0x20,0xFE, //    N
0x7C,0x82,0x82,0x82,0x7C, //    O
0xFE,0x12,0x12,0x12,0x0C, //    P
0x7C,0x82,0xA2,0x42,0xBC, //    Q
0xFE,0x12,0x32,0x52,0x8C, //    R
0x4C,0x92,0x92,0x92,0x64, //    S
0x02,0x02,0xFE,0x02,0x02, //    T
0x7E,0x80,0x80,0x80,0x7E, //    U
0x3E,0x40,0x80,0x40,0x3E, //    V
0x7E,0x80,0x70,0x80,0x7E, //    W
0xC6,0x28,0x10,0x28,0xC6, //    X
0x0E,0x10,0xE0,0x10,0x0E, //    Y
0xC2,0xA2,0x92,0x8A,0x86 //    Z
};

const int8 font_lletter_table[FONT_LOWER_CASE_TABLE_SIZE] = {
0x40,0xA8,0xA8,0xA8,0xF0, //    a
0xFE,0x88,0x88,0x88,0x70, //    b
0x70,0x88,0x88,0x88,0x88, //    c
0x70,0x88,0x88,0x88,0xFE, //    d
0x70,0xA8,0xA8,0xA8,0x10, //    e
0x08,0x08,0xFC,0x0A,0x0A, //    f
0x10,0xA8,0xA8,0xA8,0x78, //    g
0xFE,0x10,0x08,0x08,0xF0, //    h
0x00,0x88,0xFA,0x80,0x00, //    i
0x40,0x80,0x88,0x7A,0x00, //    j
0xFE,0x20,0x50,0x88,0x00, //    k
0x00,0x82,0xFE,0x80,0x00, //    l
0xF8,0x08,0xF8,0x08,0xF0, //    m
0xF8,0x10,0x08,0x08,0xF0, //    n
0x70,0x88,0x88,0x88,0x70, //    o
0xF8,0x28,0x28,0x28,0x10, //    p
0x10,0x28,0x28,0x28,0xF8, //    q
0xF8,0x10,0x08,0x08,0x00, //    r
0x90,0xA8,0xA8,0xA8,0x48, //    s
0x08,0x08,0x7E,0x88,0x88, //    t
0x78,0x80,0x80,0x40,0xF8, //    u
0x38,0x40,0x80,0x40,0x38, //    v
0x78,0x80,0x60,0x80,0x78, //    w
0x88,0x50,0x20,0x50,0x88, //    x
0x18,0xA0,0xA0,0xA0,0x78, //    y
0x88,0xC8,0xA8,0x98,0x88 //    z
};

void get_font_matrix(int8 ascii_code, int8 *buf)
{
   int8 i;
   int8 base;

   if(ascii_code == ' ')
   {
       for(i=0, base=0; i < FONT_WIDTH; i++)
      {
         *(buf+i) = font_blank_table[ base + i];
      }
   }
   else if(ascii_code >= '0' && ascii_code <= '9')
   {
       for(i=0, base = (ascii_code - '0') * FONT_WIDTH; i < FONT_WIDTH; i++)
      {
         *(buf+i) = font_number_table[ base + i];
      }
   }
   else if(ascii_code >= 'A' && ascii_code <= 'Z')
   {
       for(i=0, base = (ascii_code - 'A') * FONT_WIDTH; i < FONT_WIDTH; i++)
      {
         *(buf+i) = font_uletter_table[ base + i];
      }
   }
   else
   {
       for(i=0, base = (ascii_code - 'a') * FONT_WIDTH; i < FONT_WIDTH; i++)
      {
         *(buf+i) = font_uletter_table[ base + i];
      }
   }
}

 

这段程序会在PIC单片机的Port D点亮一排LED,用手晃动这排LED,就会看到“china”字样。


Freelancer.com - 排名第一的软件兼职工作网站
兼职编程、开发, C/C++/C#, PHP, Web开发, ASP, 外包项目, 接国外私活, 程序员兼职, 外包开发, 国外项目外包, 网站外包项目
 

你可能感兴趣的:(单片机,pic,视觉暂留,PoV,显示字符)