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

📄 main__.c

📁 该设备是一款基于AVR mega16L单片机的电子书阅读器
💻 C
📖 第 1 页 / 共 3 页
字号:
      lcd_write_data(*s);
      s++;     
   }
}
void lcd_write_string(unsigned char  *s)
{   
  while(*s>0)
   {  
      lcd_write_data(*s);
      s++;     
   }
}
void lcd_clear(void)
{
 lcd_write_command(0x01);
}
void lcd_cursor_flash(unsigned char i)
{
  if(i==1)
  {
   lcd_write_command(0x0f);
  }
  else if (i==0)
         {
          lcd_write_command(0x0c);
         }
}
void lcd_write_full(unsigned char *s)
{
  unsigned char i;
  for (i=0;i<64;i++)
  { 
   if (i==0)
     lcd_write_command(0x80);
     else if(i==16)
           lcd_write_command(0x90);
           else if(i==32) 
                  lcd_write_command(0x88);
                  else if (i==48)
                          lcd_write_command(0x98); 
        lcd_write_data(*s);
        s++;         
  }
}  
/*------------------自定义一个字符代码(一个星星和月亮)--------------------------*/
/*
void CGRAM()
{ int i;
     lcd_write_command(0x30); 
     lcd_write_command(0x40);
     for(i=0;i<16;i++)
     {
      lcd_write_data(zk[i*2]);
      lcd_write_data(zk[i*2+1]);
      }
}      
/*------------------显示字符串--------------------------*/
/*void hzkdis(unsigned char flash *s)
{ while(*s>0)
   { lcd_write_data(*s);
      s++;
      delay_ms(1);
   }
}

/*------------------显示图片------------------------2005.11.14.21:20修改---benladn911---*/
/*void Disp_Img(unsigned char flash *img)
{ unsigned int j=0;
   unsigned char x,y,i;
       for(i=0;i<9;i+=8)
       for(y=0;y<32;y++)/*原来 为 y<26 ,上下两个半屏不能正常对接显示,导致显示的图片中间有空隙*/
     /*    for(x=0;x<8;x++)
         {  lcd_write_command(0x36);//功能设置---8BIT控制界面,扩充指令集
            lcd_write_command(y+0x80);        //行地址
            lcd_write_command(x+0x80+i);     //列地址
            lcd_write_command(0x30);   //基本指令集
            lcd_write_data(img[j++]);
            lcd_write_data(img[j++]);
         }   
           
}


*/
/*------------------清整个GDRAM空间----------------------------*/
/*void clrgdram()
{ unsigned char x,y;
        for(y=0;y<64;y++)
         for(x=0;x<16;x++)
         { lcd_write_command(0x34);
            lcd_write_command(y+0x80);        //行地址
            lcd_write_command(x+0x80);     //列地址
            lcd_write_command(0x30);
            lcd_write_data(0x00);
            lcd_write_data(0x00);
                               }
   }
/*------------------整屏写入数据:全亮,或者竖条-----------------------------*/
/*void lcdfill(unsigned char disdata)
{ unsigned char x,y;
        for(y=0;y<32;y++)
         for(x=0;x<16;x++)
         { lcd_write_command(0x36);
            lcd_write_command(y+0x80);        //行地址
            lcd_write_command(x+0x80);     //列地址
            lcd_write_command(0x30);
            lcd_write_data(disdata);
            lcd_write_data(disdata);
            }
   lcd_write_command(0x34);                            
   lcd_write_command(0x36);
   }
/*------------------显示横-----------------------------*/
/*void Disp_H_Line()
{ unsigned char x,y;
   unsigned char k=0x00; 
       for(y=0;y<32;y++)
         { k=~k;
           for(x=0;x<16;x++)
         { lcd_write_command(0x36);
            lcd_write_command(y+0x80);      //行地址
            lcd_write_command(x+0x80);     //列地址
            lcd_write_command(0x30);
            lcd_write_data(k);
            lcd_write_data(k);
           };
         }
     lcd_write_command(0x34);                          
   lcd_write_command(0x36);
}

 
/*绘图显示的步骤
 1 关闭绘图显示功能
 2 先将垂直的坐标(Y)写入绘图RAM地址
 3 再将水平的位元组坐标(X)写入绘图RAM地址
 4 将D15-D8写入RAM中
 5 将D7-D0写入到RAM中
 6 打开绘图显示功能*/
