智能车制作
标题:
zbh
[打印本页]
作者:
张宝华
时间:
2011-11-23 21:48
标题:
zbh
#include <hidef.h> /* common defines and macros */
试过很多程序,包括别人实验过的程序,LCD都显示不了东西,这是为什么
#include "derivative.h" /* derivative-specific definitions */
#define LCD1602_RS PORTB_PB0
#define LCD1602_RW PORTB_PB1
#define LCD1602_EN PORTB_PB2
#define LCDIO PORTA
#define LCDIO_DIR DDRA
#define LCDIO_DIR_IN 0x00
#define LCDIO_DIR_OUT 0xFF
void SetBusCLK_16M(void)
{
CLKSEL=0X00; // disengage PLL to system
PLLCTL_PLLON=1; // turn on PLL
SYNR=0x00 | 0x01; // VCOFRQ[7:6];SYNDIV[5:0]
// fVCO= 2*fOSC*(SYNDIV + 1)/(REFDIV + 1)
// fPLL= fVCO/(2 × POSTDIV)
// fBUS= fPLL/2
// VCOCLK Frequency Ranges VCOFRQ[7:6]
// 32MHz <= fVCO <= 48MHz 00
// 48MHz < fVCO <= 80MHz 01
// Reserved 10
// 80MHz < fVCO <= 120MHz 11
REFDV=0x80 | 0x01; // REFFRQ[7:6];REFDIV[5:0]
// fREF=fOSC/(REFDIV + 1)
// REFCLK Frequency Ranges REFFRQ[7:6]
// 1MHz <= fREF <= 2MHz 00
// 2MHz < fREF <= 6MHz 01
// 6MHz < fREF <= 12MHz 10
// fREF > 12MHz 11
// pllclock=2*osc*(1+SYNR)/(1+REFDV)=32MHz;
POSTDIV=0x00; // 4:0, fPLL= fVCO/(2xPOSTDIV)
// If POSTDIV = $00 then fPLL is identical to fVCO (divide by one).
_asm(nop); // BUS CLOCK=16M
_asm(nop);
while(!(CRGFLG_LOCK==1)); //when pll is steady ,then use it;
CLKSEL_PLLSEL =1; //engage PLL to system;
}
void write_cmd(unsigned char cmd,unsigned BusyC) {
if (BusyC) ReadStatus(); //Test it busy or not
LCDIO=cmd;
LCD1602_RS=1;
LCD1602_RW=0;
LCD1602_EN=0;
LCD1602_EN=0;
LCD1602_EN=1;
}
void write_data(uchar dat,unsigned BusyC) {
if (BusyC) ReadStatus(); //Test it busy or not
LCDIO=dat;
LCD1602_RS=1;
LCD1602_RW=0;
LCD1602_EN=0;
LCD1602_EN=0;
LCD1602_EN=1;
}
int ReadStatus(void)
{
uchar cRtn;
LCDIO_DIR = LCDIO_DIR_IN;
LCD1602_RS = 0;
LCD1602_RW = 1;
LCD1602_EN = 0;
LCD1602_EN = 0;
LCD1602_EN = 1;
while (LCDIO&0x80); //Test Busy State
cRtn = LCDIO_DIR; // if Not save the port value, it should be change
LCDIO_DIR = LCDIO_DIR_OUT;
return(cRtn);
}
void LcdDelay(){
int i, j;
for (i = 0; i < 300; i++)
for(j = 0; j < 3000; j++);
}
void lcd_init() {
DDRA = 0xff;
DDRB = 0xff;
LCDIO_DIR = LCDIO_DIR_OUT;
LcdDelay();
write_cmd(0x01,0); //clear screen
write_cmd(0x38,1); //set 8 bit data transmission mode
write_cmd(0x06,1); // cursor right, disable moving
write_cmd(0x0f,1); //open display (enable lcd display)
write_cmd(0x80,1); //set lcd first display address
write_cmd(0x01,1); //clear screen
}
void main(void) {
/* put your own code here */
SetBusCLK_16M();
lcd_init();
EnableInterrupts;
for(;;) {
write_data('f',1);
DDRE_DDRE5=1;
PORTE_PE5=0;
_FEED_COP(); /* feeds the dog */
} /* loop forever */
/* please make sure t
欢迎光临 智能车制作 (http://111.231.132.190/)
Powered by Discuz! X3.2