[求助]关于串口相关寄存器的问题

[复制链接]
 楼主| zhaoyanggolden 发表于 2013-3-25 16:11 | 显示全部楼层 |阅读模式
我是新手,最近刚接触DSP,使用的是ICETEK的f2812板子调程序。最近在研究串口通信的时候遇到问题了。
使用DSP板子自带的例程编译后运行,但是串口调试助手中无法收到板子发送的数据,单步调试了一下,发现是
while(SciaRegs.SCIFFTX.bit.TXFFST !=0) { }
这一句中TXFFST这个值,一旦发送缓冲区读入第一个值后就无法归零,导致程序一直锁死,请问这个值的作用是什么,应该怎样使用才能让串口正常工作,谢谢各位了
 楼主| zhaoyanggolden 发表于 2013-3-25 16:12 | 显示全部楼层
下面是程序的源代码
  1. /* 2812A板:
  2.         使用直连的串口通信电缆;
  3.         启动串口调试助手.exe;
  4.         PC机发送一个"."为结束标志
  5. */
  6. #include "DSP281x_Device.h"
  7. #include "DSP281x_Examples.h"

  8. // Prototype statements for functions found within this file.
  9. void scia_loopback_init(void);
  10. void scia_fifo_init(void);       
  11. void scia_xmit(int a);
  12. void error(int);
  13. void wait(int nWait);
  14. interrupt void scia_rx_isr(void);
  15. interrupt void scia_tx_isr(void);

  16. // Global counts used in this example
  17. Uint16 LoopCount;
  18. Uint16 ErrorCount;
  19. char cString[17]={"Hello PC!,Over|"},cReceive,cBuffer[17],cAnswer[16]={"Oh,you say"};
  20. void main(void)
  21. {   
  22.     char ReceivedChar;   
  23.     int i,k=0,nLen,bReceive=0;
  24. // Step 1. Initialize System Control registers, PLL, WatchDog, Clocks to default state:
  25.         // This function is found in the DSP281x_SysCtrl.c file.
  26.         InitSysCtrl();

  27. // Step 2. Select GPIO for the device or for the specific application:
  28.        // This function is found in the DSP281x_Gpio.c file.
  29.        // InitGpio(); skip this as this is example selects the I/O for SCI in this file itself
  30.     EALLOW;
  31.     GpioMuxRegs.GPFMUX.all=0x0030;        // Select GPIOs to be Sci pins         
  32.                                     // Port F MUX - x000 0000 0011 0000
  33.     EDIS;

  34. // Step 3. Initialize PIE vector table:
  35.       // The PIE vector table is initialized with pointers to shell Interrupt
  36.       // Service Routines (ISR).  The shell routines are found in DSP281x_DefaultIsr.c.
  37.       // Insert user specific ISR code in the appropriate shell ISR routine in
  38.       // the DSP28_DefaultIsr.c file.

  39.       // Disable and clear all CPU interrupts:
  40.         DINT;
  41.         IER = 0x0000;
  42.         IFR = 0x0000;

  43.       // Initialize Pie Control Registers To Default State:
  44.       // This function is found in the DSP281x_PieCtrl.c file.
  45.           // InitPieCtrl();  PIE is not used for this example

  46.       // Initialize the PIE Vector Table To a Known State:
  47.       // This function is found in DSP281x_PieVect.c.
  48.       // This function populates the PIE vector table with pointers
  49.       // to the shell ISR functions found in DSP281x_DefaultIsr.c.
  50.           InitPieVectTable();  

  51.       // Enable CPU and PIE interrupts
  52.       // This example function is found in the DSP281x_PieCtrl.c file.   
  53.       EnableInterrupts();
  54.        
  55. // Step 4. Initialize all the Device Peripherals to a known state:
  56.       // This function is found in DSP281x_InitPeripherals.c
  57.       // InitPeripherals(); skip this for SCI tests
  58.        
  59. // Step 5. User specific functions, Reassign vectors (optional), Enable Interrupts:

  60.     LoopCount = 0;
  61.     ErrorCount = 0;
  62.    
  63.     scia_fifo_init();           // Initialize the SCI FIFO
  64.     scia_loopback_init();  // Initalize SCI for digital loop back

  65.     // Note: Autobaud lock is not required for this example
  66.    
  67.     // Send a character starting with 0
  68.                                                

  69. // Step 6. Send Characters forever starting with 0x00 and going through
  70. // 0xFF.  After sending each, check the recieve buffer for the correct value
  71.     for ( i=0;i<16;i++ )
  72.                         {
  73.                             scia_xmit(cString[i]);
  74.                                 while(SciaRegs.SCIFFTX.bit.TXFFST !=0) { }
  75.                                 //wait(1024);               
  76.                         }
  77.         for(;;)
  78.     {        
  79.        if ( bReceive==1 )
  80.        {for ( i=0;i<10;i++ )
  81.                         {                       
  82.                                 scia_xmit(cAnswer[i]);
  83.                              while(SciaRegs.SCIFFTX.bit.TXFFST !=0) { }
  84.                         }
  85.                        
  86.                         scia_xmit('"');
  87.                         for ( i=0;i<nLen;i++ )
  88.                         {
  89.                        
  90.                                 scia_xmit(cBuffer[i]);
  91.                             while(SciaRegs.SCIFFTX.bit.TXFFST !=0) { }
  92.                         }
  93.                        
  94.                         scia_xmit('"');
  95.                         wait(1024);               
  96.                         for ( i=9;i<16;i++ )
  97.                         {                               
  98.                                 scia_xmit(cString[i]);
  99.                             while(SciaRegs.SCIFFTX.bit.TXFFST !=0) { }
  100.                         }
  101.                 }
  102.                 k=0; bReceive=0;
  103.         while(1)
  104.          {
  105.             while(SciaRegs.SCIFFRX.bit.RXFIFST ==0) { } // 如果接受寄存器不为0则跳出
  106.             ReceivedChar = SciaRegs.SCIRXBUF.all;
  107.             cBuffer[k]=ReceivedChar;
  108.             
  109.                 if ( ReceivedChar=='.')
  110.                         {
  111.                                 cBuffer[k+1]='\0';
  112.                                 nLen=k+1;
  113.                                 bReceive=1;
  114.                                 break;
  115.                         }
  116.                     k++; k%=16;
  117.               }       
  118.       
  119.     }

  120. }        


  121. // Step 7. Insert all local Interrupt Service Routines (ISRs) and functions here:       

  122. void error(int ErrorFlag)
  123. {
  124.       ErrorCount++;

  125. }

  126. // Test 1,SCIA  DLB, 8-bit word, baud rate 0x000F, default, 1 STOP bit, no parity
  127. void scia_loopback_init()
  128. {   
  129.     // Note: Clocks were turned on to the SCIA peripheral
  130.     // in the InitSysCtrl() function
  131.    
  132.         SciaRegs.SCICCR.all =0x0007;   // 1 stop bit,  No loopback
  133.                                    // No parity,8 char bits,
  134.                                    // async mode, idle-line protocol
  135.         SciaRegs.SCICTL1.all =0x0003;  // enable TX, RX, internal SCICLK,
  136.                                    // Disable RX ERR, SLEEP, TXWAKE
  137.         SciaRegs.SCICTL2.all =0x0003;
  138.         SciaRegs.SCICTL2.bit.TXINTENA =1;
  139.         SciaRegs.SCICTL2.bit.RXBKINTENA =1;
  140.     SciaRegs.SCIHBAUD    =0x0001;
  141.     SciaRegs.SCILBAUD    =0x00e7;
  142.         SciaRegs.SCICCR.bit.LOOPBKENA =0; // disable loop back  
  143.         SciaRegs.SCICTL1.all =0x0023;     // Relinquish SCI from Reset
  144. }

  145. // Transmit a character from the SCI'
  146. void scia_xmit(int a)
  147. {
  148.     SciaRegs.SCITXBUF=a;
  149. }   

  150. // Initalize the SCI FIFO
  151. void scia_fifo_init()                                                                               
  152. {
  153.     SciaRegs.SCIFFTX.all=0xE040;
  154.     SciaRegs.SCIFFRX.all=0x204f;
  155.     SciaRegs.SCIFFCT.all=0x0;
  156.    
  157. }  

  158. void wait(int nWait)
  159. {
  160.         int i,j,k=0;
  161.         for ( i=0;i<nWait;i++ )
  162.                 for ( j=0;j<64;j++ )
  163.                         k++;
  164. }                                                                       
