串口程序

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
1 /* 硬件环境:DE2开发板
2 * 本程序主要功能:
3 * 1.每隔1秒钟用串口向外发送一个字符串
4 * 2.当用串口调试助手向其发送0-9这9个数字时,八个数码管都依次显示0-9
5 * */
6   /* 配置的系统的波特率是115200 */
7 #include " altera_avalon_uart_regs.h "
8 #include " system.h "
9 #include " altera_avalon_pio_regs.h "
10 #include " alt_types.h "
11 #include " sys/alt_irq.h "
12 #include < stdio.h >
13 #include < string .h >
14 #define UART_BASE 0x00681000
15 #define UART_IRQ 2
16 #define TIME_DELAY 1000000 // 1M,即一秒
17 // UART发送一个字节子程序
18 void Uart_send(unsigned char data)
19 {
20 alt_u16 status;
21 status = IORD_ALTERA_AVALON_UART_STATUS(UART_BASE);
22 while ( ! (status & 0x0040 )) // 等待发送完成
23 status = IORD_ALTERA_AVALON_UART_STATUS(UART_BASE);
24 IOWR_ALTERA_AVALON_UART_TXDATA(UART_BASE,data);
25 }
26 // UART发送多个字节子程序
27 void Uart_send_n(unsigned char * ptr)
28 {
29 while ( * ptr)
30 {
31 Uart_send( * ptr);
32 ptr ++ ;
33 }
34 Uart_send( 0x0a ); // 显示完一个字符串就回车换行
35 }
36
37 // UART接收子程序
38 int Uart_receive( void )
39 {
40 alt_u16 status;
41 int temp;
42 status = IORD_ALTERA_AVALON_UART_STATUS(UART_BASE);
43 while ( ! (status & 0x0080 )) // 等待发送完成
44 status = IORD_ALTERA_AVALON_UART_STATUS(UART_BASE);
45 temp = IORD_ALTERA_AVALON_UART_RXDATA(UART_BASE);
46 return temp;
47 }
48 // 串口接收中断服务程序
49 void Uart_ISR( void * context,alt_u32 id)
50 {
51 unsigned char temp;
52 temp = IORD_ALTERA_AVALON_UART_RXDATA(UART_BASE);
53 switch (temp)
54 {
55 case ' 0 ' :
56 IOWR(SEG7_DISPLAY_BASE, 0 , 0x00000000 ); break ;
57 case ' 1 ' :
58 IOWR(SEG7_DISPLAY_BASE, 0 , 0x11111111 ); break ;
59 case ' 2 ' :
60 IOWR(SEG7_DISPLAY_BASE, 0 , 0x22222222 ); break ;
61 case ' 3 ' :
62 IOWR(SEG7_DISPLAY_BASE, 0 , 0x33333333 ); break ;
63 case ' 4 ' :
64 IOWR(SEG7_DISPLAY_BASE, 0 , 0x44444444 ); break ;
65 case ' 5 ' :
66 IOWR(SEG7_DISPLAY_BASE, 0 , 0x55555555 ); break ;
67 case ' 6 ' :
68 IOWR(SEG7_DISPLAY_BASE, 0 , 0x66666666 ); break ;
69 case ' 7 ' :
70 IOWR(SEG7_DISPLAY_BASE, 0 , 0x77777777 ); break ;
71 case ' 8 ' :
72 IOWR(SEG7_DISPLAY_BASE, 0 , 0x88888888 ); break ;
73 case ' 9 ' :
74 IOWR(SEG7_DISPLAY_BASE, 0 , 0x99999999 ); break ;
75 }
76 }
77 // 串口中断初始化
78 void Uart_init()
79 {
80 IOWR_ALTERA_AVALON_UART_CONTROL(UART_BASE, 0x80 ); // 接收中断使能
81 IOWR_ALTERA_AVALON_UART_STATUS(UART_BASE, 0x0 ); // 清状态标志
82 // IOWR_ALTERA_AVALON_UART_RXDATA(UART_BASE, 0x0); // 清接收寄存器
83 alt_irq_register(UART_IRQ, 0 ,Uart_ISR);
84 }
85 int main()
86 {
87 Uart_init();
88 while ( 1 )
89 {
90 Uart_send_n( " NIOS,I love u! " );
91 // Uart_send_n(64);
92 usleep(TIME_DELAY); // 延时一微秒
93 }
94 }
95
96

 

转载于:https://www.cnblogs.com/xxfighting/archive/2010/08/26/1808889.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值