S3c2410裸板程序入门---串口
2019-12-24 来源:elecfans
流水灯跑起来了,是不是有点小兴奋,一鼓作气再来玩玩其他的,用PC机写程序,出了问题可以用perror等打印出信息,给调试带来了很大方便,可是板子上没有显示屏,没法打印怎么办?这次就写一个通过串口向PC机串口调试终端打印信息的小程序,方便以后的调试。串口通信的基本知识就不说了哈,网上很多的,直接上代码:
uart.c
1 #include 's3c2410.h'
2
3 void uart_sendchar(char a)
4 {
5 if(a == 'n'){
6 while( !(UTRSTAT0 & (0x1<<2)) );
7 UTXH0 = 'r';
8 }
9 while( !(UTRSTAT0 & (0x1<<2)) );
10 UTXH0 = a;
11 }
12
13 void sendstring(const char *str)
14 {
15 while(*str){
16 uart_sendchar(*str);
17 str++;
18 }
19 }
20
21 void uart_init()
22 {
23 GPHCON = (GPHCON & ~(0xf<<4)) | (0xa<<4);
24
25 ULCON0 = 0x3;
26 UCON0 = 0x5;
27
28 UBRDIV0 = (int)(PCLK/(115200*16)) - 1;
29
30 }
GPHCON配置使用串口0, ULCON0 = 0X3配置串口使用8位数据位,一位停止位,无校验,UCON0 = 0X5设置传输模式,默认时钟使用PCLK。UTXH0为发送buffer, 往其中写数据后发送,UTRSTAT0检测数据线上有无数据,从而控制在一个字符发送完成后再发下一个。
还有注意n的处理,因为windows的回车是rn,所以有单独的判断。
哦,你可能会问这个s3c2410.h在哪里,哦,这个其实是板子带的,就是定义了一堆寄存器地址,完全也可以自己定义。向这种格式:
s3c2410.h
1 /* WOTCH DOG register */
2 #define WTCON (*(volatile unsigned long *)0x53000000)
3 #define WTDAT (*(volatile unsigned long *)0x53000004)
4 #define WTCNT (*(volatile unsigned long *)0x53000008)
5
39 /*UART registers*/
40 #define ULCON0 (*(volatile unsigned long *)0x50000000)
41 #define UCON0 (*(volatile unsigned long *)0x50000004)
42 #define UFCON0 (*(volatile unsigned long *)0x50000008)
43 #define UMCON0 (*(volatile unsigned long *)0x5000000c)
44 #define UTRSTAT0 (*(volatile unsigned long *)0x50000010)
45 #define UTXH0 (*(volatile unsigned char *)0x50000020)
46 #define URXH0 (*(volatile unsigned char *)0x50000024)
47 #define UBRDIV0 (*(volatile unsigned long *)0x50000028)
48
49
50 /*interrupt registes*/
51 #define SRCPND (*(volatile unsigned long *)0x4A000000)
52 #define INTMOD (*(volatile unsigned long *)0x4A000004)
53 #define INTMSK (*(volatile unsigned long *)0x4A000008)
54 #define PRIORITY (*(volatile unsigned long *)0x4A00000c)
55 #define INTPND (*(volatile unsigned long *)0x4A000010)
56 #define INTOFFSET (*(volatile unsigned long *)0x4A000014)
57 #define SUBSRCPND (*(volatile unsigned long *)0x4A000018)
58 #define INTSUBMSK (*(volatile unsigned long *)0x4A00001c)
59
60 /*external interrupt registers*/
61 #define EINTMASK (*(volatile unsigned long *)0x560000a4)
62 #define EINTPEND (*(volatile unsigned long *)0x560000a8)
63
64 #define EXTINT2 (*(volatile unsigned long *)0x56000090)
65
66 /*clock registers*/
67 #define LOCKTIME (*(volatile unsigned long *)0x4c000000)
68 #define MPLLCON (*(volatile unsigned long *)0x4c000004)
69 #define UPLLCON (*(volatile unsigned long *)0x4c000008)
70 #define CLKCON (*(volatile unsigned long *)0x4c00000c)
71 #define CLKSLOW (*(volatile unsigned long *)0x4c000010)
72 #define CLKDIVN (*(volatile unsigned long *)0x4c000014)
73
74
75 /*PWM & Timer registers*/
76 #define TCFG0 (*(volatile unsigned long *)0x51000000)
77 #define TCFG1 (*(volatile unsigned long *)0x51000004)
78 #define TCON (*(volatile unsigned long *)0x51000008)
79 #define TCNTB0 (*(volatile unsigned long *)0x5100000c)
80 #define TCMPB0 (*(volatile unsigned long *)0x51000010)
81 #define TCNTO0 (*(volatile unsigned long *)0x51000014)
82 /* frequecy */
^M
83 #define FCLK (202800000)
84 #define HCLK (FCLK/2)
85 #define PCLK (FCLK/4)
注意要用volatile 和 unsigned 来保证优化和移位不会处差错。