CortexM0菜鸟之串口轮询(第9天作业)

2010年09月07日 01:18    发布者:hotpower
#include "uart.h"
UartObj::UartObj (void)
{
    UartInit();
}
void UartObj::UartInit(void)
{
unsigned int Fdiv;
//位域写法
IOCON.TXD.Bits.FUNC = IOCON_TXD;
IOCON.TXD.Bits.MODE = IOCON_PULLUP;
//寄存器写法
// IOCON.TXD.Regs = (IOCON_TXD << IOCON_FUNC)
//                | (IOCON_PULLUP << IOCON_MODE);
//位域写法
IOCON.RXD.Bits.FUNC = IOCON_RXD;
IOCON.RXD.Bits.MODE = IOCON_PULLUP;
//寄存器写法
// IOCON.RXD.Regs = (IOCON_RXD << IOCON_FUNC)
//                | (IOCON_PULLUP << IOCON_MODE);

   SYSCON.SYSAHBCLKCTRL.Bits.UARTCLK = 1;//使能UART的时钟
   SYSCON.UARTCLKDIV.Regs = 1;//UART时钟分频器值
// U0.LCR.Regs = 0x83;
//位域写法
// U0.LCR.Bits.DLAB = 1;//除数锁存器访问使能
// U0.LCR.Bits.WordLengthSelect = 3;//8位串口数据位
//寄存器写法
U0.LCR.Regs = (1 << LCR_DLAB)//除数锁存器访问使能
             | (3 << LCR_WordLengthSelect);//8位串口数据位
//设置波特率
    Fdiv = (((SystemFrequency / SYSCON.SYSAHBCLKDIV.Regs )/ SYSCON.UARTCLKDIV.Regs) / 16) /UART_BPS ; /*baud rate */
U0.DLM.Regs = Fdiv / 256;
U0.DLL.Regs = Fdiv % 256;
// U0.LCR.Regs = 3;
U0.LCR.Bits.DLAB = 0;//除数锁存器访问禁止
//寄存器写法
// U0.LCR.Regs = (0 << LCR_DLAB)//除数锁存器访问禁止
//             | (3 << LCR_WordLengthSelect);//8位串口数据位
U0.FCR.Regs = 7;
}
void UartObj::Send(unsigned char ch)
{
while(!U0.LSR.Bits.THRE);
U0.THR.Regs = ch;
}
bool UartObj::Receive(unsigned char & ch)
{
bool Result;
Result = U0.LSR.Bits.RDR;
if (Result)
{
  ch = U0.RBR.Regs;
}
return Result;
}

HotPower@163.com  2010.9.6 23:48 于雁塔菜地

菜农通讯工具:

新浪网页版:http://t.sina.com.cn/hotpower
新浪手机版:http://t.sina.cn/hotpower

网 易微 博:http://t.163.com/hotpower
腾讯QQ:    1270688699

菜农邮箱:     HotPower@163.com
菜农导航: http://www.hotpage.net.cn/

http://www.hotpage.net.cn/hotpagelogo.gifhttp://www.eechina.com/images/logo.gifhttp://www.hotpage.net.cn/hotpowerlogo.gifhttp://www.eefocus.com/images/dictionary/dict/logo.gif

网友评论

宝丫头冰凝 2011年04月19日
请问在么?想问你一些M0的知识
kongchau 2011年04月29日
:)