Chinaunix首页 | 论坛 | 博客
  • 博客访问: 406689
  • 博文数量: 77
  • 博客积分: 798
  • 博客等级: 军士长
  • 技术积分: 1238
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-01 22:33
文章分类

全部博文(77)

文章存档

2015年(1)

2014年(7)

2013年(38)

2012年(31)

分类: LINUX

2012-07-12 18:00:24

 

 

 

  // 1  serial.h                 串口的预定义

void uart0_init(void);
void putc(unsigned char c);
unsigned char getc(void);
int isDigit(unsigned char c);
int isLetter(unsigned char c);

 //  2      s3c2440|2410里的寄存器预定义和配置  (包括了一些本次用不到的寄存器配置与预定义)//

/* WOTCH DOG register */
#define     WTCON           (*(volatile unsigned long *)0x53000000)   //配置看门狗定时模式

/* SDRAM regisers */
#define     MEM_CTL_BASE    0x48000000
#define     SDRAM_BASE      0x30000000

/* NAND Flash registers */
#define NFCONF              (*(volatile unsigned int  *)0x4e000000)
#define NFCMD               (*(volatile unsigned char *)0x4e000004)
#define NFADDR              (*(volatile unsigned char *)0x4e000008)
#define NFDATA              (*(volatile unsigned char *)0x4e00000c)
#define NFSTAT              (*(volatile unsigned char *)0x4e000010)

/*GPIO registers*/
#define GPBCON              (*(volatile unsigned long *)0x56000010)
#define GPBDAT              (*(volatile unsigned long *)0x56000014)

#define GPFCON              (*(volatile unsigned long *)0x56000050)
#define GPFDAT              (*(volatile unsigned long *)0x56000054)
#define GPFUP               (*(volatile unsigned long *)0x56000058)

#define GPGCON              (*(volatile unsigned long *)0x56000060)
#define GPGDAT              (*(volatile unsigned long *)0x56000064)
#define GPGUP               (*(volatile unsigned long *)0x56000068)

#define GPHCON              (*(volatile unsigned long *)0x56000070)
#define GPHDAT              (*(volatile unsigned long *)0x56000074)
#define GPHUP               (*(volatile unsigned long *)0x56000078)

 // 串口寄存器的宏定义与配置

/*UART registers*/
#define ULCON0              (*(volatile unsigned long *)0x50000000)
#define UCON0               (*(volatile unsigned long *)0x50000004)
#define UFCON0              (*(volatile unsigned long *)0x50000008)
#define UMCON0              (*(volatile unsigned long *)0x5000000c)
#define UTRSTAT0            (*(volatile unsigned long *)0x50000010)
#define UTXH0               (*(volatile unsigned char *)0x50000020)
#define URXH0               (*(volatile unsigned char *)0x50000024)
#define UBRDIV0             (*(volatile unsigned long *)0x50000028)


/*interrupt registes*/
#define SRCPND              (*(volatile unsigned long *)0x4A000000)
#define INTMOD              (*(volatile unsigned long *)0x4A000004)
#define INTMSK              (*(volatile unsigned long *)0x4A000008)
#define PRIORITY            (*(volatile unsigned long *)0x4A00000c)
#define INTPND              (*(volatile unsigned long *)0x4A000010)
#define INTOFFSET           (*(volatile unsigned long *)0x4A000014)
#define SUBSRCPND           (*(volatile unsigned long *)0x4A000018)
#define INTSUBMSK           (*(volatile unsigned long *)0x4A00001c)

/*external interrupt registers*/
#define EINTMASK            (*(volatile unsigned long *)0x560000a4)
#define EINTPEND            (*(volatile unsigned long *)0x560000a8)

/*clock registers*/
#define LOCKTIME  (*(volatile unsigned long *)0x4c000000)
#define MPLLCON  (*(volatile unsigned long *)0x4c000004)
#define UPLLCON  (*(volatile unsigned long *)0x4c000008)
#define CLKCON  (*(volatile unsigned long *)0x4c00000c)
#define CLKSLOW  (*(volatile unsigned long *)0x4c000010)
#define CLKDIVN  (*(volatile unsigned long *)0x4c000014)


/*PWM & Timer registers*/                              
#define TCFG0  (*(volatile unsigned long *)0x51000000)
#define TCFG1  (*(volatile unsigned long *)0x51000004)
#define TCON  (*(volatile unsigned long *)0x51000008)
#define TCNTB0  (*(volatile unsigned long *)0x5100000c)
#define TCMPB0  (*(volatile unsigned long *)0x51000010)
#define TCNTO0  (*(volatile unsigned long *)0x51000014)

#define GSTATUS1    (*(volatile unsigned long *)0x560000B0)   //选中芯片型号

 // 3 main.c

#include "serial.h"       //调用上面已定义好的serial.h

int main()
{
    unsigned char c;
    uart0_init();   // 波特率115200,8N1(8个数据位,无校验位,1个停止位)

    while(1)
    {
        // 从串口接收数据后,判断其是否数字或子母,若是则加1后输出
        c = getc();
        if (isDigit(c) || isLetter(c))  /* 功能:判断字符c是否为数字或字母*/ 
            putc(c+1);
    }

    return 0;
}
  // 串口处理

#include "s3c24xx.h"
#include "serial.h"

#define TXD0READY   (1<<2)     // 方便后面配置UTRSTAT0,只保留[2]位

#define RXD0READY   (1)

#define PCLK            50000000    // init.c中的clock_init函数设置PCLK为50MHz
#define UART_CLK        PCLK        //  UART0的时钟源设为PCLK
#define UART_BAUD_RATE  115200      // 波特率
#define UART_BRD        ((UART_CLK  / (UART_BAUD_RATE * 16)) - 1)

 

/*
 * 初始化UART0
 * 115200,8N1,无流控
 */
void uart0_init(void)
{
    GPHCON  |= 0xa0;    // GPH2,GPH3用作TXD0,RXD0
    GPHUP   = 0x0c;     // GPH2,GPH3内部上拉     经过查询手册知该行代码应

为: GPHUP = 0x3f3;

    ULCON0  = 0x03;     // 8N1(8个数据位,无较验,1个停止位)


    UCON0   = 0x05;     // 查询方式,UART时钟源为PCLK
    UFCON0  = 0x00;     // 不使用FIFO


    UMCON0  = 0x00;     // 不使用流控


    UBRDIV0 = UART_BRD; // 波特率为115200
}

/
 * 发送一个字符
 */
void putc(unsigned char c)
{
    /* 等待,直到发送缓冲区中的数据已经全部发送出去 */
    while (!(UTRSTAT0 & TXD0READY));             
   
    /* 向UTXH0寄存器中写入数据,UART即自动将它发送出去 */
    UTXH0 = c;
}

/*
 * 接收字符
 */
unsigned char getc(void)
{
    /* 等待,直到接收缓冲区中的有数据 */
    while (!(UTRSTAT0 & RXD0READY));
   
    /* 直接读取URXH0寄存器,即可获得接收到的数据 */
    return URXH0; 

      /* 在前面的宏定义已有定义 #define URXH0  (*(volatile unsigned char *)0x50000024)*/
}

/*
 * 判断一个字符是否数字
 */
int isDigit(unsigned char c)
{
    if (c >= '0' && c <= '9')
        return 1;
    else
        return 0;      
}

/*
 * 判断一个字符是否英文字母
 */
int isLetter(unsigned char c)
{
    if (c >= 'a' && c <= 'z')
        return 1;
    else if (c >= 'A' && c <= 'Z')
        return 1;      
    else
        return 0;
}

 

阅读(1899) | 评论(0) | 转发(0) |
0

上一篇:u8818

下一篇:linux的一些小事情

给主人留下些什么吧!~~