📄 main.c
字号:
#include <stdio.h>
#include "710defs.h"
#include "HB_it.h"
#include "main.h"
#include "lcd_pattern.h"
extern int c;
extern U8 shuru;
extern U8 a[6];
extern U8 b[6];
extern int p;
int main(void)
{ //USI寄存器初始化
int cnt,i;
U8 wchar1[]= "请输密码";
U8 wchar2[]= "密码正确 ";
U8 wchar3[]= "密码错误 ";
U8 wchar4[]= "错误超过三次系统锁定";
LCD_IMAGE_T LCD_Size;
LCD_LOCATION_T LCD_Location;
LCDShowParameter LSP;
LCD_Size.width = 480;
LCD_Size.height = 240;
LCD_Location.StartX = 0;
LCD_Location.StartY = 0;
LCD_Location.EndX = 960;
LCD_Location.EndY = 240;
printf("***************USI测试程序**********************\n");
printf("*************Flash型号W25P10********************\n");
USIInit();
//读器件ID
printf("读器件ID:");
USIRead_ID();
//擦除扇区0
printf("写使能:\n");
USIWriteEnable();
while(USICheckBusy());
USISectorErease(0x0);
for(cnt=0;cnt<6;cnt++)
a[cnt] =cnt;//发送数据初始化
LCDInit();
LCDShow(LCD_Size, LCD_Location);
LCDFIFOBufferSet(BlackBoard);
LCDDisplayOn();
LSP.StartX = 8;
LSP.StartY = 7;
LSP.LibPlace = 0x400000;
LSP.Color = 0x07ff;
LSP.LetterChar = wchar1;
LSP.LCDBuffer = BlackBoard;
LCDOutputShow(LSP,4);
KPIInit();
printf("Please press keys.\n");
while(1)
{
if(p==1)
{
LSP.StartX = 8;
LSP.StartY = 7;
LSP.LibPlace = 0x400000;
LSP.Color = 0x07ff;
LSP.LetterChar = wchar2;
LSP.LCDBuffer = BlackBoard;
LCDOutputShow(LSP,4);
EBILedInit(0x01);
}
if(p==2)
{
LSP.StartX = 8;
LSP.StartY = 7;
LSP.LibPlace = 0x400000;
LSP.Color = 0x07ff;
LSP.LetterChar = wchar3;
LSP.LCDBuffer = BlackBoard;
LCDOutputShow(LSP,4);
EBILedInit(0x01);
Delay(0x80000);
EBILedInit(0x00);
Delay(0x80000);
}
if(p==3)
{
LSP.StartX = 8;
LSP.StartY = 7;
LSP.LibPlace = 0x400000;
LSP.Color = 0x07ff;
LSP.LetterChar = wchar4;
LSP.LCDBuffer = BlackBoard;
LCDOutputShow(LSP,10);
EBILedInit(0x01);
Delay(0x80000);
EBILedInit(0x00);
Delay(0x80000);
}
}
return 0;
}
/*******************************************************************************
* Function Name : KPIInit
* Description : KPI初始化
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void KPIInit(void)
{
REG_GPIO_CFG2 |= 0x000aaaaa; //将GPIO设置为KPI模式
REG_GPIO_CFG2 &= 0xfffaaaaa;
REG_AIC_SCR29 = 0x00000045; //将KPI中断设置为高电平有效,优先级为5
REG_AIC_MECR = 0x20000000;
REG_KPICONF = 0x00142fff;
}
/*******************************************************************************
* Function Name : USIInit
* Description : 初始化USI,设置GPIO口为USI模式,配置SCLK串行时钟
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void USIInit(void)
{
REG_GPIO_CFG5 = 0x000AA000;
REG_USI_DIVIDER = 0x1;
REG_USI_CNTRL = 0x00000044;
REG_USI_SSR=0x0;
}
/*******************************************************************************
* Function Name : Delay
* Description : 用于不精确延时
* Input : 延时时间
* Output : None
* Return : None
*******************************************************************************/
void Delay(U32 t)
{
do
{
t--;
}while(t);
}
/*******************************************************************************
* Function Name : USICheckBusy
* Description : 检测Flash状态寄存器“忙”位
* Input : None
* Output : None
* Return : None
*******************************************************************************/
U8 USICheckBusy(void)
{
REG_USI_Tx0 = 0x05; //读状态寄存器命令
REG_USI_CNTRL = 0x00000044; //发送8位
REG_USI_SSR = 0x1;//CS=0
REG_USI_CNTRL = REG_USI_CNTRL | 0x01;//启动发送
while(REG_USI_CNTRL & 0x1);//判断是否发送完成
while(1)
{
REG_USI_Tx0 = 0xff;//发送ff提供接收时钟,把状态寄存器的数据读回接收寄存器
REG_USI_CNTRL = 0x00000044;//发送8位
REG_USI_SSR=0x1;//CS=0
REG_USI_CNTRL =REG_USI_CNTRL|0x01;//启动发送
if(((REG_USI_Rx0 & 0xff) & 0x01) != 0x01)//等待检测状态寄存器的忙状态
{
break;
}
}
printf("Busy=0x%x\n",REG_USI_Rx0);
REG_USI_SSR=0x0; //CS=1
return 0;
}
/*******************************************************************************
* Function Name : USIWriteEnable
* Description : 写使能,在写数据到flash存储器或者擦除操作时前要加上写使能
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void USIWriteEnable(void)
{
REG_USI_Tx0 = 0x06;//写使能命令
REG_USI_CNTRL = 0x00000044;
REG_USI_SSR = 0x1;//CS=0
REG_USI_CNTRL = REG_USI_CNTRL | 0x01;//启动发送
while(REG_USI_CNTRL & 0x1);//等待发送结束
REG_USI_SSR = 0x0;//CS=1
}
/*******************************************************************************
* Function Name : USIRead
* Description : 从Flash读一个字节
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void USIRead(U32 R_addr,U8 *Read_buff)
{
U32 Read_cnt;
U8 Rx_temp;
//传送8位命令
REG_USI_Tx0 = 0x03;//读数据命令
REG_USI_CNTRL = 0x00000044;
REG_USI_SSR = 0x1;
REG_USI_CNTRL = REG_USI_CNTRL | 0x01;
while(REG_USI_CNTRL&0x1);
//传送24位地址
REG_USI_Tx0 = R_addr;
REG_USI_CNTRL = 0x000000c4;
REG_USI_CNTRL = REG_USI_CNTRL | 0x01;
while(REG_USI_CNTRL & 0x1);
//读出flash某一页的数据
for(Read_cnt = 0; Read_cnt <6; Read_cnt++)
{
REG_USI_Tx0 = 0xff;
REG_USI_CNTRL = 0x00000044;
REG_USI_CNTRL = REG_USI_CNTRL | 0x01;
while(REG_USI_CNTRL & 0x1);
Rx_temp=REG_USI_Rx0;
*(Read_buff++) =Rx_temp;
printf("Address\t\t0x%x: \t\t%d\n",Read_cnt,Rx_temp);
}
REG_USI_SSR=0x0;
printf("接收完成.....\n");
}
/*******************************************************************************
* Function Name : USIWrite
* Description : 向Flash写一个字节
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void USIWrite(U32 W_addr, U8 *W_buff)
{
U16 Write_cnt;
//传送8位命令
REG_USI_Tx0 = 0x02;//写命令
REG_USI_CNTRL = 0x00000044;
REG_USI_SSR = 0x1;
REG_USI_CNTRL = REG_USI_CNTRL | 0x01;
while(REG_USI_CNTRL & 0x1);
//传送24位地址
REG_USI_Tx0 = W_addr;
REG_USI_CNTRL = 0x000000c4;
REG_USI_CNTRL = REG_USI_CNTRL | 0x01;
while(REG_USI_CNTRL & 0x1);
//传送8位数据
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -