⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ledtest._c

📁 使用atmel128对45db161读写程序加LM2009R大液晶显示
💻 _C
字号:

//ICC-AVR application builder : 2008-1-23 9:13:29
// Target : M128
// Crystal: 8.0000Mhz
//#include <iom128v.h>
//#include <macros.h>
#include "2008.h"

int I_Timer=0;
int Field = 512;
uint xx[512];
//#define portout (*(volatile unsigned char*)0x1100)

void port_init(void)
{
 PORTA = 0XFF;
 DDRA  = 0XFF;
 PORTF = 0XFF;
 DDRF  = 0XFF;
 PORTB = 0xFF;
 DDRB  = 0xF7;
 PORTD = 0xFF;
 DDRD  = 0xFF;
 PORTE = 0xFF;
 DDRE  = 0xFF;
 PORTG = 0XFF;
 DDRG  = 0Xf7;
}

//TIMER0 initialize - prescale:1024
// WGM: Normal
// desired value: 20mSec
// actual value: 19.968mSec (0.2%)
void timer0_init(void)
{
 TCCR0 = 0x00; //stop
 ASSR  = 0x00; //set async mode
 TCNT0 = 0x64; //set count
 OCR0  = 0x9C;
 TCCR0 = 0x07; //start timer
}

#pragma interrupt_handler timer0_ovf_isr:17
void timer0_ovf_isr(void)
{  
  I_Timer++;
  if(I_Timer == 50)
  {
   PORTG ^= 0x10;
   I_Timer =0;
  }
  TCNT0 = 0x64; //reload counter value
}

