📄 testavrkit.i
字号:
const byte music_dt0[]=
{10,4 ,13,4 ,17,4 ,13,4 ,15,8 ,13,4 ,12,4 ,17,8 ,15,8 ,10,16 ,
13,4 ,17,4 ,20,4 ,20,4 ,22,8 ,20,4 ,18,4 ,17,16 ,
18,8 ,20,8 ,24,4 ,22,4 ,17,8 ,17,4 ,
12,8 ,10,4 ,17,4 ,15,4 ,18,8 ,18,4 ,0,4 ,
20,4 ,18,4 ,17,8 ,15,4 ,13,4 ,17,8 ,15,8 ,10,16 ,10,4 ,0,4 ,
0,8 ,0,0};
unsigned char delay_inc=0x0C;
unsigned char time_inc=0x01;
void delay(word t)
{
unsigned int i;
// uchar j;
for (i=0;i<t;i++) i+=delay_inc;
// for(j=1;j<8;j++);
}
void TestSound(void)
{
//struct MUSICDAT * music_dat;
//unsigned char c;
unsigned int i,j,k;
unsigned char name;
unsigned int time;
DDRD|=0x80;
TransmitLineF("Cable setting: Sound.SSD--PortD.PD7");
//if time_inc
while (1)
{
TransmitLineF("Please input Speed:");
time_inc=ReceiveByte();
i=0;
while (i<sizeof(music_dt0))
{
name=music_dt0[i];
time=music_dt0[i+1];
for (k=0;k<time;k++)
for (j=0;j<=time_inc;j++)
{
PORTD^=0x80;
delay(tone_frq[name]);
PORTD^=0x80;
delay(tone_frq[name]);
}
delay_ms(20);
i+=2;
}
}
//while ((name==0)&&(time==0));
}
/*****************************************************************************/
void TestDS1302(void)
{
//unsigned char State=0;
unsigned char Cmd;
//unsigned char i,j;
unsigned char RTC[6];
unsigned char Buffer[10];
TransmitLineF("Cable setting: ");
TransmitLineF(" VCC <--> DS1302.VCC");
TransmitLineF(" PORTC.4 <--> DS1302.SCL");
TransmitLineF(" PORTC.5 <--> DS1302.SDA");
TransmitLineF(" PORTC.6 <--> DS1302.RST");
TransmitLineF(" GND <--> DS1302.GND");
TransmitLineF(" ");
TransmitLineF("DS1302 Menu");
TransmitLineF("---------------------");
TransmitLineF("1.Start/Stop Timer");
TransmitLineF("2.Get Date");
TransmitLineF("3.Get Time");
TransmitLineF("4.Reset Date");
TransmitLineF("5.Reset Time");
TransmitLineF("---------------------");
TransmitLineF("Please choose item:");
while (1)
{
Cmd=ReceiveByte();
switch (Cmd)
{
case '1':
{
rtc_switch();
rtc_get_time(&RTC[2],&RTC[1],&RTC[0]);
TransmitStringF("DS1302 state is :");
ByteToHexBuffer(RTC[0],&Buffer[0]);
Buffer[2]=0;
TransmitLine(Buffer);
break;
}
case '2':
{
rtc_get_date_bcd(&RTC[5],&RTC[4],&RTC[3]);
ByteToHexBuffer(RTC[3],&Buffer[0]);
Buffer[2]='-';
ByteToHexBuffer(RTC[4],&Buffer[3]);
Buffer[5]='-';
ByteToHexBuffer(RTC[5],&Buffer[6]);
Buffer[8]=0;
TransmitStringF("Current date is 20");
TransmitLine(Buffer);
break;
}
case '3':
{
rtc_get_time_bcd(&RTC[2],&RTC[1],&RTC[0]);
ByteToHexBuffer(RTC[2],&Buffer[0]);
Buffer[2]=':';
ByteToHexBuffer(RTC[1],&Buffer[3]);
Buffer[5]=':';
ByteToHexBuffer(RTC[0],&Buffer[6]);
Buffer[8]=0;
TransmitStringF("Current time is ");
TransmitLine(Buffer);
break;
}
case '4':
{
RTC[3]=1; //date
RTC[4]=1; //month
RTC[5]=0; //year
rtc_set_date(RTC[3],RTC[4],RTC[5]);
delay_ms(10);
rtc_get_date_bcd(&RTC[5],&RTC[4],&RTC[3]);
ByteToHexBuffer(RTC[3],&Buffer[0]);
Buffer[2]='-';
ByteToHexBuffer(RTC[4],&Buffer[3]);
Buffer[5]='-';
ByteToHexBuffer(RTC[5],&Buffer[6]);
Buffer[8]=0;
TransmitStringF("Reset date is 20");
TransmitLine(Buffer);
break;
}
case '5':
{
RTC[2]=0; //
RTC[1]=0; //month
RTC[0]=0; //year
rtc_set_time(RTC[2],RTC[1],RTC[0]);
delay_ms(10);
rtc_get_time_bcd(&RTC[2],&RTC[1],&RTC[0]);
ByteToHexBuffer(RTC[2],&Buffer[0]);
Buffer[2]=':';
ByteToHexBuffer(RTC[1],&Buffer[3]);
Buffer[5]=':';
ByteToHexBuffer(RTC[0],&Buffer[6]);
Buffer[8]=0;
TransmitStringF("Reset time is ");
TransmitLine(Buffer);
break;
}
default :
{
TransmitLineF("Bad Command.");
}
}
}
}
/*****************************************************************************/
void InitUSB(void)
{
PORTC=0x00;
DDRC=0x00;
PORTA=0B00000011;
DDRA= 0B00000011;
PORTA.0=1;
PORTA.1=1;
}
void TransData(unsigned char Data)
{
while (PINA.2==1) ;
DDRC=0xFF;
PORTC=Data;
PORTA.1=0;
PORTA.1=1;
}
unsigned char ReceiveData(void)
{
DDRC=0x00;
while (PINA.3==1) ;
PORTA.0=0;
PORTA.0=1;
return PINC;
}
void TransStr(flash unsigned char * ss)
{
while (*ss)
{
TransData(*ss);
ss++;
}
}
void TestFT245BM(void)
{
unsigned char i;
TransmitLineF("Cable setting: ");
TransmitLineF("-----------------------------------------");
TransmitLineF(" USBP.UD0~UD7 <--> PortC.P0~P7");
TransmitLineF(" USBP.URD <--> PortA.P0");
TransmitLineF(" USBP.UWR <--> PortA.P1");
TransmitLineF(" USBP.UTE <--> PortA.P2");
TransmitLineF(" USBP.URE <--> PortA.P3");
TransmitLineF("-----------------------------------------");
Pause();
InitUSB();
for (i=0;i<255;i++)
{
TransData(i);
TransmitByte(i);
}
TransData(0xFF);
TransmitByte(0xFF);
TransStr("(USB)Hello World");
TransmitStringF("(COM)Hello World");
delay_ms(100);
while (1)
{
if (PINA.3==0)
{
i=ReceiveData();
TransmitByte(i);
}
if (UCSRA&(1<<7))
{
i=UDR;
TransData(i);
}
}
}
/*****************************************************************************/
void TestLCD (void)
{
unsigned char cnt,i;
unsigned char ss[3][17]={"DELPHI & AVR","CMD:","Command"};
unsigned char BUFFER[17];
unsigned char Index;
char *sp;
unsigned char Cmd;
TransmitLineF("Cable setting: DLED2 <--> PortA, DLED1 <--> PortC");
Pause();
DDRB=0xFF;
PORTB=0xFF;
for (i=1;i<2;i++)
delay_ms(1000); //LCD上电中,延迟2秒
LCD_Init(); //初始化LCD
Index=0;
cnt=1;
LCD_Ctrl(0B00000010 );
delay_us(150);
LCD_Ctrl(0B10000000);
sp=&ss[0][0];
LCD_PutStr(sp);
LCD_Ctrl(0xC0);
sp=&ss[1][0];
LCD_PutStr(sp);
delay_us(1000);
Cmd=0;
while (1)
{
if (UCSRA&(1<<7)) //串口有信息吗?
{
Cmd=UDR; //如果有,接收一个字节
if ((Cmd>=32) && (Cmd<=127))
{
BUFFER[Index]=Cmd;
if (Index++ >17 ) Index=0;
}
}
if (Cmd==0x0D )
{
LCD_Ctrl(0x90);
sp=&ss[2][0];
LCD_PutStr(sp);
delay_us(1000);
LCD_Ctrl(0xC0);
sp=&BUFFER[0];
LCD_PutStr(sp);
delay_us(1000);
Index=0;
for (i=0;i<17;i++) BUFFER[i]=0;
Cmd=0;
}
//if (cnt++>16) cnt=1;
}
}
/*****************************************************************************/
void TestAT24C04(void) //初始化各端口
{
unsigned int i;
unsigned char CmdBuf[17]; //命令缓冲区
unsigned char Chip; //芯片地址
unsigned int Addr; //内存地址
unsigned char AddrType;//内存地址格式 8 bit 或16 bit
unsigned int Cnt; //读取长度
unsigned char LED; //LED显示,作用不大
TransmitLineF("Cable setting: ");
TransmitLineF(" VCC <--> AT24C04.VCC");
TransmitLineF(" PortC.0 <--> AT24C04.SCL");
TransmitLineF(" PortC.1 <--> AT24C04.SDA");
DDRC=0xFF;
PORTC.2=0;
PORTC.3=0;
TransmitLineF(" GND <--> AT24C04.WP1");
TransmitLineF(" GND <--> AT24C04.WP2");
TransmitLineF(" GND <--> AT24C04.GND");
TransmitLineF(" ");
TWI_DefaultInit();
for (i=0;i<32 ;i++) CmdBuf[i]=0; //初始化命令缓冲区
LED=0;
while (1)
{
ReceiveCMD(CmdBuf); //读取一条命令行
if (CmdBuf[0]>0) //如果命令行长度不为零
{
//for (i=0;i<MAXCMDBUF;i++) TransmitByte(CmdBuf[i]);
TransmitByte(0x1B ); //送出命令开始符
TransmitByte(CmdBuf[1]); //送出命令
if (CmdBuf[1]=='H') //如果接收的是帮助命令
{
TransmitStringF("Command format(Hex):'1B CMD 1C'");
TransmitByte(13);
TransmitStringF("Read EEPROM Data:'R','Addr','Count'");
TransmitByte(13);
TransmitStringF("Write EEPROM Data:'W','Addr','Data'");
TransmitByte(13);
TransmitStringF("Show Help:'H'");
TransmitByte(13);
PORTB=~PORTB;
}
if (CmdBuf[1]=='R') //如果接收的是读取EEPROM命令
{
AddrType=HexToByte(&CmdBuf[2]); //EEPROM地址类型
Chip=(HexToByte(&CmdBuf[4])&0B00001110)|0xA0 ; //芯片地址
Addr=(((unsigned int)HexToByte(&CmdBuf[6]))<<8)|((unsigned int)HexToByte(&CmdBuf[8])); //内存地址
Cnt=(((unsigned int)HexToByte(&CmdBuf[10]))<<8)|((unsigned int)HexToByte(&CmdBuf[12])); //读写个数
if (Addr+Cnt>255) Cnt=255-Addr;
for (i=0;i<Cnt;i++)
{
TransmitString(ByteToHex(EEPROM_Read(Chip,AddrType,Addr+i))); //送回上位机
TransmitByte(32);
}
}
if (CmdBuf[1]=='W') //如果接收的是写入EEPROM命令
{
AddrType=HexToByte(&CmdBuf[2]);
Chip=(HexToByte(&CmdBuf[4])&0B00001110)|0xA0 ;
Addr=(((unsigned int)HexToByte(&CmdBuf[6]))<<8)|((unsigned int)HexToByte(&CmdBuf[8]));
EEPROM_Write(Chip,AddrType,Addr,HexToByte(&CmdBuf[10]));
delay_ms(5); //写EEPROM的时候要隔5ms
TransmitString(ByteToHex(EEPROM_Read(Chip,AddrType,Addr))); //回送给上位机做检查
}
TransmitByte(0x1C ); //送出命令结束符
for (i=0;i<32 ;i++) CmdBuf[i]=0; //清空命令缓冲区
}
LED++;
//PORTB&=~((LED>>7)&0x03);
}
}
/*****************************************************************************/
unsigned char IRTime=0;
unsigned char IRTiger=0;
unsigned char IRIndex=0;
unsigned char IRBuffer[64];
interrupt [5] void timer2_ovf_isr(void) //利用时间中断2来取得红外接收头的脉冲宽度
{
//#asm("cli")
if (IRTime<255)
{
IRTime++;
}
else if (IRIndex>1)
{
IRTiger=1;
}
//#asm("sei")
}
interrupt [19] void ext_int2_isr(void) //利用外部中断0来取得红外接收头的下降沿脉冲
{
//#asm("cli")
if ((IRIndex<64)&&(!IRTiger))
{
IRBuffer[IRIndex++]=IRTime;
IRTime=0;
}
//#asm("sei")
}
void TestInfrared(void)
{
unsigned char State=0;
unsigned char i,j;
unsigned char IRCmd[4];
TransmitLineF("Cable setting: IR.IR#--PortB.2(INT2)");
Pause();
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: 921.600 kHz
// Mode: Normal top=FFh
// OC2 output: Disconnected
TCCR2=0x02;
TCNT2=0x00;
OCR2=0x00;
TIMSK=(1<<6);
GICR|=(1<<5 );
MCUCR=0x02;
MCUCSR=0x00;
GIFR=0x40;
State=0;
#asm("sei")
while (1)
{
if ((IRTiger==1)&&(IRIndex>1))
{
GICR&=~(1<<5 );
for (i=2;i<IRIndex-2;i++)
{
if (IRBuffer[i]<0x20)
{
IRBuffer[i]=((IRBuffer[i]+1)&0x0C)>>3;
}
TransmitByte(IRBuffer[i]);
}
if (IRIndex>0x20)
{
for (i=0;i<4;i++)
{
IRCmd[i]=0;
for (j=0;j<8;j++)
{
IRCmd[i]|=(IRBuffer[i*8+2+j]<<j);
}
}
for (i=0;i<4;i++)
{
TransmitByte(0);
}
for (i=0;i<4;i++)
{
TransmitByte(IRCmd[i]);
}
}
IRTiger=0;
IRIndex=0;
GICR|=(1<<5 );
}
}
}
/*****************************************************************************/
void main(void)
{
unsigned char MenuIndex;
flash unsigned char *Menu[0x0b]={
"1.Test All port output",
"2.Test Key press",
"3.Test DLED",
"4.Test AT24C04",
"5.Test DS1302",
"6.Test LCD1602",
"7.Test Sound",
"8.Test FT245BM",
"9.Test RS-485",
"A.Test AT45DB041B",
"B.Test Infrared"
};
USART_Init(0x2F );
//UCSR1A=0x00;
//UCSR1B=0x18;
//UCSR1C=0x06;
//UBRR1H=0x00;
//UBRR1L=0x2F;
TransmitLineF("Test AVRKIT Menu v1.0");
TransmitLineF("-----------------------------------");
for (MenuIndex=0;MenuIndex<0x0b;MenuIndex++)
TransmitLineF(Menu[MenuIndex]);
TransmitLineF("-----------------------------------");
TransmitLineF("Please Choose Item:");
while (1)
{
MenuIndex=ReceiveByte();
if ((MenuIndex>='1')&&(MenuIndex<='9'))
{
TransmitLineF(Menu[MenuIndex-'1']);
}
else if ((MenuIndex>='A')&&(MenuIndex<='B'))
{
TransmitLineF(Menu[MenuIndex-'A'+9]);
}
else if ((MenuIndex>='a')&&(MenuIndex<='b'))
{
TransmitLineF(Menu[MenuIndex-'a'+9]);
}
else
{
TransmitLineF("Bad Command.");
}
switch (MenuIndex)
{
case '1': TestAllPort();
case '2': TestKeyPress();
case '3': TestDLED();
case '4': TestAT24C04();
case '5': TestDS1302();
case '6': TestLCD();
case '7': TestSound();
case '8': TestFT245BM();
case 'B':
case 'b': TestInfrared();
default:{TransmitLineF("Not Support");}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -