国庆大餐之,DELPHI与AVR软硬结合应用的例子。
#include <mega16.h>#include <USART.h>
#include <DELAY.h>
#include <Types.h>
/*************************************************
开发板类型:AVRKIT V2
芯片小板:M16
连线方法:
ISP1 --ISP2
PORTB--LED.G
PORTC--LED.R
*************************************************/
#pragma warn-
#define MAXEEPROM 512
eeprom unsigned char EEPROMS;
#pragma warn+
void port_init(void)
{
PORTB = 0xFF;
DDRB= 0xFF;
PORTC = 0xFF;
DDRC= 0xFF;
PORTD = 0xFF;
DDRD= 0x00;
USART_Init(47);//3.6864M=23, 7.3728M=47
}
void TestCmd(void)
{
unsigned char Cmd; //命令缓冲区
unsigned char LED; //LED数据缓冲区
unsigned char State; //LED显示状态
State=0;
LED=0;
PORTC.0=0;
TransmitLineF("USART EXAMPLE"); //从串口送出字符串
while (1)
{
Cmd=0;
if (UCSRA&(1<<RXC)) //串口有信息吗?
{
Cmd=UDR; //如果有,接收一个字节
}
if ((Cmd>='0')&&(Cmd<='9'))
{
State=Cmd-'0';
//PORTD=~PORTD;
switch (Cmd)
{
case '0':
{
TransmitLineF("USART EXAMPLE");
PORTC=~(1<<0);
break;
}
case '1':
{
TransmitLineF("Inc Counter");
PORTC=~(1<<1);
break;
}
case '2':
{
TransmitLineF("Dec Counter");
PORTC=~(1<<2);
break;
}
case '3':
{
TransmitLineF("Shift Left");
PORTC=~(1<<3);
break;
}
case '4':
{
TransmitLineF("Shift Right");
PORTC=~(1<<4);
break;
}
case '5':
{
TransmitLineF("Logic Not ");
PORTC=~(1<<5);
break;
}
case '6':
{
TransmitLineF("Data Test Mode");
PORTC=~(1<<6);
break;
}
case '7':
{
TransmitLineF("Swap High and Low");
PORTC=~(1<<7);
break;
}
default:
{
TransmitLineF("Error CMD");
}
}
}
switch (State) //如果LED显示状态条件为...
{
case 1://如果LED显示模式为第一种
{
LED++;//LED显示模式做加法运算
break;
};
case 2:
{
LED--;//LED显示模式做加法运算
break;
}
case 3:
{
LED=LED<<1;//LED显示模式做左移位运算
if (!LED) LED=0x03; //如果超越范围<灯全灭了>,则重新初始为0x03,就是只有右边二灯亮
break;
}
case 4:
{
LED=LED>>1;//LED显示模式做右移位运算
if (!LED) LED=0xC0;//如果超越范围<灯全灭了>,则重新初始为0xC0,就是只有左边二灯亮
break;
}
case 5:
{
if ((LED!=0) && (LED!=0xFF)) {LED=0;}//如果所有的LED灯不是全亮或全灭,则设置为全灭
LED=~LED; //LED显示模式做取反运算
break;
}
case 6:
{
if (LED==0x55)
{LED=0xAA;} //LED做相隔一位
else
{LED=0x55;}
break;
}
case 7:
{
if (LED==0xF0)
{LED=0x0F;} //LED高4位和低4位相互亮
else
{LED=0xF0;}
break;
}
case 8:
{
//LED=0x00;//没代码,LED不动,这个地方大家可随便自己改点什么的。
}
}
PORTB=~LED;
delay_ms(200);
}
}
char StrCmpF(unsigned char * Sour, flash unsigned char * Dest, unsigned char Case)
{
register char s;
register char d;
while ((*Sour)&&(*Dest))
{
s=*Sour;
d=*Dest;
if (Case)
{
if ((s>=0x61)&&(s<=0x7A)) {s&=0B11011111;};
if ((d>=0x61)&&(d<=0x7A)) {d&=0B11011111;};
}
if (s==d)
{
Sour++;
Dest++;
}
else
return 0;
}
if ((*Sour==0)&&(*Dest!=0)) {return 0;}
if ((*Sour!=0)&&(*Dest==0)) {return 0;}
return 1;
}
void TestString(void)
{
unsigned char CmdBuf;
unsigned char * str;
unsigned char i;
PORTC.1=0;
str=&CmdBuf;
TransmitLineF("USART String Example"); //从串口送出字符串
while (1)
{
ReceiveString(CmdBuf);
if (CmdBuf>0)
{
PORTC.7=0;
//TransmitByte(CmdBuf);
//TransmitString(str);
if (StrCmpF(str,"Hi",1))
{
TransmitStringF("Hello");
TransmitByte(0);
goto next;
}
if (StrCmpF(str,"Who",1))
{
TransmitStringF("Aleyn");
TransmitByte(0);
goto next;
}
if (StrCmpF(str,"Date",1))
{
TransmitStringF("2006-04-28");
TransmitByte(0);
goto next;
}
if (StrCmpF(str,"Time",1))
{
TransmitStringF("22:39:01");
TransmitByte(0);
goto next;
}
if (StrCmpF(str,"Good",1))
{
TransmitStringF("Thank you");
TransmitByte(0);
goto next;
}
if (StrCmpF(str,"GoodBye",1))
{
TransmitStringF("See you next time");
TransmitByte(0);
goto next;
}
TransmitStringF("Error Command");
TransmitByte(0);
next:
PORTC.7=1;
for (i=0;i<CMD_MAXBUFFER;i++) {CmdBuf=0;}
}
}
}
#defineCMD_HELP72
#defineCMD_READ82
#defineCMD_WRITE87
void TestProtocol(void)
{
unsigned int i;
unsigned char CmdBuf; //命令缓冲区
unsigned int Addr; //内存地址
unsigned int Cnt; //读取长度
PORTC.2=0;
while (1)
{
ReceiveCMD(CmdBuf); //读取一条命令行
if (CmdBuf>0) //如果命令行长度不为零
{
PORTC.7=0;
TransmitByte(CMD_BEGIN); //送出命令开始符
if (CmdBuf==CMD_HELP) //如果接收的是帮助命令
{
TransmitByte(CMD_HELP);//送出命令
TransmitStringF("Test Protocol Version 1.0");
TransmitByte(13);
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);
}
if (CmdBuf==CMD_READ) //如果接收的是读取EEPROM命令
{
Addr=(((unsigned int)HexToByte(&CmdBuf))<<8)|((unsigned int)HexToByte(&CmdBuf)); //内存地址
Cnt=(((unsigned int)HexToByte(&CmdBuf))<<8)|((unsigned int)HexToByte(&CmdBuf)); //读写个数
if (Addr+Cnt<=MAXEEPROM)
{
TransmitByte(CMD_READ);//送出命令
for (i=0;i<Cnt;i++)
{
TransmitString(ByteToHex(EEPROMS));//送回上位机
TransmitByte(32);
}
}
else
{
TransmitByte(CMD_HELP);//送出命令
TransmitStringF("ERROR:Addr+Count Large then 512");
}
}
if (CmdBuf==CMD_WRITE) //如果接收的是写入EEPROM命令
{
Addr=(((unsigned int)HexToByte(&CmdBuf))<<8)|((unsigned int)HexToByte(&CmdBuf));
if (Addr<MAXEEPROM)
{
TransmitByte(CMD_WRITE);//送出命令
EEPROMS=HexToByte(&CmdBuf);
delay_ms(2);//写EEPROM的时候要隔5ms
TransmitString(ByteToHex(EEPROMS));//回送给上位机做检查
}
else
{
TransmitByte(CMD_HELP);//送出命令
TransmitStringF("ERROR:Addr Large then 512");
}
}
TransmitByte(CMD_END); //送出命令结束符
for (i=0;i<CMD_MAXBUFFER;i++) CmdBuf=0;//清空命令缓冲区
PORTC.7=1;
}
}
}
void main()
{
port_init();
while (1)
{
if (!PIND.4) TestCmd();
if (!PIND.5) TestString();
if (!PIND.6) TestProtocol();
}
}
AVR源程序
DELPHI源程序 村长,怎么不说说具体功能呢?
小弟刚想入门dephi,呵呵。多指教。 村长好
我用delphi打开你发的源程序,出现如下错误,可能是少了什么东东
初学delphi,请多指教
http://cache.amobbs.com/bbs_upload782111/files_8/ourdev_182226.jpg to 【1楼】 benladn911
这是去年的贴子了,记不大清楚,应该是下位机是一个AT24CXX的读写器,上位机是读写应用程序。
to 【2楼】 rube
你需要安装一个CPort 3.0 的控件(去www.2ccc.com下载)。 村长是个热心人
谢谢村长! 谢谢村长 mark 顶 村长:
编译后出现如下错误:
uFMainForm.pas(97): Undeclared identifier: 'WriteBuffer'
uFMainForm.pas(133): Undeclared identifier: 'ReadBuffer'
avrpc.dpr(5): Could not compile used unit 'uFMainForm.pas'
页:
[1]