包含畫點、畫直線、畫矩形、畫圓函數等,非常實用。
#include <w78e052.h> #include <intrins.h> #include "ASCII.h" #include"math.h"
#define uchar unsigned char #define uint unsigned int #define NOP _nop_() typedef unsigned char u8; typedef unsigned int u16; typedef unsigned char u32;
sbit OE = P0^2; sbit A1 = P0^3; sbit B1 = P0^4;
sbit SHCP=P0^5; //11腳SHCP 移位時鐘 sbit STCP=P0^6; //12腳STCP 鎖存時鐘 sbit DATA=P0^7; //數據輸入
sbit BEEP=P1^1;
unsigned char ip,tp,min,sec; u16 time; unsigned char PLAYBUF[64];
u8 code yi_dong[31] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,12,11,10,9,8,7,6,5,4,3,2,1,0}; u8 code tab[10] = {"0123456789"};
const uchar code table[160]= { 0x10,0x00,0x08,0x0E,0x7F,0x70,0x22,0x40,0x14,0x40,0xFF,0x7E,0x08,0x48,0x08,0x48, 0xFF,0x48,0x08,0x48,0x2C,0x48,0x2A,0x88,0x4A,0x88,0x89,0x08,0x2A,0x08,0x10,0x08, 0x00,0x00,0x00,0x40,0x78,0x40,0x0B,0xF8,0x10,0x48,0x17,0xFE,0x20,0x48,0x7B,0xF8, 0x08,0x40,0x4B,0xFC,0x48,0x40,0x28,0x40,0x17,0xFC,0x28,0x40,0x46,0x40,0x81,0xFE, /*------------------------------------------------------------------------------ ; 源文件 / 文字 : 工 ; 寬×高(像素): 15× ------------------------------------------------------------------------------*/ 0x00,0x00,0x00,0x00,0x3F,0xFC,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00, 0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0xFF,0xFE,0x00,0x00, /*------------------------------------------------------------------------------ ; 源文件 / 文字 : 作 ; 寬×高(像素): 15×16 ------------------------------------------------------------------------------*/ 0x08,0x80,0x0C,0x80,0x09,0x00,0x13,0xFE,0x12,0x80,0x34,0x88,0x50,0xFC,0x90,0x80, 0x10,0x80,0x10,0x84,0x10,0xFE,0x10,0x80,0x10,0x80,0x10,0x80,0x10,0x80,0x10,0x80, /*------------------------------------------------------------------------------ ; 源文件 / 文字 : 室 ; 寬×高(像素): 15×16 ------------------------------------------------------------------------------*/ 0x00,0x00,0x02,0x00,0x01,0x00,0x3F,0xFE,0x20,0x04,0x40,0x08,0x3F,0xFC,0x04,0x00, 0x08,0x20,0x1F,0xF0,0x01,0x10,0x01,0x00,0x3F,0xF8,0x01,0x00,0x01,0x00,0xFF,0xFE };
/*******************************************************************************************/ /*******************************************************************************************/ /*******************************************************************************************/ //數據串行輸入 void serial_input(uchar dat) { uint m; for(m=0;m<8;m++) { if(dat & 0x80) DATA=0; else DATA=1; SHCP=0; SHCP=1; NOP; NOP; SHCP=0; NOP; NOP; dat=dat<<1; } }
void delay() { unsigned int i = 50000; while(i--); } /*******************************************************************************************/ //并出 void serial_output() { STCP=0; STCP=1; } /*******************************************************************************************/ /*******************************************************************************************/ //行顯控制 void HC138_scan(uchar temp) { OE=1; A1=0x01 & temp; B1=0x01 &(temp>>1); } /*******************************************************************************************/ /*******************************************************************************************/ //整屏顯示 void display() { uint i,j,k; unsigned char buf; serial_output(); for(k=0;k<4;k++) //顯示的四行 { for(j=0;j<2;j++) //顯示3、4列 { for(i=0;i<2;i++) // 顯示1、2列 { buf = PLAYBUF[24+(k<<1)+i+(j<<5)]; serial_input(buf); buf = PLAYBUF[16+(k<<1)+i+(j<<5)]; serial_input(buf); buf = PLAYBUF[8+(k<<1)+i+(j<<5)]; serial_input(buf); buf = PLAYBUF[(k<<1)+i+(j<<5)]; serial_input(buf); } } serial_output(); HC138_scan(k); } }
void point(unsigned char x,unsigned char y,bit c) { unsigned char i;
if(x>=32) return; if(y>=16) return; i=0x80; i>>=x-((x>>3)<<3);
if(c) PLAYBUF[(y<<1)+((x>>4)<<5)+((x>>3)&0x01)] |= i; else PLAYBUF[(y<<1)+((x>>4)<<5)+((x>>3)&0x01)] &= ~i; } //畫線 void line(u8 x1, u8 y1, u8 x2, u8 y2, bit color) { u8 x, y, t; if((x1 == x2) && (y1 == y2)) point(x1, y1, color); else if(fabs(y2 - y1) > fabs(x2 - x1)) { if(y1 > y2) { t = y1; y1 = y2; y2 = t; t = x1; x1 = x2; x2 = t; } for(y = y1; y <= y2; y ++) { x = (y - y1) * (x2 - x1) / (y2 - y1) + x1; point(x, y, color); } } else { if(x1 > x2) { t = y1; y1 = y2; y2 = t; t = x1; x1 = x2; x2 = t; } for(x = x1; x <= x2; x ++) { y = (x - x1) * (y2 - y1) / (x2 - x1) + y1; point(x, y, color); } } }
void rectangle(u8 x1,u8 y1,u8 x2,u8 y2, bit color) { line(x1,y1,x2,y1, color); line(x1,y1,x1,y2, color); line(x1,y2,x2,y2, color); line(x2,y1,x2,y2, color); } void filled_rectangle(u8 x1,u8 y1,u8 x2,u8 y2, bit color) { u8 i; for(i=y1;i<=y2;i++) { line(x1,i,x2,i, color); } } //在指定位置畫一個指定大小的圓 //(x,y):中心點 //r :半徑 void circle(u8 x0,u8 y0,u8 r, bit color) { int a,b; int di; a=0; b=r; di=3-2*r; //判斷下個點位置的標志 while(a<=b) { point(x0-b,y0-a, color); //3 point(x0+b,y0-a, color); //0 point(x0-a,y0+b, color); //1 point(x0-b,y0-a, color); //7 point(x0-a,y0-b, color); //2 point(x0+b,y0+a, color); //4 point(x0+a,y0-b, color); //5 point(x0+a,y0+b, color); //6 point(x0-b,y0+a, color); a++; /***使用Bresenham算法畫圓**/ if(di<0)di +=4*a+6; else { di+=10+4*(a-b); b--; } point(x0+a,y0+b, color); } }
void load_playbuff(unsigned char *p) { unsigned char i; for(i=0;i<64;i++) { PLAYBUF = *p++; } }
void clear_playbuff(unsigned char n) { unsigned char i; for(i=0;i<64;i++) { PLAYBUF = n; } }
unsigned char op; /*******************************************************************************************/ /*******************************************************************************************/
void put_char(u8 x,u8 y,u8 c,bit color) { unsigned char tmp_char=0,i,j;
for (i=0;i<16;i++) { tmp_char=AsciiLib[((c-0x20)*16)+i]; for (j=0;j<8;j++) { if ( (tmp_char >> 7-j) & 0x01 == 0x01) { point(x+j,y+i,color); // 字符顏色 } else { point(x+j,y+i,!color); // 背景顏色 } } } }
void put_chinese(u8 x,u8 y,u8 *c,bit color) { u8 i=0; u8 j=0; u8 tmp_char=0; u8 tmp_char2=0;
for (i=0;i<16;i++) { tmp_char=c[i*2]; tmp_char2=c[i*2+1]; for (j=0;j<8;j++) { if ( (tmp_char >> 7-j) & 0x01 == 0x01) { point(x+j,y+i,color); // 字符顏色 } else { point(x+j,y+i,!color); // 背景顏色 } if ( (tmp_char2 >> 7-j) & 0x01 == 0x01) { point(x+j+8,y+i,color); // 字符顏色 } else { point(x+j+8,y+i,!color); // 背景顏色 } } } }
void display_move(unsigned int c) { unsigned char i; for(i=0;i<16;i++) { PLAYBUF[i*2] = (PLAYBUF[i*2]<<1)+(PLAYBUF[i*2+1]>>7); PLAYBUF[i*2+1] = (PLAYBUF[i*2+1]<<1)+(PLAYBUF[i*2+32]>>7);
PLAYBUF[i*2+32] = (PLAYBUF[i*2+32]<<1)+(PLAYBUF[i*2+1+32]>>7); PLAYBUF[i*2+32+1] = (PLAYBUF[i*2+32+1]<<1)+((c>>i)&0x01); }
}
/*******************************************************************************************/ /*******************************************************************************************/ void main() { unsigned int i=1000; BEEP=0; P0=0x00; TMOD = 0x1; TH0=0xfc; TL0=0x18;//12Mhz ET0 = 1; TR0 = 1; EA = 1; min=0; sec=0; time=0; rectangle(0,0,31,15,1); circle(15,8,4,1); while(i--) { display(); } //load_playbuff(&table[2*32]); put_char(0,0,tab[min/10%10],1); put_char(8,0,tab[min%10],1); point(15,6,1); point(15,9,1); point(16,6,1); point(16,9,1); put_char(16,0,tab[sec/10%10],1); put_char(24,0,tab[sec%10],1); while(1) { display(); if(time>=1000) { time=0; sec++; if(sec>=60) { sec=0; min++; if(min>=60) { min=0; } put_char(0,0,tab[min/10%10],1); put_char(8,0,tab[min%10],1); } put_char(16,0,tab[sec/10%10],1); put_char(24,0,tab[sec%10],1); point(15,5,1); point(15,10,1); point(16,5,1); point(16,10,1);
point(15,6,1); point(15,9,1); point(16,6,1); point(16,9,1); } } }
/*******************************定時器中斷**************************/ void OSInterrupt(void) interrupt 1 { TMOD=0x1; TH0=0xfc; TL0=0x18;//12Mhz,1ms_delay time++; } /*******************************結束********************************/
|