//整屏显示竖条
//当ii=0时显示上面128×32,当ii=8时显示下面128×32
/*void LcdFill_Vertical()
{  unsigned char x,y,ii;
   for(ii=0;ii<9;ii+=8)   
     for(y=0;y<0x20;y++)     
      for(x=0;x<8;x++)
      { 
      lcd_write_command(0x36);//扩充指令 绘图显示
         lcd_write_command(y+0x80);        //行地址
         lcd_write_command(x+0x80+ii);     //列地址     
         lcd_write_data(0x55); //写数据 D15-D8 
         lcd_write_data(0x55); //写数据 D7-D0  
   lcd_write_command(0x30);
      } 
}
//当ii=0时显示上面128×32,当ii=8时显示下面128×32
//整屏显示横条线
void LcdFill_Level()
{  unsigned char x,y,ii;
   unsigned char k;   
   for(ii=0;ii<9;ii+=8)    
      for(y=0;y<0x20;y++)
      {     if((y%2)==1)
               k=0xff;
            else
               k=0;
            for(x=0;x<8;x++)
            {  lcd_write_command(0x36);  //扩充指令 绘图显示
               lcd_write_command(y+0x80);    //行地址
               lcd_write_command(x+0x80+ii);    //列地址            
               lcd_write_data(k);
               lcd_write_data(k);
               lcd_write_command(0x30);
            }
       }     
} 
/****************************************************************************** 
 * PC/AT keyboard driver. 
 * Based in part on Atmel's application note AVR313. www.atmel.com
******************************************************************************/
#include <mega16.h>
#include <key.h>
#include <scancodes.h> 
#include <delay.h>  

/******************************************************************************
 * InitializeKeyboard()
 * Initializes the ports and control registers needed for running the keyboard.
******************************************************************************/
unsigned char edge;        /* 0 = neg.  1 = pos. */
unsigned char bitcount;
void InitializeKeyboard( void )
{   
   GICR =0x40;     /* enable INT0 interrupt */
   #asm("sei");                /* enable global intterupts */
   MCUCR =0x02;    /* trigger INT0 on falling edge */
   DDRD&=~0x0c; 
   PORTD|=0x0c;
    /* PD2, PD3 are inputs */	
   edge = 0;
   bitcount = 11;
   return;
}
/******************************************************************************
 * interrupt [EXT_INT0] void ext_int0_isr (void)
 * When the keyboard triggers the external interrupt this function is called
 * to retrieve the data.
******************************************************************************/
interrupt [EXT_INT0] void ext_int0_isr (void)
{
   static unsigned char    scancode = 0;
    if(!edge)                               // Routine entered at falling edge
    {
        if( ( bitcount < 11 ) && ( bitcount > 2 ) )    
        {
           // Bit 3 to 10 is data. Parity bit, start and stop bits are ignored.
            scancode = ( scancode >> 1 );
            if( PIND&0x08 )            //如果数据是1 PD3是键盘数据输入端,PO2是中断口
            {
                scancode = scancode | 0x80;  // Store a '1' for this bit
            }
        }
          MCUCR=0x03;                 // Set interrupt on rising edge
          edge = 1;        
    } 
    else                                     // Routine entered at rising edge
    {                                
        MCUCR =0x02;                 // Set interrupt on falling edge
        edge = 0;
        if(--bitcount == 0)                  // All bits received
        {
           bitcount = 11;
           key= Decode( scancode );
        }
    }
}
unsigned char Decode( unsigned char scancode )
{      
       int i = 0; 
       unsigned char temp = 0;
       static unsigned char   shift = 0;
       unsigned char key_value=0;
      if( ( scancode == 0x12 ) || ( scancode == 0x59 ) ) 
           {
              // left or right shift按住SHIFT键
              shift = 1;              
           }
           else                                             
           {
              // a key other than shift was pressed, do a table look-up      
              for( i = 0; i < 50; i++ )//查表,寻找对应键值
              {
	       temp = char_table[i*3];//PRG_RGB可能因为要用FLASH中数据要用此函数
          	                                                 //i*3应该是表中各键值对应初始地址,因为一块就要三个数据
               if ( temp == scancode ) 
                 {
                    if( shift )                              //如果按了SHIFT就取后面那个大些的字母
                    {
                       key_value = char_table[i*3+2];
                    }
                    else
                    {
		       key_value = char_table[i*3+1];
                    }
		    break;
                 }
              }		       
              shift = 0;             
           } 
         delay_ms(20);
         return  key_value;               
}


