2008年7月23日 星期三

8051 lab

lab1
===
寫個while loop 控制P3.5值不斷在 0、1間變化
-----------------------------------

lab2
===
UART設定:
SCON=0x50;
PCON = 0x80;
BRL = 126;
BDRCON = 0x1F;

手動加上下面:
Sfr (BDRCON , 0x92);
Sfr (BRL , 0x91);
-----------------------------------

lab3
學習使用interrupt 與 timer

init_INT: 設定interrupt 並將 keyboard interrupt啟動
init_INT0、init_INT1: 2個不同interval的timer
INTLED0: timer interrupt後將LED作開關動作
INTKeyBoard: keyboard 中斷事件

程式碼如下:
void init_INT()
{
IEN0=0x80;
IEN1=0x10;
KBCON=0x00;
}

void init_INT0()
{
TMOD = (TMOD & 0x0F) | 0x20; /* Set Mode (8-bit timer with reload) */
TH1 = 0; /* Reload TL1 to count 100 clocks */
TL1 = TH1;
ET1 = 1; /* Enable Timer 1 Interrupts */
TR1 = 1; /* Start Timer 1 Running */
EA = 1; /* Global Interrupt Enable */
}

void init_INT1()
{
TMOD = (TMOD & 0x0F) | 0x20;
TH1 = 100;
TL1 = TH1;
ET1 = 1;
TR1 = 1;
EA = 1;
}

void INTLED0 (void) interrupt 3
{
overflow_count++;
if(overflow_count==1000)
{
LED = !LED;
overflow_count =0;
}
}

void delay()
{
int i, j;
i=1000;
while(i!=0)
{
j=100;
while(j!=0)
{
j--;
}
i--;
}
}

void INTKeyBoard(void) interrupt 11 using 1
//使用11號中斷 //library為1
{
int recv = INPUT;
if(FUNC==INPUT)// && mode!=1)
{
ET1 = 0;
TR1 = 0;
init_INT0();
printu("Press FUNC\n");
}
else if(NEXT==INPUT)// && mode!=2)
{
LED = 0;
ET1 = 0;
TR1 = 0;
printu("Press NEXT\n");
}
else if(PREV==INPUT)// && mode!=3)
{
LED = 1;
ET1 = 0;
TR1 = 0;
printu("Press PREV\n");
}
else if(PLAY==INPUT)// && mode!=4)
{
ET1 = 0;
TR1 = 0;
init_INT1();
printu("Press PLAY\n");
}
KBSTA = 0x00;
delay();
}
-----------------------------------

lab4

重點在於必須看懂pdf檔裡面的時序圖

下面是lab4所要加上的code:
void ReadFlashID(unsigned char *MID,unsigned char *DID)
{
K9F_FUN = COMMAND;
K9F5608 = 0x90;
K9F_FUN = ADDRESS;
K9F5608 = 0x00;
K9F_FUN = D_DATA;
*MID = K9F5608;
*DID = K9F5608;
while(!(K9F_FUN & RB));
K9F_FUN = INACTIVE;
}


void ReadPage(unsigned int BlockPage,unsigned char *PageBuf)
{
unsigned int data i;
K9F_FUN = COMMAND;
K9F5608 = 0x00;
K9F_FUN = ADDRESS;
K9F5608 = 0x00;
K9F5608 = BlockPage;
K9F5608 = BlockPage>>8;
K9F_FUN = D_DATA;
i=512;
while(!(K9F_FUN & RB));
while(i--)
*PageBuf++ = K9F5608;
K9F_FUN = D_DATA;
K9F_FUN = INACTIVE;
}



void BlockErase(unsigned int BlockPage)
{
K9F_FUN = COMMAND;
K9F5608 = 0x60;
K9F_FUN = ADDRESS;
// K9F5608 = 0; //A0-A7
K9F5608 = BlockPage; //A9-A16
K9F5608 = BlockPage >> 8; //A17-A24
// K9F_FUN = D_DATA;
K9F_FUN = COMMAND;
K9F5608 = 0xD0;
K9F_FUN = D_DATA;
while(!(K9F_FUN & RB));
K9F_FUN = COMMAND;
K9F5608 = 0x70;
if(K9F5608==0) printu("Block Erase successful\n");
else printu("Block Erase failed\n");
K9F_FUN = INACTIVE;
}

沒有留言: