C8051F350单片机1602液晶显示程序.docx
- 文档编号:12924251
- 上传时间:2023-06-09
- 格式:DOCX
- 页数:14
- 大小:17.43KB
C8051F350单片机1602液晶显示程序.docx
《C8051F350单片机1602液晶显示程序.docx》由会员分享,可在线阅读,更多相关《C8051F350单片机1602液晶显示程序.docx(14页珍藏版)》请在冰点文库上搜索。
C8051F350单片机1602液晶显示程序
#include
#include
#include
//-----------------------------------------------------------------------------
//16-bitSFRDefinitionsfor'F35x
//-----------------------------------------------------------------------------
sfr16TMR2RL=0xCA;//Timer2reloadvalue
sfr16TMR2=0xCC;//Timer2counter
sfr16ADC0DEC=0x9A;//ADC0DecimationRatioRegister
//sfr16ADC0=0xbd;
//-----------------------------------------------------------------------------
//GlobalCONSTANTS
//-----------------------------------------------------------------------------
#defineSYSCLK24500000//SYSCLKfrequencyinHz
#defineMDCLK2457600//ModulatorclockinHz(idealis
//(2.4576MHz)
#defineOWR960//DesiredOutputWordRateinHzADC0输出字速率,最大960#defineBAUDRATE115200//BaudrateofUARTinbps
#defineucharunsignedchar
#defineuintunsignedint
#defineDataPortP0
sbitLCM_EN=P1^0;
sbitLCM_RS=P1^1;
unsignedcharD1,D2,D3,D4,D5,D6,D7,D8;
//sbitLED=P0^7;
unsignedintk;
//-----------------------------------------------------------------------------
//FunctionPROTOTYPES
//-----------------------------------------------------------------------------
voidOscillator_Init(void);
voidPort_Init(void);
voidUART0_Init(void);
voidADC0_Init(void);
voiddelay(inti);
voidWriteCommandLCM(ucharCMD);
voidWriteDataLCM(uchardataW);
voidInitLcd();
voidlcd_write_char(ucharx,uchary,uchardataW);
//-----------------------------------------------------------------------------
//MAINRoutine
//-----------------------------------------------------------------------------
voidmain(void)
{
PCA0MD&=~0x40;//WDTE=0(clearwatchdogtimer
//enable)
Oscillator_Init();//Initializesystemclock
Port_Init();//InitializeCrossbarandGPIO
//UART0_Init();//InitializeUART0forprintf's
InitLcd();
ADC0_Init();//InitializeADC0
AD0INT=0;
ADC0MD=0x83;//StartcontinuousconversionsEA=1;//Enableglobalinterrupts
while
(1){//Spinforever
}
}
//-----------------------------------------------------------------------------
//InitializationSubroutines
//-----------------------------------------------------------------------------
voiddelay(inti)//大概0.43ms
{
intj,k;
for(j=0;j
for(k=0;k<2000;k++);
}
voidlcd_write_char(ucharx,uchary,uchardataWord)//y是行,x是列,都是从0开始计数{
unsignedcharadd;
if(y==0)
add=0x80+x;
else
add=0xc0+x;
WriteCommandLCM(add);
WriteDataLCM(dataWord);
}
voidWriteCommandLCM(ucharCMD)
{
//delay
(2);
LCM_EN=0;
LCM_RS=0;
DataPort&=0xf0;
//delay
(2);
DataPort|=(CMD&0xf0)>>4;
delay(5);
LCM_EN=1;
delay(10);
LCM_EN=0;
delay(10);
//CMD=CMD>>4;
DataPort&=0xf0;
//delay
(2);
DataPort|=(CMD&0x0f);
delay(10);
LCM_EN=1;
delay(10);
LCM_EN=0;
}
voidWriteDataLCM(uchardataW)
//delay
(2);
LCM_EN=0;
LCM_RS=1;
//delay
(2);
DataPort&=0xf0;
//delay
(2);
DataPort|=(dataW&0xf0)>>4;
delay
(2);
LCM_EN=1;
delay(5);
LCM_EN=0;
delay(5);
//dataW=dataW>>4;
DataPort&=0xf0;
//delay
(2);
DataPort|=(dataW&0x0f);
delay
(2);
LCM_EN=1;
delay(5);
LCM_EN=0;
}
voidInitLcd()
{
delay(2000);
WriteCommandLCM(0x28);//4位数据总线
//delay(5);
WriteCommandLCM(0x01);//清屏
WriteCommandLCM(0x0C);//显示开及光标设置//WriteCommandLCM(0x0f);
//delay(200);
}
//-----------------------------------------------------------------------------//Oscillator_Init
//-----------------------------------------------------------------------------//
//ReturnValue:
None
//Parameters:
None
//
//Thisroutineinitializesthesystemclocktousetheinternal24.5MHz
//oscillatorasitsclocksource.Alsoenablesmissingclockdetectorreset.
//
//-----------------------------------------------------------------------------
voidOscillator_Init(void)
{
OSCICN=0x83;//Configureinternaloscillatorfor配置内部振荡器
//itslowestfrequencyRSTSRC=0x04;//Enablemissingclockdetector
}
//-----------------------------------------------------------------------------
//Port_Init
//-----------------------------------------------------------------------------
//
//ReturnValue:
None
//Parameters:
None
//
//ThisfunctioninitializestheGPIOandtheCrossbar初始化GPI0和交叉开关//
//Pinout:
//
//P0.4-UARTTX(digital,push-pull)
//P0.5-UARTRX(digital,open-drain)
//
//AIN0.2-ADC0input
//
//-----------------------------------------------------------------------------
voidPort_Init(void)
{
//XBR0=0x01;//UART0Selected
XBR1=0x40;//Enablecrossbarandweakpull-upsP0MDOUT|=0xDf;//TX,LEDs=Push-pull
P1MDOUT|=0x03;
}
//-----------------------------------------------------------------------------
//UART0_Init
//-----------------------------------------------------------------------------
//
//ReturnValue:
None
//Parameters:
None
//
//ConfiguretheUART0usingTimer1,for
//
//-----------------------------------------------------------------------------
/*voiddelay(unsignedinttime)
{
unsignedinti;
while(--time)
{
for(i=0;i<125;i++);
}
}
*/
/*voidUART0_Init(void)
{
SCON0=0x10;//SCON0:
8-bitvariablebitrate串口控制寄存器
//levelofSTOPbitisignored
//RXenabled
//ninthbitsarezeros
//clearRI0andTI0bitsif(SYSCLK/BAUDRATE/2/256<1){
TH1=-(SYSCLK/BAUDRATE/2);
CKCON|=0x08;//T1M=1;SCA1:
0=xx
}elseif(SYSCLK/BAUDRATE/2/256<4){
TH1=-(SYSCLK/BAUDRATE/2/4);
CKCON&=~0x0B;//T1M=0;SCA1:
0=01
CKCON|=0x01;
}elseif(SYSCLK/BAUDRATE/2/256<12){
TH1=-(SYSCLK/BAUDRATE/2/12);
CKCON&=~0x0B;//T1M=0;SCA1:
0=00
}elseif(SYSCLK/BAUDRATE/2/256<48){
TH1=-(SYSCLK/BAUDRATE/2/48);
CKCON&=~0x0B;//T1M=0;SCA1:
0=10
CKCON|=0x02;
}else{
while
(1);//Error.Unsupportedbaudrate}
TL1=TH1;//InitTimer1定时器1是用来确定有相同的波特率TMOD&=~0xf0;//TMOD:
timer1in8-bitautoreload
TMOD|=0x20;
TR1=1;//STARTTimer1
TI0=1;//IndicateTX0ready
}*/
//-----------------------------------------------------------------------------
//ADC0_Init
//-----------------------------------------------------------------------------
//
//ReturnValue:
None
//Parameters:
None
//
//InitializetheADCtousethetemperaturesensor.(non-differential)
//
//-----------------------------------------------------------------------------
voidADC0_Init(void)
{
REF0CN|=0x03;//EnableinternalVref
ADC0CN=0x00;//Gain=1,Unipolarmode最后三位PGA增益=1,ADC控制寄存器ADC0CF=0x00;//InterruptsuponSINC3filteroutputADC0配置寄存器,位4选择哪一个滤波器转换结束后使AD0INT中断标志置1,
//andusesinternalVREF位2是VREF源选择位
ADC0CLK=(SYSCLK/MDCLK)-1;//GenerateMDCLKformodulator.调制器时钟分频系数,由最佳调制器时钟频率反求系数
//IdeallyMDCLK=2.4576MHz
//ProgramdecimationratefordesiredOWR
ADC0DEC=((unsignedlong)MDCLK/(unsignedlong)OWR///ADC0抽取比寄存器,确定ADC输出字速率,由输出字速率反求抽取比
(unsignedlong)128)-1;
ADC0BUF=0x00;//TurnoffInputBuffers
ADC0MUX=0x08;//SelectAIN0.2模拟多路选择控制寄存器
ADC0MD=0x81;//StartinternalcalibrationADC0控制寄存器,校准和转换选择while(AD0CALC!
=1);//Waituntilcalibrationiscomplete
EIE1|=0x08;//EnableADC0Interrupts转换结束中断允许ADC0MD=0x80;//EnabletheADC0(IDLEMode)
}
//-----------------------------------------------------------------------------
//InterruptServiceRoutines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//ADC0_ISR
//-----------------------------------------------------------------------------
//
//ThisISRprintstheresulttotheUART.TheISRiscalledaftereachADC//conversion.
//
//-----------------------------------------------------------------------------
voidADC0_ISR(void)interrupt10
{
unsignedlongADC_OutputVal;
staticdoubleaccumulator=0;
//unsignedlonginti;
staticunsignedintmeasurements=0;
staticunsignedintnumber=0;
//staticunsignedlongaverage;
staticunsignedintflag=1;
unsignedintresult=0;
while(!
AD0INT);
AD0INT=0;
//InitLcd();
ADC_OutputVal=0x00000000;
ADC_OutputVal=ADC0H;
ADC_OutputVal<<=16;
ADC_OutputVal+=(long)ADC0L+((long)ADC0M<<8);
//ADC_OutputVal=ADC_OutputVal*0.9992+10126;
accumulator+=ADC_OutputVal;
measurements++;
if(measurements==1)
{
number++;
if(number==1)
{
result=accumulator/measurements;
/*D1=result/10000000+0x30;
D2=(result%10000000)/1000000+0x30;
D3=(result%1000000)/100000+0x30;
D4=(result%100000)/10000+0x30;
D5=(result%10000)/1000+0x30;
D6=(result%1000)/100+0x30;
D7=(result%100)/10+0x30;
D8=(result%10)+0x30;*/
number=0;
/*D1=1;
D2=2;
D3=3;
D4=4;
D5=5;
D6=6;
D7=7;
D8=8;*/
/*HighData=result>>8;//串口发送数据LowData=result;
a[0]=HighData&0X0F;
a[1]=LowData;
for(i=0;i<2;i++)
{
SBUF0=a[i];
while(TI0==0)
{};
TI0=0;
}*/
lcd_write_char(0,0,'A');
lcd_write_char(1,0,'D');
lcd_write_char(2,0,'C');
lcd_write_char(3,0,'0');
lcd_write_char(4,0,':
');
lcd_write_char(0,1,'A');
lcd_write_char(1,1,'A');
lcd_write_char(2,1,'1');
lcd_write_char(3,1,'2');
lcd_write_char(4,1,'3');
lcd_write_char(5,1,'4');
lcd_write_char(6,1,'5');
lcd_write_char(7,1,'6');
lcd_write_char(8,1,'7');
lcd_write_char(9,1,'8');
lcd_write_char(10,1,'B');
lcd_write_char(11,1,'B');
delay(1000);
if(flag==1)
{
//WriteCommandLCM(0x01);
InitLcd();
flag=0;
}
}
measurements=0;
accumulator=0;
/*if(number==200)//采集的个数受到accumulator的限制{
average=accumulator/number;
//average=average*0.9992+10126;
a[0]=average>>24;
a[1]=average>>16;
a[
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C8051F350 单片机 1602 液晶显示 程序