#include <mega16.h>
#include <flashic.h>
//PD4是RDY/BUSY
void Initi_Flash( void )
{
   SPCR=0x5C ;  // 0101 1100   SCK在闲置时是高电平,SPI是模式3     
                            //移出数据是下降沿,锁存数据是上升沿
   DDRB=0xB0;  // all outputs except PB6= MISO 1011 1111
   DDRD&=0xef;
   PORTD&=0xef;                   //MISO是输入,其他都是输出
   PORTB=0x00;                   
   return;
}
/******************************************************************************
 * MasterOut()
 * Sends data to the flash memory. Finish = 1 if this is the last byte of the
 * transfer, 0 otherwise. Command = 1 if this is a command to send, 0 if
 * to recieve data.
******************************************************************************/
char MasterOut( char data, char finish, char isCommand )
{
   if( isCommand )   
     PORTB&=~0x10;  //将PB4清零sbi是置位即使~CS清零,处低位  
   SPDR=data ;   //将要发送数据写入SPDR,将启动或准备数据传送   
   // wait for transfer to complete, indicated by the interrupt flag 
   while( !( SPSR&0x80 ) );//当SPSR中,串行传送完成时,SPIF位置1
   if( finish )//如果这是最后一个字节,FINISH就是1
   {
    PORTB|=0x10; //~CS置位,由低电平变成高电平,实现命令
   }
   return (  SPDR );
}
/******************************************************************************
 * WriteBufferToMemory()
 * Writes the given buffer to memory. The page address is 10 bits.
 * pageAddrHigh should be xxxxx987 
 * pageAddr  should be 6543210x
 * where x is a 0 and a number is the bit of the page address.
******************************************************************************/
//将字符数组内容写到闪存上
//参数为 数组地址,数组长度,写到的闪存地址
void WriteBufferToMemory( unsigned char *buffer, unsigned char bufferLength,
                          unsigned int pageAddr )
{
   unsigned char  i        = 0;
   unsigned char  j        = 0;
   for( i = 0; i < bufferLength; i++ )
   {    // write each byte to the internal SRAM buffer of the flash memory 
      for( j = 0; j < 255; j++ );
      while(!PIND&0x10);        //每写一个字节看来就要写下字节地址,无效位,内容
      MasterOut( 0x84, 0, 1 );   // select buffer 1 
      MasterOut( 0x00, 0, 1 );   // don't care 
      MasterOut( 0x00, 0, 1 );   // don't care + 1 addr 
      MasterOut( i, 0, 1 );      // 8 address bits for address in buffer 
                                 //从BUFFER1的第一个地址开始写,它能存贮264个字节                              
      MasterOut( buffer[i], 1, 1 );
   } 
   while(!PIND&0x10);        //每写一个字节看来就要写下字节地址,无效位,内容
   // once the given buffer has been written to the SRAM buffer,
   // write it to the main memory array  
   MasterOut( 0x83, 0, 1 );   // copy buffer 1 to main memory w/ builtin erase
   MasterOut( pageAddr>>8, 0, 1 );   // pageAddrHigh
   MasterOut( pageAddr, 0, 1 );
   MasterOut( 0x00, 1, 1 );      // don't cares 
   PORTB|=0x10;
   delay_ms(150); //建议延时
   return;
}
/******************************************************************************
 * ReadMemoryToBuffer()
 * Reads data from memory. The page address is 10 bits.
 * pageAddrHigh should be xxxxx987 
 * pageAddr  should be 6543210x
 * where x is a 0 and a number is the bit of the page address.
******************************************************************************/
void ReadMemoryToBuffer( unsigned char *buffer, unsigned char bufferLength,
                         unsigned int pageAddr )
{
   unsigned char  i        = 0;
   unsigned char  j        = 0; 
       
   for( j = 0; j < 255; j++ );
   while(!PIND&0x10);   
   MasterOut( 0x52, 0, 1 );
   MasterOut( pageAddr>>8, 0, 1 );    // pageAddrHigh
   MasterOut( pageAddr, 0, 1 );
   MasterOut( 0x00, 0, 1 );   // byte offset w/in page 
   MasterOut( 0x00, 0, 1 );   // 32 don't care bits 
   MasterOut( 0x00, 0, 1 );
   MasterOut( 0x00, 0, 1 );
   MasterOut( 0x00, 0, 1 );
   for( i = 0; i < bufferLength; i ++ )
   {
      buffer[i] = MasterOut( 0x00, 0, 0 );
     // if( ( buffer[i] < ' ' ) || ( buffer[i] > '~' ) )
     // {
     //    buffer[i] = ' ';
    //  }
   }
     PORTB|=0x10;
     delay_ms(150); //建议延时
     
   return;
}

⌨️ 快捷键说明

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