这是我自己写的RS232+ G-传感器程式
请问一下TX部分传输没问题
但RX部分该怎么接收TX资料进而转换成PWM
并控制PWM的大小
我是用29466这块IC
麻烦帮我解答一下谢谢
-----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++);
}
----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++);
}
|