View Single Post
Old 25-10-2011, 12:52 PM   #1
cindar
Nhập môn đệ tử
 
Tham gia ngày: Nov 2010
Bài gửi: 2
:
Giao tiếp UART với dsPIC30F

Hiện đang làm giao tiếp PC với dsPIC30F nhưng gặp một số vướng mắc sau:
kí tự nhận đuợc trên máy tính toàn là các kí tự @%$#&
em đã config baud trên máy tính là 9600,terminal là 9600,dsPIC cũng 9600
em đã mô phỏng với MPLAB SIM chạy rất tốt nhưng nạp vào kit thì toàn ra chữ tượng hình

Code:
//---------------------------------//
//----------include file-----------//
//---------------------------------//
#include "p30F4011.h"
#include <uart.h>
#include <stdio.h>
#include <stdlib.h>

//---------------------------------//
//--------- config bits -----------//
//---------------------------------//
	_FOSC(CSW_FSCM_OFF & FRC_PLL4);				//32MHZ
	_FWDT(WDT_OFF);								//
	_FBORPOR(PBOR_OFF & MCLR_EN);				//
	_FGS(CODE_PROT_OFF);						//
	

#define	Fcy	8000000								//Tan so thuc thi lenh
#define baudvalue	9600
#define baudcalc (Fcy/baudvalue)/16-1
#define _ISR_PSV __attribute__((interrupt, auto_psv))

/*** khai bao bien cho UART ***/  
unsigned int U1MODEvalue;/* Holds the value of uart config reg */
unsigned int U1STAvalue; /* Holds the information regarding uart TX & RX interrupt modes */  
char Txdata[] = {'M','i','c','r','o','c','h','i','p',' ','I','C','D','2','\0'};

void init_uart(void);

int main(void)
{
	init_uart();
	for(;;)
	{
		while(BusyUART1());
		putsUART1((unsigned int*)Txdata);
	}
}

void init_uart(void)
{
/* Turn off UART1module */
    CloseUART1();
/* Configure uart1 receive and transmit interrupt */
    ConfigIntUART1(UART_RX_INT_EN & UART_RX_INT_PR6 & 
                   UART_TX_INT_DIS & UART_TX_INT_PR2);
/* Configure UART1 module to transmit 8 bit data with one stopbit  */
    U1MODEvalue = UART_EN & UART_IDLE_CON & 
                  UART_DIS_WAKE & UART_DIS_LOOPBACK  &
                  UART_DIS_ABAUD & UART_NO_PAR_8BIT  &
                  UART_1STOPBIT;
    U1STAvalue  = UART_INT_TX_BUF_EMPTY  &  
                  UART_TX_PIN_NORMAL &
                  UART_TX_ENABLE & UART_INT_RX_3_4_FUL &
                  UART_ADR_DETECT_DIS &
                  UART_RX_OVERRUN_CLEAR;
    OpenUART1(U1MODEvalue, U1STAvalue, baudcalc);
    return;
}
void _ISR_PSV _U1RXInterrupt(void)
{
	//code here
	IFS0bits.U1RXIF = 0;/* xoa co ngat */
}
ban đầu em chạy với thạch anh 10MHZ PPLx8,ko đuợc nên sau đó chuyển qua FRC_PPL4
em không hiểu sai chỗ nào nhưng nó chạy loạn xạ quá.
anh nào có kinh nghiệm làm về cái này giúp em với

thay đổi nội dung bởi: cindar, 25-10-2011 lúc 04:56 PM.
cindar vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn