在线咨询
eetop公众号 创芯大讲堂 创芯人才网
切换到宽版

EETOP 创芯网论坛 (原名:电子顶级开发网)

手机号码,快捷登录

手机号码,快捷登录

找回密码

  登录   注册  

快捷导航
搜帖子
芯片精品文章合集(500篇!)    创芯人才网--重磅上线啦!
查看: 2922|回复: 1

再请教44b0串口问题

[复制链接]
发表于 2007-12-4 09:56:17 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?注册

x
上一个问题已经解决,原来是波特率设置的有问题。

新的问题又来了。设定串口发送的字符为‘C’,但电脑上串口接到的数据为07 FF。

望大侠赐教。

源程序如下:

#include "44b.h"

void PortD_init(void);
void Timer0_init(void);
void Uart_init(int bps0,int bps1);
void Uart_send_char(volatile int port_num,volatile char data);
void Uart_send_int(volatile int port_num,volatile int data);
void delay(int t);
void Init_intrupt(void);
void Main(void){
int i;
char cdata = 'C';
PortD_init();
//Timer0_init();
Init_intrupt();
Uart_init(9600,9600);

for(i = 0;i < 10;i ++){
  delay(10);
  //Uart_send_int(0,i);
  Uart_send_char(0,cdata);
}
while(1);


}

void delay(int t){
int i;
while(t--)
  for(i = 0;i < 5000;i ++)
  ;
}

void PortD_init(void){
rPCOND = 0x5555;
rPUPD = 0x1;
rPDATD = 0xffff;
}


void Timer0_init(void){
void  Timer0_ISR(void);
rINTCON = 0x5; //Non-vectored,IRQ enable,FIQ disable
rINTMOD = 0x0; //ALL = IRQ mode
rINTMSK = ~(BIT_TIMER0 | BIT_GLOBAL);

pISR_TIMER0 = (unsigned)Timer0_ISR;

rTCFG0 = 0x000000ff;//timer0 prescale value = 255// MCLK = 42M
rTCFG1 = 0x00000004;

rTCNTB0 = 4000;
rTCON = 0x0000000a;
rTCON = 0x00000009;
  
}

void __irq Timer0_ISR(void){
static int v = 0x0000;
rI_ISPC = BIT_TIMER0;
v = ~v;
rPDATD = v;

}

void Init_intrupt(void){
rINTCON = 0x5; //Non-vectored,IRQ enable,FIQ disable
rINTMOD = 0x0; //ALL = IRQ mode
rINTMSK = ~(BIT_UTXD0 | BIT_UTXD1 | BIT_GLOBAL);
}

//【串口初始化】
void Uart_init(int bps0,int bps1){
//【bps0-------串口0波特率】
//【bps1-------串口1波特率】
/*
//UART波特率除数寄存器UBRDIVn值的计算公式:UBRDIVn = (int)(MCLK/(bps*16))-1
//此处MCLK=42MHz,bps=9600Hz,因此UBRDIVn = (int)(42000000 / (9600 * 16)) - 1 = 272(.4375)
rUBRDIV0 = 272;
//ULCONn线控制寄存器【规定传输帧的格式】
//[7]0,reserved
//[6]0,是否采用红外通信模式,1=infra-Red Tx/Rx mode;0=Normal mode operation
//[5:3]000, 0xx=No parity;
//   100=Odd parity;
//   101=Even parity
//   110=Parity forced/checked as 1
//   111=Parity forced/checked as 0
//[2]0,停止位的数量,0=One stop bit per frame;1=Two stop bits per frame
//[1:0]00,数据位长度;00 = 5 bits; 01 = 6 bits; 10 = 7 bits; 11 = 8 bits
rULCON0 = 0x3;
//UART控制寄存器UCONn
//UCON0 =  1 = Level在发送缓冲区为空时引发中断
//   0 = Pulse接收缓冲区接收到数据时立即引发中断
//   0 = 在UART的fifo时能情况下,禁止接收超时中断
//   0 = UART在接收操作中不产生错误状态中断
//   0 = UART不自动进入回送模式
//   0 = 不发送中止状态信号
//   01 = 向UART发送保持寄存器中写入数据模式:Interrupt request or polling mode
//   01 = 从UART接收缓冲区中读出数据的模式:Interrupt request or polling mode
rUCON0 = 0x205;
*/
rULCON0 = 0x03;             //UART0 is 8bit,1 stop bit ,no parity,Normal mode operation
rULCON1 = 0x03;        //UART1 is 8bit,1 stop bit ,no parity,Normal mode operation

rUCON0 = 0x205;        //Interrupt UART0 is requested while Tx buffer is empty
rUCON1 = 0x205;        //Interrupt UART1 is requested while Tx buffer is empty

rUFCON0 = 0x00;        //FIFO is disable
rUFCON1 = 0x00;        //FIFO is disable
  
rUMCON0 = 0x00;        //AFC  is disable
rUMCON1 = 0x00;        //AFC  is disable

rUBRDIV0 = (int)(MCLK / 16 / bps1 + 0.5) - 1;// (int)(42000000 / (9600 * 16)) - 1; //UART0 baud rate is 9600
rUBRDIV1 = (int)(MCLK / 16 / bps1 + 0.5) - 1; //UART1 baud rate is 9600

}

//【串口发送】
void Uart_send_char(volatile int port_num,volatile char data){
//【port_num】 :端口号(0 或 1)
//【data】  :字符型
/*int flag = 1;
int rTR,rUM;
while(flag){
rTR = rUTRSTAT0 & 0x2;
rUM = rUMSTAT0 & 0x1;
if((rTR == 0x2) && (rUM == 0x0)){
   if (port_num == 0)
    rUTXH0 = data;
   else
    rUTXH1 = data;
//ready = rUTRSTAT0 & 0x2;
//if(ready == 0x0)
  flag = 0;
}
}
*/
if (port_num == 0)
  rUTXH0 = data;
else
  rUTXH1 = data;
}


void Uart_send_int(volatile int port_num,volatile int data){
//【port_num】 :端口号(0 或 1)
//【data】  :整数
if (port_num == 0)
  rUTXH0 = data;
else
  rUTXH1 = data;
}
 楼主| 发表于 2007-12-4 10:04:54 | 显示全部楼层
我曾经试过用串口发送整数,也有问题
想要发送的时从0到9这10个数
但是接收到的是00 03 FC 04 FC 07 FC 18 FC 1B FC 1C FC 1F FC 20 FC 23 FC
您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

站长推荐 上一条 /2 下一条

小黑屋| 关于我们| 联系我们| 在线咨询| 隐私声明| EETOP 创芯网
( 京ICP备:10050787号 京公网安备:11010502037710 )

GMT+8, 2024-5-2 12:36 , Processed in 0.023491 second(s), 8 queries , Gzip On, Redis On.

eetop公众号 创芯大讲堂 创芯人才网
快速回复 返回顶部 返回列表