📄 main.c
字号:
///////////////////////////////////////////////////////////////////////////////////////////
//include the header and the def files
#include "c8051f310.h"
#include "intrins.h"
#include "LCD_DEF.h"
#include "LCD_DRIVER.c"
#include "lcd_img.c"
/////////////////////////////////////////////////////////////////////////////////////////////
//GLOBAL VARIABLES
bit flag ;
bit flag1;
bit read;
uint pg_adr;
uint pg_adr1;
uint num;
uint cnt;
uchar xdata rd_mem[528];
/////////////////////////////////////////////////////////////////////////////////////////////
//image page adress arrys
uint code Photo_Adr[10] = { 0, 80, 160, 240, 320, 400,
480, 560, 640, 720 } ;
struct flash_prog
{
uchar command;
uchar byte1;
uchar byte2;
uchar byte3;
};
/////////////////////////////////////////////////////////////////////////////////////////////
//delay routines
////////////////////////////////////////////////////////////////////////////////////////////
void delay1(uchar j)
{
uchar i;
for(i=0;i<j;i++);
}
////////////////////////////////////////////////////////////////////////////////////////////
//long time delay for image display
////////////////////////////////////////////////////////////////////////////////////////////
void delay2(ulong j)
{
ulong i;
for(i=0;i<j;i++)
{
_nop_();
_nop_();
_nop_();
_nop_();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////
//command convert to the 161 format
/////////////////////////////////////////////////////////////////////////////////////////////
struct flash_prog cmd_cvt(uchar cmd)
{
struct flash_prog onetime_wr;
onetime_wr.command = cmd;
onetime_wr.byte1 = (uchar) ((pg_adr1 & 0x0fff)>>6); //type force conversion operator
onetime_wr.byte2 = (uchar) ((pg_adr1 & 0x003f)<<2); //is priority the shift and logic
onetime_wr.byte3 = 0; //compute operator,so the "()" is
return onetime_wr; //needed
}
/////////////////////////////////////////////////////////////////////////////////////////////
//send program flash cmd and write data to 161
//20 pages write at a time
/////////////////////////////////////////////////////////////////////////////////////////////
void pg_wr_161(void)
{
struct flash_prog wr_cmd;
LOOP1:;
pg_adr1 = pg_adr + 700;
NSS = 0; //START THE SPI0 BUS
wr_cmd = cmd_cvt(flash_pg_wr_buf1);
flag = 0;
//send flash page write cmd 0x82H
SPI0DAT = wr_cmd.command;
while(~SPIF);
SPIF = 0;
SPI0DAT = wr_cmd.byte1;
while(~SPIF);
SPIF = 0;
SPI0DAT = wr_cmd.byte2;
while(~SPIF);
SPIF = 0;
SPI0DAT = wr_cmd.byte3;
while(~SPIF);
SPIF = 0;
interrupt_ini();
SPIF = 1;
while(~flag)
{
if(cnt==10240) //judge if the 20 pages are written
{
cnt = 0;
while(BUSY==0);
return;
}
}
delay1(50);
while(BUSY==0); //wait for the buffer to memory programing
goto LOOP1; //start to write a new page
}
//////////////////////////////////////////////////////////////////////////////////////////////
//read data from 161, page read routine
void pg_rd_161(void)
{
//uchar i;
struct flash_prog rd_cmd;
pg_adr = 0;
//pg_adr1 = pg_adr + 700;
read = 1;
NSS = 0;
dis_interrupt();
rd_cmd = cmd_cvt(flash_rd);
//send the flash page read cmd 0x52H
SPI0DAT = rd_cmd.command;
while(~SPIF);
SPIF = 0;
SPI0DAT = rd_cmd.byte1;
while(~SPIF);
SPIF = 0;
SPI0DAT = rd_cmd.byte2;
while(~SPIF);
SPIF = 0;
SPI0DAT = rd_cmd.byte3;
while(~SPIF);
SPIF = 0;
//send the command's don't care bits
SPI0DAT = 0xAA;
while(~SPIF);
SPIF = 0;
SPI0DAT = 0xAA;
while(~SPIF);
SPIF = 0;
SPI0DAT = 0xAA;
while(~SPIF);
SPIF = 0;
SPI0DAT = 0xAF;
while(~SPIF);
SPIF = 0;
delay1(5); //delay for command write compeleted
IE = 0xC0; //otherwise the last byte of command
SPI0DAT = 0; //could be corrupted
while(~flag1);
delay1(50);
NSS = 1;
flag1 = 0; //reset the page read compelete flag for read a another page
}
/////////////////////////////////////////////////////////////////////////////////////////////
//reset the flash memory
void reset_flash(void)
{
RESET = 0;
delay1(100);
RESET = 1;
delay1(10);
}
/////////////////////////////////////////////////////////////////////////////////////////////
//initial the MCU's port
void port_ini(void)
{
P0MDOUT = 0xB5;
P0MDIN = 0xFF;
P1MDOUT = 0xFF;
P3MDOUT = 0x1F;
XBR0 = 0x02;
XBR1 = 0x40;
}
//////////////////////////////////////////////////////////////////////////////////////////////
//disable WDT and enable the OSC
void osc_ini(void)
{
OSCICN = 0x83;
PCA0MD &= ~0x40;
}
///////////////////////////////////////////////////////////////////////////////////////////////
//CFG THE SPI0 BUS
//enable the SPI0 bus transmit freq is 6MHZ, 3-wire master mode
//interrupt enable,idel clock is high, second edge triggered
void cfg_spi(void)
{
SPI0CFG = 0x70;
SPI0CN = 0x01;
SPI0CKR = 0x01;
}
////////////////////////////////////////////////////////////////////////////////////////////////
//interrupt initial
//enbale SPI interrupt
void interrupt_ini(void)
{
IE = 0xC0;
}
///////////////////////////////////////////////////////////////////////////////////////////////
//disable interrupt
void dis_interrupt(void)
{
IE = 0x80;
}
///////////////////////////////////////////////////////////////////////////////////////////////
//main routine
///////////////////////////////////////////////////////////////////////////////////////////////
void main(void)
{
uchar i;
osc_ini();
port_ini();
cfg_spi();
reset_flash();
LCD_Initial_8020();
def_window(0,0,0x83,0xA1);
LCD_Clear_Window(0,0,0x83,0xA1,0xffff);
//pg_wr_161();
//pg_rd_161();
while(1)
{
for(i=0;i<10;i++)
{
LCD_Photo(0,0,0x7F,0x9F,Photo_Adr+i);
delay2(600000);
}
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////
//interrupt routine
void spi_int(void) interrupt 6
{
SPIF = 0;
num++;
cnt++;
if(TXBMT & ~read)
{
SPI0DAT = *(gImage_pic1 + num - 1 + pg_adr*528);
if(num == 528)
{
pg_adr++;
flag = 1;
num = 0;
delay1(10); //delay for finish the last byte of page
NSS = 1; //otherwise the image has a bad pixel at
IE = 0x80; //the end of pages
return;
}
if(cnt==10240)
{
num = 0;
delay(10); //delay for finish the last byte of page
NSS = 1; //otherwise the image has a bad pixel at
IE = 0x80; //the end of pages
return;
}
}
else if(read)
{
*(rd_mem + num - 1) = SPI0DAT;
delay(3);
SPI0DAT = 0;
if(num==528)
{
flag1 = 1;
num = 0;
IE = 0x80;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -