2008年10月31日 星期五

C語言練習3

MPC82G516, 82E54是台灣笙泉的8x51MCU,一直以來我都用它來作老闆要的東西
我覺得很好用喔
#include "REG_MPC82G516.h" //for MPC82G516 SFR definition               
//雖是選MPC82G516但是實上,ic是82E54
#include "INTRINS.H" //
unsigned char COMM;
unsigned char Data_1,Data_2;
sbit LCD_E = P3^7 ;
sbit LCD_RS = P3^4 ;
sbit LCD_RW = P3^5 ;
// ***********************************************
void LCD_S_Delay(void)
{
int S_Loop ;
int Null_Var ;
for ( S_Loop = 0 ; S_Loop < 200 ; S_Loop ++ )
Null_Var += 1 ;
}
// **********************************************
void LCD_L_Delay(void)
{
int L_Loop ;
for ( L_Loop = 0 ; L_Loop < 100 ; L_Loop ++ )
LCD_S_Delay( ) ;
}
//***********************************************
// Subroutine to
// Write a command to LCD module

// RS=0, R/W=0, E=H->L F(alling Edge)

// 寫入指令到lcd

void LCD_CMD_W_Timing( void )
{
LCD_RS = 0 ; // Set for Command Input
_nop_();
LCD_RW = 0 ;
_nop_();
LCD_E = 1 ;
_nop_();
_nop_();
_nop_();
_nop_();
LCD_E = 0 ;
}
//***********************************************
// Subroutine to
// Write a command to LCD module
// RS=1, R/W=0, E=H->L F(alling Edge)
// 寫入資料到lcd
void LCD_DAT_W_Timing( void )

{ LCD_RS = 1 ; // Set for Data Input
_nop_();
LCD_RW = 0 ;
_nop_();
LCD_E = 1 ;
_nop_();
_nop_();
_nop_();
_nop_();
LCD_E = 0 ;
}
LCD_RESET(void) // 初始化lcd(16*02)
{
P3 = 0x03;
LCD_CMD_W_Timing() ; // LCD Function SET
LCD_L_Delay() ;
LCD_L_Delay() ;
LCD_L_Delay() ;
LCD_L_Delay() ;
P3 = 0x03;
LCD_CMD_W_Timing() ; // LCD Function SET
LCD_L_Delay() ;
LCD_L_Delay() ;
LCD_L_Delay() ;
LCD_L_Delay() ;
//-----------------------------------------------
P3 = 0x03;
LCD_CMD_W_Timing() ; // LCD Function SET
LCD_L_Delay() ;
P3=0x02;
LCD_CMD_W_Timing() ; // LCD Function SET
LCD_L_Delay() ;
//-------------------------------------------------
P3=0x01;
LCD_CMD_W_Timing() ; // LCD Function SET
LCD_L_Delay() ;
P3=0x04;
LCD_CMD_W_Timing() ; // LCD Function SET
LCD_L_Delay() ;
//--------------------------------------------------
P3=0x00;
LCD_CMD_W_Timing() ;
// LCD Function SET

LCD_L_Delay() ;
P3=0x0f;
LCD_CMD_W_Timing() ; // LCD Function SET
LCD_L_Delay() ;
//-----------------------------------------------------
P3=0x00;
LCD_CMD_W_Timing() ; // LCD Function SET
LCD_L_Delay() ;
P3=0x02;
LCD_CMD_W_Timing() ; // LCD Function SET
LCD_L_Delay() ;
//-------------------------------------------------------
P3=0x00;
LCD_CMD_W_Timing() ; // LCD Function SET
LCD_L_Delay() ;
P3=0x01;
LCD_CMD_W_Timing() ; // LCD Function SET
LCD_L_Delay() ;
//-----------------------------------------------
P3=0x08;
LCD_CMD_W_Timing() ; // LCD Function SET
LCD_L_Delay() ;
P3=0x02;
LCD_CMD_W_Timing() ; // LCD Function SET
LCD_L_Delay() ;
}

unsigned int Get_ADC_Channel(char channel)
{ // 82e54一共有8個通道10bit的A TO D
channel &= 0x07 //make sure channel is 0-7
ADCTL = 0x88channel; //start convert
while(!(ADCTL & 0x10)){} //check if convert done
return ((unsigned int)(ADCH<<2) (ADCL & 3));
}

void Output(unsigned char d)
{ // 將所讀到的ad值 轉成ascII 且輸出到lcd
if(d>9 & d<16) Data_1 = (d + 0x37);
else if(d<10) Data_1 = (d + 0x30);
Data_2 = Data_1;
//因為lcd控制是採4bit控制 所以一個byte要送兩次
P3=Data_2>>4;
LCD_DAT_W_Timing();
LCD_S_Delay( );
P3=Data_1&0X0F;
LCD_DAT_W_Timing();
LCD_S_Delay( );
}

main(void)
{
unsigned
int VALUE;
P3=0XFF;
LCD_L_Delay() ;
P1M0=0X0F; // P1 IS ALL INPUT
P1M1=0X00; //
LCD_RESET();
P3=0x08;
LCD_CMD_W_Timing() ; // LCD Function SET
LCD_L_Delay() ;
P3=0x00;
LCD_CMD_W_Timing() ; // LCD Function SET
LCD_L_Delay() ;
while (1)
{
VALUE=Get_ADC_Channel(7);
Output(VALUE>>8); //顯示最高 nibble
Output((VALUE>>4)&0x0f); //顯示中間 nibble
Output(VALUE&0x0f); //顯示最低 nibble
P3=0x08;
LCD_CMD_W_Timing() ; // LCD Function SET
LCD_L_Delay() ;
P3=0x00;
LCD_CMD_W_Timing() ; // LCD Function SET
LCD_L_Delay() ;
LCD_L_Delay() ;
LCD_L_Delay() ;
LCD_L_Delay() ;
}
}

沒有留言:

張貼留言