資源簡介
單片機讀取光柵編碼器角度程序,程序解釋可查看本人博客,里面有詳細解釋

代碼片段和文件信息
//頭文件
#include
#include//SEI()函數_NOP()BIT();
#define?uint?unsigned?int
#define?uchar?unsigned?char
//延時函數
void?delay_1ms(void)?
{??
unsigned?int?i;??
for(i=1;i<(unsigned?int)(11.0592*143-2);i++);//定義晶振頻率
}??
void?delay(unsigned?int?n)//延時毫妙級?
{??
unsigned?int?i;?
for(i=0;i delay_1ms();?
}
//4位數碼管驅動函數
#define?SCLK0?(PORTB?&=~BIT(0))//定義接線口,需要添加#include才能使用
#define?SCLK1?(PORTB?|=BIT(0))
#define?RCLK0?(PORTB?&=~BIT(1))
#define?RCLK1?(PORTB?|=BIT(1))
#define?DAT0?(PORTB?&=~BIT(2))
#define?DAT1?(PORTB?|=BIT(2))
unsigned?char??number[]={0xc00xf90xa40xb00x990x920x820xf80x800x900x400x790x240x300x190x120x020x780x000x10};//0~9不帶小數點,10~19帶小數點
unsigned?char??location[]={0x000x010x020x040x08};//0~4
void?shumaguan(unsigned?char?weizhiunsigned?char?shuzi)
{??unsigned?char?inum;?//定義一個無字符串變量
num=number[shuzi];
for(i=0;i<8;i++)???//for?循環,循環8次,把一個數變成二進制發送出去
????????{
?SCLK0;
? if((num&0x80)==0)
?DAT0;
else
?DAT1;
num=num<<1;
?????????SCLK1;?//?把595頻率置高
????????}
num=location[weizhi];
??? for(i=0;i<8;i++)???//for?循環,循環8次,把一個數變成二進制發送出去
????????{
?????????SCLK0;//?把595?SCLK頻率置低電平
if((num&0x80)==0)
?DAT0;
else
?DAT1;
num=num<<1;
?SCLK1;
?}
RCLK0;
????????_NOP();_NOP();_NOP();_NOP();_NOP();?//延時函數,系統自帶
RCLK1;
}
unsigned?char?encoder_A;//encoder_A需要初始化?encoder_A=(PIND&0x0c)/4;
unsigned?angle;//編碼器角度變量
void?encoder(unsigned?int?X)//X為編碼器線數
{
?unsigned?char?B;
?CLI();
?B=encoder_A;
?encoder_A=(PIND&0x0c)/4;//使用外部中斷0,外部中斷1
?if((B==0?&&?encoder_A==1)||(B==1?&&?encoder_A==3)||(B==3?&&?encoder_A==2)||(B==2?&&?encoder_A==0))?
?{
??angle=angle+9000/X;
??if(angle==36000)angle=0;
?}
?else?if?((B==0?&&?encoder_A==2)||(B==2?&&?encoder_A==3)||(B==3?&&?encoder_A==1)||(B==1?&&?encoder_A==0))?
?{
??if(angle==0)angle=36000;
??angle=angle-9000/X;
?}
?else?;
?SEI();
}
//定義MEGA16接口輸入輸出
void?port_init(void)
{
?PORTA?=?0xFF;
?DDRA??=?0x00;?
?PORTB?=?0xFF;?
?DDRB??=?0xFF;?
?PORTC?=?0xFF;?
?DDRC??=?0x00;?
?PORTD?=?0xFF;?
?DDRD??=?0x00;
}
void?init_devices(void)
{
?//stop?errant?interrupts?until?set?up
?CLI();?//disable?all?interrupts
?port_init();
?
?MCUCR?=?0x05;
?GICR??=?0xC0;
?TIMSK?=?0x00;?//timer?interrupt?sources
?SEI();?//re-enable?interrupts
?//all?peripherals?are?now?initialized
}
void?main(void)
{
?init_devices();
?encoder_A=(PIND&0x0c)/4;
?while(1)
?{
??shumaguan(1angle%100/10);
??shumaguan(2angle%1000/100+10);
??shumaguan(3angle%10000/1000);
??shumaguan(4angle/10000);
?}
}
#pragma?interrupt_handler?int0_isr:iv_INT0
void?int0_isr(void)
{
?encoder(360);
}
#pragma?interrupt_handler?int1_isr:iv_INT1
void?int1_isr(void)
{
?encoder(360);
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件??????????1??2019-06-05?22:18??光電編碼器-單片機程序\BACKUP\shumaguanxiaoshudian.c.190605221814
?????文件???????1799??2019-06-05?22:23??光電編碼器-單片機程序\BACKUP\shumaguanxiaoshudian.c.190605222312
?????文件???????1801??2019-06-05?22:23??光電編碼器-單片機程序\BACKUP\shumaguanxiaoshudian.c.190605222328
?????文件???????1805??2019-06-05?22:23??光電編碼器-單片機程序\BACKUP\shumaguanxiaoshudian.c.190605222344
?????文件???????2528??2019-06-05?22:34??光電編碼器-單片機程序\BACKUP\shumaguanxiaoshudian.c.190605223410
?????文件???????2578??2019-06-05?22:52??光電編碼器-單片機程序\BACKUP\shumaguanxiaoshudian.c.190605225218
?????文件???????2590??2019-06-05?22:53??光電編碼器-單片機程序\BACKUP\shumaguanxiaoshudian.c.190605225332
?????文件???????2581??2019-06-05?22:55??光電編碼器-單片機程序\BACKUP\shumaguanxiaoshudian.c.190605225508
?????文件???????2449??2019-06-06?21:36??光電編碼器-單片機程序\BACKUP\shumaguanxiaoshudian.c.190606213622
?????文件???????2501??2019-06-06?21:39??光電編碼器-單片機程序\BACKUP\shumaguanxiaoshudian.c.190606213914
?????文件???????2478??2019-06-06?21:42??光電編碼器-單片機程序\BACKUP\shumaguanxiaoshudian.c.190606214236
?????文件???????2480??2019-06-06?21:43??光電編碼器-單片機程序\BACKUP\shumaguanxiaoshudian.c.190606214318
?????文件???????2680??2019-06-06?22:07??光電編碼器-單片機程序\BACKUP\shumaguanxiaoshudian.c.190606220748
?????文件???????2673??2019-06-06?22:09??光電編碼器-單片機程序\BACKUP\shumaguanxiaoshudian.c.190606220940
?????文件???????2683??2019-06-06?22:10??光電編碼器-單片機程序\BACKUP\shumaguanxiaoshudian.c.190606221030
?????文件???????2680??2019-06-06?22:14??光電編碼器-單片機程序\BACKUP\shumaguanxiaoshudian.c.190606221416
?????文件???????2699??2019-06-06?22:18??光電編碼器-單片機程序\BACKUP\shumaguanxiaoshudian.c.190606221822
?????文件???????2701??2019-06-06?22:18??光電編碼器-單片機程序\BACKUP\shumaguanxiaoshudian.c.190606221830
?????文件???????2727??2019-06-06?22:19??光電編碼器-單片機程序\BACKUP\shumaguanxiaoshudian.c.190606221950
?????文件???????2727??2019-06-06?22:20??光電編碼器-單片機程序\BACKUP\shumaguanxiaoshudian.c.190606222008
?????文件???????2824??2019-06-06?22:25??光電編碼器-單片機程序\BACKUP\shumaguanxiaoshudian.c.190606222532
?????文件???????2845??2019-06-06?22:26??光電編碼器-單片機程序\BACKUP\shumaguanxiaoshudian.c.190606222642
?????文件???????2851??2019-06-06?22:28??光電編碼器-單片機程序\BACKUP\shumaguanxiaoshudian.c.190606222820
?????文件???????2888??2019-06-06?22:28??光電編碼器-單片機程序\BACKUP\shumaguanxiaoshudian.c.190606222848
?????文件???????2889??2019-06-06?22:28??光電編碼器-單片機程序\BACKUP\shumaguanxiaoshudian.c.190606222858
?????文件???????2886??2019-06-06?22:33??光電編碼器-單片機程序\BACKUP\shumaguanxiaoshudian.c.190606223314
?????文件???????2977??2019-06-06?22:39??光電編碼器-單片機程序\BACKUP\shumaguanxiaoshudian.c.190606223930
?????文件???????2981??2019-06-06?22:39??光電編碼器-單片機程序\BACKUP\shumaguanxiaoshudian.c.190606223944
?????文件???????3004??2019-06-06?22:41??光電編碼器-單片機程序\BACKUP\shumaguanxiaoshudian.c.190606224104
?????文件???????3004??2019-06-06?22:41??光電編碼器-單片機程序\BACKUP\shumaguanxiaoshudian.c.190606224116
............此處省略78個文件信息
- 上一篇:步進電機S型曲線生成器
- 下一篇:DOS_基礎教程DOS入門教程
評論
共有 條評論