通过UART将单个字符从pic16f887传输到PC终端(Putty,Hyperterminal等)。

时间:2013-05-03 13:58:57

标签: serial-port char uart

我试图从pic16f887传输一个字符“a”并在终端上看到结果,但我得到的只是一个问号(使用USART终端),或者根本没有(Putty,Hyperterminal)。我不是很喜欢C,因为我只是一个初学者,但我真的需要让这个工作用于我的学校项目..我已经尝试了很多我在互联网上找到的代码,我确实设法收到了来自终端的角色,让我们说,打开一个LED,但我无法让它发送任何东西。我对代码中的某个地方有强烈的感觉。我使用MPLAB和Hi-Tech C编译器来构建项目。这是:

unsigned char cUART_char;
unsigned char cUART_data_flg;

void init_uart(void);
void UART_putc(unsigned char c);
void InterruptHandlerLow ();

void main()
{

TRISA = 0;
PORTA = 0;
TRISB = 0;
PORTB = 0;
TRISD = 0;
PORTD = 0;
ANSELH = 0;




    init_uart(); 

    while (1) 
    {
        if (cUART_data_flg==1)
        {
            UART_putc(cUART_char); 
            cUART_data_flg=0; 
        }
    }

}

void InterruptHandlerLow ()
{
  if (RCIF==1)//is interrupt occured by EUSART receive?, 
                        //then RCREG is full we have new data (cleared when RCREG is read)
  {  
    if(RCSTA&0x06) //more efficient way than following commented method to check for reception error 
    //if(RCSTAbits.FERR==1 || RCSTAbits.OERR==1 )
    {
      CREN=0;    //Overrun error (can be cleared by clearing bit CREN)
      cUART_char=RCREG;    //clear Framing error 
      CREN=1;
    }
    else
    {
       cUART_char = RCREG; // read new data into variable
       cUART_data_flg = 1; // new data received. so enable flg
    }
  }
}

void init_uart(void) // init UART module for 9600bps boud, start bit 1, stopbit 1, parity NONE
{
      // init data receive flag to zero (no data)
    TRISC7=1; //Make UART RX pin input
    TRISC6=0; //Make UART TX pin output
    SYNC = 0;   // enables for asynchronous EUART
    SPEN = 1;   // enables EUSART and sets TX (RC6) as output; ANSEL must be cleared if shared with analog I/O
    CREN = 1;

    TX9 = 0;    // 8bit mode
    RX9 = 0;

    TXEN = 1;    // enables Transmitter

    BRGH = 1;   // baud rate select
    BRG16 = 0;

    SPBRG = 25; //baud rate select  9600@4Mhz
    SPBRGH = 0;

    RCIE=1;     // receive interrupt enable
    GIE=1;      // global interrupt enable
    PEIE=1 ;    // Peripheral Interrupt Enable bit
}

void UART_putc(unsigned char c)
{
  TXEN=0;// disable transmission
  TXREG=0x61;            // load txreg with data
  TXEN=1;    // enable transmission
  while(TRMT==0) // wait here till transmit complete
  {

  }
}

如果有人在此代码中发现问题(或更多),请帮我解决此问题;) 哦,按下连接到TX引脚(RC6)的按钮时,我正在发送..

0 个答案:

没有答案