//call this routine to initialize all peripherals
void init_devices(void)
{
 //stop errant interrupts until set up
 CLI(); //disable all interrupts
 XDIV  = 0x00; //xtal divider
 XMCRA = 0x00; //external memory
 port_init();
 timer0_init();
 MCUCR = 0x00;
 EICRA = 0x00; //extended ext ints
 EICRB = 0x00; //extended ext ints
 EIMSK = 0x00;
 TIMSK = 0x01; //timer interrupt sources
 ETIMSK = 0x00; //extended timer interrupt sources
 SEI(); //re-enable interrupts
 //all peripherals are now initialized
}
void main()
{
 int i,j;
 unsigned char readXDIV;
	
 init_devices();
	
	
	
 initLCDM();     //初始化LCD
 Welcome_interface();  //显示欢迎界面 
 
 SdCmd(0x10,0x29); LCD_ChkBusy();
 SdCmd(0xf1,0x0f);LCD_ChkBusy();     //设置字型大小
 ShowText(6,2,"测试 Flash AT45DB161D 读写");
 for ( i=1; i<=48;i++) 
  {
     PORTG &= 0x04;
     PORTG ^= 0X01;
     PORTG ^= 0x02;
  }
  //=============================
  
   df_init();  //端口初始化
   j=0;
 /*kl:
  for(i=0;i<512;i++)
	xx[i]=3;    //初始化

  //测试写
	df_write_open(4099*Field);
	//for(i=0;i<5;i++)
	// {
	    df_write(xx,512);
	// }
	df_write_close();
	j++;

   if(j<10)
   {
    goto kl;
   }

*/
//	Write_page(2049,0,xx,512); //读页 一 页地址,  二页内地址,  三 数组存放取出来的数,  四 取多少个字节
	//=============================================
  df_read_open(4099*Field);
//	for(j=0;j<5;j++)
//	{
	 df_read(xx,512);
//	}
//	df_read_close();
//   Page_Scrape(2049);  //页擦除
 	//  Read_Page(2049,0,xx,512); //读页 一 页地址,  二页内地址,  三 数组存放取出来的数,  四 取多少个字节
 	//  df_read(1,1,xx,128);

  ShowNumber(0,0,512,xx);

  //=========================
 while(1)
 { 
  ;// portout = 0xaa;
 }
}
//============================
void initLCDM(void)
{
    PORTF |= 0XFF;
    SetCS;
	SetRD;
	SetWR;
	SetRS;
   // SetRST;
	//delay_ms(30);
	//ClrRST;
	//delay_ms(800);
  //  SetRST;
	//delay_ms(10);//
	SdCmd(0xa0,0x07); LCD_ChkBusy();  //使能 BUSY 输出
	SdCmd(0x81,0x40); LCD_ChkBusy();   //by default	
	SdCmd(0x00,0xCD); LCD_ChkBusy();  //Whole Chip LCD Controller Reg(Char Mode)
	SdCmd(0x01,0xf2); LCD_ChkBusy();   //BUSY=HiPolity, ClockOut=enable, InternalFreq=4MHz 	
	SdCmd(0x10,0x29); LCD_ChkBusy();   //Whole Chip Cursor Control Reg(disable cursor)
	SdCmd(0x11,0x10); LCD_ChkBusy();  //Distance of Words or Lines Reg(cursor=1,gap=0)
	SdCmd(0x90,0x05); LCD_ChkBusy();  //Shift Clock Control Reg(320x240,69Hz)
	SdCmd(0xf1,0x0f); LCD_ChkBusy();   //设置字型大小
	SdCmd(0x20,0x27); LCD_ChkBusy();  //Active Window Right  Reg
    SdCmd(0xf0,0xa0); LCD_ChkBusy();   //Font Control Reg
	SdCmd(0x30,0xef); LCD_ChkBusy();   //Active Window Bottom Reg(240 duty)
	SdCmd(0x40,0x00); LCD_ChkBusy();   //Active Window Left   Reg
	SdCmd(0x50,0x00); LCD_ChkBusy();   //Active Window Top    Reg
	SdCmd(0x21,0x27); LCD_ChkBusy();   //Display Window Right  Reg
 	SdCmd(0x30,0xef); LCD_ChkBusy(); // Display Window Bottom Reg(240 duty)
	SdCmd(0x41,0x00); LCD_ChkBusy();   //Display Window Left   Reg
    SdCmd(0x51,0x00); LCD_ChkBusy();  // Display Window Top    Reg
}
//====================================
//-----------------------------------
void SdCmd(uchar Command,uchar Com_Data)   //send command
{   
   SetWR;
   SetRD;
   ClrRS;   //RS = 0 输入命令  
   ClrCS;
   PORTF = Command;
    asm(" nop ");
   ClrWR;  //写使能
   asm(" nop ");   // 8MHZ 执行一条指令大约125ns
   asm(" nop ");
   SetWR;
   SetCS;
//====================================
   asm(" nop ");   // 8MHZ 执行一条指令大约125ns
   asm(" nop ");
   SetWR;
   SetRD;
   ClrRS;   //RS = 0 输入命令  
   ClrCS;
   PORTF = Com_Data;
    asm(" nop ");
   ClrWR;  //写使能
   asm(" nop ");   // 8MHZ 执行一条指令大约125ns
   asm(" nop ");
   SetWR;
   SetCS;
}
//===============================================
void SdData(uchar DData)    //send display data
{   
   SetWR;
   SetRD;
   SetRS;     //RS = 1 输入数据 
   ClrCS;
   PORTF = DData;
   asm(" nop ");
   ClrWR;    //写使能
   asm(" nop ");   // 8MHZ 执行一条指令大约125ns
   asm(" nop ");
   SetWR;
   SetCS;
}
//====================================
//=================================
void LCD_ChkBusy(void)
{
	uchar TempM = 0x08;
	DDRG = 0X00;           //将端口设为输入 
	while(TempM)
	{
       TempM = PING;
       TempM &= 0x08;    //读取DB.3位
	}
    DDRG = 0XFF;        //将端口设为输出
}
//====================================
void Welcome_interface(void)
{
  SdCmd(0x12,0x95);LCD_ChkBusy();  //操作图层1
  CleanScreen();
  SdCmd(0xf1,0x1f);LCD_ChkBusy();     //设置字型大小
  //SdCmd(0x60,0x0a);LCD_ChkBusy();     //设置光标X坐标
  //SdCmd(0x70,0x4b);LCD_ChkBusy();     //设置光标Y坐标
  ShowText(10,75,"金特维");  
  SdCmd(0xf1,0x0f);LCD_ChkBusy();     //设置字型大小
  //SdCmd(0x60,0x10);LCD_ChkBusy();     //设置光标X坐标
  //SdCmd(0x70,0x55);LCD_ChkBusy();     //设置光标Y坐标
  ShowText(16,85,"技术有限公司");
  SdCmd(0xf0,0xa0);LCD_ChkBusy();     //设置为简体字型
  SdCmd(0xf1,0x5f);LCD_ChkBusy();     //设置字型大小
  SdCmd(0x10,0x79);LCD_ChkBusy();     //设置光标及粗体显示
  //SdCmd(0x60,0x09);LCD_ChkBusy();     //设置光标X坐标
  //SdCmd(0x70,0x72);LCD_ChkBusy();     //设置光标Y坐标
  ShowText(9,114,"GTW - 2008");
  
}
//==================================
void CleanScreen()
{  
   uchar i,j;
   asm(" cli ");  //加入禁止中断,以免人为的外界的中断频繁产生从而使显示屏无反应,即死屏现象
   SdCmd(0x60,0x00); LCD_ChkBusy();  //set cursor X location
   SdCmd(0x70,0x00); LCD_ChkBusy();  //set cursor Y location
  	for(i=0;i<40;i++)
	 {
      	for(j=0;j<240;j++)
     		{
     		  SdData(0x00);
			  LCD_ChkBusy();
 		    }
     }
   asm(" sei ");  //开户中断
}
//---------------------------------
void ShowText(uchar x, uchar y,uchar *text)
{
    SdCmd(0x00,0xCD);LCD_ChkBusy();   //send command normal power mode, Char mode    display on, normal display 
    SdCmd(0x60,0x00+x);LCD_ChkBusy();  //set cursor X location to 0
    SdCmd(0x70,0x00+y);LCD_ChkBusy();  //set cursor Y location to 0
	while(*text>0)
	 {
	   	SdData(*text);
		text++;
		LCD_ChkBusy();
	 }
}
//====================================
// 显示数字
//====================================
void ShowNumber(uchar x, uchar y,uint n, uint* number7)
{  
    uint i, number, showNum1,showNum2,showNum3,showNum4,showNum5,showNum6;
    SdCmd(0x00,0xCD);LCD_ChkBusy();   //send command normal power mode, Char mode    display on, normal display 
    SdCmd(0x60,0x00+x);LCD_ChkBusy();  //set cursor X location to 0
    SdCmd(0x70,0x00+y);LCD_ChkBusy();  //set cursor Y location to 0
   for( i=0;i<n;i++ )
   {
	
	number = *number7;
	number7++;
	showNum1 = (number/100000);
    showNum2 = ((number%100000)/10000);
    showNum3 = ((number%10000)/1000);
    showNum4 = ((number%1000)/100);
    showNum5 = ((number%100)/10);
    showNum6 = ((number%10)/1);

   if(number>=100000)
   {
    showNum1+=48;
	SdData(showNum1);
	LCD_ChkBusy();
   }
   if(number>=10000)
   {
    showNum2+=48;
	SdData(showNum2);
	LCD_ChkBusy();
   }
   if(number>=1000)
   {
    showNum3+=48;
	SdData(showNum3);
	LCD_ChkBusy();  
   }
   if(number>=100)
   {
    showNum4+=48;
	SdData(showNum4);
	LCD_ChkBusy(); 
   }  
   if(number>=10)
   {
    showNum5+=48;
	SdData(showNum5);
	LCD_ChkBusy();
   }
    showNum6+=48;
	SdData(showNum6);
	LCD_ChkBusy();   
 }    
}
//==================================
//-----------------------------------
void delay_1us(void)
{
	asm("nop");
	asm("nop");
	asm("nop");
	asm("nop");
	asm("nop");
	asm("nop");
	asm("nop");
	asm("nop");
}
void delay_1ms(void)
{
	unsigned int i;
	for(i=1; i<(unsigned int)(8*143-2); i++)
	;
}

void delay_ms(unsigned int n)
{
	unsigned int i;
	for(i=0; i<n; i++)
	delay_1ms();
}
void delay_us(unsigned int n)
{
	unsigned int i;
	for(i=0; i<n; i++)
	delay_1us();
}
//-----------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -