laozhongyi 发表于 2022-3-27 21:05

用CCR1为什么做不了?

我用TA0的0通道实现了模拟串口的收发功能,但是同样的套路,用CCR1为什么做不了?

Edisons 发表于 2022-3-27 21:07

static uchar   bitcnt=0;
static uint   uart_buf;

static ucharRXData0;//用来存储接收的8位数据
static uchar   RXBitCnt0;//接收数据位数
ucharrx_sign=0;

volatile int Send_flag=0;

void FaultRoutine(void)
{
   while(1);// 异常挂起
}
void ConfigClocks()
{
   uint i;
for(i = 0; i < 0xFFFF; i++){};
_BIS_SR(OSCOFF);
if(CALBC1_1MHZ==0xff||CALDCO_1MHZ==0xff)
{
    while(1);
}
BCSCTL1 = CALBC1_1MHZ;// Set range
DCOCTL = CALDCO_1MHZ;// Set DCO step + modulation
BCSCTL2 |=SELM_0+DIVS_0+DIVM_0;;            // MCLK = DCO = SMCLK=1MHZ         
}
void ConfigPins()
{
P1DIR |= TXD+LED+BIT6;
P1SEL |=RXD;
P1DIR &= ~RXD;                                        // P1.2 input, other outputs

P1OUT |= TXD;                                                      // clear output pins
P1OUT &=~LED;
}

//----------发送一个字节------------------------------------------//
void send_char(uchar tchar)
{
TACTL = TACLR + TASSEL_2; //清零计数器 选择SMCLK时钟
CCR0 = BITTIME_1b;   //crr0定时间隔为1bit时间宽度
CCTL0 |= CCIE;      //打开CCR0中断
bitcnt = 10;   //待发送的位数,1位开始位,8位数据位,1位停止位
uart_buf = 0x0100;   //stop bit 停止位
uart_buf |= tchar;//data bit 数据位
uart_buf <<=1;//start bit.开始位
Send_flag = 0;
TACTL |= MC_1; //Start TA, Timer A mode control: 1 - Up to CCR0
_BIS_SR(GIE);
while(!Send_flag); //wait until send complete
Send_flag = 1;
}
//---------------------------------------------------------------------//

void RXReady(void)
{
// RXBitCnt0=0x08;//初始化接收数据计数器
TACTL=TASSEL_2+MC_2+TACLR;//SMCLK连续记数模式清零计数器
TACCTL1=CM_2+CAP+SCS+CCIE+CCIS_0;
//下降沿捕获同步捕获源捕获模式捕获中断使能捕获输入为CCI1A
_EINT();
}

// Timer A0 interrupt service routine
#pragma vector=TIMERA0_VECTOR
__interrupt void Timer_A0 (void) //串口发送程序处理过程,位操作
{
   if(bitcnt>0)
{
    if(uart_buf & 0x01)
      POUT |= TXD;//发送数据1
    else
      POUT &= ~TXD;//发送数据0
    uart_buf >>= 1;
    bitcnt--;
}
   else
{
    POUT|=TXD;//默认状态置高
    TACTL &= ~MC_3; //Close the TA when a Byte send over. 00 停止模式
    CCTL0 &= ~CCIE; //关闭CCR0中断
    Send_flag = 1;
}
}
//---------------------------------------------------------------------//

#pragma vector=TIMERA1_VECTOR
__interrupt void Timer_A1 (void)//接收串口数据处理过程,利用串口协议的时间间隔来进行。
{
               P1OUT ^= LED;
               TACCR1+=BITTIME_1b;//增加一位定时间隔
            if(TACCTL1&CAP) //判断是否为捕获模式
             {
                  RXBitCnt0=0;
            }
             switch(RXBitCnt0)
             {
                case 0:   //为捕获模式
                        RXData0=0;//清空缓冲区
                        TACCR1+=BITTIME_1b5;
                        TACCTL1&=~CAP;//改为比较模式
                        RXBitCnt0++;
                        break;   
                case 9://比较模式改为捕获模式
                        TACCTL1|=CAP;
                        rx_sign=1; //作为接收完毕的信号
                        break;
                default://1~8 有效数据位
                      RXData0=RXData0>>1;//右移一位,等待下一个数据进来
                      if(TACCTL1&SCCI)RXData0|=0x80;//输入为1
                      RXBitCnt0++;
                      break;
               }
}

_gege 发表于 2022-3-27 21:11

#define RXD 0x0002 // RXD on P1.1
#define TXD 0x0002 // TXD on P2.1
#define RS485 0x0001 //enable 485 on P2.0

