#include<reg52.h>
#include<intrins.h>
#include<LDC1000_cmd.h>
#define uchar unsigned char
#define uint unsigned int/* 12864端口定義*/#define LCD_data P0 //帶字庫液 晶12864數據口sbit LCD_RS = P3^0; //寄存器選擇輸入sbit LCD_RW = P3^1; //液晶讀/寫控制sbit LCD_EN = P3^2; //液晶使能控制sbit LCD_PSB=P3^3; //并口控制sbit LCD_RST=P3^4;/***開關端口定義***/sbit KEY1 = P1^4;sbit KEY2 = P1^5;sbit KEY3 = P2^1;
uchar code dis1[] = {" 123 "}; //液晶顯示的漢字
uchar code dis2[] = {" 金屬探測儀 "};
uchar code dis3[] = {" 電氣工程 "};
uchar code dis4[] = {"456 "};
uchar code xianshi1[] = {"有無金屬: N"};
uchar code xianshi2[] = {"金屬屬性: 未定"};
uchar code xianshi3[] = {"金屬屬性: 鋁 "};
uchar code xianshi4[] = {"有無金屬: Y "};
/***LDC1000 PROGRAM***/
void SetVCoreUp(unsigned int level);
char spi_readByte(char addr,char *data1);
char spi_readWord(char addr, unsigned int *data1);
char spi_readBytes( char addr,char *buffer, unsigned char len);
char spi_writeByte(char addr,char data1);
char spi_writeWord(char addr,unsigned int data1);
char spi_writeBytes( char addr,char *buffer, unsigned char len);
static unsigned char txlen;
static unsigned char rxlen;
static char *txbuf;
static char *rxbuf;
static char txaddr;
static char wordbuf[2];
char proximtyData[2];
char frequencyData[3];
int proximtyDataMIN;
int frequencyDataMIN;
int proximtyDataMAX;
int frequencyDataMAX;
int proximtyDataTEMP;
int frequencyDataTEMP;
#define RPMIN 0x3A
#define RPMAX 0x13
#define delayNOP(); {_nop_();_nop_();_nop _();_nop_();};void lcd_pos(uchar X,uchar Y); //確定顯示位置/***延時函數***/void delay(unsigned int m) //延時程序 { unsigned int i,j; for(i=0;i<m;i++); for(j=0;j<10;j++); }/***檢查LCD忙狀態***//***lcd_busy為1時,忙,等待。lcd-busy為0時,閑,可寫指令與數據***/bit lcd_busy(){ bit result; LCD_RS = 0; LCD_RW = 1; LCD_EN = 1; delayNOP(); result = (bit)(P0&0x80); //只要有一位不是0,那么result=1 LCD_EN = 0; return(result);}/***寫指令數據到LCD ***//***RS=L,RW=L,E=高脈沖,D0-D7=指令碼***/void lcd_wcmd(uchar cmd){ while(lcd_busy()); LCD_RS = 0; LCD_RW = 0; LCD_EN = 0; _nop_(); _nop_(); P0 = cmd; delayNOP(); LCD_EN = 1; delayNOP(); LCD_EN = 0;}/***寫顯示數據到LCD ***//**RS=H,RW=L,E=高脈沖,D0-D7=數據**/void lcd_wdat(uchar dat){ while(lcd_busy()); LCD_RS = 1; LCD_RW = 0; LCD_EN = 0; P0 = dat; delayNOP(); LCD_EN = 1; delayNOP(); LCD_EN = 0;}
/***LCD初始化設定***/
void lcd_init()
{
LCD_PSB = 1; //并口方式
LCD_RST = 0; //液晶復位
delay(3);
LCD_RST = 1;
delay(3);
lcd_wcmd(0x34); //擴充指令操作
delay(5);
lcd_wcmd(0x30); //基本指令操作
delay(5);
lcd_wcmd(0x0C); //顯示開,關光標
delay(5);
lcd_wcmd(0x01); //清除LCD的顯示內容
delay(5);
}
/***設定顯示位置***/
void lcd_pos(uchar X,uchar Y)
{
uchar pos;
if (X==0)
{X=0x80;}
else if (X==1)
{X=0x90;}
else if (X==2)
{X=0x88;}
else if (X==3)
{X=0x98;}
pos = X+Y ;
lcd_wcmd(pos); //顯示地址
}
/***LDC1000***/
char spi_readByte(char addr, char *data1)
{
rxlen = 1;
rxbuf = data1;
txaddr = addr|0x80;
P4OUT&=~BIT0;
while (!(UCB1IFG&UCTXIFG));
UCB1TXBUF=txaddr;
while (!(UCB1IFG&UCTXIFG));
UCB1TXBUF = 0;
while (UCB1STAT&UCBUSY);
*rxbuf=UCB1RXBUF;
while (UCB1STAT&UCBUSY);
P4OUT|=BIT0;
return 0;
}
char spi_readWord(char addr, unsigned int *data1)
{
rxlen = 2;
rxbuf =&wordbuf[0];
txaddr =addr|0x80;
P4OUT &=~BIT0;
while (!(UCB1IFG&UCTXIFG));
UCB1TXBUF = txaddr;
while (!(UCB1IFG&UCTXIFG));
UCB1TXBUF = 0;
while (UCB1STAT&UCBUSY);
* rxbuf = UCB1RXBUF;
rxbuf++;
while (!(UCB1IFG&UCTXIFG));
UCB1TXBUF = 0;
while (UCB1STAT&UCBUSY);
* rxbuf = UCB1RXBUF;
while (UCB1STAT & UCBUSY);
P4OUT |= BIT0;
return 0;
}
char spi_readBytes( char addr, char * buffer, unsigned char len)
{
rxlen = len;
rxbuf = buffer;
txaddr = addr | 0x80;
P4OUT &= ~BIT0;
while (!(UCB1IFG&UCTXIFG));
UCB1TXBUF = txaddr;
while (rxlen > 0) {
while (!(UCB1IFG&UCTXIFG));
UCB1TXBUF = 0;
while (UCB1STAT & UCBUSY);
* rxbuf = UCB1RXBUF;
rxbuf++;
rxlen--;
}
while (UCB1STAT & UCBUSY);
P4OUT |= BIT0;
return 0;
}
char spi_writeByte(char addr, char data1)
{
wordbuf[0] = data1;
txlen = 1;
txbuf = &wordbuf[0];
txaddr = addr & ~0x80;
P4OUT &= ~BIT0;
while (!(UCB1IFG&UCTXIFG));
UCB1TXBUF = txaddr;
while (!(UCB1IFG&UCTXIFG));
UCB1TXBUF = *txbuf;
while (UCB1STAT & UCBUSY);
P4OUT |= BIT0;
return 0;
}
char spi_writeWord(char addr, unsigned int data1)
{
wordbuf[0] = data1 >> 8;
wordbuf[1] = data1 & 0xFF;
txlen = 2;
txbuf = &wordbuf[0];
txaddr = addr & ~0x80;
P4OUT &= ~BIT0;
while (!(UCB1IFG&UCTXIFG));
UCB1TXBUF = txaddr;
while (!(UCB1IFG&UCTXIFG));
UCB1TXBUF = *txbuf;
txbuf++;
while (!(UCB1IFG&UCTXIFG));
UCB1TXBUF = *txbuf;
while (UCB1STAT & UCBUSY);
P4OUT |= BIT0;
return 0;
}
char spi_writeBytes( char addr, char * buffer, unsigned char len)
{
txlen = len;
txbuf = buffer;
txaddr = addr & ~0x80;
P4OUT &= ~BIT0;
while (!(UCB1IFG&UCTXIFG));
UCB1TXBUF = txaddr;
while (txlen > 0)
{
while (!(UCB1IFG&UCTXIFG));
UCB1TXBUF = *txbuf;
txbuf++;
txlen--;
}
while (UCB1STAT & UCBUSY);
P4OUT |= BIT0;
return 0;
}
void SetVCoreUp (unsigned int level)
{
// Open PMM registers for write access
PMMCTL0_H = 0xA5;
// Make sure no flags are set for iterative sequences
//while ((PMMIFG & SVSMHDLYIFG) == 0);
//while ((PMMIFG & SVSMLDLYIFG) == 0);
// Set SVS/SVM high side new level
SVSMHCTL = SVSHE + SVSHRVL0 * level + SVMHE + SVSMHRRL0 * level;
// Set SVM low side to new level
SVSMLCTL = SVSLE + SVMLE + SVSMLRRL0 * level;
// Wait till SVM is settled
while ((PMMIFG & SVSMLDLYIFG) == 0);
// Clear already set flags
PMMIFG &= ~(SVMLVLRIFG + SVMLIFG);
// Set VCore to new level
PMMCTL0_L = PMMCOREV0 * level;
// Wait till new level reached
if ((PMMIFG & SVMLIFG))
while ((PMMIFG & SVMLVLRIFG) == 0);
// Set SVS/SVM low side to new level
SVSMLCTL = SVSLE + SVSLRVL0 * level + SVMLE + SVSMLRRL0 * level;
// Lock PMM registers for write access
PMMCTL0_H = 0x00;
}
/***主函數***/
void main()
{
uchar i;
lcd_init(); //初始化LCD
lcd_pos(0,0); //設置顯示位置為第一行的第1個字符
i = 0;
while(dis1[ i] != '\0')
{ //顯示字符
lcd_wdat(dis1[ i]);
i++;
}
lcd_pos(1,0); //設置顯示位置為第二行的第1個字符
i = 0;
while(dis2[ i] != '\0')
{
lcd_wdat(dis2[ i]); //顯示字符
i++;
}
lcd_pos(2,0); //設置顯示位置為第三行的第1個字符
i = 0;
while(dis3[ i] != '\0')
{
lcd_wdat(dis3[ i]); //顯示字符
i++;
}
lcd_pos(3,0); //設置顯示位置為第四行的第1個字符
i = 0;
while(dis4[ i] != '\0')
{
lcd_wdat(dis4[ i]); //顯示字符
i++;
}
while(1)
{
if (KEY1==0)
{
lcd_init(); //清除LCD的顯示內容
lcd_pos(0,0); //設置顯示位置為第一行的第1個字符
i = 0;
while(xianshi1[ i] != '\0')
{ //顯示字符
lcd_wdat(xianshi1[ i]);
i++;
}
lcd_pos(1,0); //設置顯示位置為第二行的第1個字符
i = 0;
while(xianshi2[ i] != '\0')
{ //顯示字符
lcd_wdat(xianshi2[ i]);
i++;
KEY2=1;
}
}
if (KEY2==0)
{ lcd_init(); //清除LCD的顯示內容
lcd_pos(0,0); //設置顯示位置為第一行的第1個字符
i = 0;
while(xianshi4[i] != '\0')
{ //顯示字符
lcd_wdat(xianshi4[i]);
i++;
}
lcd_pos(1,0); //設置顯示位置為第二行的第1個字符
i = 0;
while(xianshi3[ i] != '\0')
{ //顯示字符
lcd_wdat(xianshi3[i]);
i++;
}
KEY3=0;
}
}
|