mini2440A 裸机编程:串口控制LED灯的亮灭

要求

在PC上发送L11控制开发板led1亮,发送L10控制开发板led1灭;

在PC上发送L21控制开发板led2亮,发送L20控制开发板led2灭;

在PC上发送L31控制开发板led3亮,发送L30控制开发板led3灭;

在PC上发送L41控制开发板led4亮,发送L40控制开发板led4灭;

PC使用串口助手发送;

代码

#include 
#include 

//0x50000020(L)
//0x50000023(B)
//(字节) UART 通道0 发送缓冲寄存器
#define UTXH0       (*(volatile unsigned int *)0x50000020)//UTXH0
#define UTRSTAT0    (*(volatile unsigned int *)0x50000010)  //R UART 通道0 Tx/Rx 状态寄存器
#define ULCON0      (*(volatile unsigned int *)0x50000000)//  //R/W UART 通道0 线路控制寄存器
#define UCON0       (*(volatile unsigned int *)0x50000004)//  //R/W UART channel 0 control register
#define UBRDIV0     (*(volatile unsigned int *)0x50000028) //  R/W 波特率分频寄存器0
#define GPHCON      (*(volatile unsigned int *)0x56000070)   //R/W 配置端口H 的引脚
//URXH0
//0x50000024(L)
//0x50000027(B)
//W
//(字节) UART 通道0 接收缓冲寄存器 –
#define URXH0       (*(volatile unsigned int *)0x50000024)//URXH0
//UFCON0 0x50000008 R/W UART 通道0 FIFO 控制寄存器 0x0
#define UFCON0      (*(volatile unsigned int *)0x50000008)//URXH0
//UMCON0 0x5000000C R/W UART 通道0 Modem 控制寄存器 0x0
#define UMCON0      (*(volatile unsigned int *)0x5000000c)//URXH0
//灯
//GPBCON  0x56000010  R/W  配置端口 B 的引脚 
#define GPBCON  (*(unsigned int *)0x56000010)
//GPBDAT  0x56000014  R/W  端口 B 的数据寄存器
#define GPBDAT  (*(unsigned int *)0x56000014)

#define PCLK    (50000000)
#define BPS     (115200)

unsigned char buff[3];
char len = 0;

void delay(int time)
{
   int i;
   for(i=0; ivoid uart0_init(void)
{
    GPHCON = (2<<4) | (2<<6);//配置串口引脚功能
    ULCON0 = 0x3;//设置串口0的数据位8,停止位1,校验位无
    UCON0 = (1<<2)|(1<<0);//使能发送,接受模式
    //UBRDIVn = (int)( UART 时钟 / ( 波特率 × 16) ) –1
    UFCON0 = 0;
    UMCON0 = 0;
    UBRDIV0 = PCLK/(BPS*16) -1;
}
void uart0_sendchar(unsigned char date)
{
  UTXH0 = date;
    while((UTRSTAT0&0x04)==0); 
 // UTXH0 = date;  
}

char uart0_recvchar(void)
{
    while((UTRSTAT0&0x01)==0);
    return URXH0;
}
void uart0_recvstring(char *ch,int len)
{
    while(len--)
    {
        *ch = uart0_recvchar();
          ch++;
    }
    *ch = '\0';
}
void uart0_sendstring(char* string)
{
    while(*string != 0)
    {
       uart0_sendchar(*string);
       string++; 
    }
}
void led_init(void)
{
  GPBCON = (1<<10)|(1<<12)|(1<<14)|(1<<16);
    GPBDAT = (1<<5)|(1<<6)|(1<<7)|(1<<8);
}
void judge_led(char *recv)
{
    if(strcmp(recv,"L10")==0)
        {
           GPBDAT = (1<<5)|(1<<6)|(1<<7)|(1<<8);
        }
        else if(strcmp(recv,"L11")==0)
        {
           GPBDAT = (0<<5)|(1<<6)|(1<<7)|(1<<8);
        }
        else if(strcmp(recv,"L21")==0)
        {
           GPBDAT = (1<<5)|(0<<6)|(1<<7)|(1<<8);
        }
        else if(strcmp(recv,"L20")==0) 
        {
           GPBDAT = (1<<5)|(1<<6)|(1<<7)|(1<<8); 
        }
        else if(strcmp(recv,"L31")==0) 
        {
           GPBDAT = (1<<5)|(1<<6)|(0<<7)|(1<<8); 
        }
        else if(strcmp(recv,"L30")==0) 
        {
           GPBDAT = (1<<5)|(1<<6)|(1<<7)|(1<<8); 
        }
        else if(strcmp(recv,"L41")==0) 
        {
           GPBDAT = (1<<5)|(1<<6)|(1<<7)|(0<<8); 
        }
        else if(strcmp(recv,"L40")==0) 
        {
           GPBDAT = (1<<5)|(1<<6)|(1<<7)|(1<<8); 
        }
}
int main(void)
{
    char recv[4]={0};

  uart0_init();
    led_init();

    while(1)
    {    
      uart0_recvstring(recv,3);  
        uart0_sendstring(recv);
        delay(0x2000);

        judge_led(recv);

        memset(recv,0,strlen(recv)+1);
    }
}

你可能感兴趣的:(单片机)