// Conditions for 2400 Baud SW UART, ACLK = 32768
#define Bitime_5 0x06 // ~ 0.5 bit length + small adjustment
#define Bitime 0x0E // 427us bit length ~ 2341 baud

unsigned int RXTXData;
unsigned char BitCnt;
void Delay(unsigned int i);
void TX_Byte(void);
void RX_Ready(void);

void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
FLL_CTL0 |= XCAP14PF; // Configure load caps
CCTL0 |= OUT; // TXD Idle as Mark
TACTL = TASSEL_1 + MC_1; // ACLK, continous mode
P1SEL |= RXD;
P1DIR &= ~RXD;
P2SEL |= TXD;
P2DIR |= TXD;
P2DIR |= RS485;
P2OUT &= ~RS485;
_EINT();

// Mainloop
for(;;)
{
RX_Ready(); // UART ready to RX one Byte
_BIS_SR(LPM3_bits + GIE); // Enter LPM3 Until character RXed
TX_Byte(); // TX Back RXed Byte Received
}
}

void Delay(unsigned int i)
{
unsigned char j;

for(j = 0; j < 5; j ++)
while(i--);
}

// Function Transmits Character from RXTXData Buffer
void TX_Byte(void)
{
P2OUT |= RS485;
Delay(10);
P2OUT &= ~RS485;
BitCnt = 0x0A; // Load Bit counter, 8data + ST/SP
CCR0 = TAR; // Current state of TA counter
CCR0 += Bitime; // Some time till first bit
RXTXData |= 0x0100; // Add mark stop bit to RXTXData
RXTXData = RXTXData << 1; // Add space start bit
CCTL0 = OUTMOD0 + CCIE; // TXD = mark = idle
while(CCTL0 & CCIE); // Wait for TX completion
}

// Function Readies UART to Receive Character into RXTXData Buffer
void RX_Ready(void)
{
BitCnt = 0x08; // Load Bit counter
CCTL0 = SCS + CCIS0 + OUTMOD0 + CM1 + CAP + CCIE; // Sync, Neg Edge, Capture
}

// Timer A0 interrupt service routine
interrupt
void TimerA0_ISR(void)
{
CCR0 += Bitime; // Add Offset to CCR0

// RX--------------------------------------------------------------------------
if(CCTL0 & CCIS0) // RX on CCI0B?
{
if(CCTL0 & CAP) // Capture mode = start bit edge
{
CCTL0 &= ~CAP; // Capture to compare mode
CCR0 += Bitime_5;
}
else
{
RXTXData = RXTXData >> 1;
if(CCTL0 & SCCI) // Get bit waiting in receive latch
RXTXData |= 0x80;

BitCnt --; // All bits RXed?
if(BitCnt == 0)
//>>>>>>>>>> Decode of Received Byte Here <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
{
CCTL0 &= ~CCIE; // All bits RXed, disable interrupt
_BIC_SR_IRQ(LPM3_bits); // Clear LPM3 bits from 0(SR)
}
//>>>>>>>>>> Decode of Received Byte Here <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
}
}
// TX--------------------------------------------------------------------------
else
{
if(BitCnt == 0)
CCTL0 &= ~CCIE; // All bits TXed, disable interrupt
else
{
CCTL0 |= OUTMOD2; // TX Space
if(RXTXData & 0x0001)
CCTL0 &= ~OUTMOD2; // TX Mark

RXTXData = RXTXData >> 1;
BitCnt--;
}
}
}

Mozarts 发表于 2022-3-27 21:12

上传程序看看是什么原因呢?

Listate 发表于 2022-3-27 21:16

这个说明低电平信号没有了。

bbapple 发表于 2022-3-27 21:17

模拟串口的功能一般没有问题的。

handleMessage 发表于 2022-3-27 21:21

如果电平信号都满足要求才可以。

zhouhuanの 发表于 2022-3-27 21:22

这个可能波特率不对的。{:smile:}

xia00 发表于 2022-3-27 21:26

定时器的效率不怎么快的。{:smile:}

hfdy01 发表于 2022-3-27 21:28

参考一下G233x的芯片的定时器模拟串口。

CallReceiver 发表于 2022-3-27 21:31

主要是采用定时器进行处理信号时序的。

boy1990 发表于 2022-3-27 21:33

模拟串口的设计还是有一定的难度的。

sourceInsight 发表于 2022-3-27 21:36

模拟串口的设计还是有一定的难度的{:smile:}
页: [1]
查看完整版本: 用CCR1为什么做不了?