|
想做一個(gè)電子名牌,上位機(jī)端大致已經(jīng)弄好,現(xiàn)在單片機(jī)端程序還有問題。程序首先讀取eeprom的數(shù)據(jù)存儲(chǔ)到buffer數(shù)組中,主函數(shù)初始化串口,根據(jù)標(biāo)志位決定是否擦除扇區(qū),并將通過串口接受的數(shù)據(jù)存入eeprom,實(shí)質(zhì)就是看串口有無數(shù)據(jù)進(jìn)來,沒有就顯示原本存儲(chǔ)的數(shù)據(jù),標(biāo)志位是一個(gè)全局變量,由中斷程序控制值。代碼我貼在下面,目前就是使用isp打開串口后,顯示會(huì)短暫消失一下,進(jìn)入了while(1)循環(huán),發(fā)送數(shù)據(jù)不起作用,有沒有大神解答一下問題。
單片機(jī)源程序如下:
#include <stc15f2k60s2.h>
#include "eeprom.h"
#define uchar unsigned char
#define uint unsigned int
/* 點(diǎn)陣顯示宏定義 */
sbit KEY = P3 ^ 2;
sbit T_STR = P3 ^ 7; // 鎖存引腳
sbit T_IO = P1 ^ 4; // 數(shù)據(jù)引腳
sbit T_CLK = P3 ^ 6; // 時(shí)鐘引腳
sbit T_OE = P3 ^ 5;
uchar row = 0, col;
uchar xdata receivedData[16][8];
uchar xdata buffer[256];
uint flag = 0;
uint count = 0;
uint eepromAddress = 0x0000;
void UartInit(void) {
PCON &= 0x7F; // 波特率不倍速
SCON = 0x50; // 8位數(shù)據(jù),可變波特率
AUXR |= 0x40; // 定時(shí)器1時(shí)鐘為Fosc
AUXR &= 0xFE; // 串口1選擇定時(shí)器1為波特率發(fā)生器
TMOD &= 0x0F; // 清除定時(shí)器1模式位
TMOD |= 0x20; // 設(shè)定定時(shí)器1為8位自動(dòng)重裝方式
TL1 = 0xDC; // 設(shè)定初值
TH1 = 0xDC; // 設(shè)定定時(shí)器重裝初值
ET1 = 0; // 禁止定時(shí)器1中斷
TR1 = 1; // 啟動(dòng)定時(shí)器1
ES = 1; // 開啟串口中斷允許
EA = 1; // 開啟全局中斷允許
}
unsigned char AsciiToHex(unsigned char ascii) {
if (ascii >= '0' && ascii <= '9') {
return ascii - '0'; // 將字符 '0'-'9' 轉(zhuǎn)換為 0x0-0x9
} else if (ascii >= 'A' && ascii <= 'F') {
return ascii - 'A' + 0xA; // 將字符 'A'-'F' 轉(zhuǎn)換為 0xA-0xF
} else {
return 0x00; // 如果不在范圍內(nèi),返回默認(rèn)值 0x00
}
}
void ReadFromEeprom(uint address) {
uint i, j;
for (i = 0; i < 16; i++) { // 遍歷16行
for (j = 0; j < 8; j++) { // 每行8個(gè)字節(jié)
unsigned char highByteAscii = IapReadByte(address); // 讀取高位 ASCII
unsigned char lowByteAscii = IapReadByte(address + 1); // 讀取低位 ASCII
// 轉(zhuǎn)換 ASCII 值為十六進(jìn)制數(shù)字
unsigned char highByte = AsciiToHex(highByteAscii);
unsigned char lowByte = AsciiToHex(lowByteAscii);
// 合并高、低位為一個(gè)8位的十六進(jìn)制數(shù)
receivedData[ i][j] = (highByte << 4) | (lowByte & 0x0F);
address += 2; // 每次讀取后地址增加2,以讀取下一個(gè)字節(jié)對(duì)
}
}
}
void WriteToEeprom(uint address, uchar data1) {
IapProgramByte(address, data1); // 使用定義好的函數(shù)寫入EEPROM
}
void Delay(unsigned int t) {
while (--t);
}
void InputByte(unsigned char dat) {
unsigned char i;
for (i = 0; i < 8; i++) {
T_IO = !(dat & 0x01);
dat >>= 1;
T_CLK = 0;
T_CLK = 1;
}
}
/*void SendString(uchar *str) {
while (*str) {
SBUF = *str++;
while (!TI);
TI = 0;
}
} */
void main(void) {
uint i,j;
ReadFromEeprom(eepromAddress);
eepromAddress = 0x0000;
UartInit();
//SendString("2"); // 發(fā)送 "2" 表示初始化完成
P3M0 = 0xff; // 推挽
P1M0 = 0xff;
P3M1 = 0;
P1M1 = 0;
AUXR |= 0x80;
while(1) {
//SendString("x");
if(flag == 1) {
ES = 0;
//SendString("3"); // 當(dāng)接收到完整數(shù)據(jù)包后發(fā)送 "3"
IapEraseSector(0x0000);
for(i = 0; i < 256; i++) {
WriteToEeprom(eepromAddress++, buffer[ i]);
}
eepromAddress = 0x0000;
ReadFromEeprom(eepromAddress);
for(i=0;i<16;i++)
{
for(j=0;j<8;j++)
{
SBUF = receivedData[ i][j];
while(!TI);
TI = 0;
}
}
eepromAddress = 0x0000;
flag = 0;
ES = 1;
}
Delay(1);
T_OE = 0;
T_STR = 0; // 鎖存無效
for (col = 0; col < 8; col++) {
InputByte(receivedData[row][col]); // 輸出到 595
}
P1 = row; // 用 P1 口的前 4 位控制 16 行
T_STR = 1; // 鎖存有效,此時(shí)一行的數(shù)據(jù)顯示到屏上
T_OE = 1;
row++;
if (row == 16) {
row = 0;
}
}
}
void ser() interrupt 4 {
uchar receivedByte;
if(RI) {
receivedByte = SBUF;
RI = 0; // 清除接收標(biāo)志位
buffer[count++] = receivedByte;
//SendString("6");
if (count == 256) {
flag = 1;
count = 0;
//SendString("1");
}
}
if(TI) {
TI = 0;
}
}
|
|