飞思卡尔程序设计.docx
- 文档编号:9886832
- 上传时间:2023-02-07
- 格式:DOCX
- 页数:9
- 大小:15.40KB
飞思卡尔程序设计.docx
《飞思卡尔程序设计.docx》由会员分享,可在线阅读,更多相关《飞思卡尔程序设计.docx(9页珍藏版)》请在冰豆网上搜索。
飞思卡尔程序设计
数据采集(CCD摄像头)程序,舵机,电机控制程序
Test.c
*Description:
ThisfileincludesI/Ofunctionfortest,thePWMoutputsfunctionfortest,function
*testingsensors.
*(c)Copyright2006,ZhaoCheng
*AllRightsReserved
*
*By:
ZhaoCheng
*Note:
Don'tchangethisfileifpossible.
**********************************************************************************/
#include
#include
#defineHIGHSPEED8000
#defineLOWSPEED11000/*速度变量,0-24000数值越大,速度越慢*/
voidPWMout(int,int);
/*24000-20000*/
voidIOtest(void)
{
staticunsignedchari=0,j=0x01,k;
DDRB=DDRA=0xFF;
PORTB=0xf0;
for(;;)
{
k=(~j)&0x7f;
PORTA=PORTB=k;
while(TCNT!
=0x0000);
while(TCNT==0x0000)
{
if(i>9)
{
j=j<<1;
i=0;
}
i++;
}
if(j>=0x80)
j=0x01;
}
}
voidPWMtest(void)
{
intcounter=-4500;
DDRB=0xff;
PORTB=0xff;
TSCR1=0x80;/*enabletimerTCNT*/
TSCR2=0x00;/*TCNTprescalersetup*/
for(;;)
{
while(TCNT!
=0x0000);
while(TCNT==0x0000);
counter=counter+30;
if(counter>=3000)
{
counter=0;
PWMout(4500,LOWSPEED);
}
if(counter==1500)
{
PWMout(-4500,LOWSPEED);
}
PORTB=(char)(counter/100);
}
}
voidSignalTest(void)
{
unsignedcharsignal;
intDirection,Velocity;
Direction=0;
Velocity=LOWSPEED;
DDRA=0x00;
DDRB=0xff;
signal=PORTA;
PORTB=~signal;
switch(signal)
{
case0x08:
/*00011000*/
case0x10:
Direction=800;
Velocity=HIGHSPEED;
break;
case0x04:
/*00100100*/
case0x20:
Direction=1500;
Velocity=HIGHSPEED;
break;
case0x02:
/*01000010*/
case0x40:
Direction=2800;
Velocity=HIGHSPEED;
break;
case0x01:
/*10000001*/
case0x80:
Direction=4000;
Velocity=LOWSPEED;
break;
case0x3c:
/*00111100overstartline*/
case0xff:
/*11111111overcrossingline*/
case0x00:
/*00000000gostraightnotneedchangedstate*/
default:
break;
}
if(signal>0x0f)
Direction=-Direction;
PWMout(Direction,LOWSPEED);
}
/*
***********************************************************************************
*LCD1620.c
*ICC-AVRapplicationbuilder:
2006-1-821:
43:
48
*Target:
M8
*Crystal:
4.0000Mhz
*
*Note:
Don'tchangethisfileifpossible.
**********************************************************************************/
#defineCMD_CLEAR0x01
#defineCMD_RESET0x02
#include
#include
#defineLCD_DATA0xff
#defineLCD_EN0x01//PORTC0
#defineLCD_RS0x02//PORTC1
#defineLCD_RW0x04//PORTC2
#defineLCD_DATAPORTPORTB
#defineLCD_ENPORTPORTA
#defineLCD_RSPORTPORTA
#defineLCD_RWPORTPORTA
voidlcd_init(void);
voidlcd_write_cmd(unsignedcmd,unsigneddata);
voidlcd_setxy(unsignedcharx,unsignedchary);
voidlcd_write_string(unsignedcharX,unsignedcharY,unsignedchar*str);
voiddelay_nus(unsignedintn);
voiddelay_nms(unsignedintn);
voidlcd_init(void)
{
DDRB|=LCD_DATA;
DDRA|=LCD_EN|LCD_RS|LCD_RW;
LCD_RWPORT&=~LCD_RW;
LCD_DATAPORT=0x30;//控制字规则:
5:
8bit,4:
16x2,3:
5x7
LCD_ENPORT|=LCD_EN;
delay_nus
(1);
LCD_ENPORT&=~LCD_EN;
delay_nus(40);
lcd_write_cmd(0,0x38);//8bittest
lcd_write_cmd(0,0x0c);//显示开
lcd_write_cmd(0,0x01);//显示清屏
lcd_write_cmd(0,0x06);//显示光标移动设置
}
voidlcd_write_cmd(unsignedcmd,unsigneddata)
{
if(cmd==0)
LCD_RSPORT&=~LCD_RS;
else
LCD_RSPORT|=LCD_RS;
LCD_DATAPORT&=0x00;
LCD_DATAPORT=data;
LCD_ENPORT|=LCD_EN;
delay_nus(10);
LCD_ENPORT&=~LCD_EN;
delay_nus(10);
}
voidlcd_setxy(unsignedcharx,unsignedchary)
{
unsignedcharaddr;
if(y==0)
addr=x+0x80;
else
addr=x+0xc0;
lcd_write_cmd(0,addr);
}
voidlcd_write_string(unsignedcharX,unsignedcharY,unsignedchar*str)
{
lcd_setxy(X,Y);
while(*str)
{
lcd_write_cmd(1,*str);
str++;
}
}
voiddelay_1us(void)//1us延时函数
{
asm("nop");
}
voiddelay_nus(unsignedintn)//Nus延时函数
{
unsignedinti=0;
for(i=0;i delay_1us(); } voiddelay_1ms(void)//1ms延时函数 { unsignedinti; for(i=0;i<1140;i++); } voiddelay_nms(unsignedintn)//Nms延时函数 { unsignedinti=0; for(i=0;i delay_1ms(); } //callthisroutinetoinitializeallperipherals voidmain(void) { lcd_init(); while (1) { lcd_write_cmd(0,0x01);//清屏 delay_nms (2); lcd_write_string(0,0,"happynewyear"); delay_nms(100); lcd_write_string(0,1,"LCDsuccessful! "); delay_nms(100); } } /*******************************程序结束*********************************/
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 卡尔 程序设计