1. Serial port programming general block diagram
Mini2440 serial port communication (UART0)
二、分步介绍
#define GPHCON (*(volatile unsigned long *)0x56000070)
#define ULCON0 (*(volatile unsigned long *)0x50000000)
#define UCON0 (*(volatile unsigned long *)0x50000004)
#define UBRDIV0 (*(volatile unsigned long *)0x50000028)
#define UTRSTAT0 (*(volatile unsigned long *)0x50000010)
#define UTXH0 (*(volatile unsigned long *)0x50000020)
#define URXH0 (*(volatile unsigned long *)0x50000024)
#define PCLK 50000000
#define BAUDRATE 115200
1. Initialization
①、Configure pin function
GPHCON &= (~(0b11<<4))|(~(0b11<<6));//Port clear
GPHCON |= (0b10<<4)|(0b10<<6);//Set function [attach] 455487 [/attach]
②、Set data format
ULCON0 = (0b11<<0) | (0b0<<2) | (0b000<<3);
Set to No parity; One stop bit per frame; 8-bits
③. Set working mode
UCON0 = (0b01<<0) | (0b01<<2);
Both sending and receiving are set to Interrupt request or polling mode
④. Set the baud rate
UBRDIV0 = (int)(PCLK/(BAUDRATE*16)) - 1;
In this experiment, UART0 uses PCLK
2. Send data
void putc(unsigned char ch)
{
while( !(UTRSTAT0 & (1<<2)));
UTXH0 = ch;
}
Only the lower eight bits are used
3. Receive data
unsigned char getc()
{
while( !(UTRSTAT0 & (1<<0)));
return URXH0;
}
Only the lower eight bits are used
|