zhangmangui 发表于 2013-3-25 17:46 | 显示全部楼层
我觉得这句没有问题的  这句的功能:如果缓冲区里面有东西没有发送完的话 这个标志置位表示数据还在发送  等待发送完成   不可能一直到这儿循环啊   一直循环的话  说明有数据一直在发送啊  这样上位机软件是可以收到数据的  还是检查一下你的串口是不是通
下面是我用的发送函数
/*------------------------------------------*/
/*形式参数:char                            */
/*返回值:void                                                */
/*函数描述:发送一个字符                                                */
/*------------------------------------------*/
void Send_Char_SCIA(char temp)
{
        while(SciaRegs.SCIFFTX.bit.TXFFST !=0);
        SciaRegs.SCITXBUF=temp;
}
/*------------------------------------------*/
/*形式参数: char *String                   */
/*返回值:void                                                */
/*函数描述:发送一个字符串                                        */
/*------------------------------------------*/
void Send_String_SCIA(char *String)
{
        while(*String !='\0')
          {
     Send_Char_SCIA(*String++);
          }
}
 楼主| zhaoyanggolden 发表于 2013-3-25 19:39 | 显示全部楼层
感谢回复,串口线没问题、上位机的接口也正常,如果代码没有问题的话,那有可能是板子上串口自身的问题了,我试了两块都是一样的结果真是
zhangmangui 发表于 2013-3-26 09:41 | 显示全部楼层
zhaoyanggolden 发表于 2013-3-25 19:39
感谢回复,串口线没问题、上位机的接口也正常,如果代码没有问题的话,那有可能是板子上串口自身的问题了, ...

