pic 16f chỉ cho khai báo một mảng ram dài tối đa 96 byte.
thường thì matrix phải liên tục cập nhập dữ liệu người ta dùng thêm ram ngoài.
Code:
#include <16f877a.h>
#device 16f877a*=16 ADC=10
#include <stdlib.h>
#include <string.h>
#fuses nowdt,noprotect,nolvp,hs,put
#use delay(clock=20000000)
#use fast_io(b)
#use fast_io(c)
#use fast_io(d)
#use rs232(baud=9600,parity=N,xmit=pin_c6,rcv=pin_c7)
#byte porta=0x05
#byte portb=0x06
#byte portc=0x07
#byte portd=0x08
#bit b0=portb.0
#bit b1=portb.1
#bit b2=portb.2
#bit b3=portb.3
#bit b4=portb.4
#bit b5=portb.5
int dich_chu=0,toc_doc_chay=0;
void doc_adc();
void lay_font(char * textptr);
unsigned char bi,temp,chieu_dai=0;
unsigned char hien_thi[96];
char string[5];
char string1[5];
char string2[5];
char string3[10]="nhiet do:#";
const BYTE TEXT[51][5] ={0x00, 0x00, 0x00, 0x00, 0x00, // SPACE
0x00, 0x00, 0x5F, 0x00, 0x00, // !
0x00, 0x03, 0x00, 0x03, 0x00, // "
0x14, 0x3E, 0x14, 0x3E, 0x14, // #
0x24, 0x2A, 0x7F, 0x2A, 0x12, // $
0x43, 0x33, 0x08, 0x66, 0x61, // %
0x36, 0x49, 0x55, 0x22, 0x50, // &
0x00, 0x05, 0x03, 0x00, 0x00, // '
0x00, 0x1C, 0x22, 0x41, 0x00, // (
0x00, 0x41, 0x22, 0x1C, 0x00, // )
0x14, 0x08, 0x3E, 0x08, 0x14, // *
0x08, 0x08, 0x3E, 0x08, 0x08, // +
0x00, 0x50, 0x30, 0x00, 0x00, // ,
0x08, 0x08, 0x08, 0x08, 0x08, // -
0x00, 0x60, 0x60, 0x00, 0x00, // .
0x20, 0x10, 0x08, 0x04, 0x02, // /
0x3E, 0x51, 0x49, 0x45, 0x3E, // 0
0x04, 0x02, 0x7F, 0x00, 0x00, // 1
0x42, 0x61, 0x51, 0x49, 0x46, // 2
0x22, 0x41, 0x49, 0x49, 0x36, // 3
0x18, 0x14, 0x12, 0x7F, 0x10, // 4
0x27, 0x45, 0x45, 0x45, 0x39, // 5
0x3E, 0x49, 0x49, 0x49, 0x32, // 6
0x01, 0x01, 0x71, 0x09, 0x07, // 7
0x36, 0x49, 0x49, 0x49, 0x36, // 8
0x26, 0x49, 0x49, 0x49, 0x3E, // 9
0x00, 0x36, 0x36, 0x00, 0x00, // :
0x00, 0x56, 0x36, 0x00, 0x00, // ;
0x08, 0x14, 0x22, 0x41, 0x00, // <
0x14, 0x14, 0x14, 0x14, 0x14, // =
0x00, 0x41, 0x22, 0x14, 0x08, // >
0x02, 0x01, 0x51, 0x09, 0x06, // ?
0x3E, 0x41, 0x59, 0x55, 0x5E, // @
0x7E, 0x09, 0x09, 0x09, 0x7E, // A
0x7F, 0x49, 0x49, 0x49, 0x36, // B
0x3E, 0x41, 0x41, 0x41, 0x22, // C
0x7F, 0x41, 0x41, 0x41, 0x3E, // D
0x7F, 0x49, 0x49, 0x49, 0x41, // E
0x7F, 0x09, 0x09, 0x09, 0x01, // F
0x3E, 0x41, 0x41, 0x49, 0x3A, // G
0x7F, 0x08, 0x08, 0x08, 0x7F, // H
0x00, 0x41, 0x7F, 0x41, 0x00, // I
0x30, 0x40, 0x40, 0x40, 0x3F, // J
0x7F, 0x08, 0x14, 0x22, 0x41, // K
0x7F, 0x40, 0x40, 0x40, 0x40, // L
0x7F, 0x02, 0x0C, 0x02, 0x7F, // M
0x7F, 0x02, 0x04, 0x08, 0x7F, // N
0x3E, 0x41, 0x41, 0x41, 0x3E, // O
0x7F, 0x09, 0x09, 0x09, 0x06, // P
0x1E, 0x21, 0x21, 0x21, 0x5E, // Q
0x7F, 0x09, 0x09, 0x09, 0x76};// R
const BYTE TEXT2[44][5]={0x26, 0x49, 0x49, 0x49, 0x32, // S
0x01, 0x01, 0x7F, 0x01, 0x01, // T
0x3F, 0x40, 0x40, 0x40, 0x3F, // U
0x1F, 0x20, 0x40, 0x20, 0x1F, // V
0x7F, 0x20, 0x10, 0x20, 0x7F, // W
0x41, 0x22, 0x1C, 0x22, 0x41, // X
0x07, 0x08, 0x70, 0x08, 0x07, // Y
0x61, 0x51, 0x49, 0x45, 0x43, // Z
0x00, 0x7F, 0x41, 0x00, 0x00, // [
0x02, 0x04, 0x08, 0x10, 0x20, // \
0x00, 0x00, 0x41, 0x7F, 0x00, // ]
0x04, 0x02, 0x01, 0x02, 0x04, // ^
0x40, 0x40, 0x40, 0x40, 0x40, // _
0x00, 0x01, 0x02, 0x04, 0x00, // `
0x20, 0x54, 0x54, 0x54, 0x78, // a
0x7F, 0x44, 0x44, 0x44, 0x38, // b
0x38, 0x44, 0x44, 0x44, 0x44, // c
0x38, 0x44, 0x44, 0x44, 0x7F, // d
0x38, 0x54, 0x54, 0x54, 0x18, // e
0x04, 0x04, 0x7E, 0x05, 0x05, // f
0x08, 0x54, 0x54, 0x54, 0x3C, // g
0x7F, 0x08, 0x04, 0x04, 0x78, // h
0x00, 0x44, 0x7D, 0x40, 0x00, // i
0x20, 0x40, 0x44, 0x3D, 0x00, // j
0x7F, 0x10, 0x28, 0x44, 0x00, // k
0x00, 0x41, 0x7F, 0x40, 0x00, // l
0x7C, 0x04, 0x78, 0x04, 0x78, // m
0x7C, 0x08, 0x04, 0x04, 0x78, // n
0x38, 0x44, 0x44, 0x44, 0x38, // o
0x7C, 0x14, 0x14, 0x14, 0x08, // p
0x08, 0x14, 0x14, 0x14, 0x7C, // q
0x00, 0x7C, 0x08, 0x04, 0x04, // r
0x48, 0x54, 0x54, 0x54, 0x20, // s
0x04, 0x04, 0x3F, 0x44, 0x44, // t
0x3C, 0x40, 0x40, 0x20, 0x7C, // u
0x1C, 0x20, 0x40, 0x20, 0x1C, // v
0x3C, 0x40, 0x30, 0x40, 0x3C, // w
0x44, 0x28, 0x10, 0x28, 0x44, // x
0x0C, 0x50, 0x50, 0x50, 0x3C, // y
0x44, 0x64, 0x54, 0x4C, 0x44, // z
0x00, 0x08, 0x36, 0x41, 0x41, // {
0x00, 0x00, 0x7F, 0x00, 0x00, // |
0x41, 0x41, 0x36, 0x08, 0x00, // }
0x02, 0x01, 0x02, 0x04, 0x02};// ~
int8 i,m,t,flag,dem;
int8 n;
char mang[];
void dich(void);
void xuat(unsigned char bi);
void xoa_hien_thi();
#INT_RDA
void ngat()
{
mang[dem]=getc();
if(mang[dem]=='#')
{
flag=1;//co ngat
dem=0;
}
else{
dem++;
}
}
void main()
{int x=0;
int dv,chuc,tram;
enable_interrupts(INT_RDA);
enable_interrupts(global);
set_tris_b(0b00000000);
set_tris_d(0b00000000);
set_tris_a(0b000001);
int dich_chu=0,toc_do_chay=0;
while(1)
{
doc_adc();
x=read_adc();
x=x/2.049;
dv=x%10;
chuc=x/10;
itoa(dv,10,string);//gan dv vao string
itoa(chuc,10,string1);//gan chuc vao string1
strcat(string1,string);//ket hop string1 va string hien thi la string1
xoa_hien_thi();//xóa vùng nho hien thi truoc khi cap nhat nhiet do
lay_font(string3); // Write character set to matrix
lay_font(string1); // Write character set to matrix
for(dich_chu=0;dich_chu<=32;dich_chu++)
{
for(toc_do_chay=0;toc_do_chay<=2;toc_do_chay++)
{
b1=1;
b0=1;
b2=1;
b0=0;
b2=0;
for(m=0;m<=63;m++)
{
n=hien_thi[m+dich_chu];
n=~n;
xuat(n);
delay_us(1000);
dich();
}
}
}
}
}
void doc_adc()
{
setup_ADC(ADC_clock_internal);
setup_ADC_ports(AN0);
set_ADC_channel(0);
delay_us(1000);
}
void dich()
{
b1=0;
b0=1;
b0=0;
b2=1;
b2=0;
}
void xuat(unsigned char temp)
{
#bit flag_bit = temp.7
b4=0;
for(i=0;i<=7;i++)
{
if(flag_bit==1)
{b4=1;}
else
{b4=0;}
b3=1;
b3=0;
temp<<=1;
}
b5=1;
b5=0;
}
void lay_font(char * textptr)
{
int i, j;//
BYTE pixelData[5]; // Stores character data
for(i=0; textptr[i] != '\0'; ++i ) // Loop through the passed string
{
if(textptr[i] == '#')//textptr khai bao vi tri con tro
break;
if(textptr[i] < 'S') //ky tu nam o mang text
memcpy(pixelData, TEXT[textptr[i]-' '], 5);//copy 5 byte gia tri text[textptr[i]-'' vao pixeldata
else if(textptr[i] <= '~') // ky tu nam o mang text2
memcpy(pixelData, TEXT2[textptr[i]-'S'], 5);//copy 5 byte gia tri text2[textptr[i]-'s' vao pixeldata
else
memcpy(pixelData, TEXT[0], 5); // khoang trang
for(j=0;j<=4;j++)
{
hien_thi[chieu_dai]=(pixelData[j]);//hien thi tung ky tu
chieu_dai++;
}
}
}
void xoa_hien_thi(void)
{
chieu_dai=0;
for(m=0;m<=96;m++)
{
hien_thi[m]=0x00;
}
}