编辑: 过于眷恋 | 2019-07-04 |
sbit SCLK = P3 ^ 5;
sbit SDIN = P3 ^ 6;
sbit SDOUT = P3 ^ 4;
// 根据需要添加合适的 nop 指令,用作延时,以满足 SPI 时钟数量不大于 10MHz #define delay // _nop_() // _nop_();
_nop_() // 向指定寄存器写
8 位数据 void write_at7456_addr_data(unsigned char addr, unsigned char dat) { unsigned char i;
SCLK = 0;
nCS = 0;
delay;
for (i=0;
iSR & SPI_I2S_FLAG_BSY) != 0);
GPIOA->BSRR = M_nCS;
// M_nCS =
1 } // 从指定寄存器读取
8 位数据 unsigned char read_at7456_addr_data(unsigned char addr) { unsigned char r;
GPIOA->BRR = M_nCS;
// M_nCS =
0 addr |= 0x80;
// D7 =
1 为读寄存器 r = SPI1->DR;
// 读数据,清除 RXNE // 等待发送缓冲器为空 while ((SPI1->SR & SPI_I2S_FLAG_TXE) == 0);
// 发送数据 SPI1->DR = addr;
// 等待接收缓冲器为非空 while ((SPI1->SR & SPI_I2S_FLAG_RXNE) == 0);
r = SPI1->DR;
// 读数据 // 等待发送缓冲器为空 while ((SPI1->SR & SPI_I2S_FLAG_TXE) == 0);
// 发送数据 SPI1->DR = 0xff;
// 等待接收缓冲器为非空 while ((SPI1->SR & SPI_I2S_FLAG_RXNE) == 0);
r = SPI1->DR;
// 读数据,清除 RXNE while ((SPI1->SR & SPI_I2S_FLAG_TXE) == 0);
while ((SPI1->SR & SPI_I2S_FLAG_BSY) != 0);
GPIOA->BSRR = M_nCS;
// M_nCS =
1 return r;
} void write_at7456_data(unsigned char dat) { GPIOA->BRR = M_nCS;
// M_nCS =
0 SPI1->DR;
// 读数据,清除 RXNE // 等待发送缓冲器为空 while ((SPI1->SR & SPI_I2S_FLAG_TXE) == 0);
// 发送数据 SPI1->DR = dat;
// 等待接收缓冲器为非空 while ((SPI1->SR & SPI_I2S_FLAG_RXNE) == 0);
SPI1->DR;
// 读数据,清除 RXNE // 等待发送缓冲器为空 while ((SPI1->SR & SPI_I2S_FLAG_TXE) == 0);
while ((SPI1->SR & SPI_I2S_FLAG_BSY) != 0);
GPIOA->BSRR = M_nCS;
// M_nCS =
1 } unsigned char read_at7456_data(void) { unsigned char r;
GPIOA->BRR = M_nCS;
// M_nCS =
0 r = SPI1->DR;
// 读数据,清除 RXNE // 等待发送缓冲器为空 while ((SPI1->SR & SPI_I2S_FLAG_TXE) == 0);
// 发送数据 SPI1->DR = 0xff;
// 等待接收缓冲器为非空 while ((SPI1->SR & SPI_I2S_FLAG_RXNE) == 0);
r = SPI1->DR;
// 读数据,清除 RXNE // 等待发送缓冲器为空 while ((SPI1->SR & SPI_I2S_FLAG_TXE) == 0);
while ((SPI1->SR & SPI_I2S_FLAG_BSY) != 0);
GPIOA->BSRR = M_nCS;
// M_nCS =
1 return r;
} AT7456 读写程序:AT7456.C // // AT7456(E) 读写控制程序 --- AT7456.C // 杭州中科微电子有限公司 // 2017-03-31 // 0571-28918103 系统部 // #include "public.h" #include "AT7456.H" VER7456 newAt7456;
// AT7456 显示字符 void DisplayChar(unsigned char row, unsigned char col, unsigned char c) { unsigned int kk;
kk = row *
30 + col;
write_at7456_addr_data(DMAH, kk / 256);
// address write_at7456_addr_data(DMAL, kk % 256);
write_at7456_addr_data(DMDI, c);
} // 显示数字 0..9 void DisplayNum(unsigned char row, unsigned char col, unsigned char c) { unsigned int kk;
kk = row *
30 + col;
write_at7456_addr_data(DMAH, kk / 256);
// address write_at7456_addr_data(DMAL, kk % 256);
write_at7456_addr_data(DMDI, (c == 0) ?
10 : c);
} // 显示的字符串为字符和数字以及空格 void DisplayString(unsigned char row, unsigned char col, unsigned char *s) { unsigned int kk;
unsigned char c;
kk = row *
30 + col;
write_at7456_addr_data(DMAH, kk / 256);
// address write_at7456_addr_data(DMAL, kk % 256);
write_at7456_addr_data(DMM, 0x01);
// Auto Inc c = *s++;
while (c != 0) { if ((c >= '0') && (c = 'A') && (c = 'a') && (c