佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1808|回复: 10

我用 pic c compiler (CCSC) & pic16f877a

[复制链接]
发表于 5-1-2008 05:33 PM | 显示全部楼层 |阅读模式


画错了 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 错)
回复

使用道具 举报


ADVERTISEMENT

发表于 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是怎样接的?

  1. // Original by 失落者
  2. // Sensor C0~C3
  3. // Stepper 1 : B0~B3
  4. // Stepper 2 : B4~B7

  5. #include <16f877a.h>
  6. #use delay(clock = 4000000)
  7. #fuses xt,nowdt,noprotect
  8. #use fast_io(B)
  9. #use fast_io(C)
  10. #byte portb = 6
  11. #byte portc = 7

  12. int i,count,count1,count2,count3;

  13. void ele_left()
  14. {
  15. if(count ==0){portb =0b10010000; } // 144;
  16. if(count ==1){portb =0b01010000; } // 80;
  17. if(count ==2){portb =0b01100000; } // 96;
  18. if(count ==3){portb =0b10100000; } // 160;
  19. }

  20. void ele_right()
  21. {
  22. if(count1 ==0){portb = 160;}
  23. if(count1 ==1){portb = 96;}
  24. if(count1 ==2){portb = 80;}
  25. if(count1 ==3){portb = 144;}
  26. }

  27. void rot_left()
  28. {
  29. if(count2 ==0){portb = 144;}
  30. if(count2 ==1){portb = 80;}
  31. if(count2 ==2){portb = 96;}
  32. if(count2 ==3){portb = 160;}
  33. }

  34. void rot_right()
  35. {
  36. if(count3 ==0){portb = 160;}
  37. if(count3 ==1){portb = 96;}
  38. if(count3 ==2){portb = 80;}
  39. if(count3 ==3){portb = 144;}
  40. }

  41. void main()
  42. {
  43. set_tris_b(0b00000000);
  44. set_tris_c(0b00001111);
  45. portb=0;
  46. count = 0;
  47. count1 = 0;
  48. count2 = 0;
  49. count3 = 0;

  50.    while(true)
  51.    {
  52.       if (input(pin_c0)==1)
  53.       {
  54.          ele_left();
  55.          count = count + 1;
  56.          if(count ==4)count =0;
  57.       }

  58.       /////////////////////////////
  59.       else if (input(pin_c1)==1)
  60.       {
  61.          ele_right();
  62.          count1 = count1 + 1;
  63.          if(count1 ==4) count1 =0;
  64.       }

  65.       ////////////////////////////
  66.       else if (input(pin_c2)==1)
  67.       {
  68.          rot_left();
  69.          count2 = count2 + 1;
  70.          if(count2 ==4) count2 =0;
  71.       }

  72.       ////////////////////////////
  73.      else if (input(pin_c3)==1)
  74.       {
  75.          rot_right();
  76.          count3 = count3 + 1;
  77.          if(count3 ==4) count3 =0;
  78.       }
  79.       else
  80.       {
  81.          portb = 0;
  82.       }
  83.       delay_ms(1000);

  84.   }
  85.   
  86. }
复制代码

[ 本帖最后由 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
谢谢
回复

使用道具 举报

Follow Us
发表于 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 编辑 ]
回复

使用道具 举报


ADVERTISEMENT

发表于 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。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 1-2-2025 07:54 AM , Processed in 0.123887 second(s), 22 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表