void beep_init(void)
{
GPIOC.ALTFN0 &= ~(3<<28);
GPIOC.ALTFN0 |= (1<<28);
GPIOC.OUTENB |= (1<<14);
GPIOC.OUT &= ~(1<<14);
}
void beep_open(void)
{
GPIOC.OUT |= (1<<14);
}
void beep_change(void)
{
GPIOC.OUT ^= (1<<14); // 0 ^ 1 = 1, 1 ^ 1 = 0
}
void beep_close(void)
{
GPIOC.OUT &= ~(1<<14);
}
void DelayMs(unsigned int Time)
{
unsigned int i,j;
for(i=0;i<Time;i++)
for(j=0;j<2000;j++);
}
void uart_init1()
{
/*UART 引脚功能设置*/
GPIOD.ALTFN0 &= ~(3 << 28);
GPIOD.ALTFN0 |= (1 << 28);
GPIOD.ALTFN1 &= ~(3 <<4);
GPIOD.ALTFN1 |= (1 << 4);
/*UART 通信帧格式*/
//1停止位,无校验,8位长
UART0.ULCON &= (0 <<0);
UART0.ULCON |= (3 <<0);
/*UART 波特率设置*/
UART0.UBRDIV = 26;
UART0.UFRACVAL = 2;
/*UART 通信模式设置*/
UART0.UCON &= ~(0XF << 0); //轮训或中断模式
UART0.UCON |=(5 <<0);
//
//UART0.UCON |= (1 << 0);
UART0.UCON |= (7 << 7); //禁用接收延时中断
UART0.UFCON |= (1 << 0); //FIFO 开启
UART0.UFCON |= (3 << 1);
UART0.UFCON &= 0; //FIFO禁用
//屏蔽发送中断
UART0.UINTM |= ~0;
//UART0.UINTM &= ~(1 << 0);
//中断控制器设置(GIC设置)
GICD_ISENABLER.ISENABLER1 &= (0<< 0);
GICD_ISENABLER.ISENABLER1 |= (1 <<7);
GICD_ITARGETSR.ITARGETSR9 &= (0 << 0);
GICD_ITARGETSR.ITARGETSR9 |= (1 << 24);
GICD_CTRL |= (1 << 0);
GICC_CTRL |= (1 << 0);
UART0.UINTP |= ~( 0);
}
char c;
int main()
{
//串口欧总管初始化
uart_init1();
//蜂鸣器初始化
beep_init();
printf("---UINTM:%d---\n",UART0.UINTM);
UART0.UINTM &= ~(1 << 0);
c=0;
while(1)
{
if(c!=0)
{
while((UART0.UTRSTAT & 4) ==0);
UART0.UTXH= c;
//UART0.UCON |= (1 << 2);
//while((UART0.UTRSTAT & 4) ==0);
//UART0.UTXH = '\n';
c=0;
}
}
}
void do_irq()
{
unsigned int irq_number=0;
irq_number = 0x3ff & GICC_IAR;
printf("-----UINTS:%d--UINTP:%d----------\n",(unsigned int)UART0.UINTS,UART0.UINTP);//打印出来中断位
switch(irq_number)
{
case 39:
beep_open();
printf("in irq");
if(UART0.UINTP & 1)
{
//printf("in irq");
c = UART0.URXH;
printf("-----------%d------------\n",c);
}
break;
}
beep_close();
GICC_EOIR |= (irq_number);
UART0.UINTP |=1;
}
这个代码可实现板子从PC端接收数据,之后在将接收到的数据发送回PC端。
出现的问题如下:
1.一开始运行代码,会自动产生发送中断(板子自动向PC端发送空格),尽管已经设置了发送中断的屏蔽码。这种情况仅在代码刚开始运行时,出现一次,之后正常。
2.芯片手册上的UART FIFO的寄存器没有初始化为0,如果不手动禁用,在刚开始运行代码,会发送一串随机数值(仅在刚开始启动阶段)
上面俩个问题还没有解决