#include "config.h"
typedef struct{
unsigned int baud; //波特率
unsigned char longth; //数据长度5\6\7\8
unsigned char stop; //停止位个数1或者2
unsigned char check; //奇偶校验位0--奇 1--偶 2--不校验
}UARTNODE;
unsigned char sum;
unsigned char rec_buf[20];
unsigned char rec_new;
int uart_init(UARTNODE set)
{
unsigned int bak;
if(set.baud<300 || set.baud >115200) return (-1);
if(set.longth <5 || set.longth>8) return (-1);
if(set.stop <1 || set.stop >2 ) return (-1);
if(set.check >3) return (-1);
U0LCR = 0X80; //DLAB = 1;ENABLE BAUD CHANGE
bak = (Fpclk/16)/(set.baud);
U0DLL = bak&0xff;
U0DLM = bak>>8;
bak = 0; //准备存放配置位
bak |= set.longth - 5; //设置数据长度
if(set.stop == 2) bak |= 1<<2; //设置停止位
if(set.check == 0 ) bak |= 1<<3; //设置校验位
else if(set.check == 1) bak = 3<<3;
else if(set.check == 2) bak =bak;
U0LCR= 0X00;
U0LCR = bak;
return 0;
}
int uart_transmit(unsigned char serial_data)
{
U0THR = serial_data;
while(!(U0LSR & 0X40));
return 0;
}
void _uart_rec_IRQ(void )
{
unsigned char i=0;
unsigned char iir;
uart_transmit('c');
rec_new = 1;
while(((iir=U0IIR) & 0x01)== 0)//0代表有中断没有处理
{
switch(iir&0x0e)
{
//触发了8个字节
case 0x04:for(i=0;i<8;i++){
rec_buf[sum++]= U0RBR;
}
break;
//超时中断
case 0x0c:
rec_buf[++sum] = U0RBR;
break;
default:break;
}
}
VICVectAddr=0; //中断清零
}
int main(void )
{
unsigned char i,j;
UARTNODE temp;
temp.baud = 9600;
temp.longth = 8;
temp.check = 0;
temp.stop = 1;
PINSEL0 = (PINSEL0 & 0XFFFFFFF0)|0X05; //使能uart0
uart_init(temp);
U0IER = 0x01; //使能接受中断
U0FCR = 0x81; //使能fifo设置触发点为8
//中断设置
IRQDisable();
VICIntSelect = 0x00000000; //设置中断位irq中断
VICVectCntl5 = 0x20|0x06; //使能并且使用通道5,
//0x06是向量号。勿写成1<<6注意了!!!!!
VICVectAddr5 = (unsigned int)_uart_rec_IRQ;
VICIntEnable |=1<<6; //使能中断,别忘了
IRQEnable();
uart_transmit('a');
uart_transmit('b');
while(1)
{
if(rec_new==1&&sum)
{
rec_new=0;
j=sum;
sum=0;
for(i=0;i<j;i++)
uart_transmit(rec_buf);
}
}
return 0;
}
那位帮看看代码。easyarm2131 zlg的板子。接受中断只进入了一次。
#include "config.h"
typedef struct{
unsigned int baud; //波特率
unsigned char longth; //数据长度5\6\7\8
unsigned char stop; //停止位个数1或者2
unsigned char check; //奇偶校验位0--奇 1--偶 2--不校验
}UARTNODE;
unsigned char sum;
unsigned char rec_buf[20];
unsigned char rec_new;
int uart_init(UARTNODE set)
{
unsigned int bak;
if(set.baud<300 || set.baud >115200) return (-1);
if(set.longth <5 || set.longth>8) return (-1);
if(set.stop <1 || set.stop >2 ) return (-1);
if(set.check >3) return (-1);
U0LCR = 0X80; //DLAB = 1;ENABLE BAUD CHANGE
bak = (Fpclk/16)/(set.baud);
U0DLL = bak&0xff;
U0DLM = bak>>8;
bak = 0; //准备存放配置位
bak |= set.longth - 5; //设置数据长度
if(set.stop == 2) bak |= 1<<2; //设置停止位
if(set.check == 0 ) bak |= 1<<3; //设置校验位
else if(set.check == 1) bak = 3<<3;
else if(set.check == 2) bak =bak;
U0LCR= 0X00;
U0LCR = bak;
return 0;
}
int uart_transmit(unsigned char serial_data)
{
U0THR = serial_data;
while(!(U0LSR & 0X40));
return 0;
}
void _uart_rec_IRQ(void )
{
unsigned char i=0;
unsigned char iir;
uart_transmit('c');
rec_new = 1;
while(((iir=U0IIR) & 0x01)== 0)//0代表有中断没有处理
{
switch(iir&0x0e)
{
//触发了8个字节
case 0x04:for(i=0;i<8;i++){
rec_buf[sum++]= U0RBR;
}
break;
//超时中断
case 0x0c:
rec_buf[++sum] = U0RBR;
break;
default:break;
}
}
VICVectAddr=0; //中断清零
}
int main(void )
{
unsigned char i,j;
UARTNODE temp;
temp.baud = 9600;
temp.longth = 8;
temp.check = 0;
temp.stop = 1;
PINSEL0 = (PINSEL0 & 0XFFFFFFF0)|0X05; //使能uart0
uart_init(temp);
U0IER = 0x01; //使能接受中断
U0FCR = 0x81; //使能fifo设置触发点为8
//中断设置
IRQDisable();
VICIntSelect = 0x00000000; //设置中断位irq中断
VICVectCntl5 = 0x20|0x06; //使能并且使用通道5,
//0x06是向量号。勿写成1<<6注意了!!!!!
VICVectAddr5 = (unsigned int)_uart_rec_IRQ;
VICIntEnable |=1<<6; //使能中断,别忘了
IRQEnable();
uart_transmit('a');
uart_transmit('b');
while(1)
{
if(rec_new==1&&sum)
{
rec_new=0;
j=sum;
sum=0;
for(i=0;i<j;i++)
uart_transmit(rec_buf[i]);
}
}
return 0;
}
|