bác nào giúp em với, em viết chương trình gửi kí tự lên PC, và từ PC gửi xuống 30F4011, nhưng đều ra các kí tự là, %!@&....
code nhận dữ liệu từ PC
Code:
#include "p30f4011.h"
#include "uart.h"
_FOSC(CSW_FSCM_OFF & FRC_PLL4); //Clock = 32 MHz
_FWDT(WDT_OFF);
_FBORPOR(MCLR_EN & PBOR_OFF & PWMxL_ACT_HI & PWMxH_ACT_HI);
_FGS(CODE_PROT_OFF);
//Chuong trinh cau hinh UART
//------------------------------------------------------------------------------
void Uart_Setup(void)
{
unsigned int baudvalue,U2MODEvalue,U2STAvalue;
CloseUART2();
ConfigIntUART2( UART_RX_INT_EN & UART_RX_INT_PR6 &
UART_TX_INT_DIS & UART_TX_INT_PR1 ) ;
baudvalue = (((7370000/9600)/16)-1); //baud=9600 bps ==> nhaapj vaof baudvalue = (((7370000/9600)/16)-1) voi Fcy = 7.37 Mhz,
U2MODEvalue = UART_EN & UART_IDLE_STOP & UART_RX_TX &
UART_DIS_WAKE & UART_DIS_LOOPBACK &
UART_DIS_ABAUD & UART_NO_PAR_8BIT &
UART_1STOPBIT;
U2STAvalue = UART_INT_TX &
UART_TX_PIN_NORMAL &
UART_TX_ENABLE &
UART_INT_RX_CHAR &
UART_ADR_DETECT_DIS &
UART_RX_OVERRUN_CLEAR;
OpenUART2(U2MODEvalue, U2STAvalue, baudvalue);
}
//========================== UART1 Interrupt Sub-Routine =====================//
void __attribute__((__interrupt__)) _U2RXInterrupt(void)
{
_U1RXIF = 0; //Xoa co ngat
lcd_putstr(ReadUART2()); //xuat chuoi ra LCD
}
//Chuong trinh chinh
//------------------------------------------------------------------------------
void main()
{ ADPCFG = 0xFFBF; //Cac chan khac la digital, chan AN0/RB0 la analog
_TRISE3 = 0;
Uart_Setup();
Init_PORTS_LCD(); //Khoi tao cac cong I/O
Init_LCD(); //Khoi tao LCD
lcd_putstr("TOC DO =");
while (1)
{
_LATE3=1;
Delay_ms(250); //Vong lap chinh
_LATE3=0;
Delay_ms(250);
}
}
code gủi dữ liệu lên PC
Code:
#include "p30f4011.h"
_FOSC(CSW_FSCM_OFF & FRC_PLL4); //Clock = 32 MHz
_FWDT(WDT_OFF);
_FBORPOR(MCLR_EN & PBOR_OFF & PWMxL_ACT_HI & PWMxH_ACT_HI);
_FGS(CODE_PROT_OFF);
//Cac prototype cho cac chuong trinh con
void Init_TMR1(void);
void Init_UART2_Module(void);
//Bang hang so trong ROM
unsigned char Chuoi1[] = "Xin chao!";
//Cac bien toan cuc
unsigned char flag, idx;
//------------------------------------------------------------------------------
//Chuong trinh chinh
int main(void) {
Init_TMR1(); //Khoi tao Timer 1
Init_UART2_Module(); //Khoi tao module UART1
flag = 0; //Xoa co bao het thoi gian cho
while (1) {
if (flag) { //Cho den khi het thoi gian cho
idx = 0;
while (Chuoi1[idx]) { //Neu chua het chuoi thi
U2TXREG = Chuoi1[idx++]; //Xuat tiep 1 ky tu
while (!U2STAbits.TRMT); //Cho den khi truyen xong ky tu
};
flag = 0;
};
};
}
//Chuong trinh con khoi tao module UART1
void Init_UART2_Module(void) {
U2MODE = 0x8000; //Main I/O, 8-bit, no parity, 1 stop bit
U2STA = 0x0400; //bit10=UTXEN
U2BRG = (((7370000/9600)/16)-1); //38400 bps @ Fcy = 8 MHz
}
//Chuong trinh con khoi tao Timer 1, tran sau moi 1 giay o muc xung 8 Mips
void Init_TMR1(void) {
TMR1 = 0; //Xoa so dem trong TMR1
PR1 = 0x7A12; //Nguong tran la 1 giay tuong ung voi clock = 32 MHz
_T1IF = 0; //Xoa co ngat cua Timer 1
T1CON = 0x8030; //Dung fcy lam clock, prescale = 1:256
_T1IE = 1; //Cho phep ngat khi Timer 1 tran
}
//Chuong trinh xu ly ngat Timer 1
void _ISR _T1Interrupt(void) {
_T1IF = 0; //Xoa co ngat
flag = 1; //Bat co cho phep xuat chuoi ky tu
}
code viết bằng VB chung cho cả gửi và nhận
Code:
Private Sub Form_Load()
With MSComm1
.Settings = "9600,N,8,1"
.CommPort = 1
.RThreshold = 1
.SThreshold = 0
.InputMode = comInputModeText
.InputLen = 0
.Handshaking = comNone
.InBufferSize = 1024
.OutBufferSize = 1024
.PortOpen = True
End With
End Sub
Private Sub Form_Unload(Cancel As Integer)
' Dong cong
MSComm1.PortOpen = False
End Sub
Private Sub Command1_Click()
MSComm1.Output = Text1.Text
End Sub
Private Sub MSComm1_OnComm()
If (MSComm1.CommEvent = comEvReceive) Then
Text2.Text = Text2.Text + MSComm1.Input
End If
End Sub