该实验所用的点阵屏为金涵电子公司的点阵屏, 该屏幕内置控制卡,并且已经做好了相关的控制协议, 所以对于51单片机就是把相关的协议指令通过串口发送给该控制卡,即可显示对应的文本。 本实验以显示“1234”为例,代码如下
#include "reg52.h" #include "string.h" #define uchar unsigned char #define uint unsigned int #define CTRL 2 #define CNT 42 sbit key1 = P3^4; sbit led1 = P1^1; char dat; int flag; uchar sendData(char *string); uchar request[] = { 0xAA, 0xA5 ,0x2C, 0x00, 0x01, 0x01, 0x12, 0x34, 0xB0, 0xA1, 0x10, 0x03, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x14, 0x00, 0xE8, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14, 0x00, 0xFF, 0xFF, 0x04, 0x00, 0xB0, 0xA1, 0xF7, 0xFE, 0x00 ,0x00, 0x5A, 0x55 }; void TimerInit(); uchar sendData(uchar *date) { int i,j; int count=0; int sum=0; uchar ctrl[2]={0}; uchar cnt[2]={0}; uchar text[8]={0}; while((*date) != '\0' ) { text[count++] = *date; // sum = sum *10 +((*date) - 48); date++; } sum = 40+count; ctrl[0] = sum; cnt[0] = count; for(i=44,j=0; i<count+44; i++,j++) { request = text[j]; } request[CTRL] = sum; request[CNT] = count; for(i=0; i<52; i++) { ES = 0; SBUF = request; while(!TI); TI = 0; ES = 1; } return 0; } void TimerInit() { TMOD = 0x20; TH1 = 0xfd; TL1 = 0xfd; TR1 = 1; REN = 1; SM0 = 0; SM1 = 1; EA = 1; ES = 1; } void delay(uint xms) { uint i,j; for(i=xms; i>0; i--) for(j=110; j>0; j--); } void main() { int i; led1 = 1; P0 = 0; TimerInit(); while(1) { if(flag == 1) { flag = 0; sendData("1234"); } /*if(key1 == 0) { delay(10); if(key1==0) { flag = 0; //sendData("1"); for(i=0; i<52; i++) { ES = 0; SBUF = re; while(!TI); TI = 0; ES = 1; } } }*/ } } void timer0() interrupt 4 { RI = 0; dat = SBUF; flag = 1; }
|