串口控制FL2440(S3C2440)的LED

开发板:FL2440

芯片:S3C2440

功能:程序运行时点亮一个LED,然后利用PC机通过串口发送一个数,开发板读到这个数后点亮另外一个LED。最基本的串口控制。

代码:

Init.s

AREA |DATA|,CODE,READONLY 
ENTRY 
ldr r13, =0x1000 
IMPORT WuqiMain 
b WuqiMain 
END

 

include.h

 1 #define rUTRSTAT0   (*(volatile unsigned *)0x50000010)    //UART 0 Tx/Rx status
 2 #define rULCON0     (*(volatile unsigned *)0x50000000)    //UART 0 Line control
 3 #define rUCON0      (*(volatile unsigned *)0x50000004)    //UART 0 Control
 4 #define rUFCON0     (*(volatile unsigned *)0x50000008)    //UART 0 FIFO control
 5 #define rUBRDIV0    (*(volatile unsigned *)0x50000028)    //UART 0 Baud rate divisor
 6 #define WrUTXH0(ch) (*(volatile unsigned char *)0x50000020)=(unsigned char)(ch)
 7 #define RdURXH0()   (*(volatile unsigned char *)0x50000027)
 8 #define rURXH0          (*(volatile unsigned *)0X50000024)
 9 #define rUTXH0  (*(volatile unsigned *)0X50000020)
10 #define rURXH0  (*(volatile unsigned *)0X50000024)
11 
12 #define GPBCON (*(volatile unsigned *)0x56000010) 
13 #define GPBDAT (*(volatile unsigned *)0x56000014) 
14 #define GPBUP (*(volatile unsigned *)0x56000018)
15 
16 #define uchar unsigned char
17 #define uint unsigned int
18 #define U32 unsigned int
19 #define U16 unsigned short
20 #define S32 int
21 #define S16 short int
22 #define U8  unsigned char
23 #define    S8  char
24 
25 #define TRUE 1
26 #define FALSE 0
27 
28 #define OK 1
29 #define FAIL 0
30 
31 #define    ESC_KEY        0x1b

 

 

uart.h

 1 #include "include.h"
 2 #include <stdarg.h>
 3 
 4 
 5 
 6 void Uart_Init(int baud)
 7 {
 8     int i;
 9     rUFCON0 = 0x0;   //UART channel 0 FIFO control register, FIFO disable
10   
11  
12 //UART0
13     rULCON0 &=0XFFFFFF00;
14     rULCON0 |=0X03;           //1位起始位,8位数据位
15     rUCON0  =0X0805;          //串口时钟PCLK,查询方式
16     rUBRDIV0 =0X1A;
17     for(i=0;i<100;i++);
18 }
19 
20 
21     
22 //=====================================================================
23 void Uart_SendByte(int data)
24 {
25    
26         if(data=='\n')
27         {
28             while(!(rUTRSTAT0 & 0x2));
29            // Delay(1);                 //because the slow response of hyper_terminal 
30             WrUTXH0('\r');
31         }
32         while(!(rUTRSTAT0 & 0x2));   //Wait until THR is empty.
33       //  Delay(1);
34         WrUTXH0(data);
35   
36    
37 }               
38 
39 //====================================================================
40 void Uart_SendString(S8 *pt)
41 {
42     while(*pt)
43         Uart_SendByte(*pt++);
44 }
45 
46 //=====================================================================
47 //If you don't use vsprintf(), the code size is reduced very much.
48 void Uart_Printf(S8 *fmt,...)
49 {
50     va_list ap;
51     S8 str[255];
52 
53     va_start(ap,fmt);
54        vsprintf(str,fmt,ap);
55     Uart_SendString(str);
56     va_end(ap);
57 }
58 
59 //=====================================================================
60 
61 /*
62 char Uart_GetKey(void)
63 {
64     
65         if(rUTRSTAT0 & 0x1)    //Receive data ready
66         return rURXH0 ;
67         else
68             return 0;
69  
70 }
71 */

 

main.c

#include"include.h"
#include "uart.h"

void Delay(int count)
{
    unsigned int i;                
    while (--count != 0)        
    {
        for (i=0; i<255; i++);        // ";" 表示空语句,CPU空转。
    }                   
}

void WuqiMain(void)
{
    char buf;
    
    GPBUP = 0x00; 
    GPBCON &= ~(1<<13);
    GPBCON |= (1<<12);
    GPBCON &= ~(1<<21);    
    GPBCON |= (1<<20);
    
    while(1)
    {
        GPBDAT &= ~(1<<6);
        GPBDAT &= 0xffe;
        
        if(rUTRSTAT0 & 0X01)  //接收是否完毕 =1结束
        {
            buf=rURXH0;       //读取数据
            while(!(rUTRSTAT0 & 0X04));//是否允许发送 =1允许
            rUTXH0=buf;
        }            
        if(buf=='1')
        {
            GPBDAT &= ~(1<<10);
            
        }    

    }
}

 

测试一下,vc6,file——new——projects——win32consoleApplication——a simple application

 1 #include "stdafx.h"
 2 #include <stdio.h>
 3 #include <string.h>
 4 
 5 
 6 int main(int argc, char* argv[])
 7 {
 8     char lpBuf[]="1";
 9     FILE * pFile=fopen("COM1","w");
10     if(pFile==NULL)
11     {
12         return 1;
13     }
14     fwrite(lpBuf,sizeof(char),strlen(lpBuf),pFile);
15     fclose(pFile);
16     return 0;
17 }

运行后开发板两个LED亮,也可用串口调试助手发送“1”来测试。

posted @ 2012-10-15 15:29  wuqi1003  阅读(811)  评论(0编辑  收藏  举报