小弟使用这个芯片时发现串口部分在全双工时会偶尔产生错误,下面的程序是不断发送 55H,如果此时 PIC的串口没有收到数据,则一切OK,屏幕上全是55H,如果我用串口调试助手向PIC不停发送任意字符,则发送的55H偶然会出错,奇怪啊
这个问题在 PIC24FJ128GA110 中却不存在
以下是代码:
//PIC24系列例程:串口通信--中断方式
//中断方式串口通信,波特率为9600,外部晶振=8MHz
//
#include <p24Fxxxx.h>
// 熔丝设置
_CONFIG1( JTAGEN_OFF & GCP_ON & GWRP_ON & BKBUG_OFF & COE_OFF & ICS_PGx1 & FWDTEN_OFF & WINDIS_OFF & FWPSA_PR128 & WDTPS_PS2048 )
_CONFIG2( IESO_OFF & SOSCSEL_SOSC & WUTSEL_FST & FNOSC_PRIPLL & FCKSM_CSDCMD & OSCIOFNC_OFF & IOL1WAY_OFF & I2C1SEL_PRI & POSCMOD_XT )
#define uchar unsigned char
#define uint unsigned int
uchar en[] = {"hello word!"};
uchar cn[] = {"这是单片机发送的信息
"};
void check(uchar c)
{
switch(c)
{
case '0': break;
case '1': uart_putchar('a');break;
case '2': uart_sendbuff(en);break;
case '3': uart_sendbuff(cn);break;
default: uart_sendbuff("请输入1/2/3以测试串口程序!
");
}
}
//串口1接收中断
void _ISR _U1RXInterrupt()
{
_U1RXIF = 0;
check(U1RXREG); //保存接收到的数据
}
//串口1接收发送中断
void _ISR _U1TXInterrupt()
{
_U1TXIF = 0;
}
//发送单字符
void uart_putchar(uchar c)
{
U1TXREG = c;
}
//串口字符串发送,参数: *p
void uart_sendbuff(uchar *p)
{
while(*p)
{
uart_putchar(*p);
p++;
while(U1STAbits.UTXBF); //等待发送结束
}
}
//串口初始化程序
void uart_set()
{
TRISCbits.TRISC7 = 0;
TRISCbits.TRISC6 = 1;
//U1BRG=( (( (16000000/2)/9600)/16)-1 ); //波特率9600
U1BRG = (16000000+8ul*9600)/16/9600-1;
U1MODE = 0x8000;
U1STA = 0x8400;
INTCON1 = 0x8000; //禁止中断嵌套
INTCON2 = 0;
IEC0 = 0x1800; //允许UART1收发中断
}
int main(void)
{
unsigned int i,j;
unsigned long k1,k2;
// 所有IO设为数字IO
AD1PCFG = 0xFFFF;
// 端口重定义
__builtin_write_OSCCONL(OSCCON & 0xbf);
// 定义 UART1 IO
RPINR18bits.U1RXR = 24; // RP24 = U1RX
RPOR12bits.RP25R = 3; // RP25 = U1TX
__builtin_write_OSCCONL(OSCCON | 0x40);
uart_set();//初始化串口
while(1) // 不断发送 55H
{
for (k1=0;k1<500000;k1++);
uart_putchar(0x55);
}
return 0;
}
一周热门 更多>