會員登陸
網站導航
最新下載
- Microchip 程式... (Tue, 17-Apr-2018)
- Microchip 程式... (Mon, 09-Apr-2018)
- Microchip2018微... (Fri, 19-Jan-2018)
- Microchip 2018 ... (Tue, 19-Dec-2017)
- 2017 精英年會特... (Wed, 11-Oct-2017)
- 2017 秋 研討會... (Wed, 11-Oct-2017)
- 2017 暑期微控制... (Fri, 07-Jul-2017)
- ATM6 講義與實作... (Tue, 27-Jun-2017)
- ATM5 講義與實作... (Tue, 27-Jun-2017)
- ATM4 講義與實作... (Tue, 27-Jun-2017)
訪問統計 (自2012/5/3)
|
正在流覽:
1 名訪客
|
pic16F1828 UR問題 |
|
初級會員 
註冊日期: 2015/07/18 15:05
所屬群組:
註冊會員
文章: 22
等級: 3; EXP: 41 HP : 0 / 60 MP : 7 / 488
|
各位先進 : 從網路參考了範例程式 ,使用在芯片PIC16F1828 , 但程式在終端機顯示上永遠是亂碼 ,且也無法接收 ,不知那裡有問題 ???
程式如下 :
#include <pic.h>
void interrupt ISR(void); void InitUART(void); void SendByteSerially(unsigned char); unsigned char ReceiveByteSerially(void); void SendStringSerially(const unsigned char*);
__CONFIG ( FOSC_HS & WDTE_OFF & PWRTE_OFF & MCLRE_ON & CP_OFF & CPD_OFF & BOREN_OFF & CLKOUTEN_OFF & IESO_OFF & FCMEN_OFF );
__CONFIG (WRT_OFF & PLLEN_OFF & STVREN_OFF & BORV_LO & LVP_OFF);
// ================================================================= void interrupt ISR(void) { if(RCIF) // If UART Rx Interrupt { if(OERR) // If over run error, then reset the receiver { CREN = 0; CREN = 1; }
SendByteSerially(RCREG); // Echo back received char } }
void InitUART(void) { TRISC6 = 1; // TX Pin TRISC7 = 1; // RX Pin
SPBRG = 25; BRGH = 1; // Fast baudrate SYNC = 0; // Asynchronous SPEN = 1; // Enable serial port pins CREN = 1; // Enable reception SREN = 0; // No effect TXIE = 0; // Disable tx interrupts RCIE = 1; // Enable rx interrupts TX9 = 0; // 8-bit transmission RX9 = 0; // 8-bit reception TXEN = 0; // Reset transmitter TXEN = 1; // Enable the transmitter }
void SendByteSerially(unsigned char Byte) // Writes a character to the serial port { while(!TXIF); // wait for previous transmission to finish TXREG = Byte; }
unsigned char ReceiveByteSerially(void) // Reads a character from the serial port { if(OERR) // If over run error, then reset the receiver { CREN = 0; CREN = 1; }
while(!RCIF); // Wait for transmission to receive
return RCREG; }
void SendStringSerially(const unsigned char* st) { while(*st) SendByteSerially(*st++); }
void main(void) { InitUART(); // Intialize UART
SendStringSerially("Hello World!"); // Send string on UART
GIE = 1; // Enable global interrupts PEIE = 1; // Enable Peripheral Interrupts
while(1) { SendByteSerially(ReceiveByteSerially()); } }
2017/02/02 17:37
|
|
|
Re: pic16F1828 UR問題 |
|
中級會員 
註冊日期: 2014/06/15 23:13
所屬群組:
註冊會員
文章: 60
等級: 6; EXP: 62 HP : 0 / 140 MP : 20 / 1583
|
亂碼的話... 一般baud rate錯也會, 另建議使用MCC產生UART程式, 快又方便啊~
2017/02/03 08:47
|
|
|
Re: pic16F1828 UR問題 |
|
版主 
註冊日期: 2004/04/30 10:53
來自 CAE, Microchip
所屬群組:
站務管理者 註冊會員 MICROCHIP
文章: 14266
等級: 72; EXP: 69 HP : 1075 / 1792 MP : 4755 / 73215
|
你用 FOSC_HS 外部 Crytal 及 PLL 關閉的設定,請問一下這 Crystal 的頻率是多少?
按你程式中的設定,BRG16 = 0, BRGH = 1, 及 SPBRG = 25 這是 9600 bps 在 4MHz 下的設定。
亂碼幾乎都是 Baud Rate 設錯了,請參考一下 Dzta Sheet : 26.0 ENHANCED UNIVERSAL SYNCHRONOUS ASYNCHRONOUS RECEIVER TRANSMITTER (EUSART) 章節裡的 Baus Rate 計算公式。
附加檔案:
擷取.JPG (113.81 KB)
2017/02/03 13:45
Ryang 於 2017年02月03日 14:13:31
|
|
|
Re: pic16F1828 UR問題 |
|
初級會員 
註冊日期: 2015/07/18 15:05
所屬群組:
註冊會員
文章: 22
等級: 3; EXP: 41 HP : 0 / 60 MP : 7 / 488
|
感謝二位前輩 , 的確是我鮑率設錯了 .
2017/02/03 23:24
|
|
您無法在此發表文章
您可以在此觀看文章
您無法回覆文章
您無法編輯自己的文章
您無法刪除自己的文章
您無法發起投票調查
您無法在此投票
您無法上傳附加檔案
您無法不經審核直接發表文章
|
搜尋
Microchip連結
網頁捷徑
2018 Winter Elite 報名
教育訓練
其它網站連結
|