接收TX资料进而转换成PWM

[复制链接]
262|10
手机看帖
扫描二维码
随时随地手机跟帖
chuxh|  楼主 | 2018-6-13 17:51 | 显示全部楼层 |阅读模式

TX部分传输没问题
但RX部分该怎么接收TX资料进而转换成PWM
并控制PWM的大小
juventus9554| | 2018-6-13 17:55 | 显示全部楼层

你用的是什么

使用特权

评论回复
chuxh|  楼主 | 2018-6-13 18:00 | 显示全部楼层
我是用29466这块IC

使用特权

评论回复
dingy| | 2018-6-13 18:03 | 显示全部楼层

你做的是什么啊

使用特权

评论回复
chuxh|  楼主 | 2018-6-13 18:06 | 显示全部楼层

我自己写的RS232+ G-传感器程式

使用特权

评论回复
chuxh|  楼主 | 2018-6-13 18:09 | 显示全部楼层
----TX部分----
//
// Custom.c
//
//   Custom functions for insertion into PSoC Express projects, including fixed functions
//
#include <m8c.h>
#include "PSoCAPI.h"
#include "custom.h"

#define START        0xa5
#define X                0x41
#define Y                0x52
#define STOP        0x5a
const BYTE data[] ={ START, X, Y, STOP};
extern SYSTEM_VARS_STRUC SystemVars;
void delay(unsigned int x);
BOOL fWaitToTx(void);
BYTE A;
BYTE B;
//
//  CustomInit() is run after driver and channel instantiation functions, but before the main loop
//
void CustomInit( void )
{
}

//
// CustomPostInputUpdate() is run after input drivers have updated SystemVars, but before transfer
//   functions are evaluated.
//
void CustomPostInputUpdate( void )
{
}

//
// CustomPreOutputUpdate() is run after transfer functions have been evaluated, but before the output
//   driver are updated with values from SystemVars.
//
void CustomPreOutputUpdate( void )
{
        unsigned char i;
        Counter8_Start();
        TX8_Start(0);
        M8C_EnableGInt;
        A = PRT1DR;
        B = PRT1DR;
        //START
        TX8_SendData(START);
        if (fWaitToTx());
        //X
        if (SystemVars.ReadWriteVars.pse_XAxis < 0)
        A = (-SystemVars.ReadWriteVars.pse_XAxis >> 3) | 0x80;
    else
        A = (SystemVars.ReadWriteVars.pse_XAxis >> 3) & 0x7f;
        TX8_SendData(A);
        if (fWaitToTx());
        delay(100);
        //Y
        if (SystemVars.ReadWriteVars.pse_YAxis < 0)
        B = (-SystemVars.ReadWriteVars.pse_YAxis >> 3) | 0x80;
    else
        B = (SystemVars.ReadWriteVars.pse_YAxis >> 3) & 0x7f;
        TX8_SendData(B);
        if (fWaitToTx());
        //STOP
        TX8_SendData(STOP);
        if (fWaitToTx());

        delay(1000);

}
BOOL fWaitToTx(void)
{
    BYTE bTxStatus;
        while ( !(bTxStatus = TX8_bReadTxStatus() & TX8_TX_COMPLETE));
        return (TRUE);  
}

void delay(unsigned int x)
{
    unsigned i;
        for(i = 0; i < x; i++);
}

使用特权

评论回复
chuxh|  楼主 | 2018-6-13 18:09 | 显示全部楼层

----RX部分----
//----------------------------------------------------------------------------
// C main line
//----------------------------------------------------------------------------

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

#define        START        0xa5
#define        X        0x41
#define Y        0x52
#define        STOP        0x5a
#define RX_OK        0x01
const BYTE data[] ={ START, X, Y, STOP};  
BOOL fWaitToRx(void);
BYTE bRxData;
void delay(unsigned int x);
int j=0;
void main(void)
{
    unsigned char i;
        //--------RS232_RX---------  
        Counter8_Start();
        RX8_DisableInt();
        RX8_Start(0);
        M8C_EnableGInt;
        //--------PWM---------
        DigBuf1_DisableInt();
        DigBuf2_DisableInt();
            PWM8_WritePeriod(120);
        PWM8_DisableInt();
        PWM8_Start();
        DigBuf3_DisableInt();
        DigBuf4_DisableInt();
            PWM81_WritePeriod(120);
        PWM81_DisableInt();
        PWM81_Start();

        while (1)
        {
          if (fWaitToRx())
          {
           if(bRxData)
           {
             if (j == 0)
                {
                    DigBuf1_Start();
                        DigBuf2_Stop();
                        DigBuf4_Start();
                        DigBuf3_Stop();

                for (i = 7; i <= 120; i++)
                {
                        PWM8_WritePulseWidth(i);
                                PWM81_WritePulseWidth(i);
                        delay(2500);
                }
                        for (i = 120; i >= 7; i--)
                {
                        PWM8_WritePulseWidth(i);
                                PWM81_WritePulseWidth(i);
                        delay(2500);
                }

                }
                else
                {
                    DigBuf1_Stop();
                        DigBuf2_Start();
                        DigBuf4_Stop();
                        DigBuf3_Start();
                for (i = 7; i <= 120; i++)
                {
                        PWM8_WritePulseWidth(i);
                                PWM81_WritePulseWidth(i);
                        delay(2500);
                }
                        for (i = 120; i >= 7; i--)
                {
                        PWM8_WritePulseWidth(i);
                                PWM81_WritePulseWidth(i);
                        delay(2500);
                }
                }
                j = 1 - j;
           }        
          }

        }
}

BOOL fWaitToRx(void)
{
    BYTE bRxStatus;
    while ( !(bRxStatus = RX8_bReadRxStatus() & RX8_RX_COMPLETE));
        if ((bRxStatus & RX8_RX_NO_ERROR) == 0)
        {
            bRxData = RX8_bReadRxData();
                return (TRUE);
        }
        else
        {
            bRxData = bRxStatus;
                return (FALSE);
        }        
}

void delay(unsigned int x)
{
   unsigned i;
   for(i = 0; i < x; i++);
}

使用特权

评论回复
houcs| | 2018-6-13 18:14 | 显示全部楼层

可以到官网建一个case

使用特权

评论回复
liuzaiy| | 2018-6-13 18:16 | 显示全部楼层

楼主是根据接收的内容确定PWM的大小吧

使用特权

评论回复
pangb| | 2018-6-13 19:29 | 显示全部楼层
写程序吧,根据斜率进行控制,搞一个公式出来

使用特权

评论回复
chuxh|  楼主 | 2018-6-13 19:32 | 显示全部楼层
好的,我明天去单位试一下,多谢各位大侠了哈,结贴了先

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

936

主题

11296

帖子

2

粉丝