查看: 1808|回复: 10
|
我用 pic c compiler (CCSC) & pic16f877a
[复制链接]
|
|
画错了 sensor 是去 port c
stepper motor 去 port b
我有一个 project 是用 sensor control stepper motor
(16f877a & pic c compiler CCSC)
我的 project 是 when sensor 1 有 input (pc0), stepper motor I 就走 left, 另一个不走
我的 project 是 when sensor 2 有 input (pc1), stepper motor I 就走 right, 另一个不走
我的 project 是 when sensor 3 有 input (pc2), stepper motor II 就走 left, 另一个不走
我的 project 是 when sensor 4 有 input (pc3), stepper motor II 就走 right, 另一个不走
我之前写的,可是有问题
#include <16f877a.h>
#use delay(clock = 4000000)
#fuses xt,nowdt,noprotect
#byte portb = 6
#byte portc = 7
byte i,count;
void ele_left()
{
if(count ==0){portb = 144;}
if(count ==1){portb = 80;}
if(count ==2){portb = 96;}
if(count ==3){portb = 160;}
}
void ele_right()
{
if(count ==0){portb = 160;}
if(count ==1){portb = 96;}
if(count ==2){portb = 80;}
if(count ==3){portb = 144;}
}
void rot_left()
{
if(count ==0){portb = 144;}
if(count ==1){portb = 80;}
if(count ==2){portb = 96;}
if(count ==3){portb = 160;}
}
void rot_right()
{
if(count ==0){portb = 160;}
if(count ==1){portb = 96;}
if(count ==2){portb = 80;}
if(count ==3){portb = 144;}
}
void main()
{
set_tris_b(0b00000000);
set_tris_c(0b00001111);
output_low(pin_b7);
output_low(pin_b6);
output_low(pin_b5);
output_low(pin_b4);
output_low(pin_b3);
output_low(pin_b2);
output_low(pin_b1);
output_low(pin_b0);
count = 0;
count1 = 0;
count2 = 0;
count3 = 0;
do
{
if (input(pin_c0)==1)
{
ele_left();
count = count + 1;
if(count ==4)
{
count ==0;
}
}
delay_ms(1000);
if (input(pin_c1)==1)
{
ele_right();
count1 = count1 + 1;
if(count1 ==4)
{
count1 ==0;
}
}
delay_ms(1000);
if (input(pin_c2)==1)
{
rot_left();
count2 = count2 + 1;
if(count2 ==4)
{
count2 ==0;
}
}
delay_ms(1000);
if (input(pin_c3)==1)
{
rot_right();
count3 = count3 + 1;
if(count3 ==4)
{
count3 ==0;
}
}
else
{
portb = 0;
}
delay_ms(1000);
}while (1);
}
可以帮我检查有什么问题吗?
谢谢各位
问题(当我input 从pc0 换去 pc3时,stepper motor II 不走 ; sequence 错) |
|
|
|
|
|
|
|
发表于 7-1-2008 08:39 AM
|
显示全部楼层
回复 1# 失落者 的帖子
首先,请问你的Motor 是怎样接的?我看了你的pattern 好像怪怪的。。。
你的code的问题是:
1. Count1,Count2,Count3 没有define。
2. 在你的 ele_right();ele_left(); rot_left(); rot_right() 里都是用Count 而已。
3. if(count ==4)count =0; 不是 count==0;
还有, 当我们做input 时, 一般都是做成Active Low, 不是Active High.
请问你的input是怎样接的?
- // Original by 失落者
- // Sensor C0~C3
- // Stepper 1 : B0~B3
- // Stepper 2 : B4~B7
- #include <16f877a.h>
- #use delay(clock = 4000000)
- #fuses xt,nowdt,noprotect
- #use fast_io(B)
- #use fast_io(C)
- #byte portb = 6
- #byte portc = 7
- int i,count,count1,count2,count3;
- void ele_left()
- {
- if(count ==0){portb =0b10010000; } // 144;
- if(count ==1){portb =0b01010000; } // 80;
- if(count ==2){portb =0b01100000; } // 96;
- if(count ==3){portb =0b10100000; } // 160;
- }
- void ele_right()
- {
- if(count1 ==0){portb = 160;}
- if(count1 ==1){portb = 96;}
- if(count1 ==2){portb = 80;}
- if(count1 ==3){portb = 144;}
- }
- void rot_left()
- {
- if(count2 ==0){portb = 144;}
- if(count2 ==1){portb = 80;}
- if(count2 ==2){portb = 96;}
- if(count2 ==3){portb = 160;}
- }
- void rot_right()
- {
- if(count3 ==0){portb = 160;}
- if(count3 ==1){portb = 96;}
- if(count3 ==2){portb = 80;}
- if(count3 ==3){portb = 144;}
- }
- void main()
- {
- set_tris_b(0b00000000);
- set_tris_c(0b00001111);
- portb=0;
- count = 0;
- count1 = 0;
- count2 = 0;
- count3 = 0;
- while(true)
- {
- if (input(pin_c0)==1)
- {
- ele_left();
- count = count + 1;
- if(count ==4)count =0;
- }
- /////////////////////////////
- else if (input(pin_c1)==1)
- {
- ele_right();
- count1 = count1 + 1;
- if(count1 ==4) count1 =0;
- }
- ////////////////////////////
- else if (input(pin_c2)==1)
- {
- rot_left();
- count2 = count2 + 1;
- if(count2 ==4) count2 =0;
- }
- ////////////////////////////
- else if (input(pin_c3)==1)
- {
- rot_right();
- count3 = count3 + 1;
- if(count3 ==4) count3 =0;
- }
- else
- {
- portb = 0;
- }
- delay_ms(1000);
- }
-
- }
复制代码
[ 本帖最后由 pic 于 8-1-2008 08:34 PM 编辑 ] |
|
|
|
|
|
|
|
楼主 |
发表于 7-1-2008 10:34 PM
|
显示全部楼层
回复 2# pic 的帖子
我的 stepper motor 有 4 条是接 pic 的..那是 signal
另外 4 条的是接 power supply .. 我没有 show 出来
我的 input 是来自 comparator op amp 741(sensor) 的
我有 4 个 comparator 就是有 4 个 sensor
如果我的 photo diode sense 到有光.. comparator 就会 active high
pinc0 就会跟着 active high
so stepper motor 1 就会转左边
else if 另一个comparotor sense 到有Ligh , 另一个 motor 就会转另一边..
至于
1. Count1,Count2,Count3 没有define。
2. 在你的 ele_right();ele_left(); rot_left(); rot_right() 里都是用Count 而已。
3. if(count ==4)count =0; 不是 count==0;
我换了... 可是还是行不通..
好象有 interupt 那样
pinc3 active hight 时.. 他不是走
void rot_right()
{
if(count3 ==0){portb = 160;}
if(count3 ==1){portb = 96;}
if(count3 ==2){portb = 80;}
if(count3 ==3){portb = 144;}
} |
|
|
|
|
|
|
|
发表于 8-1-2008 12:04 AM
|
显示全部楼层
原帖由 失落者 于 7-1-2008 10:34 PM 发表
我的 stepper motor 有 4 条是接 pic 的..那是 signal
另外 4 条的是接 power supply .. 我没有 show 出来
我的 input 是来自 comparator op amp 7
void rot_right()
{
if(count3 ==0){portb = 160;}
if(count3 ==1){portb = 96;}
if(count3 ==2){portb = 80;}
if(count3 ==3){portb = 144;}
}
...
换成十六进制是:
160 = 0xA0 = 1010 0000
96 = 0x60 = 0110 0000
80 = 0x50 = 0101 0000
144 = 0x90 = 1001 0000
你这些控制马达转动的值似乎不对。或者我没用过这种控制方法。这值是哪里的来的呢??
[ 本帖最后由 rothmans 于 8-1-2008 12:16 AM 编辑 ] |
|
|
|
|
|
|
|
发表于 8-1-2008 08:36 PM
|
显示全部楼层
原帖由 失落者 于 7-1-2008 10:34 PM 发表
我的 stepper motor 有 4 条是接 pic 的..那是 signal
另外 4 条的是接 power supply .. 我没有 show 出来
我的 input 是来自 comparator op amp 7 ...
请再试#2楼的code, 我做了修改, 加else if。
还有请放你接motor部分的电路来看看。 |
|
|
|
|
|
|
|
楼主 |
发表于 9-1-2008 10:12 PM
|
显示全部楼层
贴了谢谢
我这个 stepper motor 是 send sequence 来 drive 的
stepper motor 440-442
谢谢 |
|
|
|
|
|
|
|
发表于 9-1-2008 10:47 PM
|
显示全部楼层
原帖由 失落者 于 9-1-2008 10:12 PM 发表
我这个 stepper motor 是 send sequence 来 drive 的
stepper motor 440-442
你的Stepper motor 有8条线 是吗?
你就4条直接接去5V, 另4条直接接去PIC MCU?没有Driver'什么之类的? |
|
|
|
|
|
|
|
楼主 |
发表于 10-1-2008 10:39 AM
|
显示全部楼层
回复 7# pic 的帖子
有一个 darlington pair 吧了
还有一个invoid back emf的 circuit ........
[ 本帖最后由 失落者 于 10-1-2008 10:41 AM 编辑 ] |
|
|
|
|
|
|
|
发表于 10-1-2008 10:53 AM
|
显示全部楼层
原帖由 失落者 于 10-1-2008 10:39 AM 发表
有一个 darlington pair 吧了
还有一个invoid back emf的 circuit ........
最好把你的driver也画出,signal有没有被Invert?
#2楼修改后的code可以用吗? |
|
|
|
|
|
|
|
楼主 |
发表于 10-1-2008 10:06 PM
|
显示全部楼层
我现在暂时用 led 代表 motor 的 8 个pin
可是还是不行...
走的 sequence 不一样.. |
|
|
|
|
|
|
|
发表于 13-1-2008 02:28 PM
|
显示全部楼层
原帖由 失落者 于 10-1-2008 10:06 PM 发表
我现在暂时用 led 代表 motor 的 8 个pin
可是还是不行...
走的 sequence 不一样..
在Proteus里能够simulate, code 没有问题, 不走的话99% 是你的电路问题。
你的input, PortC, 没用的input 要pull low (可以用4.7K~10K 接去Ground)
不要只是input C0 high, 然后其他的不接,造成 C1,C2,C3 的logic undetermine。 |
|
|
|
|
|
|
| |
本周最热论坛帖子
|