Code:
#include <p33FJ256GP710.h>
#include "uart.h"
//CASE4: Star-up using internal FRC, after that switching to Primary Oscillator with PLL
_FOSCSEL(FNOSC_FRC);// Select Internal FRC at POR
_FOSC(FCKSM_CSECMD & OSCIOFNC_OFF & POSCMD_XT);// Enable Clock Switching and Configure POSC in XT mode
_FWDT(FWDTEN_OFF);// Watchdog Timer Enabled/disabled by user software
_FGS(GCP_OFF); //Code protect off
//************************************************
void Oscillatior_configuration(void)
{
//Fcy=Fosc/2 (always), Fin/N1 = (0~8MHz), Fin/N1*M = (100~200MHz), Fin/N1*M/N2 = (12.5~80MHz)
//M(2->253) , N1(2->33), N2(2,4,8)
//If using PLL: Fosc= Fin*M/(N1*N2)
//For primary oscillator, xt mode with PLL. Fin = 8MHz, =>Fosc = 80MHz, Fcy = 40Mips
// Configure PLL prescaler, PLL postscaler, PLL divisor
PLLFBD = 38; /* M=30 */
CLKDIVbits.PLLPOST = 0; /* N1=2 */
CLKDIVbits.PLLPRE = 0; /* N2=2 */
// Initiate Clock Switch to Primary Oscillator with PLL (NOSC = 0b011)
__builtin_write_OSCCONH(0x03);
__builtin_write_OSCCONL(0x01);
// Wait for Clock switch to occur
while (OSCCONbits.COSC != 0b011); //COSC update new value from NOSC when switching successful
// Wait for PLL to lock
while(OSCCONbits.LOCK!=1) {};
}
int main()
{ int i;
//configure crystal
Oscillatior_configuration();
// UART1 initialization
OpenUART1(0xC000,0x0550,0x80); //status chua dung lam'
//Send string
putsUART1((unsigned int*)"\n\rThis is a simple UART1 test\n\r");
for(i=0;i<=9;i++)
{
while(BusyUART1()); //Wait
WriteUART1(0x30+i); //Send character
}
CloseUART1();
}
chưa test nhưng chắc ko lỗi gì, nếu phần cứng chuẩn thì chỉ còn phần mềm..có oscilloscope thì cắm thẳng vào chân max232 xem tín hiệu thế nào, nếu có tín hiệu ra..thì có thể cổng com của máy tính có vấn đề, nếu ko có tín hiệu ra thì configure UART sai hoặc pin của vdk tèo rồi.
nhớ include uart.h nhé...để nó còn biết các hàm putsUART1 và WriteUART1
khuyến mãi thêm hàm đểu , gửi số kiểu int lên terminal
Code:
putiUART1(unsigned int number)
{ char buf[5];
sprintf(buf,"%u",number);
putsUART1((unsigned int*)buf);
}
nhớ include cái string.h vào nhé...