TX与RX结合问题

[复制链接]
2184|2
 楼主| 107221829 发表于 2012-12-6 14:15 | 显示全部楼层 |阅读模式
ST, TE, se, RS, start
这是我自己写的RS232+ G-传感器程式
请问一下TX部分传输没问题
但RX部分该怎么接收TX资料进而转换成PWM
并控制PWM的大小
我是用29466这块IC
麻烦帮我解答一下谢谢

-----TX部分----
  1. //
  2. // Custom.c
  3. //
  4. //   Custom functions for insertion into PSoC Express projects, including fixed functions
  5. //
  6. #include <m8c.h>
  7. #include "PSoCAPI.h"
  8. #include "custom.h"

  9. #define START        0xa5
  10. #define X                0x41
  11. #define Y                0x52
  12. #define STOP        0x5a
  13. const BYTE data[] ={ START, X, Y, STOP};
  14. extern SYSTEM_VARS_STRUC SystemVars;
  15. void delay(unsigned int x);
  16. BOOL fWaitToTx(void);
  17. BYTE A;
  18. BYTE B;
  19. //
  20. //  CustomInit() is run after driver and channel instantiation functions, but before the main loop
  21. //
  22. void CustomInit( void )
  23. {
  24. }

  25. //
  26. // CustomPostInputUpdate() is run after input drivers have updated SystemVars, but before transfer
  27. //   functions are evaluated.
  28. //
  29. void CustomPostInputUpdate( void )
  30. {
  31. }

  32. //
  33. // CustomPreOutputUpdate() is run after transfer functions have been evaluated, but before the output
  34. //   driver are updated with values from SystemVars.
  35. //
  36. void CustomPreOutputUpdate( void )
  37. {
  38.         unsigned char i;
  39.         Counter8_Start();
  40.         TX8_Start(0);
  41.         M8C_EnableGInt;
  42.         A = PRT1DR;
  43.         B = PRT1DR;
  44.         //START
  45.         TX8_SendData(START);
  46.         if (fWaitToTx());
  47.         //X
  48.         if (SystemVars.ReadWriteVars.pse_XAxis < 0)
  49.         A = (-SystemVars.ReadWriteVars.pse_XAxis >> 3) | 0x80;
  50.     else
  51.         A = (SystemVars.ReadWriteVars.pse_XAxis >> 3) & 0x7f;
  52.         TX8_SendData(A);
  53.         if (fWaitToTx());
  54.         delay(100);
  55.         //Y
  56.         if (SystemVars.ReadWriteVars.pse_YAxis < 0)
  57.         B = (-SystemVars.ReadWriteVars.pse_YAxis >> 3) | 0x80;
  58.     else
  59.         B = (SystemVars.ReadWriteVars.pse_YAxis >> 3) & 0x7f;
  60.         TX8_SendData(B);
  61.         if (fWaitToTx());
  62.         //STOP
  63.         TX8_SendData(STOP);
  64.         if (fWaitToTx());
  65.        
  66.         delay(1000);
  67.        
  68. }
  69. BOOL fWaitToTx(void)
  70. {
  71.     BYTE bTxStatus;
  72.         while ( !(bTxStatus = TX8_bReadTxStatus() & TX8_TX_COMPLETE));
  73.         return (TRUE);  
  74. }

  75. void delay(unsigned int x)
  76. {
  77.     unsigned i;
  78.         for(i = 0; i < x; i++);
  79. }
----RX部分----
  1. //----------------------------------------------------------------------------
  2. // C main line
  3. //----------------------------------------------------------------------------

  4. #include <m8c.h>        // part specific constants and macros
  5. #include "PSoCAPI.h"    // PSoC API definitions for all User Modules
  6. #include "RX8.h"

  7. #define        START        0xa5
  8. #define        X        0x41
  9. #define Y        0x52
  10. #define        STOP        0x5a
  11. #define RX_OK        0x01
  12. const BYTE data[] ={ START, X, Y, STOP};  
  13. BOOL fWaitToRx(void);
  14. BYTE bRxData;
  15. void delay(unsigned int x);
  16. int j=0;
  17. void main(void)
  18. {
  19.     unsigned char i;
  20.         //--------RS232_RX---------  
  21.         Counter8_Start();
  22.         RX8_DisableInt();
  23.         RX8_Start(0);
  24.         M8C_EnableGInt;
  25.         //--------PWM---------
  26.         DigBuf1_DisableInt();
  27.         DigBuf2_DisableInt();
  28.             PWM8_WritePeriod(120);
  29.         PWM8_DisableInt();
  30.         PWM8_Start();
  31.         DigBuf3_DisableInt();
  32.         DigBuf4_DisableInt();
  33.             PWM81_WritePeriod(120);
  34.         PWM81_DisableInt();
  35.         PWM81_Start();
  36.        
  37.         while (1)
  38.         {
  39.           if (fWaitToRx())
  40.           {
  41.            if(bRxData)
  42.            {
  43.              if (j == 0)
  44.                 {
  45.                     DigBuf1_Start();
  46.                         DigBuf2_Stop();
  47.                         DigBuf4_Start();
  48.                         DigBuf3_Stop();
  49.                        
  50.                 for (i = 7; i <= 120; i++)
  51.                 {
  52.                         PWM8_WritePulseWidth(i);
  53.                                 PWM81_WritePulseWidth(i);
  54.                         delay(2500);
  55.                 }
  56.                         for (i = 120; i >= 7; i--)
  57.                 {
  58.                         PWM8_WritePulseWidth(i);
  59.                                 PWM81_WritePulseWidth(i);
  60.                         delay(2500);
  61.                 }
  62.                        
  63.                 }
  64.                 else
  65.                 {
  66.                     DigBuf1_Stop();
  67.                         DigBuf2_Start();
  68.                         DigBuf4_Stop();
  69.                         DigBuf3_Start();
  70.                 for (i = 7; i <= 120; i++)
  71.                 {
  72.                         PWM8_WritePulseWidth(i);
  73.                                 PWM81_WritePulseWidth(i);
  74.                         delay(2500);
  75.                 }
  76.                         for (i = 120; i >= 7; i--)
  77.                 {
  78.                         PWM8_WritePulseWidth(i);
  79.                                 PWM81_WritePulseWidth(i);
  80.                         delay(2500);
  81.                 }
  82.                 }
  83.                 j = 1 - j;
  84.            }       
  85.           }
  86.          
  87.         }
  88. }

  89. BOOL fWaitToRx(void)
  90. {
  91.     BYTE bRxStatus;
  92.     while ( !(bRxStatus = RX8_bReadRxStatus() & RX8_RX_COMPLETE));
  93.         if ((bRxStatus & RX8_RX_NO_ERROR) == 0)
  94.         {
  95.             bRxData = RX8_bReadRxData();
  96.                 return (TRUE);
  97.         }
  98.         else
  99.         {
  100.             bRxData = bRxStatus;
  101.                 return (FALSE);
  102.         }       
  103. }

  104. void delay(unsigned int x)
  105. {
  106.    unsigned i;
  107.    for(i = 0; i < x; i++);
  108. }
Go_PSoC 发表于 2012-12-28 10:49 | 显示全部楼层

楼主问题解决了吗?或者可以到官网建一个case,关于建case,可以看看这个帖子

Cypress技术支持的新途径
https://bbs.21ic.com/forum.php?mo ... &fromuid=595718
Go_PSoC 发表于 2012-12-28 10:49 | 显示全部楼层
可以写中文
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

1

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部
0