//-------------------------------------------------------------------------*
// 文件名: uart.c *
// 说 明: uart构件源文件 *
//-------------------------------------------------------------------------*
#include "uart.h"
//-------------------------------------------------------------------------*
//函数名: uart_init *
//功 能: 初始化uartx模块。 *
//参 数: uartch:串口号 *
// sysclk:系统总线时钟,以MHz为单位 *
// baud:波特率,如9600,38400等,一般来说,速度越慢,通信越稳 *
//返 回: 无 *
//说 明: *
//-------------------------------------------------------------------------*
void uart_init (UART_MemMapPtr uartch, uint32 sysclk, uint32 baud)
{
register uint16 sbr, brfa;
uint8 temp;
//使能引脚
if (uartch == UART0_BASE_PTR)
{
//在PTD6上使能UART0_TXD功能
PORTD_PCR6 = PORT_PCR_MUX(0x3);
//在PTD7上使能UART0_RXD
PORTD_PCR7 = PORT_PCR_MUX(0x3);
}else if (uartch == UART1_BASE_PTR)
{
//在PTC4上使能UART1_TXD功能
PORTC_PCR4 = PORT_PCR_MUX(0x3);
//在PTC3上使能UART1_RXD
PORTC_PCR3 = PORT_PCR_MUX(0x3);
}else if (uartch == UART2_BASE_PTR)
{
//在PTD3上使能UART2_TXD功能
PORTD_PCR3 = PORT_PCR_MUX(0x3);
//在PTD2上使能UART2_RXD
PORTD_PCR2 = PORT_PCR_MUX(0x3);
}else if (uartch == UART3_BASE_PTR)
{
//在PTC17上使能UART3_TXD功能
PORTC_PCR17 = PORT_PCR_MUX(0x3);
//在PTC16上使能UART3_RXD
PORTC_PCR16 = PORT_PCR_MUX(0x3);
}else if (uartch == UART4_BASE_PTR)
{
//在PTE24上使能UART4_TXD功能
PORTE_PCR24 = PORT_PCR_MUX(0x3);
//在PTE25上使能UART4_RXD
PORTE_PCR25 = PORT_PCR_MUX(0x3);
}else if (uartch == UART5_BASE_PTR)
{
//在PTE8上使能UART5_TXD功能
PORTE_PCR8 = PORT_PCR_MUX(0x3);
//在PTE9上使能UART5_RXD
PORTE_PCR9 = PORT_PCR_MUX(0x3);
}
//使能串口时钟
if(uartch == UART0_BASE_PTR)
SIM_SCGC4 |= SIM_SCGC4_UART0_MASK;
else
if (uartch == UART1_BASE_PTR)
SIM_SCGC4 |= SIM_SCGC4_UART1_MASK;
else
if (uartch == UART2_BASE_PTR)
SIM_SCGC4 |= SIM_SCGC4_UART2_MASK;
else
if(uartch == UART3_BASE_PTR)
SIM_SCGC4 |= SIM_SCGC4_UART3_MASK;
else
if(uartch == UART4_BASE_PTR)
SIM_SCGC1 |= SIM_SCGC1_UART4_MASK;
else
SIM_SCGC1 |= SIM_SCGC1_UART5_MASK;
//禁止发送接受
UART_C2_REG(uartch) &= ~(UART_C2_TE_MASK
| UART_C2_RE_MASK );
//配置成8位无校验模式
UART_C1_REG(uartch) = 0;
//计算波特率,串口0、1使用内核时钟,其它串口使用外设时钟,系统时钟为
//外设时钟的2倍
if ((uartch == UART0_BASE_PTR) | (uartch == UART1_BASE_PTR))//
sysclk+=sysclk;
sbr = (uint16)((sysclk*1000)/(baud * 16));
temp = UART_BDH_REG(uartch) & ~(UART_BDH_SBR(0x1F));
UART_BDH_REG(uartch) = temp | UART_BDH_SBR(((sbr & 0x1F00) >> 8));
UART_BDL_REG(uartch) = (uint8)(sbr & UART_BDL_SBR_MASK);
brfa = (((sysclk*32000)/(baud * 16)) - (sbr * 32));
temp = UART_C4_REG(uartch) & ~(UART_C4_BRFA(0x1F));
UART_C4_REG(uartch) = temp | UART_C4_BRFA(brfa);
//使能发送接受
UART_C2_REG(uartch) |= (UART_C2_TE_MASK
| UART_C2_RE_MASK );
}
//-------------------------------------------------------------------------*
//函数名: uart_re1 *
//功 能: 串行接受1个字节 *
//参 数: uartch: 串口号 *
// ch: 接收到的字节 *
//返 回: 成功:1;失败:0 *
//说 明: *
//-------------------------------------------------------------------------*
uint8 uart_re1 (UART_MemMapPtr uartch,uint8 *ch)
{
uint32 k;
for (k = 0; k < 0xfbbb; k++)//有时间限制
if((UART_S1_REG(uartch) & UART_S1_RDRF_MASK)!= 0)//判断接收缓冲区是否满
{
*ch = UART_D_REG(uartch);
return 1; //接受成功
}
if(k>=0xfbbb)
{
return 0; //接受失败
}
return 0;
}
//-------------------------------------------------------------------------*
//函数名: uart_send1 *
//功 能: 串行发送1个字节 *
//参 数: uartch: 串口号 *
// ch: 要发送的字节 *
//返 回: 无 *
//说 明: *
//-------------------------------------------------------------------------*
void uart_send1 (UART_MemMapPtr uartch, uint8 ch)
{
//等待发送缓冲区空
while(!(UART_S1_REG(uartch) & UART_S1_TDRE_MASK));
//发送数据
UART_D_REG(uartch) = (uint8)ch;
}
//-------------------------------------------------------------------------*
//函数名: uart_reN *
//功 能: 串行 接收n个字节 *
//参 数: uartch: 串口号 *
// buff: 接收缓冲区 *
// len:接收长度 *
//返 回: 1:成功;0:失败 *
//说 明: *
//-------------------------------------------------------------------------*
uint8 uart_reN (UART_MemMapPtr uartch ,uint8* buff,uint16 len)
{
uint16 m=0;
while (m < len)
{
if(0==uart_re1(uartch,&buff[m]))
return 0; //接收失败
else m++;
}
return 1; //接收成功
}
//-------------------------------------------------------------------------*
//函数名: uart_sendN *
//功 能: 串行 接收n个字节 *
//参 数: uartch: 串口号 *
// buff: 发送缓冲区 *
// len:发送长度 *
//返 回: 无 *
//说 明: *
//-------------------------------------------------------------------------*
void uart_sendN (UART_MemMapPtr uartch ,uint8* buff,uint16 len)
{
int i;
for(i=0;i<len;i++)
{
uart_send1(uartch,buff[i]);
}
}
//-------------------------------------------------------------------------*
//函数名: enableuartreint *
//功 能: 开串口接收中断 *
//参 数: uartch: 串口号 *
// irqno: 对应irq号 *
//返 回: 无 *
//说 明: *
//-------------------------------------------------------------------------*
void enableuartreint(UART_MemMapPtr uartch,uint8 irqno)
{
UART_C2_REG(uartch)|=UART_C2_RIE_MASK; //开放UART接收中断
enable_irq(irqno); //开接收引脚的IRQ中断
}
//-------------------------------------------------------------------------*
//函数名: disableuartreint
没有合适的资源?快使用搜索试试~ 我知道了~
7_segment.zip_k60
共62个文件
o:11个
s:11个
lst:10个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 151 浏览量
2022-07-15
16:52:56
上传
评论
收藏 224KB ZIP 举报
温馨提示
基于kinetis K60的七段显示数码管源代码
资源推荐
资源详情
资源评论
收起资源包目录
7_segment.zip (62个子文件)
7_segment
7_segment.ewd 45KB
C
System
vectors.c 5KB
common.c 5KB
sysinit.c 8KB
main.c 1KB
User
gpio.c 4KB
uart.c 9KB
wdog.c 2KB
7_segment.dep 16KB
H
System
vectors.h 19KB
common.h 5KB
sysinit.h 4KB
MK60N512VMD100.h 793KB
User
uart.h 6KB
gpio.h 3KB
wdog.h 2KB
7_segment.eww 163B
Ram
List
Exe
Obj
7_segment.pbd 333B
settings
7_segment.cspy.bat 1KB
7_segment.dbgdt 5KB
7_segment.dni 882B
7_segment.wsdt 3KB
Flash
List
uart.lst 35KB
wdog.lst 6KB
isr.lst 2KB
common.lst 21KB
main.lst 16KB
gpio.s 10KB
uart.s 23KB
vectors.s 13KB
wdog.s 5KB
start.s 11KB
gpio.lst 14KB
main.s 9KB
sysinit.s 21KB
start.lst 14KB
light.lst 7KB
light.s 6KB
isr.s 3KB
vectors.lst 22KB
common.s 14KB
sysinit.lst 31KB
Exe
7_segment.out 45KB
7_segment.srec 6KB
7_segment.sim 2KB
Obj
light.o 7KB
wdog.o 5KB
uart.o 17KB
gpio.o 9KB
7_segment.pbd 388B
common.o 12KB
vectors.o 8KB
crt0.o 2KB
sysinit.o 14KB
main.o 7KB
start.o 8KB
isr.o 2KB
Setting
Start
crt0.s 669B
start.c 4KB
Link
Pflash.icf 2KB
Ram.icf 2KB
7_segment.ewp 47KB
共 62 条
- 1
资源评论
四散
- 粉丝: 54
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功