|
步進(jìn)電機(jī)+1602顯示C源代碼
下載:
步進(jìn)電機(jī) LCD1602顯示.doc
(3.72 KB, 下載次數(shù): 17)
2016-12-14 12:40 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
- /*此程序在SP-518USB開(kāi)發(fā)板上測(cè)試過(guò) laosong */
- /******************************步進(jìn)電機(jī)的驅(qū)動(dòng)*************************************
- 程序名稱: 按鍵控制電機(jī)正反轉(zhuǎn),LCD1602顯示正反轉(zhuǎn)
- 說(shuō)明:使用本程序你必須把 SE5設(shè)置為ON(2-3)短接
- ;FOSC = 12MHz
- ;---------------------------------------------------------------------------------
- ; 步進(jìn)電機(jī)的驅(qū)動(dòng)信號(hào)必須為脈沖信號(hào)!!! 轉(zhuǎn)動(dòng)的速度和脈沖的頻率成正比!!!
- ; 本步進(jìn)電機(jī)步進(jìn)角為 5.625度 . 一圈 360 度 , 需要64個(gè)脈沖完成!!!
- ;---------------------------------------------------------------------------------
- ; A組線圈對(duì)應(yīng) P1.4
- ; B組線圈對(duì)應(yīng) P1.5
- ; C組線圈對(duì)應(yīng) P1.6
- ; D組線圈對(duì)應(yīng) P1.7
- ; 正轉(zhuǎn)次序: AB組--BC組--CD組--DA組 (即一個(gè)脈沖,正轉(zhuǎn)5.625度)
- ;----------------------------------------------------------------------------------
- **********************************************************************************/
- #include <reg51.h>
- #include <intrins.h>
- #define uint unsigned int
- #define uchar unsigned char
- sbit lcd_rs_port = P2^7; /*定義LCD控制端口*/
- sbit lcd_rw_port = P2^6; /*定義LCD控制端口*/
- sbit lcd_en_port = P2^5; /*定義LCD控制端口*/
- #define lcd_data_port P0 /*定義LCD控制端口*/
- #define motor P1 /*步進(jìn)電機(jī)接口*/
- sbit P34 = P3^4; /*控制正轉(zhuǎn)按鍵*/
- sbit P33 = P3^3; /*控制電機(jī)停止*/
- sbit P11 = P1^1; /*控制電機(jī)反轉(zhuǎn)*/
- uchar code motor_table[][4]={{0x0f,0x0f,0x0f,0x0f},{0x3f,0x6f,0xcf,0x9f},{0x3f,0x9f,0xcf,0x6f}}; /*正反轉(zhuǎn)表*/
- uchar code lcd_table1[] = {"Motor Direction "};
- uchar code lcd_table2[][16]={{" stop "},
- {" >>>>>>>> "},
- {" <<<<<<<< "}};
- void lcd_delay(uchar ms) /*LCD1602 延時(shí)*/
- {
- uchar j;
- while(ms--){
- for(j=0;j<250;j++)
- {;}
- }
- }
- void lcd_busy_wait() /*LCD1602 忙等待*/
- {
- lcd_rs_port = 0;
- lcd_rw_port = 1;
- lcd_en_port = 1;
- lcd_data_port = 0xff;
- while (lcd_data_port&0x80);
- lcd_en_port = 0;
- }
- void lcd_command_write(uchar command) /*LCD1602 命令字寫(xiě)入*/
- {
- lcd_busy_wait();
- lcd_rs_port = 0;
- lcd_rw_port = 0;
- lcd_en_port = 0;
- lcd_data_port = command;
- lcd_en_port = 1;
- lcd_en_port = 0;
- }
- void lcd_system_reset() /*LCD1602 初始化*/
- {
- lcd_delay(20);
- lcd_command_write(0x38);
- lcd_delay(100);
- lcd_command_write(0x38);
- lcd_delay(50);
- lcd_command_write(0x38);
- lcd_delay(10);
- lcd_command_write(0x08);
- lcd_command_write(0x01);
- lcd_command_write(0x06);
- lcd_command_write(0x0c);
- }
- void lcd_char_write(uchar x_pos,y_pos,lcd_dat) /*LCD1602 字符寫(xiě)入*/
- {
- x_pos &= 0x0f; /* X位置范圍 0~15 */
- y_pos &= 0x01; /* Y位置范圍 0~ 1 */
- if(y_pos==1) x_pos += 0x40;
- x_pos += 0x80;
- lcd_command_write(x_pos);
- lcd_busy_wait();
- lcd_rs_port = 1;
- lcd_rw_port = 0;
- lcd_en_port = 0;
- lcd_data_port = lcd_dat;
- lcd_en_port = 1;
- lcd_en_port = 0;
- }
- /*1MS為單位的延時(shí)程序*/
- void delay_1ms(uchar x)
- {
- uchar j;
- while(x--) { for(j=0;j<125;j++); }
- }
- void main()
- {
- uchar i;
- uchar yunzhuang=0; /*運(yùn)轉(zhuǎn)狀況 0停止 1正轉(zhuǎn) 2反轉(zhuǎn)*/
- uchar count=0; /*電機(jī)轉(zhuǎn)動(dòng)步數(shù)*/
- motor = 0x0f; /*電機(jī)停止*/
- lcd_system_reset(); /*LCD1602 初始化*/
- for(i=0;i<16;i++) lcd_char_write(i,0,lcd_table1[i]);
- while(1){
- motor = motor_table[yunzhuang][count]; /*電機(jī)轉(zhuǎn)動(dòng)*/
- delay_1ms(5);
- count++; /*電機(jī)步數(shù)加1*/
- if(count>=4) count = 0; /*完成一圈轉(zhuǎn)動(dòng)*/
- for(i=0;i<16;i++) lcd_char_write(i,1,lcd_table2[yunzhuang][i]);
- if(P33==0){
- delay_1ms(1);
- if(P33==0){
- yunzhuang = 0;
- }
- }
- else if(P34==0){
- delay_1ms(5);
- if(P34==0){
- yunzhuang = 1;
- count = 0;
- }
- }
- else if(P11==0){
- delay_1ms(1);
- if(P11==0){
- yunzhuang = 2;
- }
- }
- }
- }
復(fù)制代碼
|
|