建议你用硬件中断进行测试   看是否进入相应的处理函数中去
 楼主| zhaoyanggolden 发表于 2013-3-26 10:24 | 显示全部楼层
能详细说一下应该怎么做吗?你的意思是不是在相应的处理函数(比如void scia_xmit(int a))中开中断,函数结尾关中断,用板子上某个按钮做中断源,看看按下按钮后它是否能进入处理函数?
蓝草飘香 发表于 2013-9-28 21:53 | 显示全部楼层
zhangmangui 发表于 2013-3-25 17:46
我觉得这句没有问题的  这句的功能:如果缓冲区里面有东西没有发送完的话 这个标志置位表示数据还在发送   ...

大侠。  请教一下。。   这一句说如果有数据没有发完,就一直循环等待, 那发完了还需要那个for循环做什么呢?
zhangmangui 发表于 2013-9-28 22:19 | 显示全部楼层
蓝草飘香 发表于 2013-9-28 21:53
大侠。  请教一下。。   这一句说如果有数据没有发完,就一直循环等待, 那发完了还需要那个for循环做什 ...

连续发的时候  等待上次发完  发完就会跳出while
继续下面的
蓝草飘香 发表于 2013-9-29 10:55 | 显示全部楼层
本帖最后由 蓝草飘香 于 2013-9-29 10:56 编辑

如果我要发31个数据。。。  程序如下:
interrupt void SCITXINTA_ISR(void)     // SCI-A发送中断函数
{
    int i;
        for(i=0;i<31;i++)
        {
            SciaRegs.SCITXBUF=buffer; //发送数据
                 while(SciaRegs.SCIFFTX.bit.TXFFST!=0);
        }

    PieCtrl.PIEACK.all=0x0100;  //使得同组其他中断能够得到响应
    EINT;  //开全局中断
}
那每发一个数据就等待FIFO发完,FIFO里面就相当于进来一个数据就发一个,等FIFO空了再发下一个?  感觉怪怪的~~
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

5

帖子

0

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