View Single Post
Old 27-06-2007, 08:16 AM   #2
ngohaibac
Đệ tử 9 túi
 
ngohaibac's Avatar
 
Tham gia ngày: Oct 2005
Nơi Cư Ngụ: BKHN
Bài gửi: 231
:
Send a message via Yahoo to ngohaibac
UART module and Auto Baud Rate Detect

Chào các bạn.

Đây là thư viện UART có chức năng tự detect Baud Rate. Khi muốn sử dụng chức năng này thì các bạn gọi hàm void UART_detectBaudRate(void);, khi đó trên PC phải truyền kí tự U để PIC đồng bộ tốc độ. Sau đó thì truyền nhận sẽ theo tốc độ đặt trên PC.

Đây là hàm UART.h
Code:
// UART.h
//=======================================================================================
#ifndef _UART_H
#define _UART_H
//========================================================================================
#include "pic18.h"
// Define for UART
#define TX			RC6
#define RX 			RC7
#define TRIS_TX	TRISC6
#define TRIS_RX	TRISC7

//========================================================================================
// Declare sosme functions
void UART_Init(unsigned int BaudRate);
void UART_PrChar(unsigned char a);
void UART_PrString(const char* str);
unsigned char UART_Read(void);
void UART_detectBaudRate(void);
#endif
UART.c
Code:
// UART.h
# include "UART.h"
# include "pic18.h"
# include "sysdef.h"

//========================================================================================
// Initialize UART
void UART_Init(unsigned int BaudRate){
	unsigned int n;

// Configure BaudRate
	BRGH = 0; 				// Low speed.
	BRG16 = 1;				// 16-Bit Baud Rate Generator - SPBRGH and SPBRG

	// Baudrate = Fosc/[16(n+1)]  => n = ((Fosc/Baudrate)>>4) - 1;  n = SPBRGH: SPBRG;
	n = ((Fosc/BaudRate)>>4) - 1;
	SPBRG = n;
	
	SPBRGH = n>>8;
	
// Enable the asyncchronous serial port.
	SYNC = 0 ;				// Asynchronous mode	
	SPEN = 1;				// Serial port enable.
	TRIS_TX = 0;
	TRIS_RX = 1;

//Configure for Transmitter mode.	
	TXEN  = 1; 				// Transmit enable 

//Configure for Receiver mode
	CREN = 1;				// Enable the reception 

	RCIF = 0;
	RCIE = 1;				// Reception Interrupt Enable
	GIE = 1;					// Global Interrupt Enable
	PEIE = 1;				// Perapheral Interrupt Enable
}	

//=====================================================================================
void UART_PrChar(unsigned char a){
	while(!TRMT);
	TXREG = a;
}	
//=====================================================================================
void UART_PrString(const char* str){
	while(*str)
		UART_PrChar(*str ++);
}	
//=====================================================================================
unsigned char UART_Read(void){
	return (RCREG);
}	
//====================================================================================
void UART_detectBaudRate(void){
	TXSTA = 0b00100100;
	RCSTA = 0b10010000;
	BAUDCON = 0b00001001;
	
	while(ABDEN);
}
Chương trình test:
Code:
/*;========================================================
/*; Ten chuong trinh	: Test IO
; Nguoi thuc hien	: Ngo Hai Bac (NOHB)
; Ngay thuc hien	: 27/06/07
; Phien ban			: 1.0		
; Mo ta phan cung	: Dung PIC18F2525/2620/4525/4620 - thach anh 20MHz
; Trinh dich			: HTPIC 18V9.50				
;				
;----------------------------------------------------------------
; Ngay hoan thanh	: 
; Ngay kiem tra	: 
; Nguoi kiem tra	: 
;----------------------------------------------------------------
; Chu thich	: 		
;========================================================*/
// Include
# include "pic18.h"
# include "sysdef.h"
# include "UART.h"

// configuration
__CONFIG(1,HS & FCMDIS & IESODIS);
__CONFIG(2,BORV21 & PWRTDIS & WDTDIS);
__CONFIG(3,MCLREN & LPT1DIS & PBANDIS);
__CONFIG(4,DEBUGDIS & LVPDIS & STVRDIS & XINSTDIS);
__CONFIG(5,UNPROTECT);
__CONFIG(6,UNPROTECT);
__CONFIG(7,UNPROTECT);

// Receiver Interrupt Function
void RxIntFcn(void);
// Interrupt Function
void interrupt MyInt  (void) {
	if (RCIE && RCIF){
		RCIF = 0;
		RxIntFcn();	
	};
}
/*=====================================================================================
  Main function
=====================================================================================*/
void main(){
	unsigned char i;
	
	ADCON1 = 0x07;
	TRISA = 0x00;
	PORTA = 0xFF;
	
	TRISB = 0x00;
	PORTB = 0xFF;
	
	UART_Init(9600);
	UART_detectBaudRate();
	while(1);
}
// Implementation of some functions
void RxIntFcn(void){
	unsigned char temp;
	temp = UART_Read();
	UART_PrChar(temp);
}
Chúc các bạn thành công.
File Kèm Theo
File Type: rar UART.rar (857 Bytes, 373 lần tải)
ngohaibac vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn