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