📄 avr-lm2029(r8803).txt
字号:
#include <iom128.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
#define uchar unsigned char // 0~255
#define uint unsigned int // 0~65535
#define SteelWireRadius 0.01
#define RollerRadius 0.5
#define pi 3.1415
//#define IniHight 10
#define _WR PORTG_Bit0
#define _RD PORTG_Bit1
#define _CS PORTF_Bit7
#define RS PORTF_Bit6
#define _RES PORTF_Bit5
#define LCD_BUSY PORTF_Bit4
#define LCDBUS PORTC
#define LED_0 PORTF_Bit0
#define LED_1 PORTF_Bit1
#define LED_2 PORTF_Bit2
#define LED_3 PORTF_Bit3
unsigned char key;
uchar ChangeCharHightNumI=5;
uchar CharHight[5];
//-----------------------------------
// dispaly data (320x240)
//-----------------------------------
//-----------------------------------
// Char dispaly data (20x15 char)
//-----------------------------------
// TOPWAY Text LOGO
unsigned char TextLogo[]={
" "
" "
" -- 信息与控制工程学院 -- "
" University "
" "
" "
" "
" "
" "
" "
" "
" "
"acbdefghijklmnopqrstuvwxyzabocdfghigklmn"
"ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMN"
"1234567890123456789012345678901234567890"
};
//-----------------------------------
// Delay Routine
//-----------------------------------
void delayms(uint m) // 12MHz Xtal, close to ms value
{
uint j;
uint i;
for(i=0; i<m; i++)
for(j=0; j<124; j++)
asm("nop");
}
//-----------------------------------
// IO Routine
//-----------------------------------
void SdCmd(uchar Command) //send command
{
_WR = 1; // init all control signal
_RD = 1;
RS = 0; // for command
LCDBUS = Command;
_CS = 0; // enable the access
asm("nop");
_WR = 0;
asm("nop");
_WR = 1;
asm("nop");
_CS = 1; // disable the access
}
void SdData(uchar DData) //send display data
{
_WR = 1; // init all control signal
_RD = 1;
RS = 1; // for diaplay data
LCDBUS = DData;
_CS = 0; // enable the access
asm("nop");
_WR = 0;
asm("nop");
_WR = 1;
asm("nop");
_CS = 1; // disable the access
}
//-----------------------------------
// Write a Text Screen
//-----------------------------------
void WriteTextScreen(uchar *TxtData)
// DisplayData should be 20(x2)x15 = 600byte
// set to char display
{
uchar TempData;
uchar i,j;
SdCmd(0x00); SdCmd(0xCD); delayms(1); // normal power mode, Char mode
// display on, normal display
SdCmd(0x60); SdCmd(0x00); delayms(1); // set cursor X location to 0
SdCmd(0x70); SdCmd(0x00); delayms(1); // set cursor Y location to 0
for (j=0; j<15; j++)
{
for(i=0; i<40; i++)
{
TempData=(*(TxtData+(j*40)+i));
SdData(TempData);
asm("nop");asm("nop");asm("nop");asm("nop");
asm("nop");asm("nop");asm("nop");asm("nop");
}
}
}
//-----------------------------------
// Init LCD module
//-----------------------------------
void initLCDM(void)
{
_RES = 1; // reset pin
_RES = 0;
delayms(10);
_RES = 1;
delayms(300);
SdCmd(0x00);SdCmd(0xCD); delayms(1); // Whole Chip LCD Controller Reg(Char Mode)
SdCmd(0x01);SdCmd(0xf1); delayms(1); // BUSY=HiPolity, ClockOut=enable, InternalFreq=4MHz
SdCmd(0x10);SdCmd(0x29); delayms(1); // Whole Chip Cursor Control Reg(disable cursor)
SdCmd(0x11);SdCmd(0x10); delayms(1); // Distance of Words or Lines Reg(cursor=1,gap=0)
SdCmd(0x90);SdCmd(0x05); delayms(1); // Shift Clock Control Reg(320x240,69Hz)
SdCmd(0xF0);SdCmd(0xA0); delayms(1); // Font Control Reg
SdCmd(0x20);SdCmd(0x27); delayms(1); // Active Window Right Reg
SdCmd(0x30);SdCmd(0xEF); delayms(1); // Active Window Bottom Reg(240 duty)
SdCmd(0x40);SdCmd(0x00); delayms(1); // Active Window Left Reg
SdCmd(0x50);SdCmd(0x00); delayms(1); // Active Window Top Reg
SdCmd(0x21);SdCmd(0x27); delayms(1); // Display Window Right Reg
SdCmd(0x31);SdCmd(0xEF); delayms(1); // Display Window Bottom Reg(240 duty)
SdCmd(0x41);SdCmd(0x00); delayms(1); // Display Window Left Reg
SdCmd(0x51);SdCmd(0x00); delayms(1); // Display Window Top Reg
SdCmd(0x81);SdCmd(0x40); delayms(1); // by default
}
//............................................................................
v
//。。。。主函数。。。。。。
void main( void )
{
unsigned char time1; //最初#define TCCLK01 PORTD_Bit6 //计数器1外部时钟输入
unsigned char time2 ;//最初#define TCCLK02 PORTD_Bit7 //计数器2外部时钟输入
//CharHight[5]='\0';//在字符数组最后一位添加结束符号
uint QuanNum=0;//从开机起转的总圈数,正转为正,反转为负
float IniHight=10.00;//初始高度暂定为十,实际中从eeprom中取
uint FloorNum=2;//初始层数设为2层,实际中从eeprom中取
float Hight;
//..............................
SREG=0x80; //打开全局中断
MCUCR=0X30; //打开SE,SM1位,允许SLEEP和POWER-DOWN
DDRB=0XF0;//将高四位配置为输出,低四位配置为输入
PORTB=0X0F;//将高四位全部输出零
EICRA=0X00; //设置外部中断为低电平产生中断请求
EIMSK=0X01; //允许外部中断
DDRF=0xEF;
DDRG=0x1F;
DDRC=0xFF;
_CS = 1;
_RD = 1;
_WR = 1;
RS = 1;
LCDBUS = 0xff; // pull up data bus
delayms(500); // delay to wait for LCD module OSC start up
initLCDM();
WriteTextScreen(TextLogo); delayms(1000);
//................................
DDRD=0X00;
TCCR0=0X06;//定时器0,普通模式 ,内部时钟256分频
TCCR1B=0X07;//定时器1,普通模式,外部时钟,上升沿驱动
TCCR2=0X07;//定时器2,普通模式,外部时钟 ,上升沿驱动
SREG=0x00; //关闭全局中断
TCNT0=0XD0;//定时器0初值208计时到256大约一秒
TCNT1=0;
TCNT2=0;//定时器1和2 计数初值为0
SREG=0x80; //打开全局中断
time1=PIND&0X40;
time2=PIND&0X80;
while(time1|time2) // while(1)
{
if(TIFR&0x01==0X01)//最初TIFR==0X01
{
if(TCNT1!=0)
{
QuanNum=QuanNum+TCNT1/4;//正方向转动频率
}
else
{
QuanNum=QuanNum-TCNT1/4;//反方向转动频率
}
TCCR0=0X07;//定时器0普通模式下时钟256分频
SREG=0x00; //关闭全局中断
TCNT0=0XD0;//定时器0初值208计时到256大约一秒
TCNT1=0;
TCNT2=0;//定时器1和2 计数初值为0
SREG=0x80; //打开全局中断
Hight=ComputeHight(IniHight,FloorNum,QuanNum);
TransferIntToChar(Hight,CharHight);
WriteLCDScreenHight(CharHight);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -